forked from Talishar/Talishar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGetNextTurn.php
More file actions
132 lines (111 loc) · 4.01 KB
/
GetNextTurn.php
File metadata and controls
132 lines (111 loc) · 4.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php
/**
* GetNextTurn.php
*
* HTTP endpoint for polling game state updates.
* This is a thin wrapper around BuildGameStateResponse().
*
* Note: The primary game state delivery mechanism is now SSE (GetUpdateSSE.php).
* This endpoint remains for backwards compatibility and fallback scenarios.
*/
include 'Libraries/HTTPLibraries.php';
include "HostFiles/Redirector.php";
include_once "Libraries/SHMOPLibraries.php";
include "WriteLog.php";
include_once "./Assets/patreon-php-master/src/PatreonDictionary.php";
include_once "./Assets/MetafyDictionary.php";
include_once "./AccountFiles/AccountSessionAPI.php";
include_once "Libraries/CacheLibraries.php";
include_once "includes/dbh.inc.php";
include_once "includes/MetafyHelper.php";
include_once "BuildGameState.php";
include_once "BuildPlayerInputPopup.php";
// Set CORS headers
SetHeaders();
header('Content-Type: application/json; charset=utf-8');
// Validate game name
$gameName = $_GET["gameName"];
if (!IsGameNameValid($gameName)) {
echo json_encode(["errorMessage" => "Invalid game name."]);
exit;
}
// Validate player ID
$playerID = TryGet("playerID", 3);
if (!is_numeric($playerID)) {
echo json_encode(["errorMessage" => "Invalid player ID."]);
exit;
}
// Check spectator permission
if ($playerID == 3 && GetCachePiece($gameName, 9) != "1") {
header('HTTP/1.0 403 Forbidden');
exit;
}
// Get auth key
$authKey = TryGet("authKey", "");
$lastUpdate = intval(TryGet("lastUpdate", 0));
if (($playerID == 1 || $playerID == 2) && $authKey == "") {
if (isset($_COOKIE["lastAuthKey"])) $authKey = $_COOKIE["lastAuthKey"];
}
// CRITICAL: Capture all needed session data upfront and release the session lock immediately.
// PHP sessions use exclusive file locks - if we hold the lock while processing game state,
// all other requests from this user will be blocked, causing session deadlock.
$sessionData = [];
$sessionData['userLoggedIn'] = IsUserLoggedIn();
$sessionData['userName'] = $sessionData['userLoggedIn'] ? LoggedInUserName() : null;
$sessionData['isPvtVoidPatron'] = isset($_SESSION["isPvtVoidPatron"]);
// Capture all Patreon campaign session IDs before releasing session
$sessionData['patreonCampaigns'] = [];
foreach(PatreonCampaign::cases() as $campaign) {
$sessionID = $campaign->SessionID();
$sessionData['patreonCampaigns'][$sessionID] = isset($_SESSION[$sessionID]);
}
// Load friend list if user is logged in (for friend hand visibility checks)
$sessionData['friendList'] = [];
$friendsListParam = TryGet("friendsList", "");
if (!empty($friendsListParam)) {
try {
$sessionData['friendList'] = json_decode($friendsListParam, true) ?? [];
} catch (Exception $e) {
// friendsList parameter parsing failed
}
}
// Release the session lock NOW - before any file I/O or processing
if (session_status() === PHP_SESSION_ACTIVE) {
session_write_close();
}
$isGamePlayer = $playerID == 1 || $playerID == 2;
$currentTime = round(microtime(true) * 1000);
// Track player connection status
if ($isGamePlayer) {
$playerStatus = intval(GetCachePiece($gameName, $playerID + 3));
if ($playerStatus == "-1") WriteLog("🔌Player $playerID has connected.");
SetCachePiece($gameName, $playerID + 1, $currentTime);
SetCachePiece($gameName, $playerID + 3, "0");
if ($playerStatus > 0) {
WriteLog("🔌Player $playerID has reconnected.");
SetCachePiece($gameName, $playerID + 3, "0");
}
}
if ($playerID == 3) {
UpdateSpectatorPresence($gameName);
}
// Check if game file exists
if (!file_exists("./Games/" . $gameName . "/GameFile.txt")) {
echo json_encode(["errorMessage" => "Game no longer exists on the server."]);
exit;
}
// Check cache value for updates (optional optimization)
$cacheVal = intval(GetCachePiece($gameName, 1));
if ($lastUpdate != 0 && $cacheVal <= $lastUpdate) {
echo "0";
exit;
}
// Build and return the game state
$response = BuildGameStateResponse($gameName, $playerID, $authKey, $sessionData, true);
if (is_string($response)) {
// Error occurred
echo json_encode(["errorMessage" => $response]);
exit;
}
echo json_encode($response);
exit;