forked from Talishar/Talishar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGetUpdateSSE.php
More file actions
132 lines (109 loc) · 3.72 KB
/
GetUpdateSSE.php
File metadata and controls
132 lines (109 loc) · 3.72 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
<?php
include 'Libraries/HTTPLibraries.php';
include "HostFiles/Redirector.php";
include "Libraries/SHMOPLibraries.php";
include "WriteLog.php";
ob_implicit_flush(true);
ob_end_flush();
// array holding allowed Origin domains
SetHeaders();
$response = new stdClass();
$gameName = $_GET["gameName"];
if (!IsGameNameValid($gameName)) {
echo ("error: Invalid gamename\n\n");
ob_flush();
flush();
exit;
}
$playerID = TryGet("playerID", 3);
if (!is_numeric($playerID)) {
echo ("error: Invalid playerID\n\n");
ob_flush();
flush();
exit;
}
$authKey = TryGet("authKey", "");
if (($playerID == 1 || $playerID == 2) && $authKey == "") {
if (isset($_COOKIE["lastAuthKey"])) $authKey = $_COOKIE["lastAuthKey"];
}
header('Content-Type: text/event-stream');
header('Cache-Control: no-cache');
$lastUpdate = 0;
$response->message = "update";
$isGamePlayer = $playerID == 1 || $playerID == 2;
ob_start();
// Loop until the client close the stream
$cacheVal = intval(GetCachePiece($gameName, 1));
$lastUpdate = $cacheVal;
$response->cacheVal = $cacheVal;
echo ("data: " . json_encode($response) . "\n\n");
ob_flush();
flush();
$count = 0;
while (true) {
if(($count % 100 == 0) && !file_exists("./Games/" . $gameName . "/GameFile.txt")) exit;
++$count;
$cacheVal = intval(GetCachePiece($gameName, 1));
if ($cacheVal > $lastUpdate) {
$lastUpdate = $cacheVal;
$response->cacheVal = $cacheVal;
echo("data: " . json_encode($response) . "\n\n");
ob_flush();
flush();
set_time_limit(120); //Reset script time limit
}
if (connection_aborted()) break;
$currentTime = round(microtime(true) * 1000);
$lastOppStatus = 0;
if($isGamePlayer) {
SetCachePiece($gameName, $playerID + 1, $currentTime);
$otherP = ($playerID == 1 ? 2 : 1);
$oppLastTime = intval(GetCachePiece($gameName, $otherP + 1));
$oppStatus = intval(GetCachePiece($gameName, $otherP + 3));
if (($currentTime - $oppLastTime) > 3000 && $oppStatus == 0 && $oppStatus != $lastOppStatus) {
WriteLog("🔌Opponent has disconnected. Waiting 60 seconds to reconnect.");
GamestateUpdated($gameName);
SetCachePiece($gameName, $otherP + 3, "1");
} else if (($currentTime - $oppLastTime) > 60000 && $oppStatus == 1 && $oppStatus != $lastOppStatus) {
WriteLog("Opponent has left the game.");
GamestateUpdated($gameName);
SetCachePiece($gameName, $otherP + 3, "2");
//$lastUpdate = 0;
$opponentDisconnected = true;
} else if (($currentTime - $oppLastTime) < 3000 && $oppStatus > 0) {
SetCachePiece($gameName, $otherP + 3, "0");
}
$lastOppStatus = intval($oppStatus);
//Handle server timeout
$lastUpdateTime = GetCachePiece($gameName, 6);
if($lastUpdateTime == "") { echo("The game no longer exists on the server."); exit; }
if($currentTime - $lastUpdateTime > 60000 && GetCachePiece($gameName, 12) != "1") //60 seconds
{
SetCachePiece($gameName, 12, "1");
$opponentInactive = true;
$response->cacheVal = $cacheVal;
echo ("data: " . json_encode($response) . "\n\n");
ob_flush();
flush();
//$lastUpdate = 0;
}
}
// Wait 100ms to check again
usleep(100000); //100 milliseconds
}
echo ("data: SOMETHING WRONG " . json_encode($response) . " " . str_pad("", 4096) . "\n\n");
ob_end_flush();
// Set file mime type event-stream
// header('Content-Type: text/event-stream');
// header('Cache-Control: no-cache');
// // Loop until the client close the stream
// while (true) {
// // Echo time
// $time = date('r');
// $padding = str_pad("", 4096);
// echo "data: The server time is: {$time}\n{$padding}\n\n";
// // Flush buffer (force sending data to client)
// flush();
// // Wait 2 seconds for the next message / event
// sleep(2);
// }