forked from Talishar/Talishar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGetPopupAPI.php
More file actions
302 lines (279 loc) · 12.6 KB
/
GetPopupAPI.php
File metadata and controls
302 lines (279 loc) · 12.6 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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
<?php
session_start();
// CRITICAL: Capture session data immediately and release the lock
// PHP sessions use exclusive file locks - holding the lock during processing
// blocks all other requests from this user, causing session deadlock.
$sessionUserId = $_SESSION["userid"] ?? null;
// Release session lock before heavy processing
// Note: loginFromCookie() will re-acquire and release session if needed
session_write_close();
include "./Libraries/HTTPLibraries.php";
$gameName = isset($_GET["gameName"]) ? $_GET["gameName"] : "";
$playerID = isset($_GET["playerID"]) ? $_GET["playerID"] : "0";
// For profile settings (playerID == 0), allow gameName == 0
if ($playerID == 0) {
// Profile settings request - skip game name validation
} else {
// Normal game request - validate game name
if (!IsGameNameValid($gameName)) {
echo ("Invalid game name.");
exit;
}
}
$authKey = TryGet("authKey", "");
$popupType = isset($_GET["popupType"]) ? $_GET["popupType"] : "";
$chainLinkIndex = TryGet("index", "");
ob_start();
include_once "./Libraries/SHMOPLibraries.php";
// For profile settings, skip ParseGamestate
if ($playerID != 0) {
include "./ParseGamestate.php";
}
include "./GameLogic.php";
include "./Libraries/UILibraries.php";
include "./Libraries/StatFunctions.php";
include "./Libraries/PlayerSettings.php";
include_once 'Assets/patreon-php-master/src/PatreonDictionary.php';
include "./GameTerms.php";
include "./HostFiles/Redirector.php";
include_once "./includes/dbh.inc.php";
include_once "./includes/functions.inc.php";
ob_end_clean();
if ($playerID == 3) {
include_once "./AccountFiles/AccountSessionAPI.php";
if (!IsUserLoggedIn()) {
loginFromCookie();
if (!IsUserLoggedIn()) {
if (!isset($response)) {
$response = new stdClass();
}
$response->error = "You must be logged in to spectate games";
echo json_encode($response);
exit;
}
}
}
// CORS etc *must* be set for all endpoints
SetHeaders();
$cardSize = 120;
$params = explode("-", $popupType);
$popupType = $params[0];
$response = new stdClass();
switch ($popupType) {
case "attackSummary":
$totalPower = 0;
$totalDefense = 0;
$powerModifiers = [];
$response->Cards = [];
EvaluateCombatChain($totalPower, $totalDefense, $powerModifiers);
for ($i = 0; $i < count($powerModifiers); $i += 2) {
$thisModifier = new stdClass();
$idArr = explode("-", $powerModifiers[$i]);
$cardID = ExtractCardID($idArr[0]);
$bonus = $powerModifiers[$i + 1];
if ($bonus == 0) continue;
$effectName = CardName($cardID);
if($effectName == "")
{
$effectName = $cardID;
$cardID = "";
}
$thisModifier->Player = $mainPlayer;
$thisModifier->Name = $effectName;
$thisModifier->cardID = $cardID;
$thisModifier->modifier = $bonus;
array_push($response->Cards, $thisModifier);
}
break;
case "myPitchPopup":
JSONPopup($response, $myPitch, PitchPieces());
break;
case "myDiscardPopup":
JSONPopup($response, $myDiscard, DiscardPieces());
break;
case "myBanishPopup":
JSONPopup($response, $myBanish, BanishPieces());
break;
case "myDeckPopup":
JSONPopup($response, $myDeck, DeckPieces());
break;
case "myStatsPopup":
if($turn[0] == "OVER") SetCachePiece($gameName, 14, 99);//$MGS_GameOver
// Get opponent's hero for export display
$opponentPlayerID = ($playerID == 1 ? 2 : 1);
$opponentDeckFile = "./Games/" . $gameName . "/p" . $opponentPlayerID . "Deck.txt";
$opponentHero = "";
if(file_exists($opponentDeckFile)) {
$opponentDeckContent = file_get_contents($opponentDeckFile);
$opponentDeckLines = explode("\r\n", $opponentDeckContent);
if(count($opponentDeckLines) > 0) {
$opponentHeroLine = explode(" ", trim($opponentDeckLines[0]));
if(count($opponentHeroLine) > 0) {
$opponentHero = $opponentHeroLine[0];
}
}
}
$myDeckFile = "./Games/" . $gameName . "/p" . $playerID . "Deck.txt";
$myDeckContents = file_exists($myDeckFile) ? file_get_contents($myDeckFile) : "";
echo(SerializeGameResult($playerID, "", $myDeckContents, $gameName, $opponentHero, "", "", includeFullLog:true));
exit;
case "mySoulPopup":
JSONPopup($response, $mySoul, SoulPieces());
break;
case "theirBanishPopup":
JSONPopup($response, $theirBanish, BanishPieces());
break;
case "theirPitchPopup":
JSONPopup($response, $theirPitch, PitchPieces());
break;
case "theirDiscardPopup":
JSONPopup($response, $theirDiscard, DiscardPieces());
break;
case "theirSoulPopup":
JSONPopup($response, $theirSoul, SoulPieces());
break;
case "chainLinkPopup":
$popupIndex = intval(($chainLinkIndex ?? $params[1]));
$response = ChainLinkObject($popupIndex);
$index = $popupIndex * ChainLinkSummaryPieces();
if (isset($chainLinkSummary[$index])) {
$response->TotalDamageDealt = $chainLinkSummary[$index];
} else {
$response->TotalDamageDealt = 0;
}
break;
case "mySettings":
global $SET_AlwaysHoldPriority, $SET_TryUI2, $SET_DarkMode, $SET_ManualMode, $SET_SkipARs, $SET_SkipDRs;
global $SET_PassDRStep, $SET_AutotargetArcane, $SET_ColorblindMode, $SET_ShortcutAttackThreshold, $SET_EnableDynamicScaling;
global $SET_Mute, $SET_Cardback, $SET_IsPatron, $SET_MuteChat, $SET_DisableStats, $SET_CasterMode, $SET_StreamerMode, $SET_AlwaysShowCounters;
global $SET_Playmat, $SET_AlwaysAllowUndo, $SET_DisableAltArts, $SET_ManualTunic, $SET_DisableFabInsights, $SET_DisableHeroIntro, $SET_MirroredBoardLayout, $SET_MirroredPlayerBoardLayout, $SET_HideHandFromFriends;
$response->Settings = [];
// For profile settings (playerID == 0), load from database
if ($playerID == 0) {
include_once "./includes/functions.inc.php";
// Use captured session data (session already closed to prevent deadlock)
$userID = $sessionUserId ?? "";
$dbSettingsFlat = LoadSavedSettings($userID);
// Convert flat array to associative array: [settingID => settingValue]
$dbSettings = [];
for ($i = 0; $i < count($dbSettingsFlat); $i += 2) {
$settingNumber = $dbSettingsFlat[$i];
$settingValue = $dbSettingsFlat[$i + 1];
$dbSettings[$settingNumber] = $settingValue;
}
// Using numeric IDs from ParseSettingsStringValueToIdInt
AddSettingFromDB($response->Settings, "HoldPrioritySetting", 0, $dbSettings);
AddSettingFromDB($response->Settings, "TryReactUI", 1, $dbSettings);
AddSettingFromDB($response->Settings, "DarkMode", 2, $dbSettings);
AddSettingFromDB($response->Settings, "ManualMode", 3, $dbSettings);
AddSettingFromDB($response->Settings, "SkipARWindow", 4, $dbSettings);
AddSettingFromDB($response->Settings, "SkipDRWindow", 5, $dbSettings);
AddSettingFromDB($response->Settings, "AutoTargetOpponent", 7, $dbSettings);
AddSettingFromDB($response->Settings, "ColorblindMode", 8, $dbSettings);
AddSettingFromDB($response->Settings, "ShortcutAttackThreshold", 9, $dbSettings);
AddSettingFromDB($response->Settings, "MuteSound", 11, $dbSettings);
AddSettingFromDB($response->Settings, "CardBack", 12, $dbSettings);
AddSettingFromDB($response->Settings, "IsPatron", 13, $dbSettings);
AddSettingFromDB($response->Settings, "MuteChat", 14, $dbSettings);
AddSettingFromDB($response->Settings, "DisableStats", 15, $dbSettings);
AddSettingFromDB($response->Settings, "DisableAltArts", 26, $dbSettings);
AddSettingFromDB($response->Settings, "IsCasterMode", 16, $dbSettings);
AddSettingFromDB($response->Settings, "IsStreamerMode", 23, $dbSettings);
AddSettingFromDB($response->Settings, "Playmat", 24, $dbSettings);
AddSettingFromDB($response->Settings, "AlwaysAllowUndo", 25, $dbSettings);
AddSettingFromDB($response->Settings, "ManualTunic", 27, $dbSettings);
AddSettingFromDB($response->Settings, "DisableFabInsights", 28, $dbSettings);
AddSettingFromDB($response->Settings, "DisableHeroIntro", 29, $dbSettings);
AddSettingFromDB($response->Settings, "MirroredBoardLayout", 30, $dbSettings);
AddSettingFromDB($response->Settings, "MirroredPlayerBoardLayout", 31, $dbSettings);
AddSettingFromDB($response->Settings, "AlwaysShowCounters", 32, $dbSettings);
AddSettingFromDB($response->Settings, "HideHandFromFriends", 33, $dbSettings);
} else {
// Normal game settings
AddSetting($response->Settings, "HoldPrioritySetting", $SET_AlwaysHoldPriority);
AddSetting($response->Settings, "TryReactUI", $SET_TryUI2);
AddSetting($response->Settings, "DarkMode", $SET_DarkMode);
AddSetting($response->Settings, "ManualMode", $SET_ManualMode);
AddSetting($response->Settings, "SkipARWindow", $SET_SkipARs);
AddSetting($response->Settings, "SkipDRWindow", $SET_SkipDRs);
AddSetting($response->Settings, "AutoTargetOpponent", $SET_AutotargetArcane);
AddSetting($response->Settings, "ColorblindMode", $SET_ColorblindMode);
AddSetting($response->Settings, "ShortcutAttackThreshold", $SET_ShortcutAttackThreshold);
AddSetting($response->Settings, "MuteSound", $SET_Mute);
AddSetting($response->Settings, "CardBack", $SET_Cardback);
AddSetting($response->Settings, "IsPatron", $SET_IsPatron);
AddSetting($response->Settings, "MuteChat", $SET_MuteChat);
AddSetting($response->Settings, "DisableStats", $SET_DisableStats);
AddSetting($response->Settings, "DisableAltArts", $SET_DisableAltArts);
AddSetting($response->Settings, "IsCasterMode", $SET_CasterMode);
AddSetting($response->Settings, "IsStreamerMode", $SET_StreamerMode);
AddSetting($response->Settings, "Playmat", $SET_Playmat);
AddSetting($response->Settings, "AlwaysAllowUndo", $SET_AlwaysAllowUndo);
AddSetting($response->Settings, "ManualTunic", $SET_ManualTunic);
AddSetting($response->Settings, "DisableFabInsights", $SET_DisableFabInsights);
AddSetting($response->Settings, "DisableHeroIntro", $SET_DisableHeroIntro);
AddSetting($response->Settings, "MirroredBoardLayout", $SET_MirroredBoardLayout);
AddSetting($response->Settings, "MirroredPlayerBoardLayout", $SET_MirroredPlayerBoardLayout);
AddSetting($response->Settings, "AlwaysShowCounters", $SET_AlwaysShowCounters);
AddSetting($response->Settings, "HideHandFromFriends", $SET_HideHandFromFriends);
$response->isSpectatingEnabled = GetCachePiece($gameName, 9) == "1";
}
break;
default:
break;
}
echo json_encode($response);
function AddSettingFromDB(&$response, $name, $settingID, $dbSettings)
{
$thisSetting = new stdClass();
$thisSetting->name = $name;
$thisSetting->value = isset($dbSettings[$settingID]) ? $dbSettings[$settingID] : null;
array_push($response, $thisSetting);
}
function AddSetting(&$response, $name, $setting)
{
global $playerID;
$mySettings = &GetSettings($playerID);
$thisSetting = new stdClass();
$thisSetting->name = $name;
// Use isset to handle new settings that may not exist for existing players
$thisSetting->value = isset($mySettings[$setting]) ? $mySettings[$setting] : "0";
array_push($response, $thisSetting);
}
function JSONPopup($response, $zone, $zonePieces)
{
$response->cards = [];
for($i=0; $i<count($zone); $i+=$zonePieces)
{
array_push($response->cards, JSONRenderedCard($zone[$i]));
}
}
function ChainLinkObject($link)
{
global $chainLinks, $mainPlayer, $defPlayer;
$chainLink = new stdClass();
$chainLink->Cards = [];
if (!is_array($chainLinks) || empty($chainLinks[$link])) {
return $chainLink;
}
for ($i = 0; $i < count($chainLinks[$link]); $i += ChainLinksPieces()) {
$card = new stdClass();
$card->Player = $chainLinks[$link][$i+1];
if ($chainLinks[$link][$i + 1] == $mainPlayer && CardType($chainLinks[$link][$i]) != "AR")
{
$powerValue = PowerValue($chainLinks[$link][$i], $mainPlayer, "CC") + $chainLinks[$link][$i + 4];
}
elseif ($chainLinks[$link][$i + 1] == $mainPlayer && (CardType($chainLinks[$link][$i]) == "AR" || DelimStringContains(CardType($chainLinks[$link][$i]), "I")))
{
$powerValue = PowerModifier($chainLinks[$link][$i]);
}
else $powerValue = 0;
$uid = $chainLinks[$link][$i + 8];
$blockValue = ($chainLinks[$link][$i + 1] == $defPlayer) ? ModifiedBlockValue($chainLinks[$link][$i], $defPlayer, "CC", "", $uid) + $chainLinks[$link][$i + 5] : 0;
$card->modifier = ($card->Player == $mainPlayer) ? $powerValue : $blockValue;
$card->cardID = $chainLinks[$link][$i];
$card->Name = CardName($chainLinks[$link][$i]);
array_push($chainLink->Cards, $card);
}
return $chainLink;
}