forked from SWU-Petranaki/SWUOnline
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJoinGameInput.php
More file actions
340 lines (311 loc) · 12.3 KB
/
JoinGameInput.php
File metadata and controls
340 lines (311 loc) · 12.3 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
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
<?php
include "Libraries/HTTPLibraries.php";
include "Libraries/SHMOPLibraries.php";
include "Libraries/GameFormats.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 'CardOverrides.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"])) {
$iBlockedThem = false;
$theyBlockedMe = false;
$myBlockedPlayers = LoadBlockedPlayers($_SESSION["userid"]);
for($i=0; $i<count($myBlockedPlayers); $i+=2) {
if($myBlockedPlayers[$i] == $p1id) {
$iBlockedThem = true;
break;
}
}
$theirBlockedPlayers = LoadBlockedPlayers($p1id);
for($i=0; $i<count($theirBlockedPlayers); $i+=2) {
if($theirBlockedPlayers[$i] == $_SESSION["userid"]) {
$theyBlockedMe = true;
break;
}
}
if ($iBlockedThem || $theyBlockedMe) {
$_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;
}
$previewSet = "LOF";
$deckObj = json_decode($json);
$deckName = $deckObj->metadata->{"name"};
$setCodeLeader = $deckObj->leader->id;
$leader = !$usesUuid ? UUIDLookup($setCodeLeader) : $setCodeLeader;
if($leader == "" && str_starts_with($setCodeLeader, $previewSet . "_")) {
$_SESSION['error'] = "<div>⚠️ Error: Leader $setCodeLeader is not available yet in Petranaki.<br/>Please tell the devs on our Discord to add it manually.</div>";
header("Location: " . $redirectPath . "/MainMenu.php");
WriteGameFile();
exit;
}
else if($leader == "") {
$_SESSION['error'] = "<div>⚠️ Error: Deck link not supported. <br/>Make sure it is not private and that the deck link is correct.</div>";
header("Location: " . $redirectPath . "/MainMenu.php");
WriteGameFile();
exit;
}
$deckFormat = 1;
$baseSetCode = $deckObj->base->id;
$base = !$usesUuid ? UUIDLookup($baseSetCode) : $baseSetCode;
if($base == "" && str_starts_with($baseSetCode, $previewSet . "_")) {
$_SESSION['error'] = "<div>⚠️ Error: Base $baseSetCode is not available yet in Petranaki.<br/>Please tell the devs on our Discord to add it manually.</div>";
header("Location: " . $redirectPath . "/MainMenu.php");
WriteGameFile();
exit;
}
else if($base == "") {
$_SESSION['error'] = "<div>⚠️ Error: Deck link not supported. <br/>Make sure it is not private and that the deck link is correct.</div>";
header("Location: " . $redirectPath . "/MainMenu.php");
WriteGameFile();
exit;
}
$deck = $deckObj->deck;
$sideboard = $deckObj->sideboard;
if(!IsAllowed($leader, $format)) {
$_SESSION['error'] = "<div>⚠️ Your deck contains a leader that is not allowed in this format.</div>";
header("Location: " . $redirectPath . "/MainMenu.php");
WriteGameFile();
exit;
}
if(!IsAllowed($base, $format)) {
$_SESSION['error'] = "<div>⚠️ Your deck contains a base that is not allowed in this format.</div>";
header("Location: " . $redirectPath . "/MainMenu.php");
WriteGameFile();
exit;
}
$validation = ValidateDeck($format, $usesUuid, $leader, $base, $deck, $sideboard);
if (!$validation->IsValid()) {
$_SESSION['error'] = "<div>" . $validation->Error($format) . "</div>";
if(count($validation->InvalidCards()) > 0) {
$rejectionDetail = $validation->RejectionDetail($format);
$_SESSION['error'] .= "<div><div><h3>" . $rejectionDetail . "</h3><h2>Invalid Cards:</h2></div><ul>"
. implode("", array_map(function($x) {
return "<li>" . JsHtmlTitleAndSub($x) . "</li>";
}, $validation->InvalidCards())) . "</ul></div>";
} else if(count($validation->UnavailableCards()) > 0) {
$rejectionDetail = $validation->RejectionDetail($format);
$_SESSION['error'] .= "<div><div><h3>" . $rejectionDetail . "</h3><h2>Unavailable Cards:</h2></div><ul>"
. implode("", array_map(function($x) {
return "<li>" . JsHtmlTitleAndSub($x) . "</li>";
}, $validation->UnavailableCards())) . "</ul></div>";
}
header("Location: " . $redirectPath . "/MainMenu.php");
WriteGameFile();
exit;
}
$cards = $validation->CardString();
$sideboardCards = $validation->SideboardString();
//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, $leader, $deckFormat);
}
} else {
$_SESSION['error'] = '⚠️ Deck link is empty. Did you maybe copy your deck link into the Game Name field?';
header("Location: " . $redirectPath . "/MainMenu.php");
WriteGameFile();
exit;
// 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"] ?? "");
$p1SWUStatsToken = ($_SESSION["swustatsAccessToken"] ?? "");
$p1IsPatron = (isset($_SESSION["isPatron"]) ? "1" : "");
$p1ContentCreatorID = ($_SESSION["patreonEnum"] ?? "");
$playerNames[1] = $p1uid;
} else if ($playerID == 2) {
$p2uid = ($_SESSION["useruid"] ?? "Player 2");
$p2id = ($_SESSION["userid"] ?? "");
$p2SWUStatsToken = ($_SESSION["swustatsAccessToken"] ?? "");
$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, $playerID + 19, $base ?? "-");
SetCachePiece($gameName, 14, $gameStatus);
GamestateUpdated($gameName);
//$authKey = ($playerID == 1 ? $p1Key : $p2Key);
//$_SESSION["authKey"] = $authKey;
$domain = (!empty(getenv("DOMAIN")) ? getenv("DOMAIN") : "petranaki.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 JsHtmlTitleAndSub($cardID) {
$forJS = CardTitle($cardID);
if($forJS == "") return $cardID;
if(CardSubtitle($cardID) != "") $forJS .= " (" . CardSubtitle($cardID) . ")";
return str_replace("'", "\'", $forJS);
}