-
-
Notifications
You must be signed in to change notification settings - Fork 38
/
LiveMap.cs
146 lines (123 loc) · 3.85 KB
/
LiveMap.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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using Newtonsoft.Json.Linq;
using System.Security.Cryptography;
using System.Threading;
using WebSocketSharp.Server;
namespace Havoc.Live_Map
{
public class LiveMap
{
WebSocketServer wssv;
public static StreamWriter file = new StreamWriter("live_map.log");
public static JArray playerLocations = new JArray();
public static JArray blipLocations = new JArray();
public bool isOpen()
{
return wssv.IsListening;
}
public LiveMap(int listenPort)
{
wssv = new WebSocketServer(listenPort);
wssv.AddWebSocketService<PlayerLocations>("/");
wssv.Start();
if (wssv.IsListening)
{
file.WriteLine("Listening on port {0}", wssv.Port);
Console.WriteLine("Listening on port {0}, and providing WebSocket services:", wssv.Port);
foreach (var path in wssv.WebSocketServices.Paths)
{
file.WriteLine("- {0}", path);
}
}
}
public void stop()
{
playerLocations.Clear();
blipLocations.Clear();
file.Flush();
wssv.Stop();
}
public void addPlayer(string name, float x = 0f, float y = 0f, float z = 0f)
{
lock (playerLocations)
{
bool updatedPlayer = false;
foreach (var item in playerLocations)
{
if (item["name"].ToString() == name)
{
// Update it
item["x"] = x;
item["y"] = y;
item["z"] = z;
updatedPlayer = true;
}
else
{
continue;
}
}
if (!updatedPlayer)
{
// Add them
JObject playerObj = new JObject();
playerObj.Add("name", name);
playerObj.Add("x", x);
playerObj.Add("y", y);
playerObj.Add("z", z);
playerLocations.Add(playerObj);
}
}
}
public void removePlayer(string name)
{
lock (playerLocations)
{
JToken token = null;
foreach (var item in playerLocations)
{
if (item["name"].ToString() == name)
{
token = item;
}
}
if(token != null)
{
playerLocations.Remove(token);
}
}
JObject obj = new JObject();
obj["type"] = "playerLeft";
obj["payload"] = name;
wssv.WebSocketServices["/"].Sessions.Broadcast(obj.ToString(Newtonsoft.Json.Formatting.None));
}
public void addBlip(string name, string type = "waypoint", float x = 0f, float y = 0f, float z = 0f)
{
JObject blip = new JObject();
blip["name"] = name;
blip["type"] = type;
blip["x"] = x;
blip["y"] = y;
blip["z"] = z;
lock (blipLocations)
{
blipLocations.Add(blip);
}
}
public void addBlips(string blipJson)
{
lock (blipLocations)
{
blipLocations = JArray.Parse(blipJson);
}
Console.WriteLine("blips: " + blipJson);
}
}
}