-
Notifications
You must be signed in to change notification settings - Fork 0
/
Glob.cs
191 lines (169 loc) · 5.38 KB
/
Glob.cs
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
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text.Json;
using System.Threading;
using Godot;
public partial class Glob : Node
{
// v data
public int myPosition = 1;
public string draging = "0";
public int numberOfPlayer = 5;
public int startIndex = 0;
public bool siblingIsOn = false;
public string[] lineType =
{
"Received GameFound packet:", // PlayerNumber : int
"Received SpawnPlayer packet:", // Slot : int (where IsLocal==true), Color : int, Username : string
};
public List<PlayerData> players = new List<PlayerData>();
public List<SystemBlock> systemMessages = new List<SystemBlock>();
public int nOfHacks = 0;
// c data
string[] colors =
{
"#00A6F6",
"#D31FD4",
"#6FE015",
"#9D9D9D",
"#FF8113",
"#FFEC16",
"#00B48B",
"#0041F6"
};
// should red player log here and get : {my position, names, colors, number of players} and update v dara.
public string fileLocation = System.Environment.GetFolderPath(
System.Environment.SpecialFolder.ApplicationData
);
string[] lines;
[Signal]
public delegate void newLineAddedEventHandler(string newline);
Control n = null;
public List<string> newlines = new List<string>();
public override void _Ready()
{
// start reading files
Thread thread = new Thread(ReadLines);
thread.Start();
//ReadLines();
}
public void connect()
{
n = GetNode<Control>("/root/Node2D/Chat");
//GD.Print(n.Name);
siblingIsOn = true;
}
private Process process;
public async void ReadLines()
{
string logPrePath = System.Environment.GetFolderPath(
System.Environment.SpecialFolder.ApplicationData
);
string logPath =
logPrePath.Substring(0, logPrePath.Length - 7)
+ @"LocalLow\Nomoon\Mindnight\Player.log";
ProcessStartInfo startInfo = new ProcessStartInfo
{
FileName = "powershell",
Arguments = $"-Command Get-Content '{logPath}' -Wait -Encoding UTF8",
RedirectStandardOutput = true,
UseShellExecute = false,
CreateNoWindow = true
};
using (process = new Process { StartInfo = startInfo })
{
process.Start();
StreamReader reader = process.StandardOutput;
while (!process.StandardOutput.EndOfStream)
{
string line = await reader.ReadLineAsync();
if (numberOfPlayer != players.Count())
{
//GD.Print("On StartF");
startFetch(line);
}
else
{
//GD.Print("On organizer");
if (siblingIsOn)
{
n.Call("organizer", line);
}
newlines.Add(line);
}
}
}
}
public void startFetch(string line)
{
if (line.Contains(lineType[0]) || line.Contains(lineType[1]))
{
if (line.Contains(lineType[0]))
{
// get numberOfPlayers
JsonDocument doc = JsonDocument.Parse(line.Substring(47));
JsonElement root = doc.RootElement;
numberOfPlayer = root.GetProperty("PlayerNumber").GetInt32();
doc.Dispose();
}
else if (line.Contains(lineType[1]))
{
// get players
JsonDocument doc = JsonDocument.Parse(line.Substring(49));
JsonElement root = doc.RootElement;
PlayerData newPlayer = new PlayerData
{
slot = root.GetProperty("Slot").GetInt32(),
name = root.GetProperty("Username").GetString(),
color = colors[root.GetProperty("Color").GetInt32()],
// [center][color=#00A6F6]1 : name[/color][/center]
IsLocal = root.GetProperty("IsLocal").GetBoolean()
};
if (newPlayer.IsLocal)
myPosition = players.Count() + 1;
players.Add(newPlayer);
doc.Dispose();
}
}
}
public class PlayerData
{
public string name;
public int slot;
public string color;
public bool IsLocal;
public int chanceOfBeingHacker = 0;
}
public void changeChance(int playerIndex, bool addOrNo, int amount)
{
if (addOrNo)
{
players[playerIndex].chanceOfBeingHacker += amount;
}
else
{
players[playerIndex].chanceOfBeingHacker -= amount;
}
var chances = GetTree().GetNodesInGroup("Chance");
foreach (var i in chances)
{
if (i.GetMeta("Index").As<int>() == playerIndex)
{
i.GetNode<ProgressBar>("ProgressBar").Value = players[
playerIndex
].chanceOfBeingHacker;
}
}
}
public override void _Notification(int what)
{
if (what == NotificationWMCloseRequest)
{
process.Kill();
GetTree().Quit(); // default behavior
}
}
}