Commit 425202c0 authored by Mahmoud Aglan's avatar Mahmoud Aglan

fix(friends): the host can cancel a challenge it sent

decline-invite only accepted the invited player, so a host backing out of the
lobby got 403 and the friend could still accept into an empty game for two
minutes. The host now cancels (result 'cancelled'); the target still declines.
Co-Authored-By: 's avatarClaude Opus 5.5 <noreply@anthropic.com>
parent 67e39c8d
...@@ -702,12 +702,15 @@ if ($method === 'POST') { ...@@ -702,12 +702,15 @@ if ($method === 'POST') {
jsonError('Invite not found or expired'); jsonError('Invite not found or expired');
} }
$gs = is_array($matches[0]['game_state']) ? $matches[0]['game_state'] : json_decode($matches[0]['game_state'] ?? '{}', true); $gs = is_array($matches[0]['game_state']) ? $matches[0]['game_state'] : json_decode($matches[0]['game_state'] ?? '{}', true);
if (($gs['invite_to'] ?? '') !== $userId) { // The invited friend declines; the host who sent it cancels.
$isTarget = ($gs['invite_to'] ?? '') === $userId;
$isHost = ($gs['invite_from'] ?? '') === $userId;
if (!$isTarget && !$isHost) {
jsonError('Not authorized to decline this invite', 403); jsonError('Not authorized to decline this invite', 403);
} }
$sdb->update('domino_matches', [ $sdb->update('domino_matches', [
'status' => 'completed', 'status' => 'completed',
'result' => 'declined' 'result' => $isHost ? 'cancelled' : 'declined'
], ['id' => 'eq.' . $matchId, 'status' => 'eq.waiting']); ], ['id' => 'eq.' . $matchId, 'status' => 'eq.waiting']);
} elseif ($gameKey === 'ludo') { } elseif ($gameKey === 'ludo') {
$matches = $sdb->get('ludo_matches', [ $matches = $sdb->get('ludo_matches', [
...@@ -720,12 +723,15 @@ if ($method === 'POST') { ...@@ -720,12 +723,15 @@ if ($method === 'POST') {
jsonError('Invite not found or expired'); jsonError('Invite not found or expired');
} }
$gs = is_array($matches[0]['game_state']) ? $matches[0]['game_state'] : json_decode($matches[0]['game_state'] ?? '{}', true); $gs = is_array($matches[0]['game_state']) ? $matches[0]['game_state'] : json_decode($matches[0]['game_state'] ?? '{}', true);
if (($gs['invite_to'] ?? '') !== $userId) { // The invited friend declines; the host who sent it cancels.
$isTarget = ($gs['invite_to'] ?? '') === $userId;
$isHost = ($gs['invite_from'] ?? '') === $userId;
if (!$isTarget && !$isHost) {
jsonError('Not authorized to decline this invite', 403); jsonError('Not authorized to decline this invite', 403);
} }
$sdb->update('ludo_matches', [ $sdb->update('ludo_matches', [
'status' => 'completed', 'status' => 'completed',
'result' => 'declined' 'result' => $isHost ? 'cancelled' : 'declined'
], ['id' => 'eq.' . $matchId, 'status' => 'eq.waiting']); ], ['id' => 'eq.' . $matchId, 'status' => 'eq.waiting']);
} elseif ($gameKey === 'backgammon') { } elseif ($gameKey === 'backgammon') {
$matches = $sdb->get('backgammon_matches', [ $matches = $sdb->get('backgammon_matches', [
...@@ -738,12 +744,15 @@ if ($method === 'POST') { ...@@ -738,12 +744,15 @@ if ($method === 'POST') {
jsonError('Invite not found or expired'); jsonError('Invite not found or expired');
} }
$gs = is_array($matches[0]['game_state']) ? $matches[0]['game_state'] : json_decode($matches[0]['game_state'] ?? '{}', true); $gs = is_array($matches[0]['game_state']) ? $matches[0]['game_state'] : json_decode($matches[0]['game_state'] ?? '{}', true);
if (($gs['invite_to'] ?? '') !== $userId) { // The invited friend declines; the host who sent it cancels.
$isTarget = ($gs['invite_to'] ?? '') === $userId;
$isHost = ($gs['invite_from'] ?? '') === $userId;
if (!$isTarget && !$isHost) {
jsonError('Not authorized to decline this invite', 403); jsonError('Not authorized to decline this invite', 403);
} }
$sdb->update('backgammon_matches', [ $sdb->update('backgammon_matches', [
'status' => 'completed', 'status' => 'completed',
'result' => 'declined' 'result' => $isHost ? 'cancelled' : 'declined'
], ['id' => 'eq.' . $matchId, 'status' => 'eq.waiting']); ], ['id' => 'eq.' . $matchId, 'status' => 'eq.waiting']);
} else { } else {
$matches = $sdb->get('matches', [ $matches = $sdb->get('matches', [
...@@ -756,12 +765,15 @@ if ($method === 'POST') { ...@@ -756,12 +765,15 @@ if ($method === 'POST') {
jsonError('Invite not found or expired'); jsonError('Invite not found or expired');
} }
$gs = is_array($matches[0]['game_state']) ? $matches[0]['game_state'] : json_decode($matches[0]['game_state'] ?? '{}', true); $gs = is_array($matches[0]['game_state']) ? $matches[0]['game_state'] : json_decode($matches[0]['game_state'] ?? '{}', true);
if (($gs['invite_to'] ?? '') !== $userId) { // The invited friend declines; the host who sent it cancels.
$isTarget = ($gs['invite_to'] ?? '') === $userId;
$isHost = ($gs['invite_from'] ?? '') === $userId;
if (!$isTarget && !$isHost) {
jsonError('Not authorized to decline this invite', 403); jsonError('Not authorized to decline this invite', 403);
} }
$sdb->update('matches', [ $sdb->update('matches', [
'status' => 'completed', 'status' => 'completed',
'result' => 'declined' 'result' => $isHost ? 'cancelled' : 'declined'
], ['id' => 'eq.' . $matchId, 'status' => 'eq.waiting']); ], ['id' => 'eq.' . $matchId, 'status' => 'eq.waiting']);
} }
......
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