-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathProcessInput2.php
215 lines (192 loc) · 6.84 KB
/
ProcessInput2.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
<?php
error_reporting(E_ALL);
ob_start();
include "GameLogic.php";
include "GameTerms.php";
include "HostFiles/Redirector.php";
include "Libraries/SHMOPLibraries.php";
include "Libraries/StatFunctions.php";
include "Libraries/UILibraries.php";
include "Libraries/PlayerSettings.php";
include "Libraries/NetworkingLibraries.php";
include "AI/CombatDummy.php";
include "AI/EncounterAI.php";
include "AI/PlayerMacros.php";
include "Libraries/HTTPLibraries.php";
require_once("Libraries/CoreLibraries.php");
include_once "./includes/dbh.inc.php";
include_once "./includes/functions.inc.php";
include_once "APIKeys/APIKeys.php";
include_once "WriteLog.php";
//We should always have a player ID as a URL parameter
$gameName = TryGET("gameName", "");
if ($gameName == "" || !IsGameNameValid($gameName)) {
echo ("Invalid game name.");
exit;
}
$playerID = $_GET["playerID"];
$authKey = $_GET["authKey"];
//We should also have some information on the type of command
$inputMode = $_GET["mode"];
$mode = $inputMode;
$buttonInput = $_GET["buttonInput"] ?? ""; //The player that is the target of the command - e.g. for changing health total
$cardID = $_GET["cardID"] ?? "";
$chkCount = $_GET["chkCount"] ?? 0;
$chkCountTheirs = $_GET["chkCountTheirs"] ?? 0;
$chkCountMine = $_GET["chkCountMine"] ?? 0;
$chkInput = [];
$chkInputTheirs = [];
$chkInputMine = [];
for ($i = 0; $i < $chkCount; ++$i) {
$chk = $_GET[("chk" . $i)] ?? "";
if ($chk != "") $chkInput[] = $chk;
}
$inputText = $_GET["inputText"] ?? "";
SetHeaders();
$numPass = 0;
if(IsReplay() && $mode == 99)
{
$filename = "./Games/" . $gameName . "/replayCommands.txt";
$file = file($filename);
$line = $file[0];
unset($file[0]);
$params = explode(" ", $line);
$playerID = $params[0];
$mode = $params[1];
$buttonInput = $params[2];
$cardID = $params[3];
$chkCount = $params[4];
$chkInput = explode("|", $params[5]);
for($i=0; $i<count($chkInput); ++$i)
{
$chkInput[$i] = trim($chkInput[$i]);
}
//Automate extra passes
for($i=1; $i<count($file); ++$i)
{
$line = $file[$i];
$params = explode(" ", $line);
if(intval($mode) != 99 || intval($params[1]) != 99) break;
++$numPass;
unset($file[$i]);
}
file_put_contents($filename, $file);
}
//First we need to parse the game state from the file
include "ParseGamestate.php";
$otherPlayer = $currentPlayer == 1 ? 2 : 1;
$skipWriteGamestate = false;
$mainPlayerGamestateStillBuilt = 0;
$makeCheckpoint = 0;
$makeBlockBackup = 0;
$MakeStartTurnBackup = false;
$MakeStartGameBackup = false;
$targetAuth = ($playerID == 1 ? $p1Key : $p2Key);
$conceded = false;
$randomSeeded = false;
if(!IsReplay()) {
if (($playerID == 1 || $playerID == 2) && $authKey == "") {
if (isset($_COOKIE["lastAuthKey"])) $authKey = $_COOKIE["lastAuthKey"];
}
if ($playerID != 3 && $authKey != $targetAuth) { echo("Invalid auth key"); exit; }
if ($playerID == 3 && !IsModeAllowedForSpectators($mode)) ExitProcessInput();
if(GetCachePiece($gameName, $playerID + 14) > 0 && GetCachePiece($gameName, 19) != $playerID) {
exit("refresh");
}
if (!IsModeAsync($mode) && $currentPlayer != $playerID) {
$currentTime = round(microtime(true) * 1000);
SetCachePiece($gameName, 2, $currentTime);
SetCachePiece($gameName, 3, $currentTime);
ExitProcessInput();
}
}
$afterResolveEffects = [];
$animations = [];
$events = [];//Clear events each time so it's only updated ones that get sent
if ((IsPatron(1) || IsPatron(2)) && !IsReplay()) {
$commandFile = fopen("./Games/" . $gameName . "/commandfile.txt", "a");
fwrite($commandFile, $playerID . " " . $mode . " " . $buttonInput . " " . $cardID . " " . $chkCount . " " . implode("|", $chkInput) . "\r\n");
fclose($commandFile);
}
if($initiativeTaken > 2 && $mode != 99 && $mode != 34 && !IsModeAsync($mode)) $initiativeTaken = 0;
if(GetCachePiece($gameName, 14) === 7 && $mode != 100001) {
return;
}
//Now we can process the command
if($chkCountTheirs > 0 || $chkCountMine > 0) {
for ($i = 0; $i < $chkCountTheirs; ++$i) {
$chk = $_GET[("chkt" . $i)] ?? "";
if ($chk != "") $chkInputTheirs[] = $chk;
}
for ($i = 0; $i < $chkCountMine; ++$i) {
$chk = $_GET[("chkm" . $i)] ?? "";
if ($chk != "") $chkInputMine[] = $chk;
}
ProcessInput($playerID, $mode, $buttonInput, $cardID, [$chkCountTheirs, $chkCountMine], [$chkInputTheirs, $chkInputMine], false, $inputText);
}
else
ProcessInput($playerID, $mode, $buttonInput, $cardID, $chkCount, $chkInput, false, $inputText);
ProcessMacros();
if ($inGameStatus == $GameStatus_Rematch) {
$origDeck = "./Games/" . $gameName . "/p1DeckOrig.txt";
if (file_exists($origDeck)) copy($origDeck, "./Games/" . $gameName . "/p1Deck.txt");
$origDeck = "./Games/" . $gameName . "/p2DeckOrig.txt";
if (file_exists($origDeck)) copy($origDeck, "./Games/" . $gameName . "/p2Deck.txt");
include "MenuFiles/ParseGamefile.php";
include "MenuFiles/WriteGamefile.php";
$gameStatus = (IsPlayerAI(2) ? $MGS_ReadyToStart : $MGS_ChooseFirstPlayer);
SetCachePiece($gameName, 14, $gameStatus);
$firstPlayer = 1;
$firstPlayerChooser = ($winner == 1 ? 2 : 1);
$p1SideboardSubmitted = "0";
$p2SideboardSubmitted = (IsPlayerAI(2) ? "1" : "0");
$playerName = $playerNames[$firstPlayerChooser];
WriteLog("$playerName lost and will choose first player for the rematch.");
WriteGameFile();
$turn[0] = "REMATCH";
include "WriteGamestate.php";
$currentTime = round(microtime(true) * 1000);
SetCachePiece($gameName, 2, $currentTime);
SetCachePiece($gameName, 3, $currentTime);
GamestateUpdated($gameName);
exit;
} else if ($winner != 0 && $turn[0] != "YESNO") {
$inGameStatus = $GameStatus_Over;
$turn[0] = "OVER";
$currentPlayer = 1;
}
CacheCombatResult();
CombatDummyAI(); //Only does anything if applicable
//EncounterAI();
if (!IsGameOver()) {
if ($playerID == 1) $p1TotalTime += time() - intval($lastUpdateTime);
else if ($playerID == 2) $p2TotalTime += time() - intval($lastUpdateTime);
$lastUpdateTime = time();
}
//Now write out the game state
if (!$skipWriteGamestate) {
//if($mainPlayerGamestateStillBuilt) UpdateMainPlayerGamestate();
//else UpdateGameState(1);
if(!IsModeAsync($mode))
{
if(GetCachePiece($gameName, 12) == "1") WriteLog("Current player is active again.");
SetCachePiece($gameName, 12, "0");
$currentPlayerActivity = 0;
}
DoGamestateUpdate();
$currentTime = round(microtime(true) * 1000);
SetCachePiece($gameName, 17, $currentTime);
if(GetCachePiece($gameName, 18) == $playerID) {
SetCachePiece($gameName, 18, "0");
if (GetCachePiece($gameName, 19) == $playerID) {
SetCachePiece($gameName, 19, "0");
}
}
include "WriteGamestate.php";
}
if ($makeCheckpoint) MakeGamestateBackup();
if ($makeBlockBackup) MakeGamestateBackup("preBlockBackup.txt");
if ($MakeStartTurnBackup) MakeStartTurnBackup();
if ($MakeStartGameBackup) MakeGamestateBackup("origGamestate.txt");
GamestateUpdated($gameName);
ExitProcessInput();