forked from SWU-Karabast/SWUOnline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathJoinGameInput.php
418 lines (385 loc) · 14.2 KB
/
JoinGameInput.php
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
<?php
include "Libraries/HTTPLibraries.php";
include "Libraries/SHMOPLibraries.php";
include "APIKeys/APIKeys.php";
include_once 'includes/functions.inc.php';
include_once 'includes/dbh.inc.php';
include_once 'CoreLogic.php';
include_once 'Libraries/CoreLibraries.php';
include_once "WriteLog.php";
include_once 'LZCompressor/LZContext.php';
include_once 'LZCompressor/LZData.php';
include_once 'LZCompressor/LZReverseDictionary.php';
include_once 'LZCompressor/LZString.php';
include_once 'LZCompressor/LZUtil.php';
include_once 'LZCompressor/LZUtil16.php';
use LZCompressor\LZString as LZString;
session_start();
if (!isset($_SESSION["userid"])) {
if (isset($_COOKIE["rememberMeToken"])) {
include_once './Assets/patreon-php-master/src/PatreonLibraries.php';
include_once './Assets/patreon-php-master/src/API.php';
include_once './Assets/patreon-php-master/src/PatreonDictionary.php';
loginFromCookie();
}
}
$gameName = $_GET["gameName"];
if (!IsGameNameValid($gameName)) {
echo ("Invalid game name.");
exit;
}
$playerID = intval($_GET["playerID"]);
$deck = TryGet("deck");
$decklink = $_GET["fabdb"];
$decksToTry = TryGet("decksToTry");
$favoriteDeck = TryGet("favoriteDeck", "0");
$favoriteDeckLink = TryGet("favoriteDecks", "0");
$set = TryGet("set");
$matchup = TryGet("matchup", "");
$starterDeck = false;
if ($matchup == "" && GetCachePiece($gameName, $playerID + 6) != "") {
$_SESSION['error'] = '⚠️ Another player has already joined the game.';
header("Location: MainMenu.php");
die();
}
include "HostFiles/Redirector.php";
include "CardDictionary.php";
include "MenuFiles/ParseGamefile.php";
include "MenuFiles/WriteGamefile.php";
if($playerID == 2 && isset($_SESSION["userid"])) {
$isBlocked = false;
$blockedPlayers = LoadBlockedPlayers($_SESSION["userid"]);
for($i=0; $i<count($blockedPlayers); ++$i) {
if($blockedPlayers[$i] == $p1id) {
$isBlocked = true;
break;
}
}
if ($isBlocked) {
$_SESSION['error'] = '⚠️ Another player has already joined the game.';
header("Location: MainMenu.php");
die();
}
if ($matchup == "" && GetCachePiece($gameName, $playerID + 6) != "") {
$_SESSION['error'] = '⚠️ Another player has already joined the game.';
header("Location: MainMenu.php");
die();
}
}
if ($decklink == "" && $deck == "" && $favoriteDeckLink == "0") {
$starterDeck = true;
switch($decksToTry) {
default:
$deck = "./test.txt";
break;
}
}
if ($favoriteDeckLink != "0" && $decklink == "") $decklink = $favoriteDeckLink;
if ($deck == "" && !IsDeckLinkValid($decklink)) {
echo '<b>' . "⚠️ Deck URL is not valid: " . $decklink . '</b>';
exit;
}
if ($matchup == "" && $playerID == 2 && $gameStatus >= $MGS_Player2Joined) {
if ($gameStatus >= $MGS_GameStarted) {
header("Location: " . $redirectPath . "/NextTurn4.php?gameName=$gameName&playerID=3");
} else {
header("Location: " . $redirectPath . "/MainMenu.php");
}
WriteGameFile();
exit;
}
$usesUuid = false;
if ($decklink != "") {
if ($playerID == 1) $p1DeckLink = $decklink;
else if ($playerID == 2) $p2DeckLink = $decklink;
$originalLink = $decklink;
if(str_contains($decklink, "swustats.net")) {
$decklinkArr = explode("gameName=", $decklink);
if(count($decklinkArr) > 1) {
$deckLinkArr = explode("&", $decklinkArr[1]);
$deckID = $deckLinkArr[0];
$decklink = "https://swustats.net/TCGEngine/APIs/LoadDeck.php?deckID=" . $deckID . "&format=json";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $decklink);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$apiDeck = curl_exec($curl);
$apiInfo = curl_getinfo($curl);
$errorMessage = curl_error($curl);
curl_close($curl);
$json = $apiDeck;
echo($json);
$usesUuid = true;
}
}
else if(str_contains($decklink, "swudb.com/deck")) {
$decklinkArr = explode("/", $decklink);
$decklink = "https://swudb.com/api/getDeckJson/" . trim($decklinkArr[count($decklinkArr) - 1]);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $decklink);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$apiDeck = curl_exec($curl);
$apiInfo = curl_getinfo($curl);
$errorMessage = curl_error($curl);
curl_close($curl);
$json = $apiDeck;
echo($json);
}
else if(str_contains($decklink, "sw-unlimited-db.com/decks")) {
$decklinkArr = explode("/", $decklink);
$deckId = trim($decklinkArr[count($decklinkArr) - 1]);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "https://sw-unlimited-db.com/umbraco/api/deckapi/get?id=" . $deckId);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$apiDeck = curl_exec($curl);
$apiInfo = curl_getinfo($curl);
$errorMessage = curl_error($curl);
curl_close($curl);
$json = $apiDeck;
echo($json);
}
else $json = $decklink;
if($json == "") {
echo "Failed to retrieve deck from API. Check to make sure you have a valid deckbuilder link. If it's a SWUDB link, make sure it's not a private deck.<BR>";
echo "Your link: " . $originalLink . "<BR>";
echo "API link: " . $decklink . "<BR>";
echo "Error Message: " . $errorMessage . "<BR>";
exit;
}
$deckObj = json_decode($json);
$deckName = $deckObj->metadata->{"name"};
$leader = !$usesUuid ? UUIDLookup($deckObj->leader->id) : $deckObj->leader->id;
$character = $leader;//TODO: Change to leader name
$deckFormat = 1;
$base = !$usesUuid ? UUIDLookup($deckObj->base->id) : $deckObj->base->id;
$deck = $deckObj->deck;
$cards = "";
$bannedSet = "JTL";
$hasBannedCard = false;
for($i=0; $i<count($deck); ++$i) {
if($usesUuid) $deck[$i]->id = CardIDLookup($deck[$i]->id);
$deck[$i]->id = CardIDOverride($deck[$i]->id);
$cardID = UUIDLookup($deck[$i]->id);
$cardID = CardUUIDOverride($cardID);
if(CardSet($cardID) == $bannedSet) {
$hasBannedCard = true;
}
for($j=0; $j<$deck[$i]->count; ++$j) {
if($cards != "") $cards .= " ";
$cards .= $cardID;
}
}
$sideboard = $deckObj->sideboard ?? [];
$sideboardCards = "";
for($i=0; $i<count($sideboard); ++$i) {
if($usesUuid) $sideboard[$i]->id = CardIDLookup($sideboard[$i]->id);
$sideboard[$i]->id = CardIDOverride($sideboard[$i]->id);
$cardID = CardUUIDOverride(UUIDLookup($sideboard[$i]->id));
if(CardSet($cardID) == $bannedSet) {
$hasBannedCard = true;
}
for($j=0; $j<$sideboard[$i]->count; ++$j) {
if($sideboardCards != "") $sideboardCards .= " ";
$sideboardCards .= $cardID;
}
}
if ($format != "livinglegendscc" && $hasBannedCard) {
$_SESSION['error'] = '⚠️ Unreleased cards must be played in the open format.';
header("Location: " . $redirectPath . "/MainMenu.php");
WriteGameFile();
exit;
}
//We have the decklist, now write to file
$filename = "./Games/" . $gameName . "/p" . $playerID . "Deck.txt";
$deckFile = fopen($filename, "w");
fwrite($deckFile, $base . " " . $leader . "\r\n");
fwrite($deckFile, $cards . "\r\n");
fwrite($deckFile, $sideboardCards . "\r\n");
fclose($deckFile);
copy($filename, "./Games/" . $gameName . "/p" . $playerID . "DeckOrig.txt");
if ($favoriteDeck == "on" && isset($_SESSION["userid"])) {
//Save deck
include_once './includes/functions.inc.php';
include_once "./includes/dbh.inc.php";
$saveLink = explode("https://", $originalLink);
$saveLink = count($saveLink) > 1 ? $saveLink[1] : $originalLink;
addFavoriteDeck($_SESSION["userid"], $saveLink, $deckName, $character, $deckFormat);
}
} else {
copy($deckFile, "./Games/" . $gameName . "/p" . $playerID . "Deck.txt");
copy($deckFile, "./Games/" . $gameName . "/p" . $playerID . "DeckOrig.txt");
}
if ($playerID == 1) {
$p1uid = ($_SESSION["useruid"] ?? "Player 1");
$p1id = ($_SESSION["userid"] ?? "");
$p1IsPatron = (isset($_SESSION["isPatron"]) ? "1" : "");
$p1ContentCreatorID = ($_SESSION["patreonEnum"] ?? "");
$playerNames[1] = $p1uid;
} else if ($playerID == 2) {
$p2uid = ($_SESSION["useruid"] ?? "Player 2");
$p2id = ($_SESSION["userid"] ?? "");
$p2IsPatron = (isset($_SESSION["isPatron"]) ? "1" : "");
$p2ContentCreatorID = ($_SESSION["patreonEnum"] ?? "");
$playerNames[2] = $p2uid;
}
if ($matchup == "") {
if ($playerID == 2) {
$gameStatus = $MGS_Player2Joined;
if (file_exists("./Games/" . $gameName . "/gamestate.txt")) unlink("./Games/" . $gameName . "/gamestate.txt");
$firstPlayerChooser = 1;
$p1roll = 0;
$p2roll = 0;
$tries = 10;
while ($p1roll == $p2roll && $tries > 0) {
$p1roll = rand(1, 6) + rand(1, 6);
$p2roll = rand(1, 6) + rand(1, 6);
WriteLog("$p1uid rolled $p1roll and $p2uid rolled $p2roll.");
--$tries;
}
$firstPlayerChooser = ($p1roll > $p2roll ? 1 : 2);
$playerName = $playerNames[$firstPlayerChooser];
WriteLog("$playerName chooses who goes first.");
$gameStatus = $MGS_ChooseFirstPlayer;
$joinerIP = $_SERVER['REMOTE_ADDR'];
}
if ($playerID == 2) $p2Key = hash("sha256", rand() . rand() . rand());
WriteGameFile();
SetCachePiece($gameName, $playerID + 1, strval(round(microtime(true) * 1000)));
SetCachePiece($gameName, $playerID + 3, "0");
SetCachePiece($gameName, $playerID + 6, $leader ?? "-");
SetCachePiece($gameName, 14, $gameStatus);
GamestateUpdated($gameName);
//$authKey = ($playerID == 1 ? $p1Key : $p2Key);
//$_SESSION["authKey"] = $authKey;
$domain = (!empty(getenv("DOMAIN")) ? getenv("DOMAIN") : "karabast.net");
if ($playerID == 1) {
$_SESSION["p1AuthKey"] = $p1Key;
setcookie("lastAuthKey", $p1Key, time() + 86400, "/", $domain);
} else if ($playerID == 2) {
$_SESSION["p2AuthKey"] = $p2Key;
setcookie("lastAuthKey", $p2Key, time() + 86400, "/", $domain);
}
}
session_write_close();
header("Location: " . $redirectPath . "/GameLobby.php?gameName=$gameName&playerID=$playerID");
function CardIDOverride($cardID) {
switch($cardID) {
case "SHD_030": return "SOR_033"; //Death Trooper
case "SHD_063": return "SOR_066"; //System Patrol Craft
case "SHD_066": return "SOR_068"; //Cargo Juggernaut
case "SHD_070": return "SOR_069"; //Resilient
case "SHD_081": return "SOR_080"; //General Tagge
case "SHD_085": return "SOR_083"; //Superlaser Technician
case "SHD_083": return "SOR_081"; //Seasoned Shoretrooper
case "SHD_166": return "SOR_162"; //Disabling Fang Fighter
case "SHD_223": return "SOR_215"; //Snapshot Reflexes
case "SHD_231": return "SOR_220"; //Surprise Strike
case "SHD_236": return "SOR_227"; //Snowtrooper Lieutenant
case "SHD_238": return "SOR_229"; //Cell Block Guard
case "SHD_257": return "SOR_247"; //Underworld Thug
case "SHD_262": return "SOR_251"; //Confiscate
case "SHD_121": return "SOR_117"; //Mercenary Company
case "TWI_077": return "SOR_078"; //Vanquish
case "TWI_107": return "SOR_111"; //Patrolling V-Wing
case "TWI_123": return "SHD_128"; //Outflank
case "TWI_124": return "SOR_124"; //Tactical Advantage
case "TWI_127": return "SOR_126"; //Resupply
case "TWI_128": return "SHD_131"; //Take Captive
case "TWI_170": return "SHD_178"; //Daring Raid
case "TWI_174": return "SOR_172"; //Open Fire
case "TWI_226": return "SOR_222"; //Waylay
case "TWI_254": return "SOR_248"; //Volunteer Soldier
case "C24_001": return "SOR_038"; //Count Dooku (Darth Tyranus)
case "C24_002": return "SOR_087"; //Darth Vader (Commanding the First Legion)
case "C24_003": return "SOR_135"; //Emperor Palpatine (Master of the Dark Side)
case "C24_004": return "SHD_141"; //Kylo Ren (Killing the Past)
case "C24_005": return "TWI_134"; //Asajj Ventress (Count Dooku's Assassin)
case "C24_006": return "TWI_135"; //Darth Maul (Revenge at Last)
default: return $cardID;
}
}
function CardUUIDOverride($cardID)
{
switch ($cardID) {
case "1706333706": return "8380936981";//Jabba's Rancor
//TODO: left here just in case we need these IDs
//case "1401885853"://con exclusive 2024 Count Dooku (Darth Tyranus)
//return "9624333142";
//case "8292269690"://con exclusive 2024 Darth Vader (Commanding the First Legion)
//return "8506660490";
//case "9954244145"://con exclusive 2024 Emperor Palpatine (Master of the Dark Side)
//return "9097316363";
//case "3038397952"://con exclusive 2024 Kylo Ren (Killing the Past)
//return "6263178121";
//case "7315203824"://con exclusive 2024 Asajj Ventress (Count Dooku's Assassin)
//return "3556557330";
//case "5866567543"://con exclusive 2024 Darth Maul (Revenge at Last)
//return "8613680163";
default: return $cardID;
}
}
function IsBanned($cardID, $format)
{
switch ($format) {
case "blitz":
case "compblitz":
switch ($cardID) {
case "WTR152":
case "ARC076": case "ARC077": //Viserai
case "ARC129": case "ARC130": case "ARC131":
case "ELE006":
case "ELE186": case "ELE187": case "ELE188":
case "ELE223":
case "CRU141":
case "CRU174": case "CRU175": case "CRU176":
case "MON239":
case "MON183": case "MON184": case "MON185":
case "EVR037":
case "EVR123": // Aether Wildfire
case "UPR103": case "EVR120": case "ELE002": case "ELE003": case "EVR121":
return true;
default:
return false;
}
break;
case "cc":
case "compcc":
switch ($cardID) {
case "WTR164": case "WTR165": case "WTR166": //Drone of Brutality
case "ARC170": case "ARC171": case "ARC172": //Plunder Run
case "CRU141":
case "MON001": //Prism
case "MON003": //Luminaris
case "MON153":
case "MON155":
case "MON239":
case "MON266": case "MON267": case "MON268": //Belittle
case "ELE003":
case "ELE006":
case "ELE114":
case "ELE172":
case "ELE186": case "ELE187": case "ELE188":
case "ELE223":
case "EVR017":
case "UPR139":
return true;
default:
return false;
}
break;
case "commoner":
switch ($cardID) {
case "ELE186": //Ball Lightning
case "ELE187":
case "ELE188":
case "MON266": //Belittle
case "MON267":
case "MON268":
return true;
default:
return false;
}
break;
default:
return false;
}
}