Last active
February 9, 2019 17:20
-
-
Save carboleda/6038d545d0f8fa5dd3531d923146a0e0 to your computer and use it in GitHub Desktop.
nodejs-tictactoe-client
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const readline = require('readline'); | |
const Constants = require('../helpers/constants'); | |
module.exports = function(GameBoardScreen) { | |
function initKeypressEvents() { | |
readline.emitKeypressEvents(process.stdin); | |
process.stdin.setRawMode(true); | |
process.stdin.on('keypress', (str, key) => { | |
console.log('key', key); | |
if(key.ctrl && key.name === 'c') { | |
process.exit(0); | |
} else if(Constants.KEY_MOVING.indexOf(key.name) !== -1) { | |
console.log('Mover cursor'); | |
onMoveCursor(key.name); | |
} | |
}); | |
} | |
function onMoveCursor(direction) { | |
const cursor = GameBoardScreen.getCursor(); | |
const previousPosition = { ...cursor.currentPosition }; | |
console.log('previousPosition', previousPosition); | |
if(direction === 'left' && cursor.currentPosition.x > 0) { | |
cursor.currentPosition.x--; | |
} else if(direction === 'right' && cursor.currentPosition.x < Constants.BOARD_SIZE - 1) { | |
cursor.currentPosition.x++; | |
} else if(direction === 'up' && cursor.currentPosition.y > 0) { | |
cursor.currentPosition.y--; | |
} else if(direction === 'down' && cursor.currentPosition.y < Constants.BOARD_SIZE - 1) { | |
cursor.currentPosition.y++; | |
} | |
if(cursor.currentPosition.x !== previousPosition.x | |
|| cursor.currentPosition.y !== previousPosition.y) { | |
cursor.previousPosition = previousPosition; | |
GameBoardScreen.updateCursor(cursor); | |
} | |
GameBoardScreen.drawBoard(); | |
} | |
return { | |
initKeypressEvents | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Utilities = require('../helpers/utilities'); | |
const Constants = require('../helpers/constants'); | |
const Table = require('cli-table'); | |
const data = Utilities | |
.arrayToMatrix(Array(Constants.BOARD_SIZE * Constants.BOARD_SIZE).fill(' '), Constants.BOARD_SIZE); | |
let cursor = { | |
currentPosition: { x: 1, y: 1 }, | |
previousPosition: null, | |
previousPositionValue: null | |
} | |
// instantiate | |
var table = new Table({ | |
"chars": { | |
"top": "═", "top-mid": "╤", "top-left": "╔", "top-right": "╗" | |
, "bottom": "═", "bottom-mid": "╧", "bottom-left": "╚", "bottom-right": "╝" | |
, "left": "║", "left-mid": "╟", "mid": "─", "mid-mid": "┼" | |
, "right": "║", "right-mid": "╢", "middle": "│" | |
}, | |
"colAligns": ["middle", "middle", "middle"] | |
}); | |
function drawBoard() { | |
//console.log(data); | |
table.slice(0); | |
data.forEach(row => { | |
table.push(row); | |
}); | |
console.log(table.toString()); | |
} | |
function updateCursor(newCursor) { | |
cursor = newCursor; | |
data[cursor.currentPosition.y][cursor.currentPosition.x] = Constants.GAMER_CHAR.GAMER1_CURSOR; | |
if(cursor.previousPosition !== null) { | |
data[cursor.previousPosition.y][cursor.previousPosition.x] = ' '; | |
} | |
} | |
function getCursor() { | |
return cursor; | |
} | |
module.exports = { | |
drawBoard, | |
updateCursor, | |
getCursor | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const Utilities = require('../helpers/utilities'); | |
const matchesHistory = require('../matches-history'); | |
const readline = require('readline'); | |
const rl = readline.createInterface({ | |
input: process.stdin, | |
output: process.stdout | |
}); | |
const SEPTUP = { | |
nickName: '', | |
matchName: null | |
}; | |
module.exports = function (socket, options) { | |
socket.on('receive available matches', selectMatch); | |
const gameOptions = [ | |
{ id: '1', title: 'Create new match', action: () => { | |
socket.emit('new match', SEPTUP); | |
options.onFinish(); | |
}}, | |
{ id: '2', title: 'Join a match', action: () => { | |
socket.emit('get available matches'); | |
}}, | |
{ id: '3', title: 'View my matches', action: () => { | |
matchesHistory.showMatchesHistory(SEPTUP.nickName); | |
}} | |
]; | |
async function init() { | |
SEPTUP.nickName = await makeQuestion('Nickname: '); | |
SEPTUP.whatDoYouWant = await makeQuestion(`${SEPTUP.nickName}, what do you want to do?: `, | |
gameOptions.map(option => `${option.id}. ${option.title}`)); | |
gameOptions.find(option => option.id === SEPTUP.whatDoYouWant).action(); | |
} | |
async function selectMatch(matches) { | |
if(matches.length > 0) { | |
const matchesChooise = matches.map((match, index) => `${index + 1}. ${match}`); | |
const matchIndex = await makeQuestion('Select a match: ', matchesChooise); | |
SEPTUP.matchName = matches[+matchIndex - 1]; | |
socket.emit('join to match', SEPTUP); | |
options.onFinish(); | |
} else { | |
Utilities.clearScreen(); | |
console.log('There aren`t match availables'); | |
console.log('Press ctrl + c to exit'); | |
} | |
} | |
return { | |
init | |
}; | |
}; | |
function makeQuestion(question, choices = []) { | |
return new Promise((resolve, reject) => { | |
const hasChoices = choices.length > 0; | |
const breakLine = choices.length > 0 ? '\n' : ' '; | |
const questionAndChoices = `${choices.join('\n')}${breakLine}${question}`; | |
//Se hace la pregunta | |
function onQuestion() { | |
Utilities.clearScreen(); | |
rl.question(questionAndChoices, onAnswer); | |
} | |
function onAnswer(answer) { | |
//console.log('makeQuestion', answer); | |
//Si la pregunta NO tiene opciones o tienes la respuesta es valida | |
if(!hasChoices | |
|| (hasChoices && +answer >= 1 && +answer <= choices.length)) { | |
//Se resuelve la promesa enviando la respuesta | |
resolve(answer); | |
} else { | |
//De lo contrario se vuelve a preguntar hasta obtener una respuesta correcta | |
onQuestion(question, choices); | |
} | |
} | |
onQuestion(); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment