-
Notifications
You must be signed in to change notification settings - Fork 3
/
managed_games.patch
712 lines (673 loc) · 25.4 KB
/
managed_games.patch
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
diff --git src/fpsgame/client.cpp src/fpsgame/client.cpp
index b0cc1c8..1e3163e 100644
--- src/fpsgame/client.cpp
+++ src/fpsgame/client.cpp
@@ -2060,8 +2060,18 @@ namespace game
case N_SERVCMD:
getstring(text, p);
+ handlecapprobe(text);
break;
+ case N_P1X_RECORDDEMO:
+ if(!getint(p)) enddemorecord(true);
+ else if(!demorecord)
+ {
+ conoutf("server triggered client demo recording");
+ setupdemorecord();
+ }
+ break;
+
default:
neterr("type", cn < 0);
return;
diff --git src/fpsgame/game.h src/fpsgame/game.h
index 02277f9..b37ef32 100644
--- src/fpsgame/game.h
+++ src/fpsgame/game.h
@@ -213,6 +213,9 @@ enum
S_FLAGFAIL
};
+// protocol extensions
+static const char * const CAP_PROBE_CLIENT_DEMO_UPLOAD = "capability_probe_protocol_extension_p1x_client_demo_upload_v2";
+
// network messages codes, c2s, c2c, s2c
enum
@@ -242,6 +245,8 @@ enum
N_INITTOKENS, N_TAKETOKEN, N_EXPIRETOKENS, N_DROPTOKENS, N_DEPOSITTOKENS, N_STEALTOKENS,
N_SERVCMD,
N_DEMOPACKET,
+ // N_P1X_CLIENT_DEMO_UPLOAD_SUPPORTED = 1000, N_P1X_RECORDDEMO, // legacy
+ N_P1X_CLIENT_DEMO_UPLOAD_SUPPORTED = 1002, N_P1X_RECORDDEMO, // guarded by CAP_PROBE_CLIENT_DEMO_UPLOAD
NUMMSG
};
@@ -272,6 +276,7 @@ static const int msgsizes[] = // size inclusive message token, 0 f
N_INITTOKENS, 0, N_TAKETOKEN, 2, N_EXPIRETOKENS, 0, N_DROPTOKENS, 0, N_DEPOSITTOKENS, 2, N_STEALTOKENS, 0,
N_SERVCMD, 0,
N_DEMOPACKET, 0,
+ N_P1X_CLIENT_DEMO_UPLOAD_SUPPORTED, 1, N_P1X_RECORDDEMO, 1,
-1
};
@@ -871,6 +876,10 @@ namespace game
extern void recordpacket(int chan, void *data, int len);
extern bool recordmsg(int type, const char *fmt = NULL, ...);
- extern void enddemorecord();
+ extern void enddemorecord(bool send = false);
+
+ // managed games
+ extern void handlecapprobe(const char *msg);
+ extern void sendclientdemo(stream *demo);
}
#include "fragmessages.h"
@@ -1052,6 +1063,7 @@ namespace server
void *authchallenge;
int authkickvictim;
char *authkickreason;
+ bool supportsclientdemoupload;
clientinfo() : getdemo(NULL), getmap(NULL), clipboard(NULL), authchallenge(NULL), authkickreason(NULL) { reset(); }
~clientinfo() { events.deletecontents(); cleanclipboard(); cleanauth(); }
@@ -1161,6 +1173,7 @@ namespace server
cleanclipboard();
cleanauth();
mapchange();
+ supportsclientdemoupload = false;
}
int geteventmillis(int servmillis, int clientmillis)
@@ -1177,6 +1190,7 @@ namespace server
extern vector<clientinfo *> clients;
extern const char *modename(int n, const char *unknown = "unknown");
+ extern int mastermode;
extern const char *mastermodename(int n, const char *unknown = "unknown");
extern void startintermission();
extern void stopdemo();
@@ -1189,6 +1203,44 @@ namespace server
extern int msgsizelookup(int msg);
extern bool serveroption(const char *arg);
extern bool delayspawn(int type);
+
+ // remote commands
+ extern bool handleservcmd(clientinfo *ci, const char *cmd);
+
+ // demos
+ struct demofile
+ {
+ string info;
+ uchar *data;
+ int len;
+ };
+ extern bool demonextmatch;
+ extern char *demodir;
+ extern bool savedemo(demofile &d, const char *povname = "server");
+
+ // managed games
+ extern bool managedgame, managedgamenextmatch;
+ extern bool specmute;
+ extern int gamelimitnextmatch;
+#define DEFAULT_GAMELIMIT (10*60*1000)
+ extern void probeforclientdemoupload(packetbuf &p);
+ extern void setspecmute(clientinfo *ci, bool on);
+ extern void queryspecmute(clientinfo *ci);
+ extern void setupmanagedgame(clientinfo *ci, bool specmute = false, char *duration = NULL);
+ extern void startmanagedgame();
+ extern void cleanupmanagedgame();
+ extern void onspawn(clientinfo *ci);
+ extern void managedgameended();
+ extern int gamemode;
+ extern string smapname;
+ extern bool resuming;
+ extern int restrictpausegame;
+ extern void pausegame(bool val, clientinfo *ci = NULL);
+ extern void resumewithcountdown(clientinfo *ci);
+ extern void sendspawn(clientinfo *ci);
+ extern clientinfo *getinfo(int cn);
+ extern const char *colorname(clientinfo *ci, const char *name = NULL);
+ extern void receiveclientdemo(int sender, uchar *data, int len);
}
// additional colors
diff --git src/fpsgame/server.cpp src/fpsgame/server.cpp
index 7d25726..4f4d1a4 100644
--- src/fpsgame/server.cpp
+++ src/fpsgame/server.cpp
@@ -292,13 +292,6 @@ namespace server
COMMAND(maprotationreset, "");
COMMANDN(maprotation, addmaprotations, "ss2V");
- struct demofile
- {
- string info;
- uchar *data;
- int len;
- };
-
vector<demofile> demos;
bool demonextmatch = false;
@@ -501,7 +494,7 @@ namespace server
return false;
}
- const char *colorname(clientinfo *ci, const char *name = NULL)
+ const char *colorname(clientinfo *ci, const char *name)
{
if(!name) name = ci->name;
if(name[0] && !duplicatename(ci, name) && ci->state.aitype == AI_NONE) return name;
@@ -756,12 +749,13 @@ namespace server
char *timestr = ctime(&t), *trim = timestr + strlen(timestr);
while(trim>timestr && iscubespace(*--trim)) *trim = '\0';
formatstring(d.info, "%s: %s, %s, %.2f%s", timestr, modename(gamemode), smapname, len > 1024*1024 ? len/(1024*1024.f) : len/1024.0f, len > 1024*1024 ? "MB" : "kB");
- sendservmsgf("demo \"%s\" recorded", d.info);
+ sendservmsgf("demo \"%s\" recorded on server", d.info);
d.data = new uchar[len];
d.len = len;
demotmp->seek(0, SEEK_SET);
demotmp->read(d.data, len);
DELETEP(demotmp);
+ if(managedgame) savedemo(d);
}
void enddemorecord()
@@ -805,7 +799,7 @@ namespace server
stream *f = opengzfile(NULL, "wb", demotmp);
if(!f) { DELETEP(demotmp); return; }
- sendservmsg("recording demo");
+ sendservmsg("recording server demo");
demorecord = f;
@@ -1032,9 +1026,10 @@ namespace server
else enddemorecord();
}
- void pausegame(bool val, clientinfo *ci = NULL)
+ void pausegame(bool val, clientinfo *ci)
{
if(gamepaused==val) return;
+ if(!val) resuming = false;
gamepaused = val;
sendf(-1, 1, "riii", N_PAUSEGAME, gamepaused ? 1 : 0, ci ? ci->clientnum : -1);
}
@@ -1300,7 +1300,7 @@ namespace server
}
uchar operator[](int msg) const { return msg >= 0 && msg < NUMMSG ? msgmask[msg] : 0; }
- } msgfilter(-1, N_CONNECT, N_SERVINFO, N_INITCLIENT, N_WELCOME, N_MAPCHANGE, N_SERVMSG, N_DAMAGE, N_HITPUSH, N_SHOTFX, N_EXPLODEFX, N_DIED, N_SPAWNSTATE, N_FORCEDEATH, N_TEAMINFO, N_ITEMACC, N_ITEMSPAWN, N_TIMEUP, N_CDIS, N_CURRENTMASTER, N_PONG, N_RESUME, N_BASESCORE, N_BASEINFO, N_BASEREGEN, N_ANNOUNCE, N_SENDDEMOLIST, N_SENDDEMO, N_DEMOPLAYBACK, N_SENDMAP, N_DROPFLAG, N_SCOREFLAG, N_RETURNFLAG, N_RESETFLAG, N_INVISFLAG, N_CLIENT, N_AUTHCHAL, N_INITAI, N_EXPIRETOKENS, N_DROPTOKENS, N_STEALTOKENS, N_DEMOPACKET, -2, N_REMIP, N_NEWMAP, N_GETMAP, N_SENDMAP, N_CLIPBOARD, -3, N_EDITENT, N_EDITF, N_EDITT, N_EDITM, N_FLIP, N_COPY, N_PASTE, N_ROTATE, N_REPLACE, N_DELCUBE, N_EDITVAR, N_EDITVSLOT, N_UNDO, N_REDO, -4, N_POS, NUMMSG),
+ } msgfilter(-1, N_CONNECT, N_SERVINFO, N_INITCLIENT, N_WELCOME, N_MAPCHANGE, N_SERVMSG, N_DAMAGE, N_HITPUSH, N_SHOTFX, N_EXPLODEFX, N_DIED, N_SPAWNSTATE, N_FORCEDEATH, N_TEAMINFO, N_ITEMACC, N_ITEMSPAWN, N_TIMEUP, N_CDIS, N_CURRENTMASTER, N_PONG, N_RESUME, N_BASESCORE, N_BASEINFO, N_BASEREGEN, N_ANNOUNCE, N_SENDDEMOLIST, N_SENDDEMO, N_DEMOPLAYBACK, N_SENDMAP, N_DROPFLAG, N_SCOREFLAG, N_RETURNFLAG, N_RESETFLAG, N_INVISFLAG, N_CLIENT, N_AUTHCHAL, N_INITAI, N_EXPIRETOKENS, N_DROPTOKENS, N_STEALTOKENS, N_DEMOPACKET, N_P1X_RECORDDEMO, -2, N_REMIP, N_NEWMAP, N_GETMAP, N_SENDMAP, N_CLIPBOARD, -3, N_EDITENT, N_EDITF, N_EDITT, N_EDITM, N_FLIP, N_COPY, N_PASTE, N_ROTATE, N_REPLACE, N_DELCUBE, N_EDITVAR, N_EDITVSLOT, N_UNDO, N_REDO, -4, N_POS, NUMMSG),
connectfilter(-1, N_CONNECT, -2, N_AUTHANS, -3, N_PING, NUMMSG);
int checktype(int type, clientinfo *ci)
@@ -1538,6 +1533,7 @@ namespace server
{
packetbuf p(MAXTRANS, ENET_PACKET_FLAG_RELIABLE);
int chan = welcomepacket(p, ci);
+ if(!ci->local) probeforclientdemoupload(p);
sendpacket(ci->clientnum, chan, p.finalize());
}
@@ -1756,7 +1756,7 @@ namespace server
gamemode = mode;
gamemillis = 0;
- gamelimit = 10*60000;
+ gamelimit = gamelimitnextmatch;
interm = 0;
nextexceeded = 0;
copystring(smapname, s);
@@ -1803,6 +1799,9 @@ namespace server
if(smode) smode->setup();
+ if(managedgamenextmatch) startmanagedgame();
+ else cleanupmanagedgame();
+
if(isdedicatedserver()) logoutf("started %s on %s", modename(mode, "unknown mode"), smapname);
}
@@ -2252,7 +2251,8 @@ namespace server
{
if(demorecord) enddemorecord();
interm = -1;
- checkvotes(true);
+ if(managedgame) managedgameended();
+ else checkvotes(true);
}
}
@@ -2405,6 +2405,7 @@ namespace server
ci->state.timeplayed += lastmillis - ci->state.lasttimeplayed;
savescore(ci);
sendf(-1, 1, "ri2", N_CDIS, n);
+ if(managedgame && ci->state.state!=CS_SPECTATOR) pausegame(true);
clients.removeobj(ci);
aiman::removeai(ci);
if(!numclients(-1, false, true)) noclients(); // bans clear when server empties
@@ -2665,7 +2666,7 @@ namespace server
authchallenged(id, val, desc);
}
- void receivefile(int sender, uchar *data, int len)
+ void receivemapdata(int sender, uchar *data, int len)
{
if(!m_edit || len <= 0 || len > 4*1024*1024) return;
clientinfo *ci = getinfo(sender);
@@ -2726,7 +2727,7 @@ namespace server
void parsepacket(int sender, int chan, packetbuf &p) // has to parse exactly each byte of the packet
{
- if(sender<0 || p.packet->flags&ENET_PACKET_FLAG_UNSEQUENCED || chan > 2) return;
+ if(sender<0 || p.packet->flags&ENET_PACKET_FLAG_UNSEQUENCED || chan >= numchannels()) return;
char text[MAXTRANS];
int type;
clientinfo *ci = sender>=0 ? getinfo(sender) : NULL, *cq = ci, *cm = ci;
@@ -2788,7 +2789,12 @@ namespace server
}
else if(chan==2)
{
- receivefile(sender, p.buf, p.maxlen);
+ receivemapdata(sender, p.buf, p.maxlen);
+ return;
+ }
+ else if(chan==3)
+ {
+ receiveclientdemo(sender, p.buf, p.maxlen);
return;
}
@@ -2976,6 +2982,7 @@ namespace server
putint(cm->messages, N_SPAWN);
sendstate(cq->state, cm->messages);
});
+ if(managedgame) onspawn(cq);
break;
}
@@ -3049,9 +3056,11 @@ namespace server
case N_TEXT:
{
QUEUE_AI;
- QUEUE_MSG;
getstring(text, p);
filtertext(text, text, true, true);
+ if(text[0]=='#' && handleservcmd(cq, &text[1])) break;
+ if(specmute && cq->state.state==CS_SPECTATOR && cq->privilege<PRIV_AUTH) break;
+ QUEUE_INT(N_TEXT);
QUEUE_STR(text);
if(isdedicatedserver() && cq) logoutf("%s (cn %d): %s", colorname(cq), cq->clientnum, text);
break;
@@ -3262,7 +3271,7 @@ namespace server
clientinfo *spinfo = (clientinfo *)getclientinfo(spectator); // no bots
if(!spinfo || !spinfo->connected || (spinfo->state.state==CS_SPECTATOR ? val : !val)) break;
- if(spinfo->state.state!=CS_SPECTATOR && val) forcespectator(spinfo);
+ if(spinfo->state.state!=CS_SPECTATOR && val) { forcespectator(spinfo); if(managedgame) pausegame(true, ci); }
else if(spinfo->state.state==CS_SPECTATOR && !val) unspectate(spinfo);
if(cq && cq != ci && cq->ownernum != ci->clientnum) cq = NULL;
@@ -3422,6 +3431,11 @@ namespace server
{
int val = getint(p);
if(ci->privilege < (restrictpausegame ? PRIV_ADMIN : PRIV_MASTER) && !ci->local) break;
+ if(managedgame && !val)
+ {
+ if(!resuming) resumewithcountdown(ci);
+ break;
+ }
pausegame(val > 0, ci);
break;
}
@@ -3505,6 +3519,13 @@ namespace server
case N_SERVCMD:
getstring(text, p);
+ handleservcmd(ci, text);
+ break;
+
+ case N_P1X_CLIENT_DEMO_UPLOAD_SUPPORTED:
+ if(!ci || ci->local) return;
+ conoutf("client %d supports the client demo upload protocol extension!", ci->clientnum);
+ ci->supportsclientdemoupload = true;
break;
#define PARSEMESSAGES 1
@@ -3541,7 +3562,7 @@ namespace server
int serverport(int infoport) { return infoport < 0 ? SAUERBRATEN_SERVER_PORT : infoport-1; }
const char *defaultmaster() { return "master.sauerbraten.org"; }
int masterport() { return SAUERBRATEN_MASTER_PORT; }
- int numchannels() { return 3; }
+ int numchannels() { return 4; } // 0: positional data, 1: reliable, 2: getdemo/sendmap, 3: sendclientdemo
#include "extinfo.h"
diff --git src/p1xbraten/capability_probing.cpp src/p1xbraten/capability_probing.cpp
new file mode 100644
index 0000000..232c554
--- /dev/null
+++ src/p1xbraten/capability_probing.cpp
@@ -0,0 +1,10 @@
+#include "game.h"
+
+namespace game {
+
+ void handlecapprobe(const char *msg)
+ {
+ if(!strcmp(msg, CAP_PROBE_CLIENT_DEMO_UPLOAD)) addmsg(N_P1X_CLIENT_DEMO_UPLOAD_SUPPORTED, "r");
+ }
+
+}
diff --git src/p1xbraten/clientdemo.cpp src/p1xbraten/clientdemo.cpp
index 839b0a9..48e29ec 100644
--- src/p1xbraten/clientdemo.cpp
+++ src/p1xbraten/clientdemo.cpp
@@ -22,11 +22,12 @@ namespace game {
int demostartmillis = 0;
- void enddemorecord()
+ void enddemorecord(bool send)
{
if(!demorecord) return;
DELETEP(demorecord);
conoutf("stopped client demo recording");
if(!demo) return;
+ if(send) sendclientdemo(demo);
DELETEP(demo);
}
@@ -52,6 +52,7 @@ namespace game {
switch (type) {
case N_SENDDEMOLIST:
case N_AUTHTRY: case N_AUTHCHAL: case N_AUTHANS: case N_REQAUTH:
+ case N_P1X_RECORDDEMO:
return;
}
int stamp[3] = { totalmillis-demostartmillis, chan, len };
diff --git src/p1xbraten/managed_games.cpp src/p1xbraten/managed_games.cpp
new file mode 100644
index 0000000..1c1b1d7
--- /dev/null
+++ src/p1xbraten/managed_games.cpp
@@ -0,0 +1,281 @@
+#include "game.h"
+
+#ifndef STANDALONE
+namespace game {
+
+ void sendclientdemo(stream *demo)
+ {
+ conoutf("sending demo to server...");
+ if(!demo) return;
+ stream::offset len = demo->size();
+ if(len > 16*1024*1024) conoutf(CON_ERROR, "demo file too large"); // todo: is 16 MB enough?
+ else if(len <= 0) conoutf(CON_ERROR, "could not read demo");
+ else sendfile(-1, 3, demo);
+ conoutf("client demo sent");
+ }
+}
+#endif
+
+namespace server {
+
+ void probeforclientdemoupload(packetbuf &p)
+ {
+ putint(p, N_SERVCMD);
+ sendstring(CAP_PROBE_CLIENT_DEMO_UPLOAD, p);
+ }
+
+ int parseduration(char *s) // MM[:SS] -> ms
+ {
+ int mins = strtoul(s, &s, 10), secs = 0;
+ if(*s == ':') secs = strtoul(s+1, &s, 10);
+ return max((mins*60 + secs)*1000, 0);
+ }
+
+ bool managedgame = false, managedgamenextmatch = false;
+ bool specmute = false, specmutenextmatch = false;
+ int gamelimitnextmatch = DEFAULT_GAMELIMIT;
+
+ void queryspecmute(clientinfo *ci)
+ {
+ if(!ci || (ci->privilege<PRIV_AUTH && !ci->local)) return;
+ sendf(ci->clientnum, 1, "ris", N_SERVMSG, specmute
+ ? "spectators are muted (use #specmute 0 to unmute)"
+ : "spectators are not muted (use #specmute 1 to mute)"
+ );
+ }
+
+ void setspecmute(clientinfo *ci, bool on)
+ {
+ if(!ci || (ci->privilege<PRIV_AUTH && !ci->local)) return;
+ specmute = on;
+ sendf(-1, 1, "ris", N_SERVMSG, specmute ? "spectators muted" : "spectators unmuted");
+ }
+
+ void setupmanagedgame(clientinfo *ci, bool mutespecs, char *duration)
+ {
+ if(!ci || (!ci->privilege && !ci->local)) return;
+
+ if(mastermode < MM_LOCKED) { mastermode = MM_LOCKED; sendf(-1, 1, "rii", N_MASTERMODE, mastermode); }
+
+ int maplimit = 0;
+ if(duration) maplimit = parseduration(duration);
+ if(maplimit) gamelimitnextmatch = maplimit;
+
+ string msg = "next match will be a managed game";
+ if(maplimit) concformatstring(msg, " (%d:%02d)", (maplimit/1000)/60, (maplimit/1000)%60);
+ sendf(-1, 1, "ris", N_SERVMSG, msg);
+ if(mutespecs) sendf(-1, 1, "ris", N_SERVMSG, "spectators will be muted");
+
+ loopv(clients)
+ {
+ clientinfo *ci = clients[i];
+ if(ci->state.state==CS_SPECTATOR) continue;
+ }
+
+ managedgamenextmatch = true;
+ specmutenextmatch = specmute || mutespecs;
+ demonextmatch = true;
+ }
+
+ void cleanupmanagedgame()
+ {
+ managedgame = managedgamenextmatch = false;
+ specmute = specmutenextmatch = false;
+ gamelimitnextmatch = DEFAULT_GAMELIMIT;
+ }
+
+ static vector<clientinfo *> notyetspawned;
+
+ void startmanagedgame()
+ {
+ managedgame = true;
+ managedgamenextmatch = false;
+ specmute = specmutenextmatch;
+ specmutenextmatch = false;
+ gamelimitnextmatch = DEFAULT_GAMELIMIT;
+
+ notyetspawned.shrink(0);
+ loopv(clients)
+ {
+ clientinfo *ci = clients[i];
+ if(ci->state.state!=CS_SPECTATOR) notyetspawned.add(ci);
+ }
+ pausegame(true);
+ sendf(-1, 1, "ris", N_SERVMSG, "waiting for all players to load the map");
+ resuming = true;
+ }
+
+ void managedgameended()
+ {
+ managedgame = false;
+ loopv(clients)
+ {
+ clientinfo *ci = clients[i];
+ if(ci->state.state==CS_SPECTATOR) continue;
+ if(ci->supportsclientdemoupload) sendf(ci->clientnum, 1, "rii", N_P1X_RECORDDEMO, 0);
+ }
+ }
+
+ const char *demofilename(const char *povname)
+ {
+ if(!povname) return NULL;
+
+ static string ts;
+ ts[0] = 0;
+ time_t t = time(NULL);
+ size_t len = strftime(ts, sizeof(ts), "%Y-%m-%d_%H.%M.%S", localtime(&t));
+ ts[min(len, sizeof(ts)-1)] = 0;
+
+ static string basename;
+ formatstring(basename, "%s_%s_%s_%s", ts, gamemodes[gamemode-STARTGAMEMODE].name, smapname, povname);
+ filtertext(basename, basename, false);
+ len = strlen(basename);
+ if(len < 4 || strcasecmp(&basename[len-4], ".dmo")) concatstring(basename, ".dmo");
+
+ const char* fname = getdemofile(basename, true);
+ return fname ? fname : basename;
+ }
+
+ bool savedemo(demofile &d, const char *povname)
+ {
+ const char *fname = demofilename(povname);
+ if(!fname) { conoutf(CON_WARN, "could not build demo file name"); return false; }
+ stream *f = openrawfile(fname, "wb");
+ if(!f) { conoutf(CON_WARN, "could not open %s to save demo", fname); return false; }
+ f->write(d.data, d.len);
+ conoutf("saved demo \"%s\"", fname);
+ delete f;
+ return true;
+ }
+
+
+ void notifyprivclients(int minpriv, char *msg)
+ {
+ loopv(clients)
+ {
+ clientinfo *ci = clients[i];
+ if(!ci || ci->privilege < minpriv) continue;
+ sendf(ci->clientnum, 1, "ris", N_SERVMSG, msg);
+ }
+ }
+
+ void receiveclientdemo(int sender, uchar *data, int len)
+ {
+ conoutf("receiving client demo file (length %d)", len);
+ if(len <= 0 || len > 16*1024*1024) return;
+ clientinfo *ci = getinfo(sender);
+ if(ci->state.state==CS_SPECTATOR) return;
+ demofile d; d.data = data; d.len = len;
+ savedemo(d, ci->name);
+ static string msg;
+ formatstring(msg, "received client-side demo from %s", colorname(ci));
+ notifyprivclients(PRIV_ADMIN, msg);
+ }
+
+ struct messageevent : gameevent
+ {
+ int millis;
+ clientinfo *ci = NULL;
+ string msg;
+
+ bool flush(clientinfo *_ , int fmillis)
+ {
+ if(millis > fmillis) return false;
+ if(!ci || ci->connected) sendf(-1, 1, "ris", N_SERVMSG, msg);
+ return true;
+ }
+ };
+
+ struct startevent : gameevent
+ {
+ int millis;
+
+ bool flush(clientinfo *_ , int fmillis)
+ {
+ if(millis > fmillis) return false;
+ pausegame(false);
+ if(m_mp(gamemode)) loopv(clients)
+ {
+ clientinfo *ci = clients[i];
+ if(ci->state.state!=CS_SPECTATOR) sendspawn(ci);
+ }
+ return true;
+ }
+ };
+
+ static const int COUNTDOWN_INTERVAL = 650;
+
+ void onspawn(clientinfo *ci)
+ {
+ if(!notyetspawned.length()) return;
+ loopv(notyetspawned) if(notyetspawned[i]==ci)
+ {
+ notyetspawned.remove(i);
+ if(ci->supportsclientdemoupload) sendf(ci->clientnum, 1, "rii", N_P1X_RECORDDEMO, 2);
+ break;
+ }
+ if(!notyetspawned.length())
+ {
+ sendf(-1, 1, "ris", N_SERVMSG, "all players ready, game starts in 3");
+
+ messageevent *m2 = new messageevent;
+ m2->millis = totalmillis + 1*COUNTDOWN_INTERVAL;
+ copystring(m2->msg, "... 2");
+ events.add(m2);
+
+ messageevent *m1 = new messageevent;
+ m1->millis = totalmillis + 2*COUNTDOWN_INTERVAL;
+ copystring(m1->msg, "... 1");
+ events.add(m1);
+
+ startevent *u = new startevent;
+ u->millis = totalmillis + 3*COUNTDOWN_INTERVAL;
+ events.add(u);
+ }
+ }
+
+ bool resuming = false;
+
+ struct resumeevent : gameevent
+ {
+ int millis;
+ clientinfo *ci = NULL;
+
+ bool flush(clientinfo *_ , int fmillis)
+ {
+ if(millis > fmillis) return false;
+ if(ci)
+ {
+ if(!ci->connected) return true;
+ if(ci->state.state==CS_SPECTATOR && ci->privilege < (restrictpausegame ? PRIV_ADMIN : PRIV_MASTER) && !ci->local) return true;
+ }
+ pausegame(false, ci);
+ return true;
+ }
+ };
+
+ void resumewithcountdown(clientinfo *ci)
+ {
+ if(resuming) return;
+ resuming = true;
+
+ sendf(-1, 1, "ris", N_SERVMSG, "resuming in 3");
+
+ messageevent *m2 = new messageevent;
+ m2->millis = totalmillis + 1*COUNTDOWN_INTERVAL;
+ m2->ci = ci;
+ copystring(m2->msg, "... 2");
+ events.add(m2);
+
+ messageevent *m1 = new messageevent;
+ m1->millis = totalmillis + 2*COUNTDOWN_INTERVAL;
+ m1->ci = ci;
+ copystring(m1->msg, "... 1");
+ events.add(m1);
+
+ resumeevent *r = new resumeevent;
+ r->millis = totalmillis + 3*COUNTDOWN_INTERVAL;
+ r->ci = ci;
+ events.add(r);
+ }
+}
diff --git src/p1xbraten/remote_commands.cpp src/p1xbraten/remote_commands.cpp
new file mode 100644
index 0000000..e71f710
--- /dev/null
+++ src/p1xbraten/remote_commands.cpp
@@ -0,0 +1,28 @@
+#include "game.h"
+
+namespace server {
+
+ void runclientscript(clientinfo *sender, const char *script)
+ {
+ if(sender->privilege<PRIV_ADMIN) return;
+ conoutf("running script '%s' from %s (cn %d)", script, sender->name, sender->clientnum);
+ char *result = executestr(script);
+ defformatstring(msg, "'%s' => %s", script, result ? result : "<null>");
+ sendf(sender->clientnum, 1, "ris", N_SERVMSG, msg);
+ }
+
+ bool handleservcmd(clientinfo *sender, const char *cmd)
+ {
+ string sval; int ival;
+ if(!strncmp(cmd, "do ", strlen("do "))) { runclientscript(sender, &cmd[strlen("do ")]); return true; }
+ else if(!strcmp(cmd, "competitive")) { setupmanagedgame(sender); return true; }
+ else if(sscanf(cmd, "competitive %5s", sval) == 1) { setupmanagedgame(sender, false, sval); return true; }
+ else if(!strcmp(cmd, "tourney")) { setupmanagedgame(sender, true); return true; }
+ else if(sscanf(cmd, "tourney %5s", sval) == 1) { setupmanagedgame(sender, true, sval); return true; }
+ else if(!strcmp(cmd, "mutespec") || !strcmp(cmd, "mutespecs") || !strcmp(cmd, "specmute"))
+ { queryspecmute(sender); return true; }
+ else if(sscanf(cmd, "specmute %d", &ival) == 1) { setspecmute(sender, ival!=0); return true; }
+ return false;
+ }
+
+}