Skip to content

Commit

Permalink
Partial fix for issue #10: verification doesn't complete if a game is…
Browse files Browse the repository at this point in the history
… restarted before it's done
  • Loading branch information
monicanagent committed Nov 24, 2018
1 parent b22166e commit bbedca1
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 27 deletions.
132 changes: 119 additions & 13 deletions src/web/scripts/CypherPokerAnalyzer.js
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,40 @@ class CypherPokerAnalyzer extends EventDispatcher {
return (this.game.cypherpoker);
}

/**
* @property {CypherPoker} cypherpoker A reference to the
* {@link CypherPokerContract#game}'s <code>cypherpoker</code> instance or
* <code>null</code> if none exists.
*/
get cypherpoker() {
if ((this._game != null) && (this._game != undefined)) {
return (this._game.cypherpoker);
}
return (null);
}

/**
* @property {TableObject} A copy of the table associated with the {@link CypherPokerContract#game}
* instance.
*/
get table() {
if (this._table == undefined) {
this._table = this.game.getTable();
}
return (this._table);
}

/**
* @property {Array} Indexed list of {CypherPokerPlayer} instances copied
* from the associated {@link CypherPokerContract#game} instance.
*/
get players() {
if (this._players == undefined) {
this.refreshPlayers();
}
return (this._players);
}

/**
* @property {Object} analysis The partial or full analysis of the
* completed game.
Expand All @@ -266,6 +300,79 @@ class CypherPokerAnalyzer extends EventDispatcher {
return (this._analysis);
}

/**
* Refreshes the {@link CypherPokerAnalyzer#players} array with data from
* the associated {@link CypherPokerAnalyzer#game}.
*
* @private
*/
refreshPlayers() {
this._players = new Array();
for (var count=0; count < this.game.players.length; count++) {
this._players.push(this.game.players[count].copy());
}
}

/**
* Returns a {@link CypherPokerPlayer} instance associated with the analyzer's
* game instance.
*
* @param {String} privateID The private ID of the player to return.
*
* @return {CypherPokerPlayer} The {@link CypherPokerPlayer} for the private ID
* associated with this game. <code>null</code> is returned if no matching
* player private ID can be found.
*/
getPlayer(privateID) {
for (var count=0; count < this.players.length; count++) {
if (this.players[count].privateID == privateID) {
return (this.players[count]);
}
}
return (null);
}

/**
* Returns a condensed array containing the copied properties of the
* {@link CypherPokerAnalyzer#players} array. Use the object returned by
* this function with <code>JSON.stringify</code> instead of using
* {@link CypherPokerAnalyzer#players} directly in order to prevent circular
* reference errors.
*
* @param {Boolean} [includeKeychains=false] If true, the {@link CypherPokerPlayer#keychain}
* array of each player will be included in the returned object.
* @param {Boolean} [includePasswords=false] If true, the {@link CypherPokerAccount#password}
* property of each {@link CypherPokerPlayer#account} reference will be included
* with the returned object.
*
* @return {Object} The condensed players array associated with this instance's
* game reference.
*/
getPlayers(includeKeychains=false, includePasswords=false) {
var returnArr = new Array();
for (var count=0; count < this.players.length; count++) {
var playerObj = this.players[count].toObject(includeKeychains, includePasswords);
returnArr.push(playerObj);
}
return (returnArr);
}

/**
* Returns the {@link CypherPokerPlayer} that is currently flagged as the dealer
* in the {@link CypherPokerContract#players} array.
*
* @return {CypherPokerPlayer} The {@link CypherPokerPlayer} instance that
* is flagged as a dealer. <code>null</code> is returned if no dealer is flagged.
*/
getDealer() {
for (var count=0; count < this.players.length; count++) {
if (this.players[count].isDealer) {
return (this.players[count]);
}
}
return (null);
}

/**
* Removes all of the event listeners added to the {@link CypherPokerAnalyzer.game}
* reference at instantiation.
Expand Down Expand Up @@ -312,7 +419,7 @@ class CypherPokerAnalyzer extends EventDispatcher {
cardsArr.push(generatedDeck[count].mapping);
this.mappedDeck.push(this.game.getMappedCard(generatedDeck[count].mapping));
}
infoObj.fromPID = event.game.getDealer().privateID;
infoObj.fromPID = this.getDealer().privateID;
infoObj.cards = cardsArr;
this.deck.push (infoObj);
}
Expand Down Expand Up @@ -431,9 +538,9 @@ class CypherPokerAnalyzer extends EventDispatcher {
//partially decrypted public or private cards:
var selected = resultObj.data.payload.selected;
//the player that dealt (selected) the cards:
var dealingPlayer = this.game.getPlayer(resultObj.data.payload.sourcePID);
var dealingPlayer = this.getPlayer(resultObj.data.payload.sourcePID);
//the player that sent the "gamedeal" message:
var fromPlayer = this.game.getPlayer(resultObj.from);
var fromPlayer = this.getPlayer(resultObj.from);
//the selected card values:
if (dealingPlayer.privateID == fromPlayer.privateID) {
//player is selecting a card
Expand All @@ -459,9 +566,9 @@ class CypherPokerAnalyzer extends EventDispatcher {

/**
* Event handler invoked when the associated {@link CypherPokerAnalyzer#game}
* instance dispatches a {@link CypherPoker#event:gameanalyze} event.
* instance dispatches a {@link CypherPokerGame#event:gameanalyze} event.
*
* @param {Event} event A {@link CypherPoker#event:gameanalyze} event object.
* @param {Event} event A {@link CypherPokerGame#event:gameanalyze} event object.
*
* @async
* @private
Expand All @@ -482,7 +589,7 @@ class CypherPokerAnalyzer extends EventDispatcher {
*/
onKCSTimeout(context, game) {
context.analysis.complete = true;
//throw (new Error("Not all players have committed their keychains in time (table ID: "+game.table.tableID+")"));
throw (new Error("Not all players have committed their keychains in time (table ID: "+context.table.tableID+")"));
}

