Browse Source

Expand the number of players to a configurable amount.

pull/21/head
Jeffrey Oliver 5 years ago
parent
commit
32f71a240b
7 changed files with 59 additions and 42 deletions
  1. +12
    -0
      config.js
  2. +36
    -32
      game.js
  3. +2
    -2
      public/js/app/controllers.js
  4. +1
    -1
      public/js/app/filters.js
  5. +2
    -2
      public/js/app/services.js
  6. +4
    -3
      server.js
  7. +2
    -2
      views/lobby.html

+ 12
- 0
config.js View File

@ -0,0 +1,12 @@
var config = {};
config.port = 3000;
config.minPlayers = 3;
config.maxPlayers = 10;
config.whiteCardsPerHand = 7;
config.pointsToWin = 5;
module.exports = config;

+ 36
- 32
game.js View File

@ -1,5 +1,6 @@
var _ = require('underscore');
var cards = require('./cards.js');
var config = require('./config.js');
var gameList = [];
@ -16,7 +17,8 @@ function removeFromArray(array, item) {
function list() {
return toInfo(_.filter(gameList, function(x) {
return x.players.length < 4 && !x.isStarted
// This is used to determine what games are shown in the lobby
return x.players.length < config.maxPlayers;
}));
}
@ -26,7 +28,12 @@ function listAll() {
function toInfo(fullGameList) {
return _.map(fullGameList, function(game) {
return { id: game.id, name: game.name, players: game.players.length };
return {
id: game.id,
name: game.name,
players: game.players.length,
maxPlayers: game.maxPlayers
};
});
}
@ -41,7 +48,8 @@ function addGame(game) {
game.currentBlackCard = "";
game.isReadyForScoring = false;
game.isReadyForReview = false;
game.pointsToWin = 5;
game.pointsToWin = config.pointsToWin;
game.maxPlayers = config.maxPlayers;
gameList.push(game);
return game;
}
@ -61,19 +69,20 @@ function joinGame(game, player) {
isCzar: false
};
for(var i = 0; i < 7; i++) {
for(var i = 0; i < config.whiteCardsPerHand; i++) {
drawWhiteCard(game, joiningPlayer);
}
game.players.push(joiningPlayer);
if(game.players.length === 4) {
// Need to add a start game button, not just start automatically
if(game.players.length === config.minPlayers) {
if(!game.isStarted){
startGame(game);
} else {
//someone may have dropped and rejoined. If it was the Czar, we need to re-elect the re-joining player
var currentCzar = _.find(game.players, function(p) {
return p.isCzar == true;
return p.isCzar === true;
});
if(!currentCzar){
game.players[game.players.length - 1].isCzar = true;
@ -105,6 +114,18 @@ function startGame(game) {
game.players[0].isCzar = true;
}
function selectCardCzar(game) {
for (var i = 0; i < game.players.length; i++) {
if (game.players[i].isCzar === true) {
game.players[i].isCzar = false;
var nextCzar = (i + 1) % game.players.length;
game.players[nextCzar].isCzar = true;
game.players[nextCzar].isReady = false;
return;
}
}
}
function roundEnded(game) {
game.winnerId = null;
game.winningCardId = null;
@ -123,32 +144,15 @@ function roundEnded(game) {
player.selectedWhiteCardId = null;
});
if(game.players[0].isCzar === true) {
game.players[0].isCzar = false;
game.players[1].isCzar = true;
game.players[1].isReady = false;
}
else if(game.players[1].isCzar === true) {
game.players[1].isCzar = false;
game.players[2].isCzar = true;
game.players[2].isReady = false;
}
else if(game.players[2].isCzar === true) {
game.players[2].isCzar = false;
game.players[3].isCzar = true;
game.players[3].isReady = false;
}
else if(game.players[3].isCzar === true) {
game.players[3].isCzar = false;
game.players[0].isCzar = true;
game.players[0].isReady = false;
// create a function to rotate through the players
selectCardCzar(game);
if(game.isOver){
_.map(game.players, function(p) {
p.awesomePoints = 0;
});
game.isOver = false;
}
if(game.isOver){
_.map(game.players, function(p) {
p.awesomePoints = 0;
});
game.isOver = false;
}
}
function drawWhiteCard(game, player) {
@ -198,7 +202,7 @@ function selectCard(gameId, playerId, whiteCardId) {
return x.selectedWhiteCardId;
});
if(readyPlayers.length === 3) {
if(readyPlayers.length === game.players.length - 1) {
game.isReadyForScoring = true;
}
}


+ 2
- 2
public/js/app/controllers.js View File

@ -36,7 +36,7 @@ angular.module('myApp.controllers', [])
$scope.$on('enterGame', function() {
$scope.inLobby = false;
})
});
})
.controller('GameCtrl', function($scope, $routeParams, GameService){
@ -282,4 +282,4 @@ angular.module('myApp.controllers', [])
}
initSocket();
$scope.$emit('enterLobby');
});
});

