109 lines
4.0 KiB
PHP
109 lines
4.0 KiB
PHP
<?php
|
|
|
|
namespace App\Http\Controllers\API;
|
|
|
|
use App\Http\Controllers\Controller;
|
|
use Illuminate\Http\Request;
|
|
use App\Models\DailyStepsCount;
|
|
use APP\Models\User;
|
|
use Carbon\Carbon;
|
|
use App\Models\LeaderboardMaster;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
|
|
class StepCountController extends Controller
|
|
{
|
|
public function store_step_count(Request $request){
|
|
|
|
// dd($request->step_count);
|
|
$token = readHeaderToken();
|
|
if ($token) {
|
|
$stepCount = $request->step_count;
|
|
$userId = $token['sub'];
|
|
// dd($userId);
|
|
$date = Carbon::now()->toDateString();
|
|
$dailyStepCount = DailyStepsCount::where('user_id', $userId)
|
|
->where('date', $date)
|
|
->first();
|
|
// dd($dailyStepCount);
|
|
Log::info("dailyStepCount found at that date");
|
|
// dd($dailyStepCount->step_count);
|
|
|
|
if ($dailyStepCount) {
|
|
$oldStepCounts = $dailyStepCount->step_count;
|
|
$oldPoints = $dailyStepCount->points;
|
|
|
|
// if($oldStepCounts > $stepCount){
|
|
// $stepCount = $oldStepCounts + $stepCount;
|
|
// }
|
|
$dailyStepCount->update(['step_count' => $stepCount]);
|
|
|
|
$latestSteps = $dailyStepCount->step_count;
|
|
// Update the step count for the current date and update points
|
|
|
|
$latestPoints = intval(floor($latestSteps/1000));
|
|
if($latestPoints > $oldPoints){
|
|
$currentPoints = $latestPoints - $oldPoints;
|
|
|
|
//update points
|
|
$dailyStepCount->update(['points' => $latestPoints]);
|
|
|
|
$leaderboardMaster = LeaderboardMaster::where('user_id', $userId)->first();
|
|
|
|
if ($leaderboardMaster) {
|
|
// $latestPoints = $latestPoints*5;
|
|
$leaderboardMaster->total_score += 5*$currentPoints;
|
|
$leaderboardMaster->save();
|
|
}
|
|
|
|
}
|
|
|
|
$updated_step_counts = $points = intval(floor($dailyStepCount->step_count/1000));
|
|
$updated_points = $dailyStepCount->points;
|
|
if($updated_step_counts < $updated_points){
|
|
$leaderboardMaster = LeaderboardMaster::where('user_id', $userId)->first();
|
|
if ($leaderboardMaster) {
|
|
|
|
$leaderboardMaster->total_score -= 5*$updated_points;
|
|
$leaderboardMaster->total_score += 5*$updated_step_counts;
|
|
$leaderboardMaster->save();
|
|
}
|
|
$dailyStepCount->update(['points' => $updated_step_counts]);
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
Log::info("New user Creating");
|
|
// Create a new record for the new day
|
|
$points = intval(floor($stepCount/1000));
|
|
$newDailyStepCount = new DailyStepsCount([
|
|
'user_id' => $userId,
|
|
'date' => $date,
|
|
'step_count' => $stepCount,
|
|
'points' => $points,
|
|
]);
|
|
|
|
$newDailyStepCount->save();
|
|
|
|
$leaderboardMaster = LeaderboardMaster::where('user_id', $userId)->first();
|
|
if ($leaderboardMaster) {
|
|
|
|
$leaderboardMaster->total_score += 5*$points;
|
|
$leaderboardMaster->save();
|
|
}
|
|
}
|
|
|
|
return response(['status' => "Success"], 200);
|
|
|
|
} else {
|
|
return response()->json([
|
|
'success' => false,
|
|
'message' => 'Authentication failed.',
|
|
]);
|
|
}
|
|
|
|
}
|
|
}
|