samedi 3 janvier 2015

Need Javascript Architecture Hints for a simple game

i'm studying NodeJs and trying to apply some concepts to a simple game. The Game is a Tennis Match Simulation beetwen two players. I need hints for understand if the architecture is correct, expecially because i'm not sure if the PointEngine module have sense. During the match the function SelectPointToPlay look in the scoreCard, holded by the Scorer module, and invoke a specific function of the PointEngine. This function virtually play the point and return a Point object that contains the winner of the point and a message for move forward the match (later i think to change this return with an event).


My doubt involved this part:



/* This function analyze the score and invoke the point engine */
Match.prototype.selectPointToPlay = function(){
// ... retrieve currentSet, currentGame an currentPoint with an if-else hierarchy ...
var pEngine = new PointEngine();
var p = pEngine.situationsMap[currentSet][currentGame][currentPoint](this);
this.pointPlayed(p);
};


when i do pEngine.situationsMap[currentSet][currentGame]currentPoint; i am passing the Match to the PointEngine, is this a good approach?


Also i have a problem with the function that virtually play the point because the object has no method 'getEcc' (method that compare players' skills and determines the point winner) :



/* function invoked in the 1st set on the game 5-4 or 6-5 for the responding player */
PointEngine.prototype._00g4NLnormalPoint = function(aMatch){
if(aMatch.scorer.getCurrentGameScore().toString().indexOf('40')>=0){
var server = aMatch.getServingPlayer();
var responder=aMatch.getRespondingPlayer();
var serverEcc = this.getEcc(server);
var responderEcc = this.getEcc(responder);
var winner = serverEcc >= responderEcc ? server : responder;
return new Point(winner,'normalPoint');
}else
return new Point(aMatch.getServingPlayer(),'normalPoint');
};


Here the complete code:



---------------------
Main.js
---------------------
// LOADING PLAYERS
var players = require("./players/players");
var roger = players.roger;
var rafa = players.rafa;

//LOADING MATCH MODULE
var Match = require("./match");

m1 = new Match(roger, rafa);
m1.startMatch();


---------------------
Match.js
---------------------
Match.prototype.startMatch = function(){
this.playNewSet();
}

/*Reset the set score card and start a new Game*/
Match.prototype.playNewSet = function() {
this.setCount++;
//set scorer.p1SetScore and scorer.p2SetScore to 0
this.scorer.newSet();
this.playNewGame();
};

/*Reset the Game score card and select the point to be played*/
Match.prototype.playNewGame = function() {
this.gameCount++;
//set scorer.p1GameScore and scorer.p2GameScore to 0
this.scorer.newGame();

/* Change the serving player pointer*/
this.servingPlayer = this.servingPlayer == this.player1 ? this.player2 : this.player1;

this.selectPointToPlay();
};

/* This function analyze the score and invoke the point engine */
Match.prototype.selectPointToPlay = function(){
// ... retrieve currentSet, currentGame an currentPoint with an if-else hierarchy ...
var pEngine = new PointEngine();
var p = pEngine.situationsMap[currentSet][currentGame][currentPoint](this);
this.pointPlayed(p);
};

/* Analyze the result of the point played and move forward the scorer and the match */
Match.prototype.pointPlayed = function (point) {
var pointWinner = (point.winner == this.player1 ? 1 : 2);
//add one point to the point winner
this.scorer.addOnePoint(pointWinner);

if(point.type=='normalPoint') {
this.selectPointToPlay();
}
else if(point.type=='gameFinished'){
this.playNewGame();
}else if(point.type=='setFinished'){
this.playNewSet();
}else if(point.type=='matchFinished'){
}
};

---------------
PointEngine.js
------------------
function Point(winner,type){
this.winner = winner;
this.type = type;
}

PointEngine = function(){
this.situationsMap = {
ZeroZero: {
game4Win: { gamePoint: this._00g4WgamePoint, breakPoint: this._00g4WbreakPoint, normalPoint: this._00g4WnormalPoint },
game4NotLose: { gamePoint: this._00g4NLgamePoint, breakPoint: this._00g4NLbreakPoint, normalPoint: this._00g4NLnormalPoint },
normalGame:{ gamePoint: this._00gamePoint, breakPoint: this._00breakPoint, normalPoint: this._00normalPoint }
},
OneZero: {
game4Win: { gamePoint: this._10g4WgamePoint, breakPoint: this._10g4WbreakPoint, normalPoint: this._10g4WnormalPoint },
game4NotLose: { gamePoint: this._10g4NLgamePoint, breakPoint: this._10g4NLbreakPoint, normalPoint: this._10g4NLnormalPoint },
normalGame:{ gamePoint: this._10gamePoint, breakPoint: this._10breakPoint, normalPoint: this._10normalPoint }
},
ZeroOne: {
game4Win: { gamePoint: this._01g4WgamePoint, breakPoint: this._01g4WbreakPoint, normalPoint: this._01g4WnormalPoint },
game4NotLose: { gamePoint: this._01g4NLgamePoint, breakPoint: this._01g4NLbreakPoint, normalPoint: this._01g4NLnormalPoint },
normalGame:{ gamePoint: this._01gamePoint, breakPoint: this._01breakPoint, normalPoint: this._01normalPoint }
},
OneOne: {
game4Win: { gamePoint: this._11g4WgamePoint, breakPoint: this._11g4WbreakPoint, normalPoint: this._11g4WnormalPoint },
game4NotLose: { gamePoint: this._11g4NLgamePoint, breakPoint: this._11g4NLbreakPoint, normalPoint: this._11g4NLnormalPoint },
normalGame:{ gamePoint: this._11gamePoint, breakPoint: this._11breakPoint, normalPoint: this._11normalPoint }
}
}

/*Return the sum of Energy, Commitment and Concentration */
function getEcc(aPlayer){
return aPlayer.energy + aPlayer.commitment + aPlayer.concentration;
}
};

Aucun commentaire:

Enregistrer un commentaire