This repository was archived by the owner on Aug 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMultiplayer.cs
More file actions
223 lines (207 loc) · 7.46 KB
/
Multiplayer.cs
File metadata and controls
223 lines (207 loc) · 7.46 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
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
using System;
using Lidgren.Network;
using UnityEngine;
using System.Linq;
using System.Threading;
namespace BSCM
{
public class Multiplayer
{
private NetClient client_connection = null;
private NetServer server_connection = null;
private bool isServer = PluginConfig.Instance.isServer;
private Vector3 latestPosition;
private Quaternion latestRotation;
private ScoreController ScoreController;
private SongController SongController;
private bool clientReady = false;
private long latency = 0;
public Multiplayer()
{
if (isServer)
{
try
{
NetPeerConfiguration config = new NetPeerConfiguration("BSCM");
config.Port = PluginConfig.Instance.port;
server_connection = new NetServer(config);
server_connection.Start();
}
catch
{
Plugin.Log.Error("Error while starting Server");
}
Plugin.Log.Info("Server Ready");
}
else
{
try
{
var config = new NetPeerConfiguration("BSCM");
client_connection = new NetClient(config);
client_connection.Start();
client_connection.Connect(host: PluginConfig.Instance.url, port: PluginConfig.Instance.port);
Thread.Sleep(1000);
Plugin.Log.Info("Client Ready");
}
catch
{
Plugin.Log.Error("Error while connecting to Server");
}
}
Plugin.Log.Info("Multiplayer Ready !");
}
public void startSong()
{
if (ScoreController != null || SongController != null)
return; // startSong already executed for this song
ScoreController = Resources.FindObjectsOfTypeAll<ScoreController>().FirstOrDefault();
if (ScoreController == null)
Plugin.Log.Error("Couldn't find ScoreController object");
SongController = Resources.FindObjectsOfTypeAll<SongController>().FirstOrDefault();
if (SongController == null)
Plugin.Log.Error("Couldn't find SongController object");
setGameStatus(false);
Plugin.Log.Info("Pausing Game");
if(isServer)
{
Plugin.Log.Info("Waiting for client");
// wait for client to switch to ready state
while (!clientReady) {
checkMessages();
}
// start game on client
Plugin.Log.Info("Starting client game");
clientReady = false;
sendData("start");
// wait for estimated latency
Plugin.Log.Info("Latency: "+latency);
// start game on server
Plugin.Log.Info("Starting server game");
setGameStatus(true);
Plugin.Log.Info("Server game started");
}
else
{
// send ready state to server
sendData("ready");
Plugin.Log.Info("Client ready [c]");
}
}
public Vector3 getLatestPosition()
{
return latestPosition;
}
public Quaternion getLatestRotation()
{
return latestRotation;
}
public void sendCoords(Vector3 pos, Quaternion rot)
{
string data = pos.x + ";" + pos.y + ";" + pos.z + ";" + rot.x + ";" + rot.y + ";" + rot.z + ";" + rot.w;
sendData(data);
}
public void stop()
{
if (isServer)
server_connection.Shutdown("Plugin Exit");
else
client_connection.Disconnect("Plugin Exit");
}
private long getTimestamp()
{
return ((DateTimeOffset)DateTime.UtcNow).ToUnixTimeMilliseconds();
}
private void setGameStatus(bool enabled)
{
if (enabled)
{
ScoreController.enabled = true;
SongController.StartSong();
}
else
{
ScoreController.enabled = false;
SongController.PauseSong();
}
}
private void parseMessage(string message)
{
if(isServer && message == "ready")
{
// when client is ready
latency = getTimestamp();
sendData("ping");
Plugin.Log.Info("Sending ping");
}
else if(!isServer && message == "ping")
{
// reply to server ping
sendData("pong");
Plugin.Log.Info("Sending pong");
}
else if (isServer && message == "pong")
{
// when client replies to pong
latency = getTimestamp() - latency;
clientReady = true;
Plugin.Log.Info("Client ready");
}
else if(!isServer && message == "start")
{
// start the client game
setGameStatus(true);
Plugin.Log.Info("Client game started");
}
else
{
// set new coords for remote saber
string[] data = message.Split(';');
latestPosition = new Vector3(float.Parse(data[0]), float.Parse(data[1]), float.Parse(data[2]));
latestRotation = new Quaternion(float.Parse(data[3]), float.Parse(data[4]), float.Parse(data[5]), float.Parse(data[6]));
}
}
public void checkMessages()
{
NetIncomingMessage message;
if(isServer)
{
while ((message = server_connection.ReadMessage()) != null)
{
if (message.MessageType == NetIncomingMessageType.Data)
parseMessage(message.ReadString());
else
Plugin.Log.Debug(message.MessageType.ToString() + ":" + message.ReadString());
}
}
else
{
while ((message = client_connection.ReadMessage()) != null)
{
if (message.MessageType == NetIncomingMessageType.Data)
parseMessage(message.ReadString());
else
Plugin.Log.Debug(message.MessageType.ToString() + ":" + message.ReadString());
}
}
}
private void sendData(string data)
{
if (isServer)
{
NetOutgoingMessage msg = server_connection.CreateMessage();
msg.Write(data);
if (server_connection.Connections.Count > 0)
server_connection.SendMessage(msg, server_connection.Connections[0], NetDeliveryMethod.ReliableOrdered);
else
Plugin.Log.Critical("Client not connected");
}
else
{
NetOutgoingMessage msg = client_connection.CreateMessage();
msg.Write(data);
client_connection.SendMessage(msg, NetDeliveryMethod.ReliableOrdered);
}
}
}
}