forked from Talishar/Talishar
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWriteLog.php
More file actions
81 lines (75 loc) · 2.92 KB
/
WriteLog.php
File metadata and controls
81 lines (75 loc) · 2.92 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
<?php
function WriteLog($text, $playerColor = 0, $highlight=false, $path="./", $highlightColor="brown")
{
global $gameName;
$filename = "{$path}Games/$gameName/gamelog.txt";
if(file_exists($filename)) $handler = fopen($filename, "a");
else return; //File does not exist
$playerSpan = ($playerColor != 0 ? "<span style='color:<PLAYER{$playerColor}COLOR>;'>" : "");
$playerSpanClose = ($playerColor != 0 ? "</span>" : "");
if($highlight) $output = $playerSpan . "<p style='background: $highlightColor;font-size: max(1em, 14px);margin-bottom:0px;'><span style='color:azure;'>" . $text . "</span></p>" . $playerSpanClose;
else $output = $playerSpan . $text . $playerSpanClose;
fwrite($handler, "$output\r\n");
fflush($handler); // Force immediate write to disk for SSE visibility
fclose($handler);
if(function_exists("GetSettings") && (IsPatron(1) || IsPatron(2))) {
$filename = "{$path}Games/$gameName/fullGamelog.txt";
$handler = fopen($filename, "a");
fwrite($handler, "$output\r\n");
fflush($handler); // Force immediate write to disk for SSE visibility
fclose($handler);
}
}
function ClearLog($n=30)
{
global $gameName;
$filename = "./Games/$gameName/gamelog.txt";
$handle = fopen("./Games/$gameName/gamelog.txt", "r");
$lines = [];
if ($handle) {
while (!feof($handle)) {
$lines[] = fgets($handle);
}
fclose($handle);
$lines = array_slice($lines, -$n);
}
$handle = fopen($filename, "w");
fwrite($handle, implode("", $lines));
fclose($handle);
}
function WriteSystemMessage($text, $path="./")
{
global $gameName;
$filename = "{$path}Games/$gameName/gamelog.txt";
if(file_exists($filename)) $handler = fopen($filename, "a");
else return; //File does not exist
fwrite($handler, "$text\r\n");
fflush($handler); // Force immediate write to disk for SSE visibility
fclose($handler);
if(function_exists("GetSettings") && (IsPatron(1) || IsPatron(2))) {
$filename = "{$path}Games/$gameName/fullGamelog.txt";
$handler = fopen($filename, "a");
fwrite($handler, "$text\r\n");
fflush($handler); // Force immediate write to disk for SSE visibility
fclose($handler);
}
}
function JSONLog($gameName, $playerID, $path="./")
{
$response = "";
$filename = "{$path}Games/$gameName/gamelog.txt";
clearstatcache(true, $filename); // Clear file stat cache to get fresh file size
if (!file_exists($filename)) return "";
$filesize = filesize($filename);
if ($filesize > 0) {
$handler = fopen($filename, "r");
$line = fread($handler, $filesize);
fclose($handler);
$red = "#cb0202";
$blue = "#128ee5";
$player1Color = $playerID == 1 || $playerID == 3 ? $blue : $red;
$player2Color = $playerID == 2 ? $blue : $red;
$response = str_replace(["\r\n", "<PLAYER1COLOR>", "<PLAYER2COLOR>"], ["<br>", $player1Color, $player2Color], $line);
}
return $response;
}