/**
Expand All @@ -495,16 +602,15 @@ class CypherPokerAnalyzer extends EventDispatcher {
* @private
*/
async onPlayerKeychain(event) {
console.log ("CypherPokerAnalyzer.onPlayerKeychain("+event+")");
this._active = true;
if (this._keychains == undefined) {
this._keychains = new Object();
}
var player = event.player;
var game = event.game;
if (game.gameStarted) {
// return (false);
}
this._keychains[player.privateID] = Array.from(event.keychain);
console.log ("this.allKeychainsCommitted="+this.allKeychainsCommitted);
if (this.allKeychainsCommitted) {
this.removeGameListeners();
//all keychains committed, we can clear the timeout and start the analysis
Expand Down Expand Up @@ -650,7 +756,7 @@ class CypherPokerAnalyzer extends EventDispatcher {
for (count2 = 0; count2 < promiseResults.length; count2++) {
var card = this.getMappedCard(promiseResults[count2].data.result);
if (card == null) {
var error = new Error("Final decryption (deal "+count+") by \""+this.game.getPlayer(fromPID).account.address+"\" for \""+this.game.getPlayer(sourcePID).account.address+"\" does not map: "+promiseResults[count2].data.result);
var error = new Error("Final decryption (deal "+count+") by \""+this.getPlayer(fromPID).account.address+"\" for \""+this.getPlayer(sourcePID).account.address+"\" does not map: "+promiseResults[count2].data.result);
console.dir (card);
console.dir (promiseResults[count2].data.result);
error.code = 2;
Expand All @@ -665,7 +771,7 @@ class CypherPokerAnalyzer extends EventDispatcher {
}
}
if (this.removeFromDeck(cards, encryptedDeck) == false) {
var error = new Error("Duplicates found in \"select\" deal index "+count+" for \""+this.game.getPlayer(fromPID).account.address+"\" for \""+this.game.getPlayer(sourcePID).account.address+"\".");
var error = new Error("Duplicates found in \"select\" deal index "+count+" for \""+this.getPlayer(fromPID).account.address+"\" for \""+this.getPlayer(sourcePID).account.address+"\".");
error.code = 2;
this._analysis.error = error;
this._analysis.complete = true;
Expand Down Expand Up @@ -712,7 +818,7 @@ class CypherPokerAnalyzer extends EventDispatcher {
compareDeck.push(promiseResults[count2].data.result);
}
if (this.compareDecks(compareDeck, cards) == false) {
var error = new Error("Previous round ("+count+") of decryption by \""+this.game.getPlayer(fromPID).account.address+"\" for \""+this.game.getPlayer(sourcePID).account.address+"\" does not match computed results.");
var error = new Error("Previous round ("+count+") of decryption by \""+this.getPlayer(fromPID).account.address+"\" for \""+this.getPlayer(sourcePID).account.address+"\" does not match computed results.");
console.dir(compareDeck);
console.dir(cards);
error.code = 2;
Expand Down Expand Up @@ -802,7 +908,7 @@ class CypherPokerAnalyzer extends EventDispatcher {
var winningPlayers = new Array();
var winningHands = new Array();
for (var privateID in playersObj) {
var player = this.game.getPlayer(privateID);
var player = this.getPlayer(privateID);
//private ID may actually be some other object property (e.g. onEventPromise)
if (player != null) {
if (player.hasFolded == false) {
Expand Down
13 changes: 2 additions & 11 deletions src/web/scripts/CypherPokerGame.js
Original file line number Diff line number Diff line change
Expand Up @@ -1737,23 +1737,14 @@ class CypherPokerGame extends EventDispatcher {
if (context == null) {
context = this;
}
//context._gameEnding = true;
/*
context._gameEnding = true;
if (context.analyzer != null) {
if (context.analyzer.active != false) {
//re-check every half second
setTimeout(context.restartGame, 500, context);
return (false);
}
}
if (context.contract != null) {
if (context.contract.active != false) {
//re-check every half second
setTimeout(context.restartGame, 500, context);
return (false);
}
}
*/
context.pot = 0;
context._gameStarted = false;
context.resetPlayerStates(true, true, true);
Expand Down Expand Up @@ -1799,7 +1790,7 @@ class CypherPokerGame extends EventDispatcher {
//anyone but the current dealer will throw an error in sendGameParams
}
context._gameStarted = true;
//context._gameEnding = false;
context._gameEnding = false;
var result = await context.processMessageQueue();
return (true);
}
Expand Down
5 changes: 2 additions & 3 deletions src/web/scripts/libs/EventDispatcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,11 @@ class EventDispatcher {
}

/**
* Removes an event listener from the extending instance. The standard capture phase
* parameter is ignored.
* Removes an event listener from the extending instance.
*
* @param {String} type The event type to remove the function from.
* @param {Function} listener The listening function to remove.
* @param {}
* @param {Object} [context=null] The context or scope in which the listener exists.
*
* @augments EventDispatcher
*/
Expand Down

0 comments on commit bbedca1

Please sign in to comment.