PixelPlacerBot/app/Http/Controllers/CommandController.php

54 lines
1.6 KiB
PHP
Raw Permalink Normal View History

2024-07-28 18:06:05 -04:00
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\CommandHistory;
use App\Models\CurrentState;
use App\Models\GameSession;
use App\Models\PaletteColor;
use App\Models\TwitchUser;
class CommandController extends Controller
{
public function parseChatMessage(Request $request)
{
$request->validate([
'username' => 'required|string',
'x' => 'required|string',
'y' => 'required|string',
'color' => 'required|string',
]);
$user = TwitchUser::firstOrCreate(['username' => $request->username]);
$currentGame = GameSession::whereNull('ended_at')->latest()->first();
if (!$currentGame) {
return response()->json(['error' => 'No active game session'], 404);
}
$color = PaletteColor::where('hex_value', $request->color)->first();
if (!$color) {
return response()->json(['error' => 'Invalid color'], 400);
}
$command = CommandHistory::create([
'twitch_user_id' => $user->id,
'game_session_id' => $currentGame->id,
'x' => $request->x,
'y' => $request->y,
'palette_color_id' => $color->id,
'timestamp' => now(),
]);
CurrentState::updateOrCreate(
['game_session_id' => $currentGame->id, 'x' => $request->x, 'y' => $request->y],
['palette_color_id' => $color->id, 'updated_by' => $user->id, 'timestamp' => now()]
);
// Logic to check if the current state matches the winning state
// ...
return response()->json($command);
}
}