-
Notifications
You must be signed in to change notification settings - Fork 55
/
model.js
1006 lines (898 loc) · 23.7 KB
/
model.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
var crypto = require('crypto');
/**
* Current version supports only two colours of checkers and two players as
* the author is not yet aware of rules with three or more types of checkers
* and/or players.
* @readonly
* @enum {number}
*/
var PieceType = {
/** White piece */
WHITE : 0,
/** Black piece */
BLACK : 1
};
/**
* Common utilities
* @constructor
*/
function Utils() {
}
/**
* Generate unique ID for an object in model
* @returns {number}
*/
Utils.generateID = function () {
// TODO: Use a better approach - eg. a database ObjectID?
return Math.floor(Math.random() * 99999999);
};
/**
* Sanitize rule's name so that it is safe to use as a filename
* @param {string} name - Rule's name to sanitize (eg. 'RuleBgCasual')
* @returns {string}
*/
Utils.sanitizeName = function (name) {
return name.replace(/[^-_A-Za-z0-9]/gi, "");
};
/**
* Load rule object by path and class name
* @param {string} path - Path where rule files are stored ('../../lib/rules/' in browser sample)
* @param {string} ruleName - Name of rule - equal to rule's class name (eg. 'RuleBgCasual')
* @returns {Rule} - Rule object
*/
Utils.loadRule = function (ruleName) {
var path = './rules/';
var fileName = Utils.sanitizeName(ruleName);
var file = path + fileName + '.js';
console.log('Loading rule in file ' + file);
var rule = require(file);
rule.name = fileName;
console.log(rule);
return rule;
};
/**
* Extract item object from array
* @param {Array} array - Array of elements
* @param {Object} item - Element to remove from array
* @returns {} - The element removed from array or null, if not found
*/
Utils.extractItem = function (array, item) {
for (var i = 0; i < array.length; i++) {
if (array[i] === item) {
array.splice(i, 1);
return;
}
}
};
/**
* Remove item from array
* @param {Array} array - Array of elements
* @param {Object} item - Element to remove from array
*/
Utils.removeItem = function (array, item) {
Utils.extractItem(array, item);
};
/**
* Rotate array elements left.
*
* Example:
* [6, 4, 1] becomes [4, 1, 6]
*
* @param {Array} array - Array of elements
*/
Utils.rotateLeft = function (array) {
array.unshift(array.pop());
};
/**
* Create shallow copy of object.
*
* @param {Object} oldObj - Object to copy
* @returns {Object} - Shallow copy of object
*/
Utils.shallowCopy = function (oldObj) {
var newObj = {};
for(var i in oldObj) {
if(oldObj.hasOwnProperty(i)) {
newObj[i] = oldObj[i];
}
}
return newObj;
};
/**
* Create deep copy of a value object.
* The object should have no functions/methods.
*
* @param {Object} oldObj - Object to copy
* @returns {Object} - Deep copy of object
*/
Utils.deepCopy = function (oldObj) {
return JSON.parse(JSON.stringify(oldObj));
};
/**
* Simulate server load/hosting throttling
*
* @param {Object} oldObj - Object to copy
* @returns {Object} - Deep copy of object
*/
Utils.simulateServerLoad = function () {
for (var i = 0; i < 100000; i++) {
for (var k = 1; k < 1000; k++) {
var q = Math.sqrt(1000000007);
}
}
};
/**
* Get random element from an array
*
* @param {Array} arr - Array to choose element from
* @returns {Object} - Random element in array
*/
Utils.getRandomElement = function (arr) {
var idx = Math.floor(Math.random() * arr.length);
return arr[idx];
};
/**
* Check if array include (contains) a specific value
*
* @param {number[]} array - Array with numeric values to check
* @param {number} value - Value to search for in array
* @returns {boolean} - True if value is contained inside the array
*/
Utils.includes = function (array, value) {
var i;
for (i = 0; i < array.length; i++) {
if (array[i] === value) {
return true;
}
}
return false;
};
/**
* Random generator.
* @constructor
*/
function Random() {
}
/**
* Get random number from 1 to 6
* @returns {number} - Random value from 1 to 6
*/
Random.get = function() {
// TODO: replace with quality random generator
// Combine Math random generator with crypto one
var buffer = crypto.randomBytes(1);
var value = buffer.readUInt8(0);
console.log(value);
if (value > 255) {
value = 255;
}
var k = value / 256;
return (Math.floor(k * 6) + 1);
};
/**
* Pieces are round checkers that are being moved around the board.
* @constructor
* @param {PieceType} type - Type of piece
* @param {number} id - ID of piece
*/
function Piece(type, id) {
/**
* Type of piece (white/black)
* @type {PieceType}
*/
this.type = type;
/**
* ID of piece
* @type {number}
*/
this.id = id;
}
/**
* Dice with basic functionality to roll using good random generator.
* @constructor
*/
function Dice() {
/**
* Values of the two dice
* @type {Array}
*/
this.values = [0, 0];
/**
* List of moves the player can make. Usually moves are equal to values,
* but in most rules doubles (eg. 6:6) are played four times, instead of
* two, in which case moves array will contain four values in stead of
* only two (eg. [6, 6, 6, 6]).
* @type {Array}
*/
this.moves = [];
/**
* After dice is rolled, movesLeft contains the same values as moves.
* When the player makes a move, the corresponding value is removed from
* movesLeft array. If the player wants to undo the moves made, movesLeft is
* replaced with moves.
* @type {Array}
*/
this.movesLeft = [];
/**
* After a piece is moved, the value of the die used is added to movesPlayed array.
* @type {Array}
*/
this.movesPlayed = [];
}
/**
* Roll dice and return result as a new Dice object
* @returns {Dice} - New dice with random values
*/
Dice.roll = function() {
var dice = new Dice();
dice.values[0] = Random.get();
dice.values[1] = Random.get();
dice.values.sort(function (a, b) { return b - a; });
return dice;
};
/**
* Roll dice and return result as a new Dice object
* @param {Dice} dice - New dice with random values
* @param {number} move - New dice with random values
*/
Dice.markAsPlayed = function (dice, move) {
for (var i = 0; i < dice.movesLeft.length; i++) {
if (dice.movesLeft[i] === move) {
dice.movesPlayed.push(dice.movesLeft[i]);
dice.movesLeft.splice(i, 1);
return;
}
}
throw new Error("No such move!");
};
/**
* Check if the dice object has double (equal) values.
* @param {Dice} dice - New dice with random values
* @returns {boolean} - True if dice object has dobule values, false otherwise
*/
Dice.isDouble = function (dice) {
return dice.values[0] === dice.values[1];
};
/**
* Get remaining moves from dice object - moves that have not been played.
* @param {Dice} dice - Dice object
* @returns {Array} - Array containing remaining move values
*/
Dice.getRemainingMoves = function (dice) {
var remaining = [];
var played = [];
played = played.concat(dice.movesPlayed);
for (var i = 0; i < dice.moves.length; i++) {
var index = played.indexOf(dice.moves[i]);
if (index >= 0) {
played.splice(index, 1);
}
else {
remaining.push(dice.moves[i]);
}
}
return remaining;
};
/**
* State contains points and pieces and very basic methods to move pieces
* around without enforcing any rules. Those methods are responsible for
* required changes to internal state only, the UI layer should handle
* graphical movement of pieces itself.
* @constructor
*/
function State() {
/**
* All popular variants of the game have a total of 24 positions on the board
* and two positions outside - the place on the bar where pieces go when
* hit and the place next to board where pieces go when beared off.
* Number of positions is not strictly defined here to allow more options
* when creating new rules.
* The points, bar, outside and pieces properties should be initialized by the
* Rule object. Each element in those properties should contain a stack
* (last in, first out).
* @type {Array}
*/
this.points = [];
/**
* Players have separate bar places and so separate list.
* First element of array is for white pieces and second one for black.
* @type {Array[]}
*/
this.bar = [[],[]];
this.whiteBar = this.bar[PieceType.WHITE];
this.blackBar = this.bar[PieceType.BLACK];
/**
* Players have separate outside places and so separate list.
* First element of array is for white pieces and second one for black.
* @type {Array[]}
*/
this.outside = [[],[]];
this.whiteOutside = this.outside[PieceType.WHITE];
this.blackOutside = this.outside[PieceType.BLACK];
/**
* A two dimensional array is also used to store references to all white and
* black pieces independent of their position - just for convenience.
*/
this.pieces = [[],[]];
this.whitePieces = this.pieces[PieceType.WHITE];
this.blackPieces = this.pieces[PieceType.BLACK];
/**
* Counter for generating unique IDs for pieces within this state
* @type {number}
*/
this.nextPieceID = 1;
}
/**
* Clear state
* @param {State} state - Board state
*/
State.clear = function(state) {
state.nextPieceID = 1;
for (var i = 0; i < state.points.length; i++) {
state.points[i].length = 0;
}
state.whiteBar.length = 0;
state.blackBar.length = 0;
state.whiteOutside.length = 0;
state.blackOutside.length = 0;
state.whitePieces.length = 0;
state.blackPieces.length = 0;
};
/**
* Count number of pieces of specified type at selected position
* @param {State} state - Board state
* @param {number} position - Denormalized point position
* @param {PieceType} type - Piece type
* @returns {number} - Number of pieces of specified type
*/
State.countAtPos = function(state, position, type) {
var cnt = 0;
for (var i = 0; i < state.points[position].length; i++) {
if (state.points[position][i].type == type) {
cnt++;
}
}
return cnt;
};
/**
* Count number of all pieces at selected position, regardless of type
* @param {State} state - Board state
* @param {number} position - Denormalized point position
* @returns {number} - Number of pieces of specified type
*/
State.countAllAtPos = function(state, position) {
var cnt = 0;
for (var i = 0; i < state.points[position].length; i++) {
cnt++;
}
return cnt;
};
/**
* Check if there are no pieces at the specified point
* @param {State} state - Board state
* @param {number} position - Denormalized point position
* @returns {boolean} - Returns true if there are no pieces at that point
*/
State.isPosFree = function(state, position) {
return state.points[position].length <= 0;
};
/**
* Get top piece, checking type in the process
* @param {State} state - Board state
* @param {number} position - Denormalized point position
* @param {PieceType} type - Piece type
* @returns {boolean} - Returns true if top piece at position is of the specified type. Returns false if there are no pieces at that point.
*/
State.checkTopPieceType = function(state, position, type) {
if (state.points[position].length > 0) {
var numPieces = state.points[position].length;
var piece = state.points[position][numPieces - 1];
if (piece.type === type) {
return true;
}
}
return false;
};
/**
* Get top piece at specified position
* @param {State} state - Board state
* @param {number} position - Denormalized point position
* @returns {Piece} - Returns type of top piece or null if there are no pieces at that position
*/
State.getTopPiece = function(state, position) {
var point = state.points[position];
//console.log(state, position);
if (point.length === 0) {
return null;
}
return point[point.length - 1];
};
/**
* Get type of top piece at specified position
* @param {State} state - Board state
* @param {number} position - Denormalized point position
* @returns {PieceType} - Returns type of top piece or null if there are no pieces at that position
*/
State.getTopPieceType = function(state, position) {
var point = state.points[position];
//console.log(state, position);
if (point.length === 0) {
return null;
}
return point[point.length - 1].type;
};
/**
* Check if there are any pieces on the bar.
* @param {State} state - State to check
* @param {PieceType} type - Type of piece (white/black)
* @returns {boolean} - True if there are any pieces on the bar
*/
State.havePiecesOnBar = function(state, type) {
return state.bar[type].length > 0;
};
/**
* Check if a specific piece is on the bar.
* @param {State} state - State to check
* @param {Piece} piece - Piece
* @returns {boolean} - True if there are any pieces on the bar
*/
State.isPieceOnBar = function(state, piece) {
var bar = state.bar[piece.type];
for (var i = 0; i < bar.length; i++) {
if (bar[i].id === piece.id) {
return true;
}
}
return false;
};
/**
* Check if the piece is outside.
* @param {State} state - State to check
* @param {Piece} piece - Piece
* @returns {boolean} - True if there are any pieces on the bar
*/
State.isPieceOutside = function(state, piece) {
var outside = state.outside[piece.type];
for (var i = 0; i < outside.length; i++) {
if (outside[i].id === piece.id) {
return true;
}
}
return false;
};
/**
* Get top piece at bar
* @param {State} state - Board state
* @param {PieceType} type - Type of piece (white/black)
* @returns {Piece} - Returns type of top piece or null if there are no pieces at that position
*/
State.getBarTopPiece = function(state, type) {
var point = state.bar[type];
if (point.length === 0) {
return null;
}
return point[point.length - 1];
};
/**
* Get position of piece on board. Return null if the piece is on bar or outside the board.
* @param {State} state - Game
* @param {Piece} piece - Piece
* @returns {number} - Position of piece on board or null if on bar/outside the board
*/
State.getPiecePos = function (state, piece) {
var i, k;
for (i = 0; i < state.points.length; i++) {
for (k = 0; k < state.points[i].length; k++) {
if (state.points[i][k].id === piece.id) {
return i;
}
}
}
return null;
};
/**
* Creates a deep copy of state object
* @param {State} state - Game state
* @returns {State} - New state object
*/
State.clone = function (state) {
var newState = Utils.deepCopy(state);
newState.whiteBar = newState.bar[PieceType.WHITE];
newState.blackBar = newState.bar[PieceType.BLACK];
newState.whiteOutside = newState.outside[PieceType.WHITE];
newState.blackOutside = newState.outside[PieceType.BLACK];
newState.whitePieces = newState.pieces[PieceType.WHITE];
newState.blackPieces = newState.pieces[PieceType.BLACK];
return newState;
};
State.rebuildRefs = function (state) {
state.whiteBar = state.bar[PieceType.WHITE];
state.blackBar = state.bar[PieceType.BLACK];
state.whiteOutside = state.outside[PieceType.WHITE];
state.blackOutside = state.outside[PieceType.BLACK];
state.whitePieces = state.pieces[PieceType.WHITE];
state.blackPieces = state.pieces[PieceType.BLACK];
};
/**
* Player's statistics
* @constructor
*/
function PlayerStats() {
/**
* Total number of wins
* @type {number}
*/
this.wins = 0;
/**
* Total number of loses
* @type {number}
*/
this.loses = 0;
/**
* Percent of doubles rolled relative to total number of dice rolled
* @type {number}
*/
this.doubles = 0;
}
/**
* Player
* @constructor
*/
function Player() {
/**
* Unique ID
* @type {number}
*/
this.id = 0;
/**
* Username
* @type {string}
*/
this.name = '';
/**
* Reference to current match
* @type {Match}
*/
this.currentMatch = null;
/**
* Reference to rule for current game
* @type {Rule}
*/
this.currentRule = null;
/**
* Player's piece type for current game
* @type {PieceType}
*/
this.currentPieceType = null;
/**
* Player's statistics
* @type {PlayerStats}
*/
this.stats = new PlayerStats();
// TODO: Remove socketID from this class
/**
* ID of player's socket
* @type {string}
*/
this.socketID = null;
}
/**
* Create new player object with unique ID.
* Player object is not saved to database.
* @returns {Player} - New player object
*/
Player.createNew = function() {
var player = new Player();
player.id = Utils.generateID();
return player;
};
/**
* Game
* @constructor
*/
function Game() {
/**
* Unique ID of game
* @type {number}
*/
this.id = 0;
/**
* Board state
* @type {State}
*/
this.state = null;
/**
* Flag that shows if game has started
* @type {boolean}
*/
this.hasStarted = false;
/**
* Flag that shows if game is over/has finished
* @type {boolean}
*/
this.isOver = false;
/**
* Number (index) of turn
* @type {number}
*/
this.turnNumber = 0;
/**
* Show which player's turn it is
* @type {Player}
*/
this.turnPlayer = null;
/**
* Dice for current turn. Should be null if dice haven't been rolled yet.
* @type {Dice}
*/
this.turnDice = null;
/**
* Flag that shows if the moves made in current turn have been confirmed by the player.
* @type {boolean}
*/
this.turnConfirmed = false;
/**
* Previous game state, used for undoing moves
*/
this.previousState = null;
/**
* Previous dice state, used for undoing moves
*/
this.previousTurnDice = null;
/**
* Sequence number that is incremented each time a piece is moved during the game
*/
this.moveSequence = 0;
}
/**
* Create new game object with unique ID and initialize it.
* Game object is not saved in database.
* @param {Rule} rule - Rule object to use
* @returns {Game} - A new game object with unique ID
*/
Game.createNew = function(rule) {
var game = new Game();
game.id = Utils.generateID();
Game.init(game, rule);
return game;
};
/**
* Initialize game object
* @param {Game} game - Game to initialize
* @param {Rule} rule - Rule to use
*/
Game.init = function (game, rule) {
game.state = new State();
rule.initialize(game.state);
rule.resetState(game.state);
};
/**
* Check if it is specified player's turn
* @param {Game} game - Game
* @param {Player} player - Specified player
* @returns {boolean} - True if it is specified player's turn
*/
Game.isPlayerTurn = function (game, player) {
return (game.turnPlayer) &&
(player) &&
(game.turnPlayer.id === player.id);
};
/**
* Check if it is specified player's turn, but check by their piece type, and not player object
* @param {Game} game - Game
* @param {PieceType} type - Player's piece type
* @returns {boolean} - True if it is specified player's turn
*/
Game.isTypeTurn = function (game, type) {
return (game.turnPlayer) &&
(game.turnPlayer.currentPieceType === type);
};
/**
* Check if dice has been rolled
* @param {Game} game - Game
* @returns {boolean} - True if dice has been rolled (turnDice is not null)
*/
Game.diceWasRolled = function (game) {
return (game.turnDice != null);
};
/**
* Check if there are more moves to make
* @param {Game} game - Game
* @returns {boolean} - True if there are any moves left to make
*/
Game.hasMoreMoves = function (game) {
return (game.turnDice) &&
(game.turnDice.movesLeft.length > 0);
};
/**
* Check if a specific move value is available in movesLeft
* @param {Game} game - Game
* @param {number} value - Move value to check for
* @returns {boolean} - True if specified move value is available
*/
Game.hasMove = function (game, value) {
return (game.turnDice) &&
(game.turnDice.movesLeft.indexOf(value) > -1);
};
/**
* Store current game state - in case the player wants to undo moves later
* @param {Game} game - Game
*/
Game.snapshotState = function (game) {
game.previousState = State.clone(game.state);
game.previousTurnDice = Utils.deepCopy(game.turnDice);
};
/**
* Restore game state from last snapshot - if player requested to undoing of moves
* @param {Game} game - Game
*/
Game.restoreState = function (game) {
game.state = State.clone(game.previousState);
game.turnDice = Utils.deepCopy(game.previousTurnDice);
};
/**
* Match
* @constructor
*/
function Match() {
/**
* Unique ID of match object
* @type {number}
*/
this.id = 0;
/**
* Player that created the match
* @type {Player}
*/
this.host = null;
/**
* Player that joined the match
* @type {Player}
*/
this.guest = null;
/**
* List of IDs of all players participating in the match
* @type {Array}
*/
this.players = [];
/**
* Name of the rule used for current match.
* Equals the class name of the rule (eg. 'RuleBgCasual').
* @type {string}
*/
this.ruleName = '';
/**
* Match length - the score needed to win the match.
* @type {number}
*/
this.length = 5;
/**
* Score of players for current match
* @type {Array}
*/
this.score = [];
/**
* Current game
* @type {Game}
*/
this.currentGame = null;
/**
* Is match over
* @type {boolean}
*/
this.isOver = false;
}
/**
* Create new match object with unique ID and initialize it.
* Match object is not saved in database.
* @param {Rule} rule - Rule object to use
* @returns {Match} - A new match object with unique ID
*/
Match.createNew = function(rule) {
var match = new Match();
match.id = Utils.generateID();
match.ruleName = rule.name;
match.score = [0, 0];
return match;
};
/**
* Create new game for this match. Fill game object with data from match
* Game object is not saved in database.
* @param {Rule} rule - Rule object to use
* @returns {Match} - A new match object with unique ID
*/
Match.createNewGame = function(match, rule) {
var game = Game.createNew(rule);
match.currentGame = game;
return game;
};
/**
* Add host player to match
* @param {Match} match - Match to add player to
* @param {Player} player - Player to add
* @throws Throws error if the match already has a host player
*/
Match.addHostPlayer = function (match, player) {
if (match.host)
{
throw new Error("Match already has a host player!");
}
match.host = player;
match.players.push(player.id);
};
/**
* Add guest player
* @param {Match} match - Match to add player to
* @param {Player} player - Player to add
* @throws Throws error if the match already has a guest player
*/
Match.addGuestPlayer = function (match, player) {
if (match.guest)
{
throw new Error("Match already has a guest player!");
}
match.guest = player;
match.players.push(player.id);
};
/**
* Check if specified player is the host of the match
* @param {Match} match - Match
* @param {Player} player - Specified player
* @returns {boolean} - True if there is a host player and their ID matches that of the player parameter
*/
Match.isHost = function (match, player) {
return (match.host) &&
(player) &&
(match.host.id === player.id);
};
/**
* Check if another player has joined the match
* @param {Match} match - Match
* @returns {boolean} - True if a another player has joined the match
*/
Match.hasGuestJoined = function (match) {
return (match.guest != null);
};
/**
* Move action types are determined by rules. This is only a default list
* of actions that are shared by most rules.
* @readonly
* @enum {string}
*/
var MoveActionType = {
/** MOVE: Move piece from one point to another */
MOVE: 'move',
/** RECOVER: Recover piece from bar and place it on board */
RECOVER: 'recover',
/** HIT: Hit opponent's piece and sent it to bar */
HIT: 'hit',
/** BEAR: Bear piece - move it outside the board */
BEAR: 'bear'
};
/**
* Actions that can result from making a piece move.
* Rules can assign additional properties to those, depending on the action
* type
* @constructor
*/
function MoveAction() {
/**
* Action type, depends on rule (eg. move, bear, hit)
* @type {MoveActionType|string}
*/
this.type = '';
}
module.exports = {
'PieceType': PieceType,
'Utils': Utils,
'Random': Random,
'Piece': Piece,
'Dice': Dice,
'State': State,
'PlayerStats': PlayerStats,