109 lines
3.9 KiB
PHP
Executable File
109 lines
3.9 KiB
PHP
Executable File
<?php
|
|
|
|
namespace App\Http\Controllers;
|
|
|
|
use Illuminate\Http\Request;
|
|
use App\Helpers\TwitchHelper;
|
|
use App\Models\TwitchUser;
|
|
use App\Models\CommandHistory;
|
|
use App\Models\CurrentState;
|
|
use App\Models\PaletteColor;
|
|
use App\Http\Controllers\GameSessionController;
|
|
use Illuminate\Support\Facades\Log;
|
|
|
|
class TwitchController extends Controller
|
|
{
|
|
public function parseChatMessage(Request $request)
|
|
{
|
|
Log::info("Entered parseChatMessage");
|
|
$message = $request->input('message');
|
|
$username = $request->input('username');
|
|
$gameSession = app(GameSessionController::class)->getCurrentGameSession();
|
|
|
|
Log::debug('TwitchController::parseChatMessage', [
|
|
'message' => $message,
|
|
'username' => $username,
|
|
'gameSessionId' => $gameSession->id
|
|
]);
|
|
|
|
if (!$message || !$username || !$gameSession) {
|
|
Log::debug('TwitchController::parseChatMessage - Missing input or invalid game session', [
|
|
'message' => $message,
|
|
'username' => $username,
|
|
'gameSession' => $gameSession
|
|
]);
|
|
return response()->json(['error' => 'No message, username, or valid game session provided'], 400);
|
|
}
|
|
|
|
$user = TwitchUser::firstOrCreate(['username' => $username]);
|
|
|
|
$parsedCommand = TwitchHelper::parseMessage($message);
|
|
Log::debug('TwitchController::parseChatMessage - Parsed Command', ['parsedCommand' => $parsedCommand]);
|
|
|
|
if ($parsedCommand) {
|
|
$color = $this->getColorFromName($parsedCommand['color'], $gameSession->palette_id);
|
|
if (!$color) {
|
|
return response()->json(['error' => 'Invalid color'], 400);
|
|
}
|
|
|
|
CommandHistory::create([
|
|
'twitch_user_id' => $user->id,
|
|
'command' => $parsedCommand['command'],
|
|
'x' => $parsedCommand['x'] ?? null,
|
|
'y' => $parsedCommand['y'] ?? null,
|
|
'palette_color_id' => $color->id,
|
|
'game_session_id' => $gameSession->id,
|
|
]);
|
|
|
|
switch ($parsedCommand['command']) {
|
|
case 'place':
|
|
$this->handlePlaceCommand($parsedCommand, $user->id, $gameSession->id, $color->id);
|
|
break;
|
|
// Add cases for other commands like 'row', 'column', 'fill'
|
|
}
|
|
|
|
return response()->json([
|
|
'command' => $parsedCommand,
|
|
'color' => [
|
|
'hex_value' => $color->hex_value
|
|
]
|
|
]);
|
|
}
|
|
|
|
Log::debug('TwitchController::parseChatMessage - Invalid command', ['message' => $message]);
|
|
return response()->json(['error' => 'Invalid command'], 400);
|
|
}
|
|
|
|
private function handlePlaceCommand($parsedCommand, $userId, $gameSessionId, $colorId)
|
|
{
|
|
Log::debug('TwitchController::handlePlaceCommand', ['command' => $parsedCommand]);
|
|
|
|
$x = $parsedCommand['x'];
|
|
$y = $parsedCommand['y'];
|
|
|
|
$currentState = CurrentState::where('x', $x)
|
|
->where('y', $y)
|
|
->where('game_session_id', $gameSessionId)
|
|
->first();
|
|
Log::debug('TwitchController::handlePlaceCommand - Retrieved Current State', ['currentState' => $currentState]);
|
|
|
|
if ($currentState) {
|
|
$currentState->update(['palette_color_id' => $colorId, 'updated_by' => $userId, 'updated_at' => now()]);
|
|
} else {
|
|
CurrentState::create([
|
|
'x' => $x,
|
|
'y' => $y,
|
|
'palette_color_id' => $colorId,
|
|
'updated_by' => $userId,
|
|
'updated_at' => now(),
|
|
'game_session_id' => $gameSessionId,
|
|
]);
|
|
}
|
|
}
|
|
|
|
private function getColorFromName($colorName, $paletteId)
|
|
{
|
|
return PaletteColor::where('palette_id', $paletteId)->where('name', $colorName)->first();
|
|
}
|
|
}
|