@@ -231,7 +231,11 @@ Board.prototype.nextTurn = function () {
231
231
self . distributeResources ( tileIds [ i ] ) ;
232
232
}
233
233
} else {
234
- self . moveRobber ( player ) ;
234
+ self . moveRobber ( player )
235
+ . then ( function ( tileId ) {
236
+ // steal resource from player who has a settlement
237
+ // adjacent to tileId
238
+ } ) ;
235
239
}
236
240
self . startTurn ( player ) ;
237
241
socket . emit ( 'start' ) ;
@@ -412,109 +416,115 @@ Board.prototype.yearOfPlentyCard = function (player, resourceType) {
412
416
} ;
413
417
414
418
/**
415
- * placement methods must trigger propagate event.
419
+ * returns true if player is done with initial setup.
420
+ * @param {string } playerId
416
421
*/
417
- Board . prototype . isValidSettlement = function ( playerId , intersectionId ) {
418
- // Check to see if this is a valid settlement location.
419
- var self = this ,
420
- intersections = components . intersections ,
421
- settlements = self . settlements ,
422
- roads = self . roads . byPlayerId ( playerId ) ,
423
- intersection = utils . getTileIdsFromIntersectionId ( intersectionId ) ,
424
- isLegal ;
425
- if ( intersections . indexOf ( intersectionId ) < 0 ) {
426
- return ;
427
- }
428
-
429
- // check to see if location is legal
430
- // - location is not occupied
431
- // - at least 2 roads away from any other settlement.
432
- isLegal = settlements . every ( function ( settlement ) {
422
+ Board . prototype . isSetupComplete = function ( playerId ) {
423
+ // to see if player has completed setup, check to see they have placed
424
+ // at least 2 roads.
425
+ return this . roads . byPlayerId ( playerId ) . length >= 2 ;
426
+ } ;
427
+
428
+ Board . prototype . hasConnectedRoad = function ( playerId , intersectionId ) {
429
+ var roads = this . roads . byPlayerId ( playerId ) ;
430
+ return roads . some ( function ( road ) {
431
+ var edge = road . edge ;
432
+ if ( edge [ 0 ] === intersectionId || edge [ 1 ] === intersectionId ) {
433
+ return true ;
434
+ }
435
+ } ) ;
436
+ } ;
437
+
438
+ Board . prototype . isIntersection = function ( intersectionId ) {
439
+ var intersections = components . intersections ;
440
+ return intersections . indexOf ( intersectionId ) >= 0 ;
441
+ } ;
442
+
443
+ /**
444
+ * returns true if there is not a settlement on the given intersection and
445
+ * there are no settlements < 2 roads away.
446
+ * @param {string } intersectionId
447
+ */
448
+ Board . prototype . isIntersectionOccupied = function ( intersectionId ) {
449
+ var settlements = this . settlements ,
450
+ intersection = utils . getTileIdsFromIntersectionId ( intersectionId ) ;
451
+ return settlements . some ( function ( settlement ) {
433
452
var otherIntersection = utils . getTileIdsFromIntersectionId ( settlement . intersectionId ) ,
434
453
count = 0 ;
435
454
otherIntersection . forEach ( function ( point ) {
436
455
if ( intersection . indexOf ( point ) >= 0 ) {
437
456
count ++ ;
438
457
}
439
458
} ) ;
440
- return count < 2 ;
441
- } ) ;
442
- if ( ! isLegal ) {
443
- return ;
444
- }
445
- if ( settlements . byPlayerId ( playerId ) . length < 2 ) {
446
- // if we're in the setup phase, don't have the requirement that settlement
447
- // must touch road.
448
- return true ;
449
- }
450
- // - player has a road that is connected
451
- isLegal = roads . some ( function ( road ) {
452
- var edge = road . edge ;
453
- if ( edge [ 0 ] === intersectionId || edge [ 1 ] === intersectionId ) {
454
- return true ;
455
- }
459
+ return count >= 2 ;
456
460
} ) ;
457
- if ( ! isLegal ) {
458
- return ;
459
- }
460
- return true ;
461
461
} ;
462
462
463
- Board . prototype . isValidRoad = function ( playerId , edge ) {
464
- var intersections = components . intersections ,
465
- roads = this . roads ,
466
- startId = edge [ 0 ] ,
467
- endId = edge [ 1 ] ,
468
- startIntersection = utils . getTileIdsFromIntersectionId ( startId ) ,
469
- endIntersection = utils . getTileIdsFromIntersectionId ( endId ) ,
470
- isLegal ;
463
+ /**
464
+ * checks to see if player can build a settlement at given intersection
465
+ * @param {string } playerId
466
+ * @param {string } intersectionId
467
+ */
468
+ Board . prototype . isValidSettlement = function ( playerId , intersectionId ) {
469
+ var self = this ;
470
+ return self . isIntersection ( intersectionId ) &&
471
+ ! self . isIntersectionOccupied ( intersectionId ) &&
472
+ ( self . isSetupComplete ( playerId ) ?
473
+ self . hasConnectedRoad ( playerId , intersectionId ) : true ) ;
474
+ } ;
471
475
472
- if ( intersections . indexOf ( startId ) < 0 || intersections . indexOf ( endId ) < 0 ) {
473
- return ;
474
- }
476
+ Board . prototype . isEdge = function ( edge ) {
475
477
// Make sure start and end positions are one edge length apart.
476
- var count = 2 ;
478
+ if ( ! this . isIntersection ( edge [ 0 ] ) || ! this . isIntersection ( edge [ 1 ] ) ) {
479
+ return false ;
480
+ }
481
+ var count = 2 ,
482
+ startIntersection = utils . getTileIdsFromIntersectionId ( edge [ 0 ] ) ,
483
+ endIntersection = utils . getTileIdsFromIntersectionId ( edge [ 1 ] ) ;
477
484
startIntersection . forEach ( function ( tileId ) {
478
485
if ( endIntersection . indexOf ( tileId ) >= 0 ) {
479
486
count -- ;
480
487
}
481
488
} ) ;
482
- if ( count ) { return ; }
489
+ return ! count ;
490
+ } ;
483
491
484
- // - not already an existing road here.
485
- isLegal = roads . every ( function ( road ) {
492
+ Board . prototype . isEdgeOccupied = function ( edge ) {
493
+ var roads = this . roads ,
494
+ startId = edge [ 0 ] ,
495
+ endId = edge [ 1 ] ;
496
+
497
+ return roads . some ( function ( road ) {
486
498
var otherEdge = road . edge ,
487
499
intersectionId = otherEdge [ 0 ] ;
488
500
if ( startId === intersectionId || endId === intersectionId ) {
489
501
intersectionId = otherEdge [ 1 ] ;
490
502
if ( startId === intersectionId || endId === intersectionId ) {
491
- return false ;
503
+ return true ;
492
504
}
493
505
}
494
- return true ;
495
506
} ) ;
496
- if ( ! isLegal ) { return ; }
497
-
498
- // check to see if location is legal
499
- // - connecting to another road / settlement
500
- roads = roads . byPlayerId ( playerId ) ;
501
- if ( roads . length >= 2 ) {
502
- isLegal = roads . some ( function ( road ) {
503
- var otherEdge = road . edge ,
504
- intersectionId = otherEdge [ 0 ] ;
505
- if ( intersectionId === startId || intersectionId === endId ) {
506
- return true ;
507
- }
508
- intersectionId = otherEdge [ 1 ] ;
509
- if ( intersectionId === startId || intersectionId === endId ) {
510
- return true ;
511
- }
512
- } ) ;
513
- if ( ! isLegal ) { return ; }
514
- }
515
- return true ;
516
507
} ;
517
508
509
+ /**
510
+ * check to see if player can build a road on the given edge.
511
+ * @param {string } playerId
512
+ * @param {array } edge
513
+ */
514
+ Board . prototype . isValidRoad = function ( playerId , edge ) {
515
+ var self = this ;
516
+ return self . isEdge ( edge ) &&
517
+ ! self . isEdgeOccupied ( edge ) &&
518
+ ( self . isSetupComplete ( playerId ) ?
519
+ self . hasConnectedRoad ( playerId , edge [ 0 ] ) || self . hasConnectedRoad ( playerId , edge [ 1 ] ) :
520
+ true ) ;
521
+ } ;
522
+
523
+ /**
524
+ * check to see if player can build a city on given intersection
525
+ * @param {string } playerId
526
+ * @param {string } intersectionId
527
+ */
518
528
Board . prototype . isValidCity = function ( playerId , intersectionId ) {
519
529
var settlements = this . settlements . byPlayerId ( playerId ) ;
520
530
return settlements . some ( function ( settlement ) {
@@ -579,7 +589,25 @@ Board.prototype.initResourceValues = function (resourceMap) {
579
589
return diceMap ;
580
590
} ;
581
591
592
+ /**
593
+ * prompt player to choose robber location
594
+ * @param {object } player
595
+ */
582
596
Board . prototype . moveRobber = function ( player ) {
597
+ // return a promise. when we receive client response, resolve promise
598
+ var self = this ,
599
+ deferred = Q . defer ( ) ;
600
+ player . socket . emit ( 'moverobber' ) ;
601
+ player . socket . once ( 'moverobber' , function ( tileId ) {
602
+ tileId = + tileId ;
603
+ if ( tileId >= 0 && tileId <= 18 ) {
604
+ self . robber = tileId ;
605
+ deferred . resolve ( tileId ) ;
606
+ } else {
607
+ deferred . reject ( new Error ( 'Invalid robber location' ) ) ;
608
+ }
609
+ } ) ;
610
+ return deferred . promise ;
583
611
} ;
584
612
585
613
Board . prototype . rollDice = function ( ) {
0 commit comments