-
Notifications
You must be signed in to change notification settings - Fork 3
/
anticrux-server.js
1577 lines (1421 loc) · 50.3 KB
/
anticrux-server.js
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
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
AntiCrux - Suicide chess engine acting as desktop engine, web page, mobile application, Internet chess server and library
Copyright (C) 2016-2018, ecrucru
https://github.com/ecrucru/anticrux/
http://ecrucru.free.fr/?page=anticrux
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as
published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
@license
*/
"use strict";
//======== Libraries
var net = require('net');
var AntiCrux = require('anticrux');
//======== Instance of the server
var server = net.createServer(function(pSocket) {
//-- Rejects extra connections
if (server.acsrv_connections >= server.acsrv_maxConnections)
{
server.acsrv_quit(pSocket, 'Too many connections.');
return;
}
else
{
//pSocket.setTimeout(36000000); //10 minutes
server.acsrv_connections++;
server.acsrv_session++;
pSocket.acsrv_session = server.acsrv_session;
}
//-- Registers the socket
if (!server.hasOwnProperty('acsrv_sockets'))
server.acsrv_sockets = [];
server.acsrv_sockets[pSocket.acsrv_session] = pSocket;
//-- Default options
pSocket.acsrv_options = {
// Changeable options
style : 1, //Format to send the board
block : false, //Use blocks to parse the output of a sent command
defprompt : false, //Display "fics%" when a prompt is needed
seekinfo : false, //Receive the games waiting for a new player
seekremove : false, //Receive notifications for games no more waiting for players
level : 5, //Strength of the AI
mode960 : false, //Use predefined start positions
noticePossibleVictory : true, //Use kibitz to tell that the AI will win the current game
// Internal variables (do not change)
_lastBlock : '', //Identifier of the last received block
_pendingChallenge : false, //Is accept/decline pending ?
// Protected options
_maxLevel : 20, //The level impacts the memory, especially when you have multiple players
_playOnConnect : false, //Proposition of a new game on startup
_enableDebug : false, //Debug: enable some extra commands
_consoleInboundFlow : false, //Debug: show all the received commands
_consoleOutboundFlow : false, //Debug: show all the emitted commands
_shutdownPassword : '' //Password to shutdown the server remotely (disabled if blank)
};
//-- New AI
pSocket.acsrv_ai = new AntiCrux();
pSocket.acsrv_ai.setLevel(pSocket.acsrv_options.level);
pSocket.acsrv_aicolor = pSocket.acsrv_ai.constants.player.none;
//-- Functions
pSocket.acsrv_send_seeks = function() {
if (this.acsrv_options.seekinfo && (this.acsrv_state !== 'playing'))
{
this.acsrv_post_raw('<sc>');
if (!this.acsrv_options._pendingChallenge)
this.acsrv_post_raw('<s> '+this.acsrv_session+' w=AntiCrux ti=02 rt='+this.acsrv_ai.options.ai.elo+'E t=180 i=180 r=u tp=suicide c=? rr=0-9999 a=t f=t');
return true;
}
else
return false;
};
pSocket.acsrv_remove_seeks = function() {
if (this.acsrv_options.seekremove)
{
this.acsrv_post_raw('<sr> '+this.acsrv_session);
return true;
}
else
return false;
};
pSocket.acsrv_post = function(pContent, pDefPrompt) {
var buffer;
// Data to the client
buffer = pContent;
if (buffer.length > 0)
buffer += "\r\n";
if (this.acsrv_options.block && (this.acsrv_options._lastBlock.length > 0))
buffer = String.fromCharCode(15) + this.acsrv_options._lastBlock + String.fromCharCode(16) + buffer + String.fromCharCode(17) + "\r\n";
this.acsrv_options._lastBlock = '';
if (pDefPrompt && this.acsrv_options.defprompt)
buffer += 'fics% ';
this.write(buffer);
// Data to the console
if (pSocket.acsrv_options._consoleOutboundFlow)
{
buffer = pContent.split("\r").join('').split("\n").join("\\n");
if (buffer.length > 0)
server.acsrv_console(pSocket, '> '+buffer);
}
};
pSocket.acsrv_post_raw = function(pContent) {
var tmp = this.acsrv_options._lastBlock;
this.acsrv_options._lastBlock = '';
this.acsrv_post(pContent, false);
this.acsrv_options._lastBlock = tmp;
};
pSocket.acsrv_set_state = function(pState) {
this.acsrv_state = pState;
if (this.acsrv_options._consoleInboundFlow || this.acsrv_options._consoleOutboundFlow)
server.acsrv_console(pSocket, 'State changed to "'+this.acsrv_state+'"');
};
//-- Welcome screen
pSocket.acsrv_post(
"\r\n" +
" ______________________________________________\r\n" +
" < AntiCrux " + pSocket.acsrv_ai.options.ai.version + " will win unless you are good... >\r\n" +
" ----------------------------------------------\r\n" +
" \\ ^__^\r\n" +
" \\ (oo)\\_______\r\n" +
" (__)\\ )\\/\\\r\n" +
" U ||----w |\r\n" +
" || ||\r\n" +
"\r\n" +
"\r\n" +
" Get it for free at https://github.com/ecrucru/anticrux/\r\n" +
"\r\n" +
" To be compatible with any existing chess program, the server\r\n" +
" mimics FICS, but it is not a FICS server. Don't be confused !\r\n" +
"\r\n" +
" Activate AntiChess960 with the command : set mode960 on\r\n" +
" Change the level between 1 and 20 with : set level 10\r\n" +
"\r\n" +
"\r\n" +
"========================================================================\r\n" +
"\r\n", false);
pSocket.write('login: ');
pSocket.acsrv_set_state('login');
//-- Events for the socket
pSocket.on('data', function(data) {
if (!pSocket.hasOwnProperty('acsrv_datastream'))
pSocket.acsrv_datastream = '';
pSocket.acsrv_datastream += data;
server.acsrv_process(pSocket);
})
.on('error', function(/*err*/) {
//throw err;
})
.on('close', function(/*had_error*/) {
server.acsrv_connections--;
});
})
//-- Events for the server
.on('error', function(err) {
throw err;
})
.on('listening', function() {
// Definitions
server.acsrv_upDate = (new Date().toUTCString());
server.acsrv_maxConnections = 6;
server.acsrv_connections = 0;
server.acsrv_session = 0;
server.acsrv_stats = { win:0, loss:0, draw:0 };
// Verboses
console.log('');
console.log('AntiCrux Server '+(new AntiCrux().options.ai.version)+' is now listening to new connections on port '+server.address().port+'...');
console.log('https://github.com/ecrucru/anticrux/');
console.log('License: GNU Affero General Public License version 3');
console.log('');
console.log('Press Ctrl+C to kill the server.');
console.log('');
})
;
//======== Routines
server.acsrv_fixedString = function(pMessage, pLength, pBefore) {
var result = pMessage;
if (pBefore === undefined)
pBefore = false;
if (typeof result !== 'string')
result = result.toString();
while (result.length < pLength)
{
if (pBefore)
result = ' ' + result;
else
result += ' ';
}
return result;//.substring(0, pLength);
};
server.acsrv_uptime = function(pSeconds) {
var d, h, m, s;
//-- Converts
s = Math.floor(pSeconds);
d = Math.floor(s / 86400);
h = Math.floor((s - 86400*d) / 3600);
m = Math.floor((s - 86400*d - 3600*h) / 60);
s = s - 86400*d - 3600*h - 60*m;
//-- Formats
return d+' days, '+h+' hrs, '+m+' mins, '+s+' secs';
};
//======== Processing
server.acsrv_count_sockets = function() {
var session, counter;
//-- Identifies the valid connections
counter = 0;
for (session in server.acsrv_sockets)
{
if (server.acsrv_sockets[session].destroyed)
{
delete server.acsrv_sockets[session];
continue;
}
counter++;
}
return counter;
};
server.acsrv_process = function(pSocket) {
var pos, line, match, tab, i, b, news, move, node, hist, buffer, tmp, white, black;
while (true)
{
//-- Gets the next line of data
pos = pSocket.acsrv_datastream.indexOf("\n");
if (pos === -1)
break;
line = pSocket.acsrv_datastream.substring(0, pos);
pSocket.acsrv_datastream = pSocket.acsrv_datastream.substring(pos+1);
line = line.split("\r").join('');
line = line.split('@').join('');
line = line.split('$').join('').trim();
match = line.match(/^([0-9])+\s(.*)$/);
if (match !== null)
{
pSocket.acsrv_options._lastBlock = match[1];
line = match[2].trim();
}
else
pSocket.acsrv_options._lastBlock = '';
if (pSocket.acsrv_options._consoleInboundFlow && (line.length !== 0))
server.acsrv_console(pSocket, '< '+line);
tab = line.toLowerCase().split(' ');
for (i=0 ; (i<10) || (i<tab.length) ; i++)
if (typeof tab[i] === 'undefined')
tab[i] = '';
//-- Global commands
if (['login', 'confirm_login'].indexOf(pSocket.acsrv_state) == -1)
{
//- Quits the application
if ( ((tab[0] == 'quit') || (tab[0] == 'bye') || (tab[0] == 'exit')) &&
(pSocket.acsrv_state != 'playing')
) {
server.acsrv_quit(pSocket, 'Bye!');
return;
}
//- Shutdown (unofficial)
if (tab[0] == 'shutdown')
{
line = line.substring(9, line.length);
if (pSocket.acsrv_options._shutdownPassword.length === 0)
pSocket.acsrv_post('This command is disabled.', true);
else
if (line == pSocket.acsrv_options._shutdownPassword)
server.acsrv_shutdown();
else
{
server.acsrv_console(pSocket, 'Failed attempt to shutdown the server');
pSocket.acsrv_post('Incorrect password to shut down the server.', true);
}
continue;
}
//- Best players
if (tab[0] == 'best')
{
pSocket.acsrv_post(
" Suicide\r\n" +
" 1. AntiCrux " + pSocket.acsrv_ai.options.ai.elo + "\r\n" +
" 2. " + server.acsrv_fixedString(pSocket.acsrv_login, 12) + " ++++",
true);
continue;
}
if (tab[0] == 'cbest')
{
pSocket.acsrv_post(" Suicide\r\n 1. AntiCrux " + pSocket.acsrv_ai.options.ai.elo, true);
continue;
}
if (tab[0] == 'hbest')
{
pSocket.acsrv_post(" Suicide\r\n 1. " + server.acsrv_fixedString(pSocket.acsrv_login, 12) + " ++++", true);
continue;
}
//- Date
if (tab[0] == 'date')
{
pSocket.acsrv_post('Server time - '+(new Date().toUTCString()), true);
continue;
}
//- Debug
if ((tab[0] == 'debug') && pSocket.acsrv_options._enableDebug)
{
pSocket.acsrv_post(line.substring(6, line.length), true);
continue;
}
//- Uptime
if (tab[0] == 'uptime')
{
pSocket.acsrv_post(
"AntiCrux Server " + pSocket.acsrv_ai.options.ai.version + "\r\n" +
"The server has been up since " + server.acsrv_upDate + ".\r\n" +
"(Up for " + server.acsrv_uptime(process.uptime()) + ")\r\n" +
"\r\n" +
"There are currently "+server.acsrv_count_sockets()+" players.\r\n" +
"\r\n" +
"Player limit: "+server.acsrv_maxConnections+" users (+ 0 admins)\r\n" +
"Unregistered user restriction at "+server.acsrv_maxConnections+" users.",
true
);
continue;
}
//- News
if (tab[0] == 'news')
{
buffer = '';
news = [ '1 (Sat, Feb 4, 2017) AntiCrux Server is under development',
'2 (Sun, Feb 5, 2017) AntiCrux Server will be available on GitHub',
'3 (Sun, Feb 12, 2017) The first release of AntiCrux Server is ready',
'4 (Thu, Feb 16, 2017) AntiCrux Server now plays AntiChess960',
'5 (Sat, Feb 17, 2018) AntiCrux Server is improved',
];
for (i=0 ; i<news.length ; i++)
{
if (buffer.length > 0)
buffer += "\r\n";
buffer += news[i];
}
pSocket.acsrv_post(buffer, true);
continue;
}
//- Message to the server
if (tab[0] == 'say')
{
server.acsrv_console(pSocket, line.substring(4));
pSocket.acsrv_post('', true);
continue;
}
//- Message to all the users on the server
if (tab[0] == 'shout')
{
server.acsrv_shout(pSocket, line.substring(6));
continue;
}
//- Group of users
if (tab[0] == 'showlist')
{
if (tab[1].length === 0)
pSocket.acsrv_post("Lists:\r\n\r\ncomputer is PUBLIC", true);
else
if (tab[1] == 'computer')
pSocket.acsrv_post("-- computer list: 1 name --\r\nAntiCrux", true);
else
pSocket.acsrv_post('"'+tab[1]+'" does not match any list name.', true);
continue;
}
//- Limits
if (tab[0] == 'limits')
{
pSocket.acsrv_post(
"Current hardcoded limits (maximums unless specified otherwise):\r\n" +
" Server:\r\n" +
" Channels: 0\r\n" +
" Players: "+server.acsrv_maxConnections+"\r\n" +
" Connections: "+server.acsrv_maxConnections+" users (+ 0 admins)\r\n" +
"\r\n" +
" Lists:\r\n" +
" Censor: 0\r\n" +
" Channels: 0\r\n" +
" Gnotify: 0\r\n" +
" Noplay: 0\r\n" +
" Notify: 0\r\n" +
"\r\n" +
" Games:\r\n" +
" Adjourned games: 0\r\n" +
" History games: 0\r\n" +
" Journal entries: 0\r\n" +
" Observed games: 0\r\n" +
" RD to be active: 0\r\n" +
" Seeks pending: 1\r\n" +
" Simul participants: 0\r\n" +
"\r\n" +
" Misc:\r\n" +
" Aliases: 0\r\n" +
" Messages: 0\r\n" +
" Messages to a player in 24 hours: 0\r\n" +
" Maximum communication size: 0 character.",
true
);
continue;
}
//- Active games
if (tab[0] == 'games')
{
server.acsrv_games(pSocket);
continue;
}
//- List of players
if (tab[0] == 'handles')
{
server.acsrv_handles(pSocket, tab[1]);
pSocket.acsrv_post('', true);
continue;
}
//- List of players for starting a game : AntiCrux only :-)
if (tab[0] == 'who')
{
pSocket.acsrv_post(pSocket.acsrv_ai.options.ai.elo + ' AntiCrux(C)', true);
continue;
}
//- Options and silenced commands
if (tab[0] == 'alias')
{
pSocket.acsrv_post('', true);
continue;
}
if (tab[0] == 'style')
{
tab = ['set', 'style', '12'];
//No continue
}
if ((tab[0] == 'set') || (tab[0] == 'iset'))
{
if (tab[1] == 'interface')
server.acsrv_console(pSocket, 'Declared software: ' + line.substring(14));
else if ((tab[1] == 'style') && (tab[2].length > 0))
{
pSocket.acsrv_options.style = parseInt(tab[2]);
pSocket.acsrv_post('Style '+pSocket.acsrv_options.style+' set.', true);
}
else if (tab[1] == 'level')
{
if (!tab[2].match(/^[0-9]+$/))
pSocket.acsrv_post('Unknown level '+tab[2]+'.', true);
else
{
i = Math.max(1, Math.min(parseInt(tab[2]), pSocket.acsrv_options._maxLevel, 20));
if (pSocket.acsrv_ai.setLevel(i))
{
pSocket.acsrv_options.level = i;
pSocket.acsrv_post('Level '+i+' set.', true);
pSocket.acsrv_send_seeks();
}
else
throw 'Internal error';
}
}
else
{
if (!tab[1].match(/^[a-zA-Z0-9]+$/) || !pSocket.acsrv_options.hasOwnProperty(tab[1]))
pSocket.acsrv_post('No such variable "'+tab[1]+'".', true);
else
{
switch (typeof pSocket.acsrv_options[tab[1]])
{
case 'string':
pSocket.acsrv_options[tab[1]] = tab[2];
break;
case 'boolean':
pSocket.acsrv_options[tab[1]] = (['true', 'on', '1'].indexOf(tab[2].toLowerCase()) !== -1);
break;
case 'number':
pSocket.acsrv_options[tab[1]] = parseInt(tab[2]);
break;
default:
throw 'Internal error';
}
pSocket.acsrv_post('Variable "'+tab[1]+'" set.', true);
if (tab[1].indexOf('seek') !== -1)
pSocket.acsrv_send_seeks();
}
}
continue;
}
//- Variables
if (['var', 'variable', 'variables'].indexOf(tab[0]) !== -1)
{
buffer = "Variable settings of "+pSocket.acsrv_login+":\r\n";
for (tmp in pSocket.acsrv_options)
if (tmp.substring(0, 1) != '_') //Private values
buffer += "\r\n" + tmp + '=' + pSocket.acsrv_options[tmp];
pSocket.acsrv_post(buffer, true);
continue;
}
//- Refresh
if (tab[0] == 'refresh')
{
if (pSocket.acsrv_state != 'playing')
{
pSocket.acsrv_post('You are neither playing, observing nor examining a game.', true);
continue;
}
}
//- Finger
if (tab[0] == 'finger')
{
if (tab[1].length === 0)
pSocket.acsrv_post('You are '+pSocket.acsrv_login+'.', true);
else if (tab[1] == pSocket.acsrv_login.toLowerCase())
pSocket.acsrv_post(
"Finger of "+pSocket.acsrv_login+":\r\n" +
"\r\n" +
"Session: "+pSocket.acsrv_session+"\r\n" +
"\r\n" +
" 1: Your account cannot be registered.\r\n" +
" 2: You only have one opponent, aka AntiCrux "+pSocket.acsrv_ai.options.ai.version+" rated at "+pSocket.acsrv_ai.options.ai.elo+".\r\n" +
" 3: Do you enjoy classical chess ? It is the wrong place to be here, unless you also like suicide chess.\r\n",
true
);
else if (tab[1] == 'anticrux')
pSocket.acsrv_post(
"Finger of AntiCrux(C):\r\n" +
"\r\n" +
"On for: "+server.acsrv_uptime(process.uptime())+"\r\n" +
"\r\n" +
" rating RD win loss draw total best\r\n" +
"Suicide "+pSocket.acsrv_ai.options.ai.elo+" ? "+server.acsrv_fixedString(server.acsrv_stats.win, 4)+" "+server.acsrv_fixedString(server.acsrv_stats.loss, 4)+" "+server.acsrv_fixedString(server.acsrv_stats.draw, 4)+" "+server.acsrv_fixedString(server.acsrv_stats.win+server.acsrv_stats.loss+server.acsrv_stats.draw, 4)+" "+pSocket.acsrv_ai.options.ai.elo+" (today)\r\n" +
"\r\n" +
"Timeseal: Off\r\n" +
"\r\n" +
" 1: This computer plays AntiChess only, a.k.a. suicide chess.\r\n" +
" 2: The engine born in 2016 is AntiCrux "+pSocket.acsrv_ai.options.ai.version+" available on GitHub.\r\n" +
" 3: I use neither database, nor time control. Using the opening book depends on the selected strength.\r\n" +
" 4: I use classical techniques to play, unless you change my level.\r\n" +
" 5: I play unrated games, so I will accept most of your commands.\r\n" +
" 6: I will certainly never play other variants than AntiChess.",
true
);
else
pSocket.acsrv_post('There is no player matching the name '+tab[1]+'.', true);
continue;
}
//- Help based on the online documentation (http://www.freechess.org/Help/HelpFiles/[COMMAND].html)
if ((tab[0] == 'info') || (tab[0] == 'help'))
{
pSocket.acsrv_post(
"\r\n" +
"Supported commands:\r\n" +
" abort Cancel the current game\r\n" +
" accept Accept a match proposed by AntiCrux\r\n" +
" best/cbest/hbest Show the best rated players\r\n" +
" date Show the date\r\n" +
" debug [command] Ask the server to send a given command (debug)\r\n" +
" decline Refuse a match proposed by AntiCrux\r\n" +
" draw Offer a draw\r\n" +
" finger [player] Show some information about a player\r\n" +
" flip Rotate the board\r\n" +
" games List the active games\r\n" +
" getgame Invoke 'match AntiCrux suicide'\r\n" +
" handles [mask] Show the list of players\r\n" +
" help/info Show the list of available commands\r\n" +
" history View the history games (unmanaged)\r\n" +
" journal View the journal (unmanaged)\r\n" +
" limits Display the limits of the server\r\n" +
" match AntiCrux suicide Start an unrated game against AntiCrux\r\n" +
" moves Show the history of the moves\r\n" +
" news Show the news of the server\r\n" +
" nickname [name] Change your name\r\n" +
" play [id] Play the game which ID is given by 'sought' or seek\r\n" +
" promote Predefine the piece for the promotions\r\n" +
" quit Leave the program\r\n" +
" refresh Display the board\r\n" +
" rematch Ask for a new match\r\n" +
" resign Declare one's own defeat\r\n" +
" say [message] Send a message to AntiCrux\r\n" +
" seek unrated suicide Find a partner\r\n" +
" showlist Display the list of users\r\n" +
" shout [message] Send a message to all the players\r\n" +
" shutdown [password] Remote shutdown of the server\r\n" +
" sought List the available games\r\n" +
" stored View the adjourned games (unmanaged)\r\n" +
" switch Exchange the players\r\n" +
" takeback Undo your last move\r\n" +
" uptime Give some details about the server\r\n" +
" variable Show the active options\r\n" +
" who Show the available players\r\n" +
"\r\n" +
"Unsupported commands:\r\n" +
" addlist, adjourn, alias, allobservers, assess, backward, bell,\r\n" +
" boards, bsetup, bugwho, clearmessages, convert_bcf, convert_elo,\r\n" +
" convert_uscf, copygame, crank, cshout, examine, flag, fmessage,\r\n" +
" follow, forward, gnotify, goboard, hrank, inchannel, it, jkill,\r\n" +
" jsave, kibitz, llogons, logons, mailhelp, mailmess, mailmoves,\r\n" +
" mailoldmoves, mailsource, mailstored, messages, mexamine, moretime,\r\n" +
" next, observe, oldmoves, password, pause, pending, pfollow, play,\r\n" +
" pobserve, pstat, qtell, rank, resume, revert, set, simabort,\r\n" +
" simadjourn, simallabort, simalladjourn, simgames, simmatch, simnext,\r\n" +
" simobserve, simopen, simpass, simprev, smoves, smposition, sposition,\r\n" +
" statistics, style, sublist, tell, time, unalias, unexamine, unobserve,\r\n" +
" unpause, unseek, swhisper, withdraw, xkibitz, xtell, xwhisper, znotify\r\n",
true
);
continue;
}
}
//-- Logs under the provided name
if (pSocket.acsrv_state == 'login')
{
if (line.match(/^[a-zA-Z0-9]+$/) && !line.match(/anticrux/i))
pSocket.acsrv_login = line;
else
{
server.acsrv_quit(pSocket, 'Invalid login name'); //Timeseal is not supported
return;
}
pSocket.acsrv_post('Press return to enter the server as "' + pSocket.acsrv_login + '":', false);
pSocket.acsrv_set_state('confirm_login');
continue;
}
//-- Confirms the login
if ((pSocket.acsrv_state == 'confirm_login') && (line.length === 0))
{
//- Accepts the player
pSocket.acsrv_post("**** Starting FICS session as " + pSocket.acsrv_login + "(U) ****\r\nfics% ", false); //Important syntax to recognize the type of server
server.acsrv_console(pSocket, 'New connected player');
//- Transmits the notification
pSocket.acsrv_set_state('home');
if (pSocket.acsrv_options._playOnConnect)
{
pSocket.acsrv_aicolor = pSocket.acsrv_ai.constants.player.none; //The color is always random, but the player can force it by proposing another match
pSocket.acsrv_options._pendingChallenge = true;
buffer = "Challenge: AntiCrux ("+pSocket.acsrv_ai.options.ai.elo+") "+pSocket.acsrv_login+" (++++) unrated suicide 180 180.\r\n" +
'You can "accept" or "decline", or propose different parameters.';
pSocket.acsrv_post(buffer, true);
}
continue;
}
//-- Command during the game
if (pSocket.acsrv_state == 'playing')
{
switch (tab[0])
{
//- Leaves the game
case 'bye':
case 'exit':
case 'quit':
case 'adjourn':
pSocket.acsrv_post('You must first abort the game.', true);
break;
//- Aborts the current game (automatically accepted)
case 'abort':
if (pSocket.acsrv_aicolor == pSocket.acsrv_ai.constants.player.white)
pSocket.acsrv_post('{Game '+pSocket.acsrv_session+' (AntiCrux vs. '+pSocket.acsrv_login+') Game aborted by mutual agreement} *', true);
else
pSocket.acsrv_post('{Game '+pSocket.acsrv_session+' ('+pSocket.acsrv_login+' vs. AntiCrux) Game aborted by mutual agreement} *', true);
pSocket.acsrv_set_state('home');
pSocket.acsrv_send_seeks();
break;
//- Resigns the current game (automatically accepted)
case 'resign':
server.acsrv_stats.win++;
if (pSocket.acsrv_aicolor == pSocket.acsrv_ai.constants.player.white)
pSocket.acsrv_post('{Game '+pSocket.acsrv_session+' (AntiCrux vs. '+pSocket.acsrv_login+') '+pSocket.acsrv_login+' resigns} 1-0', true);
else
pSocket.acsrv_post('{Game '+pSocket.acsrv_session+' ('+pSocket.acsrv_login+' vs. AntiCrux) '+pSocket.acsrv_login+' resigns} 0-1', true);
pSocket.acsrv_set_state('home');
pSocket.acsrv_send_seeks();
break;
//- Accepts a new game
case 'play':
pSocket.acsrv_post('You cannot accept seeks while you are playing a game.', true);
break;
//- Rotates the board
case 'flip':
pSocket.acsrv_ai.options.board.rotated = !pSocket.acsrv_ai.options.board.rotated;
server.acsrv_board(pSocket);
break;
//- Switches the board
case 'switch':
pSocket.acsrv_post('AntiCrux accepts the switch request.', false);
pSocket.acsrv_aicolor = (pSocket.acsrv_aicolor == pSocket.acsrv_ai.constants.player.black ? pSocket.acsrv_ai.constants.player.white : pSocket.acsrv_ai.constants.player.black);
server.acsrv_play_ai(pSocket);
server.acsrv_board(pSocket);
server.acsrv_end_game(pSocket);
break;
//- Receives a draw offer
case 'draw':
if (!pSocket.acsrv_ai.isDraw() && !pSocket.acsrv_ai.isPossibleDraw())
pSocket.acsrv_post('AntiCrux declines the draw request.', true);
else
{
server.acsrv_stats.draw++;
buffer = "AntiCrux accepts the draw request.\r\n";
if (pSocket.acsrv_aicolor == pSocket.acsrv_ai.constants.player.white)
buffer += "{Game "+pSocket.acsrv_session+" (AntiCrux vs. "+pSocket.acsrv_login+") Game drawn by mutual agreement} 1/2-1/2\r\n";
else
buffer += "{Game "+pSocket.acsrv_session+" ("+pSocket.acsrv_login+" vs. AntiCrux) Game drawn by mutual agreement} 1/2-1/2\r\n";
buffer += 'No ratings adjustment done.';
pSocket.acsrv_post(buffer, true);
pSocket.acsrv_set_state('home');
pSocket.acsrv_send_seeks();
}
break;
//- Reverts the last move
case 'takeback':
if (pSocket.acsrv_ai.getHistory().length >= 2)
{
pSocket.acsrv_post('AntiCrux accepts the takeback request.', true);
pSocket.acsrv_ai.undoMove();
pSocket.acsrv_ai.undoMove();
}
else
pSocket.acsrv_post('AntiCrux declines the takeback request.', true);
server.acsrv_board(pSocket);
break;
//- Defines the piece to promote
case 'promote':
buffer = '';
switch (tab[1])
{
case 'q':
pSocket.acsrv_promote = pSocket.acsrv_ai.constants.piece.queen;
buffer = 'QUEEN';
break;
case 'r':
pSocket.acsrv_promote = pSocket.acsrv_ai.constants.piece.rook;
buffer = 'ROOK';
break;
case 'b':
pSocket.acsrv_promote = pSocket.acsrv_ai.constants.piece.bishop;
buffer = 'BISHOP';
break;
case 'k':
case 'n':
pSocket.acsrv_promote = pSocket.acsrv_ai.constants.piece.knight;
buffer = 'KNIGHT';
break;
case 'ki':
pSocket.acsrv_promote = pSocket.acsrv_ai.constants.piece.king;
buffer = 'KING';
break;
default:
tab[1] = '';
break;
}
buffer = (buffer.length > 0 ? 'Promotion piece set to '+buffer+'.' : '');
if (tab[1].length === 0)
buffer += "Purpose: set the piece a pawn will be promoted to at the back rank\r\n" +
"Usage: promote {q,r,b,[kn],ki}\r\n" +
"Examples: promote q; promote b";
pSocket.acsrv_post(buffer, true);
break;
//- Refreshes the board
case 'refresh':
server.acsrv_board(pSocket);
break;
//- Moves
case 'moves':
// Checks
if ((tab[1].length > 0) && (parseInt(tab[1]) != pSocket.acsrv_session))
{
pSocket.acsrv_post('There is no such game.', true);
continue;
}
// Init
white = (pSocket.acsrv_aicolor==pSocket.acsrv_ai.constants.player.white ? 'AntiCrux' : pSocket.acsrv_login);
black = (pSocket.acsrv_aicolor==pSocket.acsrv_ai.constants.player.black ? 'AntiCrux' : pSocket.acsrv_login);
// Header
buffer = "Movelist for game "+pSocket.acsrv_session+":\r\n" +
"\r\n" +
white+" ("+(pSocket.acsrv_aicolor==pSocket.acsrv_ai.constants.player.white ? pSocket.acsrv_ai.options.ai.elo : '++++')+") vs. "+black+" ("+(pSocket.acsrv_aicolor==pSocket.acsrv_ai.constants.player.black ? pSocket.acsrv_ai.options.ai.elo : '++++')+") --- "+(new Date().toUTCString())+"\r\n" +
"Unrated suicide match, initial time: 180 minutes, increment: 180 seconds.\r\n" +
"\r\n" +
"Move " + server.acsrv_fixedString(white, 18) + " " + server.acsrv_fixedString(black, 18) + "\r\n" +
"---- ---------------- ----------------\r\n";
// History
pSocket._trace = pSocket._trace || new AntiCrux();
pSocket._trace.copyOptions(pSocket.acsrv_ai);
if (pSocket._trace.loadFen(pSocket.acsrv_ai.getInitialPosition()))
{
hist = pSocket.acsrv_ai.getHistory();
for (i=0 ; i<hist.length ; i++)
{
if (i % 2 === 0)
buffer += server.acsrv_fixedString(Math.ceil((i+1)/2), 3, true) + '. ';
buffer += server.acsrv_fixedString(pSocket._trace.moveToString(hist[i]), 8) + '(0:00) ';
if (pSocket._trace.movePiece(hist[i], true, pSocket._trace.constants.player.none) == pSocket._trace.constants.noMove)
{
buffer = '';
break;
}
if (i % 2 === 1)
buffer += "\r\n";
}
if (i % 2 === 1)
buffer += "\r\n";
}
// Status
if (!pSocket.acsrv_ai.isEndGame())
buffer += " {Still in progress} *\r\n";
pSocket.acsrv_post(buffer, true);
break;
//- Move a piece
default:
line = line.split('-').join('');
if (!pSocket.acsrv_ai.isMove(line))
{
if (tab[0].length > 0)
pSocket.acsrv_post('Unknown command: '+tab[0], true);
}
else
{
if (pSocket.acsrv_ai.getPlayer() == pSocket.acsrv_aicolor)
throw 'Internal error';
node = pSocket.acsrv_ai._sy_copy(pSocket.acsrv_ai.getMainNode(), false);
move = pSocket.acsrv_ai.movePiece(line, true, pSocket.acsrv_ai.getPlayer());
if (move == pSocket.acsrv_ai.constants.noMove)
{
pSocket.acsrv_post('Invalid move', true);
server.acsrv_board(pSocket);
}
else
{
// Trace the last move
if (pSocket.acsrv_ai.hasPendingPromotion())
move += 10000 * pSocket.acsrv_ai.promote(pSocket.acsrv_promote);
pSocket.acsrv_lastMove = pSocket.acsrv_ai.moveToString(move, node);
pSocket.acsrv_ai.updateHalfMoveClock();
pSocket.acsrv_ai.logMove(move, null);
// Next player
pSocket.acsrv_ai.switchPlayer();
server.acsrv_board(pSocket);
if (!server.acsrv_end_game(pSocket))
{
server.acsrv_play_ai(pSocket);
server.acsrv_board(pSocket);
server.acsrv_end_game(pSocket);
}
}
}
break;
}
continue;
}
//-- Commands when not playing
if (pSocket.acsrv_state == 'home')
{
//- Views the available games
if (tab[0] == 'sought')
{
pSocket.acsrv_post(
server.acsrv_fixedString(pSocket.acsrv_session, 3)+" "+pSocket.acsrv_ai.options.ai.elo+" AntiCrux(C) 180 180 unrated suicide 0-9999 m\r\n" +
'1 ads displayed.', true);
continue;
}
//- Rejects the proposal of a match
if (tab[0] == 'decline')
{
if (pSocket.acsrv_options._pendingChallenge)
{
pSocket.acsrv_options._pendingChallenge = false;
pSocket.acsrv_send_seeks();
}
else
pSocket.acsrv_post('There is no pending challenge that you may decline.', true);
continue;
}
//- Accepts the proposed game
if (tab[0] == 'accept')
{
if (pSocket.acsrv_options._pendingChallenge)
server.acsrv_start_game(pSocket);
else
pSocket.acsrv_post('There is no pending challenge that you may accept.', true);
continue;
}
//- Plays a game
if (tab[0] == 'play')
{
if (parseInt(tab[1]) != pSocket.acsrv_session)
pSocket.acsrv_post('That seek is not available.', true);
else
{
pSocket.acsrv_aicolor = pSocket.acsrv_ai.constants.player.none;
server.acsrv_start_game(pSocket);
}
continue;
}
//- Ask for a game
if (tab[0] == 'getgame')
{
tab = ['match', 'anticrux', 'suicide'];
//No continue
}
//- Proposes a new match
if (tab[0] == 'rematch')
{
// Checks if a match occurred already
if (pSocket.acsrv_aicolor == pSocket.acsrv_ai.constants.player.none)
{
tab = ['match', 'anticrux', 'suicide'];
//No continue
}
else
// Starts a new match from the opposite side
{
pSocket.acsrv_post("AntiCrux accepts the match offer.\r\n", false);
pSocket.acsrv_aicolor = (pSocket.acsrv_aicolor == pSocket.acsrv_ai.constants.player.white ? pSocket.acsrv_ai.constants.player.black : pSocket.acsrv_ai.constants.player.white);
server.acsrv_start_game(pSocket);
continue;
}
}
//- Gets a match request
if ((tab[0] == 'match') || (tab[0] == 'seek'))
{
// Checks that only the server is targetted