-
Notifications
You must be signed in to change notification settings - Fork 17
/
Team.pde
323 lines (274 loc) · 9.99 KB
/
Team.pde
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
import org.json.*;
class Team {
String shortName; //max 8 chars
String longName; //max 24 chars
String unicastIP, multicastIP;
color c=(#000000);
boolean isCyan; //default: cyan@left
boolean newYellowCard, newRedCard, newRepair, newDoubleYellow, newPenaltyKick, newGoal; // Pending commands, effective only on gamestate change
int Score, RepairCount, RedCardCount, YellowCardCount, DoubleYellowCardCount, PenaltyCount;
long RepairOut;
int tableindex=0;
org.json.JSONObject worldstate_json;
String wsBuffer;
Robot[] r=new Robot[5];
File logFile;
PrintWriter logFileOut;
Client connectedClient;
boolean firstWorldState;
Team(color c, boolean uileftside) {
this.c=c;
this.isCyan=uileftside;
//robots
float x=0, y=64;
r[0]=new Robot(x, y);
r[1]=new Robot(x+56, y);
r[2]=new Robot(x, y + 56);
r[3]=new Robot(x+56, y + 56);
r[4]=new Robot(x+28, y + 112);
this.reset();
}
//===================================
void resetname(){
if (this.isCyan) {
this.shortName=Config.defaultCyanTeamShortName;
this.longName=Config.defaultCyanTeamLongName;
}
else {
this.shortName=Config.defaultMagentaTeamShortName;
this.longName=Config.defaultMagentaTeamLongName;
}
}
void logWorldstate(String teamWorldstate, int ageMs)
{
if(logFileOut == null)
return;
if(firstWorldState) {
logFileOut.println("["); // Start of JSON array
firstWorldState = false;
}else{
logFileOut.println(","); // Separator for the new JSON object
}
logFileOut.print("{");
logFileOut.print("\"teamName\": " + shortName + ",");
logFileOut.print("\"timestamp\": " + (System.currentTimeMillis() - ageMs) + ",");
logFileOut.print("\"gametimeMs\": " + getGameTime() + ",");
logFileOut.print("\"worldstate\": " + teamWorldstate);
logFileOut.print("}");
}
void reset() {
if(logFileOut != null) {
logFileOut.println("]"); // End JSON array
logFileOut.close();
}
logFileOut = null;
logFile = null;
this.resetname();
this.worldstate_json = null;
this.wsBuffer = "";
this.Score=0;
this.RepairCount=0;
this.RedCardCount=0;
this.YellowCardCount=0;
this.DoubleYellowCardCount=0;
this.PenaltyCount=0;
this.RepairOut=0;
this.newYellowCard=false;
this.newRedCard=false;
this.newRepair=false;
this.newDoubleYellow=false;
this.newPenaltyKick=false;
for (int i=0; i<5; i++)
r[i].reset();
if(this.connectedClient != null && this.connectedClient.active())
this.connectedClient.stop();
this.connectedClient = null;
this.firstWorldState = true;
}
// Function called when team connects and is accepted
void teamConnected(TableRow teamselect)
{
shortName=teamselect.getString("shortname8");
longName=teamselect.getString("longame24");
unicastIP = teamselect.getString("UnicastAddr");
multicastIP = teamselect.getString("MulticastAddr");
if(connectedClient != null)
BaseStationServer.disconnect(connectedClient);
connectedClient = connectingClient;
connectingClient.write(COMM_WELCOME);
connectingClient = null;
if(this.logFile == null || this.logFileOut == null)
{
this.logFile = new File(mainApplet.dataPath("tmp/" + Log.getTimedName() + "." + (isCyan?"A":"B") + ".msl"));
try{
this.logFileOut = new PrintWriter(new BufferedWriter(new FileWriter(logFile, true)));
}catch(IOException e){ }
}
}
//*******************************************************************
//*******************************************************************
void repair_timer_start() {
this.RepairOut=getSplitTime()+Config.repairPenalty_ms;
}
//*******************************************************************
void repair_timer_check() {
long remain=RepairOut-getSplitTime();
if (StateMachine.isInterval()) {
remain = -1;
println("Repair reseted!");
}
if (remain>=0)
for(int i=0; i<RepairCount; i++) r[i].waittime=int(remain/1000);
else {
for(int i=0; i<RepairCount; i++) r[i].waittime=-1;
RepairCount=0;
println("Repair OUT: "+shortName+" @"+(isCyan?"left":"right"));
}
}
//*******************************************************************
public void double_yellow_timer_start() {
r[5-DoubleYellowCardCount].DoubleYellowOut=getAbsoluteTime()+Config.doubleYellowPenalty_ms;
}
public void double_yellow_timer_check() {
for (int i=(5-DoubleYellowCardCount); i<5; i++) {
long remain;
if (StateMachine.isHalf()) {
remain=r[i].DoubleYellowOut-getAbsoluteTime();
}
else {
remain = r[i].waittime * 1000;
r[i].DoubleYellowOut = remain + getAbsoluteTime();
}
if (remain>=0)
r[i].waittime=PApplet.parseInt(remain/1000);
else { //shift right & reset
r[i].reset();
for (int j=4; j>0; j--) {
if (!r[j].state.equals("doubleyellow") && r[j-1].state.equals("doubleyellow")){
r[j].setRstate(r[j-1]);
r[j-1].reset();
}
}
DoubleYellowCardCount--;
println("Double Yellow end: "+shortName+" @"+(isCyan?"left":"right"));
}
}
}
//*******************************************************************
void setDoubleYellowOutRemain() {
println("setDoubleYellowOutRemain");
for (int j=0; j<5; j++) {
if (r[j].state.equals("doubleyellow")) r[j].DoubleYellowOutRemain=r[j].DoubleYellowOut-getGameTime();
else r[j].DoubleYellowOutRemain=0;
}
}
//*******************************************************************
void resumeDoubleYellowOutRemain() {
println("resumeDoubleYellowOutRemain");
for (int j=0; j<5; j++) {
if (r[j].state.equals("doubleyellow")) r[j].DoubleYellowOut=r[j].DoubleYellowOutRemain;
r[j].DoubleYellowOutRemain=0;
}
}
//*******************************************************************
void repairclear() {
this.RepairCount=0;
this.RepairOut=0;
for (int i=0; i<5; i++)
if (r[i].state.equals("repair")) r[i].reset_to_play();
}
//*******************************************************************
void checkflags() {
if (this.newRepair) {
this.RepairCount++;
this.repair_timer_start();
this.newRepair=false;
// Hack: send command only on game change
if(this.isCyan) event_message_v2(ButtonsEnum.BTN_C_REPAIR, true);
else event_message_v2(ButtonsEnum.BTN_M_REPAIR, true);
}
if (this.newYellowCard) {
this.YellowCardCount=1;
this.newYellowCard=false;
// Hack: send command only on game change
if(this.isCyan) event_message_v2(ButtonsEnum.BTN_C_YELLOW, true);
else event_message_v2(ButtonsEnum.BTN_M_YELLOW, true);
}
if (this.newRedCard) {
this.RedCardCount++;
this.newRedCard=false;
// Hack: send command only on game change
if(this.isCyan) event_message_v2(ButtonsEnum.BTN_C_RED, true);
else event_message_v2(ButtonsEnum.BTN_M_RED, true);
}
if (this.newDoubleYellow) {
this.DoubleYellowCardCount++;
this.YellowCardCount=0;
this.double_yellow_timer_start();
this.newDoubleYellow=false;
if(this.isCyan) send_event_v2(""+COMM_DOUBLE_YELLOW_CYAN, "Double Yellow", this);
else send_event_v2(""+COMM_DOUBLE_YELLOW_MAGENTA, "Double Yellow", this);
}
if (this.newPenaltyKick) {
this.PenaltyCount++;
this.newPenaltyKick=false;
}
}
//*******************************************************************
//*******************************************************************
void updateUI() {
if(connectedClient != null && !connectedClient.active())
{
println("Connection to team \"" + longName + "\" dropped.");
Log.logMessage("Team " + shortName + " dropped");
BaseStationServer.disconnect(connectedClient);
resetname();
connectedClient = null;
}
//team names
String sn=shortName;
String ln=longName;
if (sn.length()>Config.maxShortName) sn=shortName.substring(0, Config.maxShortName);
if (ln.length()>Config.maxLongName) ln=longName.substring(0, Config.maxLongName);
rectMode(CENTER);
fill(255);
textFont(teamFont);
textAlign(CENTER, CENTER);
if (isCyan) text(sn, 163, 50);
else text(sn, 837, 50);
textFont(panelFont);
if (isCyan) text(ln, 163, 90);
else text(ln, 837, 90);
// robot state
for (int i=0; i<5; i++) r[i].state="play";//in-game: white, default setting
for (int i=0; i<RepairCount; i++) r[i].state="repair";//in-repair: blue
for (int i=RepairCount; i< min(RepairCount+YellowCardCount, 5); i++) r[i].state="yellow"; //yellow-card: yellow
for (int i=(RepairCount+YellowCardCount); i<min(RepairCount+YellowCardCount+RedCardCount, 5); i++) r[i].state="red";//red
for (int i=(5-DoubleYellowCardCount); i<5; i++) r[i].state="doubleyellow";//doubleyellow
if (RepairCount > 0) //repair #
repair_timer_check();
if (DoubleYellowCardCount > 0) //double yellow #
double_yellow_timer_check();
for (int i=0; i<5; i++)
r[i].updateUI(c,isCyan);
textAlign(LEFT, BOTTOM);
textFont(debugFont);
fill(#ffff00);
textLeading(20);
String ts="Goals."+this.Score+" Penalty:"+this.PenaltyCount+"\nYellow:"+this.YellowCardCount+" Red:"+this.RedCardCount+"\nRepair:"+this.RepairCount+" 2xYellow:"+this.DoubleYellowCardCount;
if (isCyan) text(ts, 40, height-18);
else text(ts, width - 190, height-18);
}
//*******************************************************************
boolean IPBelongs(String clientipstr)
{
if(this.unicastIP == null)
return false;
String[] iptokens;
if (!clientipstr.equals("0:0:0:0:0:0:0:1")) {
iptokens=split(clientipstr,'.');
if (iptokens!=null) clientipstr=iptokens[0]+"."+iptokens[1]+"."+iptokens[2]+".*";
}
return this.unicastIP.equals(clientipstr);
}
}