Backend implementation of full match chat

This commit is contained in:
Vari 2024-10-06 21:51:53 +02:00
parent fd5d25a26a
commit 84a10713d5
8 changed files with 153 additions and 6 deletions

View File

@ -0,0 +1,27 @@
<?php
namespace App\Classes\Frontend;
use Illuminate\Support\Carbon;
use JsonSerializable;
class ChatMessage implements JsonSerializable
{
public function __construct(
public string $gameId,
public string $userId,
public Carbon $messageTime,
public string $message,
)
{}
public function jsonSerialize(): array
{
return [
'gameId' => $this->gameId,
'userId' => $this->userId,
'messageTime' => $this->messageTime->toJSON(),
'message' => $this->message,
];
}
}

View File

@ -2,6 +2,7 @@
namespace App\Console\Commands;
use App\Enums\Game\Faction;
use App\Enums\Game\Matchmaking\MatchStatus;
use App\Models\Game\Matchmaking\Game;
use App\Models\Game\Matchmaking\QueuedPlayer;
@ -39,7 +40,7 @@ class MatchmakingCleanup extends Command
/**
* Execute the console command.
*/
public function handle()
public function handle(): void
{
$log = Log::channel('matchmaking_cleanup');
@ -50,12 +51,18 @@ public function handle()
$log->info('Queued players: ' . json_encode($cleanedQueuedPlayers, JSON_PRETTY_PRINT));
$deletedClosedGames = Game::where('status', '=', MatchStatus::Closed->value)
$closedGamesToKill = Game::where('status', '=', MatchStatus::Closed->value)
->where('updated_at', '<', Carbon::now()->subSeconds(static::GAME_MAX_TIME * 60))
->delete();
->get();
$log->info('Deleted Closed games: ' . json_encode($deletedClosedGames, JSON_PRETTY_PRINT));
foreach ($closedGamesToKill as $game) {
//archive the games set to closed, so we don't lose the chat history
$game->archiveGame(Faction::None);
$game->status = MatchStatus::Killed;
$game->save();
}
$log->info('Closed games set to Killed: ' . json_encode($closedGamesToKill->getQueueableIds(), JSON_PRETTY_PRINT));
// delete games where the hunter crashed on loading into the arena, leaving the game stuck at created
$deletedCreatedGames = Game::where('status', '=', MatchStatus::Created)

View File

@ -2,19 +2,26 @@
namespace App\Http\Controllers\Api;
use App\Classes\Frontend\ChatMessage;
use App\Enums\Game\Matchmaking\MatchStatus;
use App\Http\Controllers\Controller;
use App\Http\Requests\Api\Moderation\CheckChatMessageRequest;
use App\Http\Requests\Api\Moderation\ReportPlayerRequest;
use App\Http\Requests\Api\Player\CheckUsernameRequest;
use App\Http\Responses\Api\Player\CheckUsernameResponse;
use App\Models\Admin\BadChatMessage;
use App\Models\Game\Matchmaking\Game;
use App\Models\Game\Moderation\PlayerReport;
use App\Models\User\User;
use ConsoleTVs\Profanity\Builder;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Response;
class ModerationController extends Controller
{
const CHAT_CACHE_KEY = 'chat-messages-';
public function checkUsername(CheckUsernameRequest $request): mixed
{
@ -25,23 +32,39 @@ public function checkChatMessage(CheckChatMessageRequest $request): mixed
{
// Lobby Host that send the request to check the message. We log him too since only the lobby host checks the messages in teh backend.
$hostUser = Auth::user();
$activeGame = $hostUser->games()->whereNotIn('status', [MatchStatus::Killed])->first();
if($activeGame === null)
// Don't log and check anything if the user is not in a game, we don't care about chatting in the locker room.
return Response::json([]);
// User that send the chat message
$messageUser = User::find($request->userId);
$gameMessages = Cache::get(static::CHAT_CACHE_KEY . $activeGame->id, []);
$gameMessages[] = new ChatMessage(
$activeGame->id,
$messageUser->id,
Carbon::now(),
$request->message,
);
Cache::put(static::CHAT_CACHE_KEY . $activeGame->id, $gameMessages, 1800 /** 30 Minutes */);
$checker = Builder::blocker($request->message);
if($checker->clean())
return response('{}');
$this->logBadMessage($hostUser, $messageUser, $request->message);
$this->logBadMessage($hostUser, $messageUser, $request->message, $activeGame);
return response('Bad Message >:(');
}
public function logBadMessage(User $hostUser, User $messageUser, string $message): void {
public function logBadMessage(User $hostUser, User $messageUser, string $message, Game $match): void {
$badMessage = new BadChatMessage();
$badMessage->hostUser()->associate($hostUser);
$badMessage->user()->associate($messageUser);
$badMessage->message = $message;
$badMessage->match_id = $match->id;
$badMessage->save();
}

View File

@ -3,6 +3,7 @@
namespace App\Models\Admin\Archive;
use App\Enums\Game\Faction;
use Illuminate\Database\Eloquent\Concerns\HasUuids;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
@ -11,8 +12,11 @@
*/
class ArchivedGame extends Model
{
use HasUuids;
protected $casts = [
'dominant_faction' => Faction::class,
'chat_messages' => 'array',
];
public function archivedPlayerProgressions(): HasMany

View File

@ -8,6 +8,7 @@
use App\Enums\Game\Faction;
use App\Enums\Game\Matchmaking\MatchmakingSide;
use App\Enums\Game\Matchmaking\MatchStatus;
use App\Http\Controllers\Api\ModerationController;
use App\Http\Requests\Api\Matchmaking\PlayerEndOfMatchRequest;
use App\Models\Admin\Archive\ArchivedGame;
use App\Models\Admin\Archive\ArchivedPlayerProgression;
@ -107,6 +108,9 @@ public function archiveGame(Faction $dominantFaction): void
$archived = new ArchivedGame();
$archived->id = $this->id;
$archived->dominant_faction = $dominantFaction;
$archived->chat_messages = Cache::get(ModerationController::CHAT_CACHE_KEY . $this->id, []);
$archived->save();
}
}

View File

@ -0,0 +1,28 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('archived_games', function (Blueprint $table) {
$table->json('chat_messages')->after('dominant_faction');
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('archived_games', function (Blueprint $table) {
$table->dropColumn('chat_messages');
});
}
};

View File

@ -0,0 +1,28 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('bad_chat_messages', function (Blueprint $table) {
$table->uuid('match_id')->after('id')->nullable();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('bad_chat_messages', function (Blueprint $table) {
$table->dropColumn('match_id');
});
}
};

View File

@ -1,6 +1,10 @@
@php
use App\Models\Admin\Archive\ArchivedGame;
/** @var \App\Models\Admin\BadChatMessage $message */
$match = ArchivedGame::find($message->match_id);
@endphp
<form action="{{ route('chat-filter.handle', ['message' => $message->id]) }}" method="post" id="{{ $message->id }}form">
@csrf
<tr>
@ -33,6 +37,26 @@
<div class="bg-slate-700 border border-slate-500 p-2 rounded-md">
{{ $message->message }}
</div>
@if($match !== null)
<x-inputs.button
type="button"
href="#{{ $message->match_id }}-chat"
rel="modal:open"
>
@if($match === null)
not found :(
@else
match chat
@endif
</x-inputs.button>
<div id="{{ $match->id }}-chat" class="modal">
<div class="flex flex-col items-center gap-5">
<span>
{{ json_encode($match->chat_messages) }}
</span>
</div>
</div>
@endif
</td>
<td>
@if($message->handled)
@ -120,3 +144,5 @@ class="create"
@endif
</tr>
</form>