You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
66 lines
1.8 KiB
66 lines
1.8 KiB
// Sends Updates to socket when players change |
|
const wrtColor = require('../common/wrtColor') |
|
|
|
function mapRemoveLastActivityProp (player) { |
|
// Make this specific by props to remove |
|
let {lastActivity, ...other} = player |
|
// return { |
|
// id: player.id, |
|
// username: player.username, |
|
// gameid: player.gameid |
|
// } |
|
return other |
|
} |
|
|
|
function continuousEmitPlayers (state,io) { |
|
let previousPlayersRelevant = [] |
|
let timerId = setTimeout(function tick() { |
|
// get players excluding the "lastActivity" because it is constantly updating |
|
playersRelevant = state.players.map(mapRemoveLastActivityProp) |
|
// console.log(playersRelevant) |
|
// convert to string for comparison |
|
if ((JSON.stringify(playersRelevant)) !== (JSON.stringify(previousPlayersRelevant))) { |
|
// if changes happen, emit to all sockets |
|
// wrtColor.blue('change to Players. Push to Sockets') |
|
io.emit('playersUpdate', playersRelevant) |
|
} |
|
previousPlayersRelevant = playersRelevant |
|
timerId = setTimeout(tick, 1000) |
|
}, 1000) |
|
} |
|
|
|
function socketGetPlayers (state,socket) { |
|
playersRelevant = state.players.map(mapRemoveLastActivityProp) |
|
socket.emit('playersUpdate', playersRelevant) |
|
} |
|
|
|
function checkValidPlayerName(players,name) { |
|
|
|
let response = { |
|
valid: true, |
|
reason: "" |
|
} |
|
|
|
// Returns True/False |
|
// 15 character limit |
|
// No special characters |
|
// name not in use already |
|
|
|
|
|
} |
|
|
|
function socketRegisterPlayer (io,state,data) { |
|
wrtColor.warn('socketRegisterPlayer') |
|
console.log(data) |
|
//console.log(state.players) |
|
io.emit('msg',{poop:"salad"}) |
|
let checkResult = checkValidPlayerName(state.players, name) |
|
console.log(checkResult) |
|
// add them to state.players |
|
} |
|
|
|
module.exports = { |
|
continuousEmitPlayers: continuousEmitPlayers, |
|
socketGetPlayers: socketGetPlayers, |
|
socketRegisterPlayer: socketRegisterPlayer, |
|
} |