Commit cbe84447 authored by Mahmoud Aglan's avatar Mahmoud Aglan

fix: bot testing now auto-creates Swiss API link if missing

Previously the bot testing section was hidden when swiss_api_tournament_id
was null. Now it always shows, and when you click "Fill with Bots" it
auto-creates the Swiss org/event/tournament on the fly.
Co-Authored-By: 's avatarClaude Opus 4.6 <noreply@anthropic.com>
parent b95c3bce
...@@ -24,6 +24,55 @@ class BotSimulationService ...@@ -24,6 +24,55 @@ class BotSimulationService
'العمري', 'الحربي', 'الشهري' 'العمري', 'الحربي', 'الشهري'
]; ];
private static function ensureSwissApiLink(string $tournamentId, array $tournament): array
{
$db = Database::getInstance();
// Get or create a Swiss API organization
$orgResponse = SwissApiService::listOrganizations();
$orgs = SwissApiService::isSuccess($orgResponse) ? (SwissApiService::getBody($orgResponse) ?? []) : [];
$swissOrgId = $orgs[0]['id'] ?? null;
if (!$swissOrgId) {
$createOrgResp = SwissApiService::createOrganization('El3ab Bot Testing', SWISS_API_EMAIL);
if (!SwissApiService::isSuccess($createOrgResp)) {
return ['success' => false, 'error' => 'فشل في إنشاء منظمة Swiss API'];
}
$swissOrgId = SwissApiService::getBody($createOrgResp)['id'] ?? null;
}
// Create event
$eventResp = SwissApiService::createEvent(
$swissOrgId,
$tournament['name'] ?? 'Bot Test Tournament',
date('Y-m-d'),
date('Y-m-d', strtotime('+1 day'))
);
if (!SwissApiService::isSuccess($eventResp)) {
return ['success' => false, 'error' => 'فشل في إنشاء حدث Swiss API'];
}
$swissEventId = SwissApiService::getBody($eventResp)['id'] ?? null;
// Create tournament
$tournResp = SwissApiService::createTournament($swissEventId, [
'name' => $tournament['name'] ?? 'Bot Test',
'tournamentType' => 'swiss',
'roundsNumber' => $tournament['rounds_total'] ?? $tournament['swiss_rounds'] ?? 7,
'maxPlayers' => $tournament['max_players'] ?? 200,
]);
if (!SwissApiService::isSuccess($tournResp)) {
return ['success' => false, 'error' => 'فشل في إنشاء بطولة Swiss API'];
}
$swissTournamentId = SwissApiService::getBody($tournResp)['id'] ?? null;
// Update local tournament with Swiss API link
$db->update('el3ab_tournaments', ['id' => "eq.{$tournamentId}"], [
'swiss_api_tournament_id' => $swissTournamentId,
]);
return ['success' => true, 'swiss_api_tournament_id' => $swissTournamentId];
}
public static function generateBotPlayers(int $count, int $centerRating = 1500, int $stdDev = 300): array public static function generateBotPlayers(int $count, int $centerRating = 1500, int $stdDev = 300): array
{ {
$players = []; $players = [];
...@@ -76,8 +125,15 @@ class BotSimulationService ...@@ -76,8 +125,15 @@ class BotSimulationService
return ['success' => false, 'error' => 'البطولة غير موجودة']; return ['success' => false, 'error' => 'البطولة غير موجودة'];
} }
if (!$tournament['swiss_api_tournament_id']) { $swissTournamentId = $tournament['swiss_api_tournament_id'];
return ['success' => false, 'error' => 'البطولة غير مرتبطة بـ Swiss API'];
// Auto-create Swiss API tournament if not linked yet
if (!$swissTournamentId) {
$linkResult = self::ensureSwissApiLink($tournamentId, $tournament);
if (!$linkResult['success']) {
return $linkResult;
}
$swissTournamentId = $linkResult['swiss_api_tournament_id'];
} }
$players = self::generateBotPlayers($count, $centerRating, $stdDev); $players = self::generateBotPlayers($count, $centerRating, $stdDev);
...@@ -87,7 +143,7 @@ class BotSimulationService ...@@ -87,7 +143,7 @@ class BotSimulationService
'fideRatingStandard' => $p['rating'], 'fideRatingStandard' => $p['rating'],
], $players); ], $players);
$response = SwissApiService::bulkImportPlayers($tournament['swiss_api_tournament_id'], $swissPlayers); $response = SwissApiService::bulkImportPlayers($swissTournamentId, $swissPlayers);
if (!SwissApiService::isSuccess($response)) { if (!SwissApiService::isSuccess($response)) {
return ['success' => false, 'error' => 'فشل في إضافة اللاعبين للـ Swiss API: ' . SwissApiService::getError($response)]; return ['success' => false, 'error' => 'فشل في إضافة اللاعبين للـ Swiss API: ' . SwissApiService::getError($response)];
......
...@@ -69,9 +69,7 @@ ...@@ -69,9 +69,7 @@
<?php endif; ?> <?php endif; ?>
<!-- Bot Testing --> <!-- Bot Testing -->
<?php if (!empty($tournament['swiss_api_tournament_id'])): ?>
<div class="mt-4"> <div class="mt-4">
<?php include __DIR__ . '/_bot_testing.php'; ?> <?php include __DIR__ . '/_bot_testing.php'; ?>
</div> </div>
<?php endif; ?>
</div> </div>
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment