forked from Talishar/Talishar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
WriteGamestate.php
110 lines (97 loc) · 5.01 KB
/
WriteGamestate.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
<?php
UpdateGameState($playerID);
if(!isset($filename) || !str_contains($filename, "gamestate.txt")) $filename = "./Games/" . $gameName . "/gamestate.txt";
$handler = fopen($filename, "w");
$lockTries = 0;
while (!flock($handler, LOCK_EX) && $lockTries < 10) {
usleep(100000); //50ms
++$lockTries;
}
if ($lockTries == 10) { fclose($handler); exit; }
$gamestateContent = "";
$gamestateContent .= implode(" ", $playerHealths) . "\r\n";
//Player 1
$gamestateContent .= implode(" ", $p1Hand) . "\r\n";
$gamestateContent .= implode(" ", $p1Deck) . "\r\n";
$gamestateContent .= implode(" ", $p1CharEquip) . "\r\n";
$gamestateContent .= implode(" ", $p1Resources) . "\r\n";
$gamestateContent .= implode(" ", $p1Arsenal) . "\r\n";
$gamestateContent .= implode(" ", $p1Items) . "\r\n";
$gamestateContent .= implode(" ", $p1Auras) . "\r\n";
$gamestateContent .= implode(" ", $p1Discard) . "\r\n";
$gamestateContent .= implode(" ", $p1Pitch) . "\r\n";
$gamestateContent .= implode(" ", $p1Banish) . "\r\n";
$gamestateContent .= implode(" ", $p1ClassState) . "\r\n";
$gamestateContent .= implode(" ", $p1CharacterEffects) . "\r\n";
$gamestateContent .= implode(" ", $p1Soul) . "\r\n";
$gamestateContent .= implode(" ", $p1CardStats) . "\r\n";
$gamestateContent .= implode(" ", $p1TurnStats) . "\r\n";
$gamestateContent .= implode(" ", $p1Allies) . "\r\n";
$gamestateContent .= implode(" ", $p1Permanents) . "\r\n";
$gamestateContent .= implode(" ", $p1Settings) . "\r\n";
//Player 2
$gamestateContent .= implode(" ", $p2Hand) . "\r\n";
$gamestateContent .= implode(" ", $p2Deck) . "\r\n";
$gamestateContent .= implode(" ", $p2CharEquip) . "\r\n";
$gamestateContent .= implode(" ", $p2Resources) . "\r\n";
$gamestateContent .= implode(" ", $p2Arsenal) . "\r\n";
$gamestateContent .= implode(" ", $p2Items) . "\r\n";
$gamestateContent .= implode(" ", $p2Auras) . "\r\n";
$gamestateContent .= implode(" ", $p2Discard) . "\r\n";
$gamestateContent .= implode(" ", $p2Pitch) . "\r\n";
$gamestateContent .= implode(" ", $p2Banish) . "\r\n";
$gamestateContent .= implode(" ", $p2ClassState) . "\r\n";
$gamestateContent .= implode(" ", $p2CharacterEffects) . "\r\n";
$gamestateContent .= implode(" ", $p2Soul) . "\r\n";
$gamestateContent .= implode(" ", $p2CardStats) . "\r\n";
$gamestateContent .= implode(" ", $p2TurnStats) . "\r\n";
$gamestateContent .= implode(" ", $p2Allies) . "\r\n";
$gamestateContent .= implode(" ", $p2Permanents) . "\r\n";
$gamestateContent .= implode(" ", $p2Settings) . "\r\n";
$gamestateContent .= implode(" ", $landmarks) . "\r\n";
$gamestateContent .= $winner . "\r\n";
$gamestateContent .= $firstPlayer . "\r\n";
$gamestateContent .= $currentPlayer . "\r\n";
$gamestateContent .= $currentTurn . "\r\n";
$gamestateContent .= implode(" ", $turn) . "\r\n";
$gamestateContent .= $actionPoints . "\r\n";
$gamestateContent .= implode(" ", $combatChain) . "\r\n";
$gamestateContent .= implode(" ", $combatChainState) . "\r\n";
$gamestateContent .= implode(" ", $currentTurnEffects) . "\r\n";
$gamestateContent .= implode(" ", $currentTurnEffectsFromCombat) . "\r\n";
$gamestateContent .= implode(" ", $nextTurnEffects) . "\r\n";
$gamestateContent .= implode(" ", $decisionQueue) . "\r\n";
$gamestateContent .= implode(" ", $dqVars) . "\r\n";
$gamestateContent .= implode(" ", $dqState) . "\r\n";
$gamestateContent .= implode(" ", $layers) . "\r\n";
$gamestateContent .= implode(" ", $layerPriority) . "\r\n";
$gamestateContent .= $mainPlayer . "\r\n";
$gamestateContent .= implode(" ", $lastPlayed) . "\r\n";
$gamestateContent .= count($chainLinks) . "\r\n";
for ($i = 0; $i < count($chainLinks); ++$i) {
$gamestateContent .= implode(" ", $chainLinks[$i]) . "\r\n";
}
$gamestateContent .= implode(" ", $chainLinkSummary) . "\r\n";
$gamestateContent .= $p1Key . "\r\n";
$gamestateContent .= $p2Key . "\r\n";
$gamestateContent .= $permanentUniqueIDCounter . "\r\n";
$gamestateContent .= $inGameStatus . "\r\n"; //Game status -- 0 = START, 1 = PLAY, 2 = OVER
$gamestateContent .= "\r\n"; //Animations - Deprecated
$gamestateContent .= $currentPlayerActivity . "\r\n"; //Current Player activity status -- 0 = active, 2 = inactive
$gamestateContent .= "\r\n"; //Unused
$gamestateContent .= "\r\n"; //Unused
$gamestateContent .= $p1TotalTime . "\r\n"; //Player 1 total time
$gamestateContent .= $p2TotalTime . "\r\n"; //Player 2 total time
$gamestateContent .= $lastUpdateTime . "\r\n"; //Last update time
$gamestateContent .= $roguelikeGameID . "\r\n"; //Last update time
$gamestateContent .= implode(" ", $events) . "\r\n";//Events
$gamestateContent .= $EffectContext . "\r\n";//Update number the gamestate is for
$gamestateContent .= implode(" ", $p1Inventory) . "\r\n";
$gamestateContent .= implode(" ", $p2Inventory) . "\r\n";
$gamestateContent .= $p1IsAI . "\r\n";
$gamestateContent .= $p2IsAI . "\r\n";
fwrite($handler, $gamestateContent);
flock($handler, LOCK_UN);
fclose($handler);
WriteGamestateCache($gameName, $gamestateContent);
if($useRedis) WriteCache($gameName . "GS", $gamestateContent);