forked from SWU-Karabast/SWUOnline
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProcessInput2.php
182 lines (161 loc) · 5.75 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
<?php
error_reporting(E_ALL);
ob_start();
include "WriteLog.php";
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";
//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;
$chkInput = [];
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 (!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;
//Now we can process the command
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");
WriteLog("Player $firstPlayerChooser 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();
include "WriteGamestate.php";
}
if ($makeCheckpoint) MakeGamestateBackup();
if ($makeBlockBackup) MakeGamestateBackup("preBlockBackup.txt");
if ($MakeStartTurnBackup) MakeStartTurnBackup();
if ($MakeStartGameBackup) MakeGamestateBackup("origGamestate.txt");
GamestateUpdated($gameName);
ExitProcessInput();