|
|
|
@ -24,161 +24,160 @@ function socketConnect(project, name = playerName){ |
|
|
|
|
connecting = true; |
|
|
|
|
|
|
|
|
|
let urlQueries = '?game=' + project.name + '&name=' + name; |
|
|
|
|
$.get('data/settings/get_port.php', port => { |
|
|
|
|
let url = 'https://' + location.hostname + ':' + port + urlQueries; |
|
|
|
|
let url = 'https://' + location.hostname + urlQueries; |
|
|
|
|
|
|
|
|
|
socket = io.connect(url); |
|
|
|
|
socket.on('connect', () => { |
|
|
|
|
console.log('Connected to ', url); |
|
|
|
|
$('#start_feedback, #give_feedback').attr('disabled', false); |
|
|
|
|
socket.on('disconnect', () => { |
|
|
|
|
connecting = false; |
|
|
|
|
console.log('Disconnected from ' + url); |
|
|
|
|
$('#lobby, #room, #game_room').filter(':visible').fadeOut(menuesFadeTime, () => { |
|
|
|
|
$('#main').fadeIn(menuesFadeTime); |
|
|
|
|
$('#start_feedback, #give_feedback').attr('disabled', 'disabled'); |
|
|
|
|
}); |
|
|
|
|
}); |
|
|
|
|
socket.emit('room-list'); |
|
|
|
|
socket.on('room-list', (roomList) => { |
|
|
|
|
roomList = roomList.filter(r => r.game === project.name); |
|
|
|
|
console.log('Received room list: ', roomList); |
|
|
|
|
$('#room_listings').html(''); |
|
|
|
|
for (let room of roomList){ |
|
|
|
|
let button = $('<button></button>'); |
|
|
|
|
button.html(room.name); |
|
|
|
|
button.addClass('room_listing'); |
|
|
|
|
button.on('click', () => socket.emit('join-lobby', room.id)); |
|
|
|
|
$('#room_listings').append(button); |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
$('#main').fadeOut(menuesFadeTime, () => { |
|
|
|
|
$('#lobby > h1').html('Welcome, ' + name + '!'); |
|
|
|
|
$('#lobby > input').val(name + "'s room"); |
|
|
|
|
$('#lobby').fadeIn(menuesFadeTime); |
|
|
|
|
roomTyped($('#lobby > input')); |
|
|
|
|
}); |
|
|
|
|
socket.on('created-lobby', (room) => { |
|
|
|
|
console.log('You successfully created a room!'); |
|
|
|
|
$('#create_room').attr('disabled', false); |
|
|
|
|
$('#lobby').fadeOut(menuesFadeTime, () => { |
|
|
|
|
roomSetup(room); |
|
|
|
|
}); |
|
|
|
|
}); |
|
|
|
|
socket.on('client-list', (room, clients) => { |
|
|
|
|
console.log('Received client list: ', clients); |
|
|
|
|
|
|
|
|
|
let players = clients.filter(c => c.isPlayer); |
|
|
|
|
let spectators = clients.filter(c => c.isSpectator); |
|
|
|
|
let leader = players[0]; |
|
|
|
|
socket = io.connect(url, { |
|
|
|
|
path: "/chainreact/" |
|
|
|
|
}); |
|
|
|
|
socket.on('connect', () => { |
|
|
|
|
console.log('Connected to ', url); |
|
|
|
|
$('#start_feedback, #give_feedback').attr('disabled', false); |
|
|
|
|
socket.on('disconnect', () => { |
|
|
|
|
connecting = false; |
|
|
|
|
console.log('Disconnected from ' + url); |
|
|
|
|
$('#lobby, #room, #game_room').filter(':visible').fadeOut(menuesFadeTime, () => { |
|
|
|
|
$('#main').fadeIn(menuesFadeTime); |
|
|
|
|
$('#start_feedback, #give_feedback').attr('disabled', 'disabled'); |
|
|
|
|
}); |
|
|
|
|
}); |
|
|
|
|
socket.emit('room-list'); |
|
|
|
|
socket.on('room-list', (roomList) => { |
|
|
|
|
roomList = roomList.filter(r => r.game === project.name); |
|
|
|
|
console.log('Received room list: ', roomList); |
|
|
|
|
$('#room_listings').html(''); |
|
|
|
|
for (let room of roomList){ |
|
|
|
|
let button = $('<button></button>'); |
|
|
|
|
button.html(room.name); |
|
|
|
|
button.addClass('room_listing'); |
|
|
|
|
button.on('click', () => socket.emit('join-lobby', room.id)); |
|
|
|
|
$('#room_listings').append(button); |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
$('#main').fadeOut(menuesFadeTime, () => { |
|
|
|
|
$('#lobby > h1').html('Welcome, ' + name + '!'); |
|
|
|
|
$('#lobby > input').val(name + "'s room"); |
|
|
|
|
$('#lobby').fadeIn(menuesFadeTime); |
|
|
|
|
roomTyped($('#lobby > input')); |
|
|
|
|
}); |
|
|
|
|
socket.on('created-lobby', (room) => { |
|
|
|
|
console.log('You successfully created a room!'); |
|
|
|
|
$('#create_room').attr('disabled', false); |
|
|
|
|
$('#lobby').fadeOut(menuesFadeTime, () => { |
|
|
|
|
roomSetup(room); |
|
|
|
|
}); |
|
|
|
|
}); |
|
|
|
|
socket.on('client-list', (room, clients) => { |
|
|
|
|
console.log('Received client list: ', clients); |
|
|
|
|
|
|
|
|
|
if (game) |
|
|
|
|
game.players = players; |
|
|
|
|
let players = clients.filter(c => c.isPlayer); |
|
|
|
|
let spectators = clients.filter(c => c.isSpectator); |
|
|
|
|
let leader = players[0]; |
|
|
|
|
|
|
|
|
|
setInterfacePlayers(players); |
|
|
|
|
setInterfaceSpectators(spectators); |
|
|
|
|
if (game) |
|
|
|
|
game.players = players; |
|
|
|
|
|
|
|
|
|
function setInterfacePlayers(players){ |
|
|
|
|
$('#player_listings').html(''); |
|
|
|
|
for (let c of players){ |
|
|
|
|
let div = $('<div></div>'); |
|
|
|
|
let span = $('<span></span>'); |
|
|
|
|
let ready = $('<button></button>'); |
|
|
|
|
div.addClass('player_listing'); |
|
|
|
|
ready.addClass('player_ready'); |
|
|
|
|
ready.on('click', () => { |
|
|
|
|
if (c.id !== socket.id) |
|
|
|
|
return; |
|
|
|
|
let isReady = $(ready).data('isReady'); |
|
|
|
|
$(ready).data('isReady', !isReady); |
|
|
|
|
socket.emit('set-ready', !isReady); |
|
|
|
|
}); |
|
|
|
|
span.html(c.name); |
|
|
|
|
ready.css('background-color', c.isReady ? 'green' : 'transparent'); |
|
|
|
|
ready.data('isReady', c.isReady); |
|
|
|
|
div.append(span, ready); |
|
|
|
|
$('#player_listings').append(div); |
|
|
|
|
} |
|
|
|
|
setInterfacePlayers(players); |
|
|
|
|
setInterfaceSpectators(spectators); |
|
|
|
|
|
|
|
|
|
let allReady = players.find(c => !c.isReady) == null; |
|
|
|
|
let startClickable = allReady && players.length <= 4 && (players.length > 1 || productionMode); |
|
|
|
|
if (startClickable && players[0]) |
|
|
|
|
startClickable = players[0].id === socket.id; |
|
|
|
|
$('#start_button').attr('disabled', startClickable ? false : 'disabled'); |
|
|
|
|
function setInterfacePlayers(players){ |
|
|
|
|
$('#player_listings').html(''); |
|
|
|
|
for (let c of players){ |
|
|
|
|
let div = $('<div></div>'); |
|
|
|
|
let span = $('<span></span>'); |
|
|
|
|
let ready = $('<button></button>'); |
|
|
|
|
div.addClass('player_listing'); |
|
|
|
|
ready.addClass('player_ready'); |
|
|
|
|
ready.on('click', () => { |
|
|
|
|
if (c.id !== socket.id) |
|
|
|
|
return; |
|
|
|
|
let isReady = $(ready).data('isReady'); |
|
|
|
|
$(ready).data('isReady', !isReady); |
|
|
|
|
socket.emit('set-ready', !isReady); |
|
|
|
|
}); |
|
|
|
|
span.html(c.name); |
|
|
|
|
ready.css('background-color', c.isReady ? 'green' : 'transparent'); |
|
|
|
|
ready.data('isReady', c.isReady); |
|
|
|
|
div.append(span, ready); |
|
|
|
|
$('#player_listings').append(div); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (players.find(p => p.id === socket.id)){ |
|
|
|
|
$('#player_list .join_type').hide(); |
|
|
|
|
} else { |
|
|
|
|
$('#player_list .join_type').show(); |
|
|
|
|
} |
|
|
|
|
let allReady = players.find(c => !c.isReady) == null; |
|
|
|
|
let startClickable = allReady && players.length <= 4 && (players.length > 1 || productionMode); |
|
|
|
|
if (startClickable && players[0]) |
|
|
|
|
startClickable = players[0].id === socket.id; |
|
|
|
|
$('#start_button').attr('disabled', startClickable ? false : 'disabled'); |
|
|
|
|
|
|
|
|
|
refreshGamePlayers(players); |
|
|
|
|
} |
|
|
|
|
if (players.find(p => p.id === socket.id)){ |
|
|
|
|
$('#player_list .join_type').hide(); |
|
|
|
|
} else { |
|
|
|
|
$('#player_list .join_type').show(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
function setInterfaceSpectators(spectators){ |
|
|
|
|
$('.spectator_listings').html(''); |
|
|
|
|
spectators.forEach(s => { |
|
|
|
|
let div = $('<div></div>'); |
|
|
|
|
div.addClass('spectator_listing'); |
|
|
|
|
div.html(s.name); |
|
|
|
|
refreshGamePlayers(players); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
$('.spectator_listings').append(div); |
|
|
|
|
}); |
|
|
|
|
if (spectators.find(p => p.id === socket.id)){ |
|
|
|
|
$('.spectator_list .join_type').hide(); |
|
|
|
|
} else { |
|
|
|
|
$('.spectator_list .join_type').show(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
function setInterfaceSpectators(spectators){ |
|
|
|
|
$('.spectator_listings').html(''); |
|
|
|
|
spectators.forEach(s => { |
|
|
|
|
let div = $('<div></div>'); |
|
|
|
|
div.addClass('spectator_listing'); |
|
|
|
|
div.html(s.name); |
|
|
|
|
|
|
|
|
|
if (leader){ |
|
|
|
|
if (leader.id === socket.id) |
|
|
|
|
readGameSettings(); |
|
|
|
|
else |
|
|
|
|
disableSettings(); |
|
|
|
|
} else |
|
|
|
|
disableSettings(); |
|
|
|
|
$('.spectator_listings').append(div); |
|
|
|
|
}); |
|
|
|
|
if (spectators.find(p => p.id === socket.id)){ |
|
|
|
|
$('.spectator_list .join_type').hide(); |
|
|
|
|
} else { |
|
|
|
|
$('.spectator_list .join_type').show(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
function disableSettings(){ |
|
|
|
|
$('.setting').attr('disabled', 'disabled'); |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
socket.on('member-joined', (room, joinedId, joinedName) => { |
|
|
|
|
if (joinedId === socket.id){ |
|
|
|
|
console.log('You joined a room!'); |
|
|
|
|
$('#lobby').fadeOut(menuesFadeTime, () => { |
|
|
|
|
if (room.hasStarted){ |
|
|
|
|
$('#game_room').fadeIn(menuesFadeTime); |
|
|
|
|
} else { |
|
|
|
|
roomSetup(room); |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
else |
|
|
|
|
console.log('A player joined your room: ' + joinedName + ' (' + joinedId + ')'); |
|
|
|
|
}); |
|
|
|
|
socket.on('member-left', (room, leftId, leftName) => { |
|
|
|
|
console.log('A player left your room: ' + leftName + ' (' + leftId + ')'); |
|
|
|
|
if (game) |
|
|
|
|
game.onPlayerLeft(leftId); |
|
|
|
|
}); |
|
|
|
|
socket.on('start-game', (room, seed) => startGame(room, seed)); |
|
|
|
|
socket.on('start-spectate', (room, data, hues, turnId) => spectateGame(room, data, hues, turnId)); |
|
|
|
|
socket.on('game-settings', (room, settings) => applySettings(settings)); |
|
|
|
|
socket.on('left-lobby', () => { |
|
|
|
|
console.log('You left a room!'); |
|
|
|
|
$('#leave_room').get(0).disabled = false; |
|
|
|
|
game = null; |
|
|
|
|
socket.removeAllListeners('player-colors'); |
|
|
|
|
socket.removeAllListeners('current-turn'); |
|
|
|
|
socket.removeAllListeners('set-slot'); |
|
|
|
|
$('#room, #game_room').filter(':visible').fadeOut(menuesFadeTime, () => { |
|
|
|
|
$('#lobby').fadeIn(menuesFadeTime); |
|
|
|
|
}); |
|
|
|
|
}); |
|
|
|
|
}); |
|
|
|
|
}); |
|
|
|
|
if (leader){ |
|
|
|
|
if (leader.id === socket.id) |
|
|
|
|
readGameSettings(); |
|
|
|
|
else |
|
|
|
|
disableSettings(); |
|
|
|
|
} else |
|
|
|
|
disableSettings(); |
|
|
|
|
|
|
|
|
|
function disableSettings(){ |
|
|
|
|
$('.setting').attr('disabled', 'disabled'); |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
socket.on('member-joined', (room, joinedId, joinedName) => { |
|
|
|
|
if (joinedId === socket.id){ |
|
|
|
|
console.log('You joined a room!'); |
|
|
|
|
$('#lobby').fadeOut(menuesFadeTime, () => { |
|
|
|
|
if (room.hasStarted){ |
|
|
|
|
$('#game_room').fadeIn(menuesFadeTime); |
|
|
|
|
} else { |
|
|
|
|
roomSetup(room); |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
else |
|
|
|
|
console.log('A player joined your room: ' + joinedName + ' (' + joinedId + ')'); |
|
|
|
|
}); |
|
|
|
|
socket.on('member-left', (room, leftId, leftName) => { |
|
|
|
|
console.log('A player left your room: ' + leftName + ' (' + leftId + ')'); |
|
|
|
|
if (game) |
|
|
|
|
game.onPlayerLeft(leftId); |
|
|
|
|
}); |
|
|
|
|
socket.on('start-game', (room, seed) => startGame(room, seed)); |
|
|
|
|
socket.on('start-spectate', (room, data, hues, turnId) => spectateGame(room, data, hues, turnId)); |
|
|
|
|
socket.on('game-settings', (room, settings) => applySettings(settings)); |
|
|
|
|
socket.on('left-lobby', () => { |
|
|
|
|
console.log('You left a room!'); |
|
|
|
|
$('#leave_room').get(0).disabled = false; |
|
|
|
|
game = null; |
|
|
|
|
socket.removeAllListeners('player-colors'); |
|
|
|
|
socket.removeAllListeners('current-turn'); |
|
|
|
|
socket.removeAllListeners('set-slot'); |
|
|
|
|
$('#room, #game_room').filter(':visible').fadeOut(menuesFadeTime, () => { |
|
|
|
|
$('#lobby').fadeIn(menuesFadeTime); |
|
|
|
|
}); |
|
|
|
|
}); |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
function applySettings(settings){ |
|
|
|
|