Skip to content

Commit

Permalink
Fix players not being able to join mid-game, and cards never being se…
Browse files Browse the repository at this point in the history
…rved on reload
  • Loading branch information
JordanPawlett committed Apr 25, 2020
1 parent 30f1ce3 commit 3b132fa
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 15 deletions.
2 changes: 1 addition & 1 deletion services/decks-service/src/decks-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ export default class DecksService extends Service {
}
await this.broker.broadcast('cache.clean.decks');
await this.broker.cacher?.clean();
}, 1000);
}, 5000);
}
},
);
Expand Down
15 changes: 14 additions & 1 deletion services/games-service/src/game.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export default class Game extends TurnHandler {
private nextTurnTimeout: NodeJS.Timer = null;
private players: { [id: string]: GamePlayer } = this.initalizePlayers(this._room);
private gameState = GameState.TURN_SETUP;
protected lastGameState = null;

constructor(private _room: Room, broker: ServiceBroker, logger: LoggerInstance) {
super(broker, logger);
Expand All @@ -80,6 +81,7 @@ export default class Game extends TurnHandler {
return this.fetchCards(this._room.options.decks)
.then(() => {
this.logger.info('Game started, sending data', initialData);
this.lastGameState = initialData;
this.broker.emit('games.updated', initialData);
this.handleNextTurn();
});
Expand Down Expand Up @@ -120,12 +122,13 @@ export default class Game extends TurnHandler {
state: GameState.ENEDED,
};

this.lastGameState = initialData;
this.broker.emit('games.updated', initialData);
}

private handleNextTurn() {
if (this.nextTurnTimeout) {
clearTimeout(this.nextTurnTimeout)
clearTimeout(this.nextTurnTimeout);
}

// Target should actually be based on the first user score to get to that.
Expand All @@ -150,6 +153,7 @@ export default class Game extends TurnHandler {
};

this.logger.info('Starting turn', withState);
this.lastGameState = withState;
await this.broker.emit('games.updated', withState);
this.setGameTimer();
})
Expand Down Expand Up @@ -183,6 +187,8 @@ export default class Game extends TurnHandler {
winningCards: [],
state: GameState.SELECTING_WINNER,
};

this.lastGameState = initialData;
await this.broker.emit('games.updated', initialData);
}

Expand Down Expand Up @@ -220,6 +226,7 @@ export default class Game extends TurnHandler {
state: GameState.TURN_SETUP,
};

this.lastGameState = initialData;
this.broker.emit('games.updated', initialData);

this.nextTurnTimeout = setTimeout(() => {
Expand Down Expand Up @@ -256,6 +263,7 @@ export default class Game extends TurnHandler {

// Store the end state of each round in a collection.
this.turns.push(initialData);
this.lastGameState = initialData;
// Emit the winning card, and winning player, for front-end display
await this.broker.emit('games.updated', initialData);

Expand Down Expand Up @@ -286,6 +294,11 @@ export default class Game extends TurnHandler {
// Ensure the new player is including in the match.
this.players[playerId] = { _id: playerId, cards: [], isCzar: false, score: 0 };
await this.dealWhiteCards(this.players[playerId]);

// Implement this.
// if (this.lastGameState) {
// emit update to only the player that joined.
// }
}

public destroy() {
Expand Down
19 changes: 10 additions & 9 deletions services/games-service/src/turn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,37 +105,38 @@ export default class TurnHandler {
}

private pickBlackCard(): Promise<Card> {
const index = this.getRandomIndex(this.blackCards.length);
const index = this.getRandomIndex(this.blackCards.length - 1);
// Remove the card so it cannot be chosen again.
this.blackCards.splice(index, 1);
const id = this.blackCards[index];
return this.broker.call('cards.get', { id });
}

private pickWhiteCard(): string {
const index = this.getRandomIndex(this.whiteCards.length);
const index = this.getRandomIndex(this.whiteCards.length - 1);
// Remove the card so it cannot be chosen again.
this.whiteCards.splice(index, 1);
return this.whiteCards[index];
}

private emitCardsToPlayer(player: GamePlayer) {
// Get all the cards and deal them to the player.
return this.broker.call('cards.get', { id: player.cards })
.then(cards => this.broker.emit('games.deal', { clientId: player._id, cards }));
}

// Given a player, deal all the white cards to it.
protected dealWhiteCards(player: GamePlayer): Promise<void> {
const cardsNeeded = 10 - player.cards.length;
if (!cardsNeeded) {
return;
return this.emitCardsToPlayer(player);
}

// for (let i = 0; i < cardsNeeded; i++) {
// player.cards.push(this.pickWhiteCard());
// }
while (player.cards?.length !== 10) {
player.cards.push(this.pickWhiteCard());
}

// Get all the cards and deal them to the player.
return this.broker.call('cards.get', { id: player.cards })
.then(cards => this.broker.emit('games.deal', { clientId: player._id, cards }));
return this.emitCardsToPlayer(player);
}

private async ensurePlayersHaveCards(players: { [id: string]: GamePlayer }) {
Expand Down
2 changes: 1 addition & 1 deletion services/rooms-service/src/rooms-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,7 @@ export default class RoomsService extends Service {
*/
private async afterAddPlayer(ctx: Context<{ clientId: string; roomId: string }>, res: Room) {
const { clientId, roomId } = ctx.params;
const prop = ctx.action.name === 'join-players' ? 'player' : 'spectator';
const prop = ctx.action.name === 'rooms.join-players' ? 'player' : 'spectator';
await ctx.emit(`${this.name}.${prop}.joined`, { clientId, roomId });
return ctx.call(`${this.name}.get`, { id: roomId, populate: ['players', 'spectators'] });
}
Expand Down
19 changes: 16 additions & 3 deletions skaffold.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,6 @@ deploy:
profiles:
- name: gcb
patches:
- op: replace
path: /deploy/kubectl/manifests/0
value: services/nginx/k8s-svc.yaml
- op: replace
path: /build/artifacts/1/docker/dockerfile
value: Dockerfile
Expand Down Expand Up @@ -108,3 +105,19 @@ profiles:
- config/managed-cert.yaml
- config/load-balancer-backend-config.yaml
- config/ingress.yaml

- services/nginx/k8s-svc.yaml
- services/nginx/k8s-pod.yaml
- config/secret.yaml
- config/base-config.yaml
- services/admin-gateway-service/k8s-pod.yaml
- services/admin-gateway-service/k8s-svc.yaml
- services/web-gateway-service/k8s-pod.yaml
- services/web-gateway-service/k8s-svc.yaml
- services/websocket-gateway-service/k8s-pod.yaml
- services/websocket-gateway-service/k8s-svc.yaml
- services/cards-service/k8s-pod.yaml
- services/decks-service/k8s-pod.yaml
- services/clients-service/k8s-pod.yaml
- services/rooms-service/k8s-pod.yaml
- services/games-service/k8s-pod.yaml

0 comments on commit 3b132fa

Please sign in to comment.