+ 1
- 1
public/js/app/filters.js View File

@ -6,5 +6,5 @@ angular.module('myApp.filters', []).
filter('interpolate', ['version', function(version) {
return function(text) {
return String(text).replace(/\%VERSION\%/mg, version);
}
};
}]);

+ 2
- 2
public/js/app/services.js View File

@ -9,7 +9,7 @@ angular.module('myApp.services', [])
.factory('GameService', function($http) {
var s4 = function() {
return Math.floor(Math.random() * 0x10000).toString();
}
};
var guid = function(){
return s4() + s4() + "-" + s4() + "-" + s4() + "-" + s4() + "-" + s4() + s4() + s4();
};
@ -46,5 +46,5 @@ angular.module('myApp.services', [])
readyForNextRound: function(gameId, playerId) {
$http.post("readyForNextRound", { playerId: playerId, gameId: gameId });
}
}
};
});

+ 4
- 3
server.js View File

@ -6,8 +6,9 @@ var routes = require('./routes/routes.js');
var players = { };
var io = require('socket.io').listen(server);
var socketCount = 0;
var config = require('./config.js');
server.listen(process.env.PORT || 3000);
server.listen(process.env.PORT || config.port || 3000);
app.set('view engine', 'ejs');
app.set('view options', { layout: false });
@ -38,7 +39,7 @@ var lobbySocket = io
console.info('lobby socket connect');
var gameList = Game.list();
socket.emit('lobbyJoin', gameList);
})
});
io.sockets.on('connection', function(socket) {
socketCount+=1;
@ -90,7 +91,7 @@ app.post('/joingame', function (req, res) {
return null;
}
if(game.isStarted || game.players.length >= 4) {
if(game.players.length >= config.maxPlayers) {
res.writeHead(500, { 'Content-Type': 'application/json' });
res.write(JSON.stringify({ error: "too many players" }));
res.end();


+ 2
- 2
views/lobby.html View File

@ -1,7 +1,7 @@
<h4>Instructions</h4>
<ul>
<li>Enter your name at the top.</li>
<li>You need 4 players to play.</li>
<li>You need at least 3 players to play.</li>
<li>Use the Create Game link at the top to start a game or...</li>
<li>click below to join a game.</li>
</ul>
@ -11,7 +11,7 @@
<strong id="noGames" ng-show="availableGames.length == 0">no open games... use the Create Game link at the top to start one.</strong>
<div ng-repeat="game in availableGames" class="well">
<button ng-click="joinGame(game.id)" type="button" class="btn btn-primary" style="margin-right: 10px;">Join Game</button>
<span style="font-size: larger">{{game.name}} - {{game.players}} of 4 players</span>
<span style="font-size: larger">{{game.name}} - {{game.players}} of {{game.maxPlayers}} players</span>
</div>
</div>
</div>

Loading…
Cancel
Save