patternjavascriptMinor
Cleaning if / else mess in Node.js poker game
Viewed 0 times
pokerelsenodegamemesscleaning
Problem
This is the third rewrite of the poker bot I am writing. The first one was such a mess of
This is my first major project in any language, and version 1 was my first JavaScript project ever. I tried to read some formatting articles such as the Google formatting guide and follow those but I haven't read any formal books yet. I should probably do that. Any recommendations would rock.
I was going to wait until it was finished to post here, however if there were any major changes to anything it would be easier to do before the rest of the functions were finished.
The project is in node.js using the Node - Steam library.
This is the main program that calls the module running the game. Some values are hard-coded in, which will have to change once I finish the first module and start on a second.
```
var fs = require('fs')
var Steam = require('steam')
var _ = require('lodash')
var startedRooms = []
if (fs.existsSync('servers')) {
Steam.servers = JSON.parse(fs.readFileSync('servers'))
}
var bot = new Steam.SteamClient()
bot.logOn({
accountName: 'SpencerFlem',
password: 'Ap3rtur3',
shaSentryfile: fs.readFileSync('sentryfile')
})
bot.on('loggedOn', function() {
var mainChat = '103582791434524271'
console.log('Logged in!')
bot.setPersonaName('Dealer')
bot.setPersonaState(Steam.EPersonaState.Online); // to display your bot's status as "Online"
bot.joinChat(mainChat);
})
bot.on('servers', function(servers) {
fs.writeFile('servers', JSON.stringify(servers))
})
bot.on('chatEnter', function(room) {
startupRoom(room)
})
// MAIN
var allData = {}
gameLookup = {
'103582791434524271':'./Games/Poker/5 Card Draw/5 Card Draw.js' // This to the variable to use functions from?
}
v
if / elses that I could not deal with it. The second version was cluttered, and I had trouble following what happened but did work. I decided to scrap it for a new clean module based / more functional version but it seems to fall into the same traps.This is my first major project in any language, and version 1 was my first JavaScript project ever. I tried to read some formatting articles such as the Google formatting guide and follow those but I haven't read any formal books yet. I should probably do that. Any recommendations would rock.
I was going to wait until it was finished to post here, however if there were any major changes to anything it would be easier to do before the rest of the functions were finished.
The project is in node.js using the Node - Steam library.
This is the main program that calls the module running the game. Some values are hard-coded in, which will have to change once I finish the first module and start on a second.
```
var fs = require('fs')
var Steam = require('steam')
var _ = require('lodash')
var startedRooms = []
if (fs.existsSync('servers')) {
Steam.servers = JSON.parse(fs.readFileSync('servers'))
}
var bot = new Steam.SteamClient()
bot.logOn({
accountName: 'SpencerFlem',
password: 'Ap3rtur3',
shaSentryfile: fs.readFileSync('sentryfile')
})
bot.on('loggedOn', function() {
var mainChat = '103582791434524271'
console.log('Logged in!')
bot.setPersonaName('Dealer')
bot.setPersonaState(Steam.EPersonaState.Online); // to display your bot's status as "Online"
bot.joinChat(mainChat);
})
bot.on('servers', function(servers) {
fs.writeFile('servers', JSON.stringify(servers))
})
bot.on('chatEnter', function(room) {
startupRoom(room)
})
// MAIN
var allData = {}
gameLookup = {
'103582791434524271':'./Games/Poker/5 Card Draw/5 Card Draw.js' // This to the variable to use functions from?
}
v
Solution
All of these Nested if Statements can definitely be changed into one level if statements
so it would look like this when we add the nested if statements to the expression of the first.
While we are talking about shortening the if statements or making them cleaner, I assume that
are all boolean values.
If they are you can drop the
I found a little blurb here
that you could eliminate a variable (middle man),
other than these things, I am not sure that you can clean up those if/else statements, it looks like they are as clean as they can be while still doing what you want them to do.
I am sure that you could smoosh some things into functions
like this
These both look like they are doing almost the same thing. once you get the functions running well, post a follow up question and we can take a look at them and see if they are efficient.
if (commandList.indexOf('lockChat') !== -1) {
if (lockChat === true) {
bot.lockChat(room)
}
}
if (commandList.indexOf('unlockChat') !== -1) {
if (unlockChat === true) {
bot.unlockChat(room)
}
}
if (commandList.indexOf('setModerated') !== -1) {
if (setModerated === true) {
bot.setModerated(room)
}
}
if (commandList.indexOf('setUnmoderated') !== -1) {
if (setUnmoderated === true) {
bot.setUnmoderated(room)
}
}so it would look like this when we add the nested if statements to the expression of the first.
if (commandList.indexOf('lockChat') !== -1 && lockChat === true) {
bot.lockChat(room)
}
if (commandList.indexOf('unlockChat') !== -1 && unlockChat === true) {
bot.unlockChat(room)
}
if (commandList.indexOf('setModerated') !== -1 && setModerated === true) {
bot.setModerated(room)
}
if (commandList.indexOf('setUnmoderated') !== -1 && setUnmoderated === true) {
bot.setUnmoderated(room)
}While we are talking about shortening the if statements or making them cleaner, I assume that
lockChat
unlockChat
setModerated
setUnmoderated
are all boolean values.
If they are you can drop the
=== true part of those expressions.I found a little blurb here
if (receivedMessage.indexOf('BET') !== -1 && roomData.turn === player && playerData[player].canBet === true) {
var amount = extractNumbers(receivedMessage)
var localMessages = bet(amount)
for (var i = 0; i < localMessages.length; i++) {
messages.push(localMessages[i])
}
}that you could eliminate a variable (middle man),
amount you only use it once, just use extractNumbers(receivedMessage) inside bet(extractNumbers(receivedMessage)other than these things, I am not sure that you can clean up those if/else statements, it looks like they are as clean as they can be while still doing what you want them to do.
I am sure that you could smoosh some things into functions
like this
if (receivedMessage.indexOf('CHECK') !== -1 && roomData.turn === player && playerData[player].canCheck === true) { // if both?
var localMessages = check()
for (var i = 0; i < localMessages.length; i++) { // Array holding each to its required values and its outcome? Would solve both problem and allow for similar values
messages.push(localMessages[i])
}
}
if (receivedMessage.indexOf('BET') !== -1 && roomData.turn === player && playerData[player].canBet === true) {
var amount = extractNumbers(receivedMessage)
var localMessages = bet(amount)
for (var i = 0; i < localMessages.length; i++) {
messages.push(localMessages[i])
}
}These both look like they are doing almost the same thing. once you get the functions running well, post a follow up question and we can take a look at them and see if they are efficient.
Code Snippets
if (commandList.indexOf('lockChat') !== -1) {
if (lockChat === true) {
bot.lockChat(room)
}
}
if (commandList.indexOf('unlockChat') !== -1) {
if (unlockChat === true) {
bot.unlockChat(room)
}
}
if (commandList.indexOf('setModerated') !== -1) {
if (setModerated === true) {
bot.setModerated(room)
}
}
if (commandList.indexOf('setUnmoderated') !== -1) {
if (setUnmoderated === true) {
bot.setUnmoderated(room)
}
}if (commandList.indexOf('lockChat') !== -1 && lockChat === true) {
bot.lockChat(room)
}
if (commandList.indexOf('unlockChat') !== -1 && unlockChat === true) {
bot.unlockChat(room)
}
if (commandList.indexOf('setModerated') !== -1 && setModerated === true) {
bot.setModerated(room)
}
if (commandList.indexOf('setUnmoderated') !== -1 && setUnmoderated === true) {
bot.setUnmoderated(room)
}if (receivedMessage.indexOf('BET') !== -1 && roomData.turn === player && playerData[player].canBet === true) {
var amount = extractNumbers(receivedMessage)
var localMessages = bet(amount)
for (var i = 0; i < localMessages.length; i++) {
messages.push(localMessages[i])
}
}if (receivedMessage.indexOf('CHECK') !== -1 && roomData.turn === player && playerData[player].canCheck === true) { // if both?
var localMessages = check()
for (var i = 0; i < localMessages.length; i++) { // Array holding each to its required values and its outcome? Would solve both problem and allow for similar values
messages.push(localMessages[i])
}
}
if (receivedMessage.indexOf('BET') !== -1 && roomData.turn === player && playerData[player].canBet === true) {
var amount = extractNumbers(receivedMessage)
var localMessages = bet(amount)
for (var i = 0; i < localMessages.length; i++) {
messages.push(localMessages[i])
}
}Context
StackExchange Code Review Q#52570, answer score: 7
Revisions (0)
No revisions yet.