Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Substantial refactor of command logic #39

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

dnathe4th
Copy link
Contributor

Resolves #35

var playerState = state.players[playerIdx];
if (playerState == null) {
throw new GameException('Unknown player');
const playerIsReady = (command, playerIdx) => {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer to be consistent with the existing style, rather than have a mixture of styles:

function playerIsReady(command, playerIds) {

Unless there is any reason for this syntax?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope, no reason. Habit

if (!bs) {
throw new GameException('Unknown command or invalid state');
}
const skipEmit = bs
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You define skipEmit then you don't use it.

throw new GameException('Unknown player');
const playerIsReady = (command, playerIdx) => {
if (state.players[playerIdx].isReady !== true) {
throw new GameException('You cannot start the game');
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are calling this function in many other cases, not just when someone tries to start the game. Might be better to use a message related to not being ready. To be honest, I cannot remember why it is important to check that a player is ready before he starts a game.

// You can always leave, even if your state id is old.
playerLeft(playerIdx);
};
const playerIsUndead = (command, playerIdx) => {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To me, all these functions would be a lot clearer if they were named, e.g., checkPlayerIsAlive, checkItIsOurTurn, etc.

}
action = actions[command.action];
};
const invalidPlayer = (stateField) => (command, playerIdx) => {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For clarity, rename to something like checkPlayerDoesNotActOnSelf, and the message should end in on himself or similar.

];
behaviors[['play-action', stateNames.START_OF_TURN]] = [
playersTurn,
(command, playerIdx) => {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like how the behaviours are all very readable at a glance now. Maybe factor all of them out into named functions?

@@ -234,7 +234,8 @@ module.exports = function createGame(options) {
if (onlyAiLeft()) {
destroyGame();
}
emitState(true);
emitState();
game.emit('statechange');
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible for statechange to be emitted twice now? Would that cause any issues?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the emitting of "statechange" was such a rare usecase for emitState I just pulled it out (removing the need for emitState to take a parameter) and call it explicitly here.

if (!state.players[playerIdx]) {
throw new GameException('Unknown player');
}
if (command.stateId != state.stateId) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The original file had a comment You can always leave, even if your state id is old - this is important

influence.revealed = true;
state.players[playerIdx].influenceCount--;
addHistory(state.state.reason, curTurnHistGroup(), '%s; {%d} revealed %s', state.state.message, playerIdx, command.role);
const reasonMap = {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this really better than an if statement??

Copy link
Contributor Author

@dnathe4th dnathe4th Apr 17, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I overdid it here in my pursuit of removing all else's

for (var i = 0; i < state.players[playerIdx].influence.length; i++) {
var influence = state.players[playerIdx].influence[i];
if (influence.role != command.role || influence.revealed) {
continue
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: missing semicolon

@octachrome
Copy link
Owner

I like what you've done here. Some of my comments are nit picking, sorry about that. But hey, when you open a pull request just to improve my code, I expect perfection :)

Thanks for taking the time.

@octachrome
Copy link
Owner

Any progress?

@dnathe4th
Copy link
Contributor Author

dnathe4th commented May 4, 2020 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

command is an unwieldy function
2 participants