PixelPlacerBot/app/Http/Controllers/TwitchController.php

109 lines
3.9 KiB
PHP
Raw Normal View History

2024-07-20 13:02:52 -04:00
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Helpers\TwitchHelper;
2024-07-27 15:40:35 -04:00
use App\Models\TwitchUser;
use App\Models\CommandHistory;
use App\Models\CurrentState;
2024-07-28 18:06:05 -04:00
use App\Models\PaletteColor;
use App\Http\Controllers\GameSessionController;
2024-07-27 15:40:35 -04:00
use Illuminate\Support\Facades\Log;
2024-07-20 13:02:52 -04:00
class TwitchController extends Controller
{
public function parseChatMessage(Request $request)
{
2024-07-27 15:40:35 -04:00
Log::info("Entered parseChatMessage");
2024-07-20 13:02:52 -04:00
$message = $request->input('message');
2024-07-27 15:40:35 -04:00
$username = $request->input('username');
2024-07-28 18:06:05 -04:00
$gameSession = app(GameSessionController::class)->getCurrentGameSession();
2024-07-20 13:02:52 -04:00
2024-07-27 15:40:35 -04:00
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);
2024-07-20 13:02:52 -04:00
}
2024-07-28 18:06:05 -04:00
$user = TwitchUser::firstOrCreate(['username' => $username]);
2024-07-27 15:40:35 -04:00
2024-07-20 13:02:52 -04:00
$parsedCommand = TwitchHelper::parseMessage($message);
2024-07-27 15:40:35 -04:00
Log::debug('TwitchController::parseChatMessage - Parsed Command', ['parsedCommand' => $parsedCommand]);
2024-07-20 13:02:52 -04:00
if ($parsedCommand) {
2024-07-28 18:06:05 -04:00
$color = $this->getColorFromName($parsedCommand['color'], $gameSession->palette_id);
if (!$color) {
return response()->json(['error' => 'Invalid color'], 400);
}
2024-07-27 15:40:35 -04:00
CommandHistory::create([
2024-07-28 18:06:05 -04:00
'twitch_user_id' => $user->id,
2024-07-27 15:40:35 -04:00
'command' => $parsedCommand['command'],
'x' => $parsedCommand['x'] ?? null,
'y' => $parsedCommand['y'] ?? null,
2024-07-28 18:06:05 -04:00
'palette_color_id' => $color->id,
2024-07-27 15:40:35 -04:00
'game_session_id' => $gameSession->id,
]);
switch ($parsedCommand['command']) {
case 'place':
2024-07-28 18:06:05 -04:00
$this->handlePlaceCommand($parsedCommand, $user->id, $gameSession->id, $color->id);
2024-07-27 15:40:35 -04:00
break;
// Add cases for other commands like 'row', 'column', 'fill'
}
2024-07-28 18:06:05 -04:00
return response()->json([
'command' => $parsedCommand,
'color' => [
'hex_value' => $color->hex_value
]
]);
2024-07-20 13:02:52 -04:00
}
2024-07-27 15:40:35 -04:00
Log::debug('TwitchController::parseChatMessage - Invalid command', ['message' => $message]);
2024-07-20 13:02:52 -04:00
return response()->json(['error' => 'Invalid command'], 400);
}
2024-07-27 15:40:35 -04:00
2024-07-28 18:06:05 -04:00
private function handlePlaceCommand($parsedCommand, $userId, $gameSessionId, $colorId)
2024-07-27 15:40:35 -04:00
{
Log::debug('TwitchController::handlePlaceCommand', ['command' => $parsedCommand]);
$x = $parsedCommand['x'];
$y = $parsedCommand['y'];
2024-07-28 18:06:05 -04:00
$currentState = CurrentState::where('x', $x)
->where('y', $y)
->where('game_session_id', $gameSessionId)
->first();
2024-07-27 15:40:35 -04:00
Log::debug('TwitchController::handlePlaceCommand - Retrieved Current State', ['currentState' => $currentState]);
if ($currentState) {
2024-07-28 18:06:05 -04:00
$currentState->update(['palette_color_id' => $colorId, 'updated_by' => $userId, 'updated_at' => now()]);
2024-07-27 15:40:35 -04:00
} else {
2024-07-28 18:06:05 -04:00
CurrentState::create([
'x' => $x,
'y' => $y,
'palette_color_id' => $colorId,
'updated_by' => $userId,
'updated_at' => now(),
'game_session_id' => $gameSessionId,
]);
2024-07-27 15:40:35 -04:00
}
}
2024-07-28 18:06:05 -04:00
private function getColorFromName($colorName, $paletteId)
2024-07-27 15:40:35 -04:00
{
2024-07-28 18:06:05 -04:00
return PaletteColor::where('palette_id', $paletteId)->where('name', $colorName)->first();
2024-07-27 15:40:35 -04:00
}
2024-07-20 13:02:52 -04:00
}