- Add database migrations for market_listings, missions and character cosmetics - Create controllers for marketplace, missions, presence and character customization - Implement admin world dashboard with live platform metrics - Add feature tests for world functionality - Include phase 2 design documents and gap analysis
111 lines
3.5 KiB
PHP
Executable File
111 lines
3.5 KiB
PHP
Executable File
<?php
|
|
|
|
namespace App\Support;
|
|
|
|
use App\Models\Character;
|
|
use App\Models\Mission;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
/**
|
|
* Tracks citizen mission progress and pays rewards on completion.
|
|
*/
|
|
class MissionTracker
|
|
{
|
|
/**
|
|
* Metric keys readable from a character snapshot.
|
|
*
|
|
* @var list<string>
|
|
*/
|
|
public const METRICS = ['exercise_count', 'buildings', 'transactions', 'education'];
|
|
|
|
/**
|
|
* Ensure the character has an active row for every city mission.
|
|
*/
|
|
public static function ensureTracking(Character $character): void
|
|
{
|
|
$missions = Mission::query()
|
|
->where('city_id', $character->city_id)
|
|
->where('is_active', true)
|
|
->get();
|
|
|
|
foreach ($missions as $mission) {
|
|
$exists = DB::table('character_missions')
|
|
->where('character_id', $character->id)
|
|
->where('mission_id', $mission->id)
|
|
->exists();
|
|
|
|
if (! $exists) {
|
|
DB::table('character_missions')->insert([
|
|
'character_id' => $character->id,
|
|
'mission_id' => $mission->id,
|
|
'progress' => 0,
|
|
'is_completed' => false,
|
|
'created_at' => now(),
|
|
'updated_at' => now(),
|
|
]);
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Recompute progress for a metric and complete missions that hit target.
|
|
*
|
|
* @return list<array{code: string, title: string, reward: float}>
|
|
*/
|
|
public static function sync(Character $character, string $metric): array
|
|
{
|
|
if (! in_array($metric, self::METRICS, true)) {
|
|
return [];
|
|
}
|
|
|
|
self::ensureTracking($character);
|
|
$character->refresh();
|
|
$value = (int) match ($metric) {
|
|
'exercise_count' => $character->exercise_count,
|
|
'education' => $character->education,
|
|
'buildings' => $character->buildings()->where('type', 'house')->count(),
|
|
'transactions' => DB::table('transactions')
|
|
->where('from_character_id', $character->id)
|
|
->orWhere('to_character_id', $character->id)
|
|
->count(),
|
|
default => 0,
|
|
};
|
|
|
|
$completed = [];
|
|
$rows = DB::table('character_missions')
|
|
->where('character_id', $character->id)
|
|
->where('is_completed', false)
|
|
->get();
|
|
|
|
foreach ($rows as $row) {
|
|
$mission = Mission::query()->find($row->mission_id);
|
|
if ($mission === null || $mission->metric !== $metric) {
|
|
continue;
|
|
}
|
|
|
|
$progress = min($value, (int) $mission->target);
|
|
DB::table('character_missions')->where('id', $row->id)->update([
|
|
'progress' => $progress,
|
|
'is_completed' => $progress >= (int) $mission->target,
|
|
'completed_at' => $progress >= (int) $mission->target ? now() : null,
|
|
'updated_at' => now(),
|
|
]);
|
|
|
|
if ($progress >= (int) $mission->target) {
|
|
$character->money = round((float) $character->money + (float) $mission->reward, 2);
|
|
$completed[] = [
|
|
'code' => $mission->code,
|
|
'title' => $mission->title,
|
|
'reward' => (float) $mission->reward,
|
|
];
|
|
}
|
|
}
|
|
|
|
if ($completed !== []) {
|
|
$character->save();
|
|
}
|
|
|
|
return $completed;
|
|
}
|
|
}
|