Skip to content

Commit

Permalink
Merge pull request #147 from AgenoDrei/development
Browse files Browse the repository at this point in the history
Development [MultiLanguage, Minischach winCondition formalization; incorp. all in into Editor]
  • Loading branch information
AgenoDrei authored Jul 15, 2017
2 parents 8b5a058 + b0792d6 commit 6947429
Show file tree
Hide file tree
Showing 39 changed files with 1,316 additions and 297 deletions.
1 change: 0 additions & 1 deletion controller/socketController.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ module.exports = function(configuration, gameHandler) {
switch (m.type) { //see protocol
case "hello":
gameHandler.getGame(m.gameId).done(function(game) {
debugger;
game.connect(m.joinId, connection).done(function(player) {
var response = {
type: "hello",
Expand Down
3 changes: 3 additions & 0 deletions docs/convertMongoStructure.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```shell
db.lvls.find().toArray().forEach(function(doc){doc.description={"de":doc.description,"en":""};db.lvls.update({_id:doc._id},doc)})
```
16 changes: 12 additions & 4 deletions model/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,19 @@ module.exports = {
NONE: 3
},

figureType: {
CHIP: 0,
PAWN: 1,
KNIGHT: 2,
BISHOP: 3,
ROOK: 4,
QUEEN: 5,
KING: 6
},

winCondition: {
CHIPS: 0,
FIGURES: 1,
PAWN_TO_LAST_ROW: 2
// FIG_TO_CENTER: 3
PAWN_TO_LAST_ROW: 2,
FIG_REACHES_FIELD: 3
},

reviewStatus: {
Expand Down
35 changes: 26 additions & 9 deletions model/gameHandler.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,36 @@
var Promise = require('promise');
var gameTypes = require('./constants').gameType;
var conStates = require('./constants').connectionState;
var playerType = require('./constants').playerType;
var gameState = require('./constants').gameState;
var Game = require('./gameModel/gameModel');
var helper = require('./helper');
const Promise = require('promise');
const gameType = require('./constants').gameType;
const conStates = require('./constants').connectionState;
const playerType = require('./constants').playerType;
const gameState = require('./constants').gameState;
const SingleplayerGame = require('./gameModel/singleplayerGame');
const MultiplayerGame = require('./gameModel/multiplayerGame');
const MinichessGame = require('./gameModel/minichessGame');
const helper = require('./helper');

module.exports = function(dataAccess) {
this.games = [];

this.createGame = function(gameParameters) {
return new Promise(function(fulfill, reject) {
dataAccess.getLevelById(gameParameters.level).done(function(level) {
var type = gameTypes[gameParameters.type];
var newGame = new Game(type, gameParameters.mode, gameParameters.local, level, gameParameters.name);
var type = gameType[gameParameters.type];
let newGame = null;
switch(type) {
case gameType.SP:
newGame = new SingleplayerGame(type, gameParameters.mode, gameParameters.local, level, gameParameters.name);
break;
case gameType.MP:
newGame = new MultiplayerGame(type, gameParameters.mode, gameParameters.local, level, gameParameters.name);
break;
case gameType.MINI:
newGame = new MinichessGame(type, gameParameters.mode, gameParameters.local, level, gameParameters.name);

break;
default:
reject('Invalid game type')
}
//var newGame = new Game(type, gameParameters.mode, gameParameters.local, level, gameParameters.name);

this.games.push(newGame);

Expand Down
94 changes: 94 additions & 0 deletions model/gameModel/game.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
const Promise = require('promise');
const shortid = require('shortid');
const gameTypes = require('../constants').gameType;
const conStates = require('../constants').connectionState;
const gameStates = require('../constants').gameState;
const playerType = require('../constants').playerType;
const helper = require('../helper');
const Board = require('./gameBoard/gameBoard');
const Chip = require('./gameBoard/chip');
//var ProgressModel = require('./progressModel');
const GameEnd = require('./gameEnds/gameEnd');
const ChipGameEnd = require('./gameEnds/chipGameEnd');
const FigureGameEnd = require('./gameEnds/figureGameEnd');
const LastRowGameEnd = require('./gameEnds/lastRowGameEnd');
const GameEndManager = require('./gameEnds/gameEndManager');
const History = require('./history');


class Game {
constructor(type, mode, local, level, name) {
this.name = name;
this.toBeNext = playerType.PLAYERONE;
this.type = type;
this.mode = mode;
this.local = (local == 'true');
this.level = level;
this.id = shortid.generate();
this.player1 = {
connection: null,
state: conStates.EMPTY,
joinId: shortid.generate()
};
this.player2 = {
connection: null,
state: conStates.EMPTY,
joinId: shortid.generate()
};
this.board = new Board(this);
this.board.loadLevel(this.level);
this.win = new GameEndManager();

this.history = new History(this.board);
console.log('New Game created with ID ' + this.id + ' and name ' + this.name + ', using level ' + this.level._id);
}

getId() {
return this.id;
}

/*
* Overwrite
*/
turn(origX, origY, destX, destY, player) {
}

undo() {
return this.history.undo();
}

connect(joinId, connection) {
var player1 = this.player1;
var player2 = this.player2;
return new Promise(function(fulfill, reject) {
if (joinId == player1.joinId && player1.state == conStates.JOINED) {
player1.connection = connection;
player1.state = conStates.CONNECTED;
fulfill(playerType.PLAYERONE);
} else if (joinId == player2.joinId && player2.state == conStates.JOINED) {
player2.connection = connection;
player2.state = conStates.CONNECTED;
fulfill(playerType.PLAYERTWO);
}
reject('Invalid joinId or already joined');
});
}

sendToAll(message) {
console.log("Server> ", message);
try {
this.player1.connection.sendUTF(JSON.stringify(message));
if(!this.local) this.player2.connection.sendUTF(JSON.stringify(message));
} catch(e) {
console.log('Error on sending to all in game...');
}
}

endGame() {
console.log("Server> Game finished");
this.sendToAll({type: "exit"});
}

}

module.exports = Game;
1 change: 1 addition & 0 deletions model/gameModel/gameBoard/figure.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class Figure {
if(helper.enemy(beater.player, this.player)) {
this.board.parentGame.history.setLastBeat(this.board.fields[this.x][this.y].getFigure());
this.board.fields[this.x][this.y].setFigure(null);
debugger;
this.board.parentGame.win.captureFigure(this.player, this.constructor.name);
return true;
}
Expand Down
27 changes: 27 additions & 0 deletions model/gameModel/gameEnds/chipGameEnd.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
const GameEnd = require('./gameEnd');
const playerType = require('../../constants').playerType;
const gameType = require('../../constants').gameType;
const gameState = require('../../constants').gameState;
const winCondition = require('../../constants').winCondition;

class ChipGameEnd extends GameEnd {
constructor(board) {
super(board);
}

isWin() {
if(this.chips[playerType.PLAYERTWO] == 0 && this.chips[playerType.BOTH] == 0
&& this.score[playerType.PLAYERONE] > this.score[playerType.PLAYERTWO]) {
return gameState.WIN_PLAYER1;
} else if(this.chips[playerType.PLAYERONE] == 0 && this.chips[playerType.BOTH] == 0
&& this.score[playerType.PLAYERONE] < this.score[playerType.PLAYERTWO]) {
return gameState.WIN_PLAYER2;
} else if(this.chips[playerType.PLAYERTWO] == 0 && this.chips[playerType.PLAYERONE] == 0 && this.chips[playerType.BOTH] == 0
&& this.score[playerType.PLAYERONE] == this.score[playerType.PLAYERTWO]) {
return gameState.WIN_DRAW;
}
return gameState.VALID_TURN;
}
}

module.exports = ChipGameEnd;
21 changes: 21 additions & 0 deletions model/gameModel/gameEnds/figureGameEnd.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const GameEnd = require('./gameEnd');
const playerType = require('../../constants').playerType;
const gameType = require('../../constants').gameType;
const gameState = require('../../constants').gameState;
const winCondition = require('../../constants').winCondition;

class FigureGameEnd extends GameEnd {
constructor(board) {
super(board);
}

isWin() {
if(this.figures[playerType.PLAYERONE] == 0)
return gameState.WIN_PLAYER2;
else if(this.figures[playerType.PLAYERTWO] == 0)
return gameState.WIN_PLAYER1;
return gameState.VALID_TURN;
}
}

module.exports = FigureGameEnd;
31 changes: 31 additions & 0 deletions model/gameModel/gameEnds/figureReachFieldGameEnd.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const GameEnd = require('./gameEnd');
const playerType = require('../../constants').playerType;
const gameType = require('../../constants').gameType;
const gameState = require('../../constants').gameState;
const winCondition = require('../../constants').winCondition;
const figureType = require('../../constants').figureType;
const Pawn = require('../gameBoard/pawn');
const helper = require('../../helper');

class FigureReachFieldGameEnd extends GameEnd {
constructor(board, figure, field, player) {
super(board);
this.winFigure = figure;
this.winField = field;
this.winPlayer = player;
}

isWin() {
let curFigure = this.board.getField(this.winField.x,this.winField.y).getFigure();
if(curFigure == null)
return gameState.VALID_TURN;
// console.log(curFigure.constructor.name);
if(figureType[curFigure.constructor.name.toUpperCase()] == this.winFigure) {
if(curFigure.player == this.winPlayer)
return (this.winPlayer + 1); // +1 such that transition from constants.playerType to constants.gameState !
}
return gameState.VALID_TURN;
}
}

module.exports = FigureReachFieldGameEnd;
54 changes: 54 additions & 0 deletions model/gameModel/gameEnds/gameEnd.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
var playerType = require('../../constants').playerType;
const gameType = require('../../constants').gameType;
const gameState = require('../../constants').gameState;
const winCondition = require('../../constants').winCondition;
const helper = require('../../helper');

class GameEnd {
constructor(board) {
this.board = board;
this.chips = [this.board.chips[0], this.board.chips[1], this.board.chips[2]];
this.figures = this.board.figures;
this.turnCount = 0;
this.score = [0, 0];
console.log("WinCondition created!");
}

isWin() {
return gameState.VALID_TURN;
}

captureFigure(player, type) {
this.figures[player]--;
}

captureChip(player, type) {
var currentPlayer = null;
if(player == playerType.PLAYERONE) {
currentPlayer = playerType.PLAYERONE;
} else if(player == playerType.PLAYERTWO) {
currentPlayer = playerType.PLAYERTWO;
}

switch(type) {
case playerType.PLAYERONE: //Yellow Chip
this.chips[playerType.PLAYERONE]--;
this.score[playerType.PLAYERTWO]++;
break;
case playerType.PLAYERTWO: //Blue Chip
this.chips[playerType.PLAYERTWO]--;
this.score[playerType.PLAYERONE]++;
break;
case playerType.BOTH: //Green Chip
this.chips[playerType.BOTH]--;
this.score[currentPlayer]++;
break;
}
}

countUpTurn() {
this.turnCount++;
}
};

module.exports = GameEnd;
44 changes: 44 additions & 0 deletions model/gameModel/gameEnds/gameEndManager.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
const gameState = require('../../constants').gameState;
const GameEnd = require('./gameEnd');
const ChipGameEnd = require('./chipGameEnd');
const FigureGameEnd = require('./figureGameEnd');
const LastRowGameEnd = require('./lastRowGameEnd');


class GameEndManager {
constructor() {
this.ends = [];
}

addGameEnd(end) {
this.ends.push(end);
}

checkGameEnd() {
for(let end of this.ends) {
let win = end.isWin();
if(win != gameState.VALID_TURN)
return win;
}
return gameState.VALID_TURN;
}

captureFigure(player, type) {
if(this.ends.length > 0)
this.ends[0].captureFigure(player, type);
}

captureChip(player, type) {
if(this.ends.length > 0)
this.ends[0].captureChip(player, type);
}

countUpTurn() {
for(let end of this.ends) {
end.countUpTurn();
}
}

};

module.exports = GameEndManager;
Loading

0 comments on commit 6947429

Please sign in to comment.