forked from Talishar/Talishar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLoadReplayTurn.php
More file actions
95 lines (79 loc) · 2.63 KB
/
LoadReplayTurn.php
File metadata and controls
95 lines (79 loc) · 2.63 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
<?php
include 'Libraries/HTTPLibraries.php';
include "HostFiles/Redirector.php";
include_once "Libraries/SHMOPLibraries.php";
SetHeaders();
header('Content-Type: application/json; charset=utf-8');
$response = new stdClass();
// Get parameters
$gameName = TryGet("gameName", "");
$turnNumber = intval(TryGet("turnNumber", 0));
$playerID = intval(TryGet("playerID", 3));
if (!IsGameNameValid($gameName)) {
$response->errorMessage = "Invalid game name.";
echo json_encode($response);
exit;
}
// Check if this is a replay
$isReplay = GetCachePiece($gameName, 10);
if ($isReplay !== "1") {
$response->errorMessage = "This game is not a replay.";
echo json_encode($response);
exit;
}
// Game directory
$gameDir = "./Games/" . $gameName . "/";
if (!is_dir($gameDir)) {
$response->errorMessage = "Game directory not found.";
echo json_encode($response);
exit;
}
// The original gamestate is stored as gamestate.txt (copied from replay's origGamestate.txt)
$origGamestateFile = $gameDir . "gamestate.txt";
if (!file_exists($origGamestateFile)) {
$response->errorMessage = "Game state file not found at: " . $origGamestateFile;
echo json_encode($response);
exit;
}
// Store the original gamestate content for later use
$origGamestateContent = file_get_contents($origGamestateFile);
// If turnNumber is 0, return the original gamestate
if ($turnNumber === 0) {
WriteGamestateCache($gameName, $origGamestateContent);
$response->success = true;
$response->message = "Loaded original gamestate.";
echo json_encode($response);
exit;
}
// Otherwise, try to load turn_{player}-{sequence}_Gamestate.txt
// The sequence number represents the game turn number
$turnLoaded = false;
// First, try the expected format: turn_{player}-{turnNumber}_Gamestate.txt
for ($p = 1; $p <= 2; $p++) {
$turnFile = $gameDir . "turn_" . $p . "-" . $turnNumber . "_Gamestate.txt";
if (file_exists($turnFile)) {
$gamestate = file_get_contents($turnFile);
WriteGamestateCache($gameName, $gamestate);
$response->success = true;
$response->message = "Loaded turn " . $turnNumber . ".";
$response->turnFile = basename($turnFile);
$turnLoaded = true;
break;
}
}
// If not found with exact match, list available turn files for debugging
if (!$turnLoaded) {
$turnFiles = glob($gameDir . "turn_*_Gamestate.txt");
$availableTurns = [];
foreach ($turnFiles as $file) {
$availableTurns[] = basename($file);
}
$response->errorMessage = "Turn " . $turnNumber . " not found.";
$response->availableTurns = $availableTurns;
$response->requestedTurnNumber = $turnNumber;
echo json_encode($response);
exit;
}
echo json_encode($response);
exit;
?>