-
Notifications
You must be signed in to change notification settings - Fork 0
/
MultiUserManager.cs
356 lines (311 loc) · 10.6 KB
/
MultiUserManager.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
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
using System.Collections;
using System.Collections.Generic;
using System;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using UnityEngine;
using Random = System.Random;
public class MultiUserManager : MonoBehaviour
{
public GameObject[] m_Robots = null;
private int m_index = 0;
private int robotPositionIndex = 0;
public Transform[] spawnPositions;
public GameObject[] setupPrefab;
private GameObject setup;
// TCP server
private int recv;
private Socket newsock;
private Socket client;
private WebsiteCommands websiteCommands = new WebsiteCommands();
private bool currentGameStart = false;
private string currentGameSetup = "A";
private string currentGameType = "";
private int currentCam = 0;
private RobotCustomizer robotCustomizer;
private GameTimer gameTimer;
private Thread thread;
private float previousRealTime;
private bool resetCoolDown = false;
private ScoreKeeper scoreKeeper;
private IntakeControl intake;
private CameraPosition camera;
private bool sendingScore;
private void Start()
{
scoreKeeper = GameObject.Find("ScoreKeeper").GetComponent<ScoreKeeper>();
intake = GameObject.Find("Intake").GetComponent<IntakeControl>();
camera = GameObject.Find("Render Streaming Camera").GetComponent<CameraPosition>();
robotCustomizer = m_Robots[m_index].GetComponent<RobotCustomizer>();
gameTimer = GameObject.Find("ScoreKeeper").GetComponent<GameTimer>();
setSpawn(0);
resetField("A");
print("Started.....");
thread = new Thread(startTCPServer);
thread.Start();
}
private void OnDestroy()
{
client.Close();
newsock.Close();
thread.Abort();
}
// Some sort of TCP connection to the website to handle user pref like which robot, position of the robot, dimensions of the robot, color of the robot, team number of the robot, start/stop game, and select which game mode to run (freeplay, autonomous, teleop, and full match)
#region TCP server for sending and receiving data
void startTCPServer()
{
int recv;
byte[] data = new byte[1024];
IPEndPoint ipep = new IPEndPoint(IPAddress.Any,
9052);
newsock = new
Socket(AddressFamily.InterNetwork,
SocketType.Stream, ProtocolType.Tcp);
newsock.Bind(ipep);
newsock.Listen(10);
print("Waiting for a TCP client...");
client = newsock.Accept();
IPEndPoint clientep =
(IPEndPoint)client.RemoteEndPoint;
print("Connected with {0} at port {1}" +
clientep.Address + clientep.Port);
string welcome = "Welcome to my test server";
data = Encoding.ASCII.GetBytes(welcome);
client.Send(data, data.Length,
SocketFlags.None);
while (true)
{
try
{
data = new byte[1024];
recv = client.Receive(data);
if (recv == 0)
break;
string message = Encoding.ASCII.GetString(data, 0, recv);
print(message);
websiteCommands = WebsiteCommands.CreateFromJSON(message);
if (sendingScore)
{
var sendData = new byte[1024];
sendingScore = false;
var score = new WebsiteScore();
score.gameType = websiteCommands.gameType;
if (websiteCommands.robotTeam == 0)
score.score = scoreKeeper.getScoreRed();
else
score.score = scoreKeeper.getScoreBlue();
string scoreJSON = JsonUtility.ToJson(score);
sendData = Encoding.ASCII.GetBytes(scoreJSON);
//client.Send(data, recv, SocketFlags.None);
client.Send(sendData, SocketFlags.None);
}
}
catch (SocketException)
{
print("Client lost connection");
break;
}
}
print("Disconnected from {0}" +
clientep.Address);
client.Close();
newsock.Close();
startTCPServer();
}
#endregion
#region Game Control
private void setSpawn(int index)
{
robotPositionIndex = index;
transform.position = spawnPositions[index].position;
transform.rotation = spawnPositions[index].rotation;
resetRobot();
}
private void resetRobot()
{
intake.resetBalls();
var newRotation = m_Robots[m_index].transform.rotation.eulerAngles;
newRotation.x = -90f;
newRotation.y = 0f;
newRotation.z = 180f;
var newRotationQ = m_Robots[m_index].transform.rotation;
newRotationQ.eulerAngles = newRotation;
//
m_Robots[m_index].transform.position = transform.position;
m_Robots[m_index].transform.rotation = newRotationQ;
}
private void resetField(string type)
{
scoreKeeper.resetScore();
Destroy(setup);
int index;
if (type == "A")
{
index = 0;
}
else if (type == "B")
{
index = 1;
}
else if (type == "C")
{
index = 2;
}
else
{
Random rnd = new Random();
index = rnd.Next(3);
}
if (index == 0)
gameTimer.setGameSetup("A");
else if (index == 1)
gameTimer.setGameSetup("B");
else if (index == 2)
gameTimer.setGameSetup("C");
GameObject[] gos = GameObject.FindGameObjectsWithTag("Ring");
foreach (GameObject a in gos)
{
Destroy(a);
}
setup = (GameObject)Instantiate(setupPrefab[index], new Vector3(0, 0.5f, 0), Quaternion.identity);
for (int x = 0; x < setup.GetComponentsInChildren<Rigidbody>().Length; x++)
{
if (setup.GetComponentsInChildren<Rigidbody>()[x].tag == "Wobble")
{
setup.GetComponentsInChildren<Rigidbody>()[x].centerOfMass = new Vector3(0, -0.6f, 0);
}
}
}
#endregion
#region Robot Selector
public void OnButtonClick(int index)
{
if (index < m_Robots.Length)
{
m_Robots[m_index].SetActive(false);
m_index = index;
m_Robots[m_index].SetActive(true);
}
robotCustomizer = m_Robots[m_index].GetComponent<RobotCustomizer>();
}
#endregion
void FixedUpdate()
{
// Setting new robot position
if (websiteCommands.position != robotPositionIndex)
{
setSpawn(websiteCommands.position);
}
// Changing robot
if (websiteCommands.robotType != m_index)
{
OnButtonClick(websiteCommands.robotType);
}
// Reset field
if (websiteCommands.resetField && !resetCoolDown)
{
resetCoolDown = true;
previousRealTime = Time.realtimeSinceStartup;
resetField(websiteCommands.gameSetup);
resetRobot();
}
else if (Time.realtimeSinceStartup - previousRealTime > 5)
{
resetCoolDown = false;
}
// Game setup
if (websiteCommands.gameSetup != currentGameSetup)
{
currentGameSetup = websiteCommands.gameSetup;
resetField(websiteCommands.gameSetup);
resetRobot();
}
// Start game
if (websiteCommands.startGame && !currentGameStart)
{
currentGameStart = true;
gameTimer.startGame();
}
else if (!websiteCommands.startGame && currentGameStart)
{
currentGameStart = false;
gameTimer.stopGame();
}
// Game type
if (websiteCommands.gameType != currentGameType)
{
gameTimer.setGameType(websiteCommands.gameType);
currentGameType = websiteCommands.gameType;
}
// Camera control
if (websiteCommands.cam != currentCam)
{
currentCam = websiteCommands.cam;
camera.switchCamera(currentCam);
}
// Sending Score
if (gameTimer.getTimer() <= 0 && (websiteCommands.gameType == "auto" || websiteCommands.gameType == "teleop"))
{
sendingScore = true;
}
// Robot config
/*
if (websiteCommands.incSize)
{
print("Increase size 1");
robotCustomizer.IncreaseRobotSize_PointerDown();
}
else if (websiteCommands.decSize)
{
print("Dec size 1");
robotCustomizer.DecreaseRobotSize_PointerDown();
}
else if (websiteCommands.incWheel)
{
print("Increase wheel 1");
robotCustomizer.IncAxelDis_PointerDown();
}
else if (websiteCommands.decWheel)
{
print("Dec wheel 1");
robotCustomizer.DecAxelDis_PointerDown();
}
else
{
robotCustomizer.OnPointerUp();
}
*/
}
[System.Serializable]
public class WebsiteCommands
{
public int position;
public int robotTeam;
public int robotType;
public bool resetField;
public bool startGame;
public string gameType = "";
public string gameSetup = "";
public float size;
public float wheelSize;
public int cam;
public static WebsiteCommands CreateFromJSON(string jsonString)
{
return JsonUtility.FromJson<WebsiteCommands>(jsonString);
}
// Given JSON input:
// {"name":"Dr Charles","lives":3,"health":0.8}
// this example will return a PlayerInfo object with
// name == "Dr Charles", lives == 3, and health == 0.8f.
// {"position":0,"robotType":0,"resetField":true, "startGame":false, "gameType": " freeplay", "gameSetup":"A", "incSize": false, "decSize":false, "incWheel":false, "decWheel":false}
// {"position":0-4,"robotType":0-2,"resetField":true/false, "startGame":false/true, "gameType": "teleop/auto/freeplay", "gameSetup":"A/B/C/Random", "incSize":true/false, "decSize":true/false, "incWheel":true/false, "decWheel":true/false}
}
[System.Serializable]
public class WebsiteScore
{
public string gameType;
public int score;
}
}