Skip to content

Commit

Permalink
refactored country getters/setters in room_module, added flag icons t…
Browse files Browse the repository at this point in the history
…o the initial room selecting dialog (just for fun)
  • Loading branch information
Dmitriy Samoilov committed Mar 31, 2011
1 parent 7548b02 commit 9f38838
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 23 deletions.
20 changes: 13 additions & 7 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,8 @@

jQuery('#log').html('');
jQuery('#player' + current_player_id + '_arrow').show().animate({left: (current_player_id == 1 ? '+=100' : '-=100')}, 4000, 'easeOutBounce', function(){ jQuery('#player' + current_player_id + '_arrow').fadeOut(3000)});


// using here workaround with 38,40 pseudo-ASCII codes (button-up/button-down) to move the shape
// these buttons generate only onkeydown/onkeyup events (which occur only once) so user should
// push the button many times in order to constantly move it (one step at a time - really SLOW)
Expand Down Expand Up @@ -201,23 +203,27 @@
socket.on('message', function(obj){
switch(obj.type) {
case 'list_of_rooms':
var img_for_room, is_room_disabled_for_select = '';
var img_for_room, is_room_disabled_for_select = '', flags = '';
for(var i = 0; i < obj.number_of_rooms; i++) {
switch(obj.rooms[i].number_of_connected_players) {
case 0:
img_for_room = '<img src="images/green_dot.png" width="12" height="12" alt="Available"/ >';
is_room_disabled_for_select = ''
is_room_disabled_for_select = '';
flags = '';
break;
case 1:
img_for_room = '<img src="images/yellow_dot.png" width="12" height="11" alt="Available"/ >';
is_room_disabled_for_select = ''
flags = '<img src="country_icons/' + obj.rooms[i].player1_country.code + '.png" width="16" height="11" title="' + obj.rooms[i].player1_country.name + '" alt="' + obj.rooms[i].player1_country.name + '"/ >';
break;
case 2:
img_for_room = '<img src="images/red_dot.png" width="12" height="11" alt="Full"/ >';
is_room_disabled_for_select = 'disabled';
flags = '<img src="country_icons/' + obj.rooms[i].player1_country.code + '.png" width="16" height="11" title="' + obj.rooms[i].player1_country.name + '" alt="' + obj.rooms[i].player1_country.name + '"/ >';
flags += ' vs <img src="country_icons/' + obj.rooms[i].player2_country.code + '.png" width="16" height="11" title="' + obj.rooms[i].player2_country.name + '" alt="' + obj.rooms[i].player2_country.name + '"/ >'
break;
}
$('#form_for_list_of_rooms').append(img_for_room+'<input type="radio" name="room_id" onclick="window.room_id=parseInt(this.value)" '+is_room_disabled_for_select+' value="' + i + '"> #' + i + " [" + obj.rooms[i].number_of_connected_players + "] players<br/ >");
$('#form_for_list_of_rooms').append(img_for_room+'<input type="radio" name="room_id" onclick="window.room_id=parseInt(this.value)" '+is_room_disabled_for_select+' value="' + i + '"> #' + i + " [" + obj.rooms[i].number_of_connected_players + "] players " + flags + "<br/ >");
}
$('#form_for_list_of_rooms').append('<input type="submit" value="Connect"/ >');
jQuery.facebox({ div: '#list_of_rooms' });
Expand All @@ -228,10 +234,10 @@
init();
// TODO refactor
if(obj.player_id == 1) {
jQuery('#player1_flag').html('<img src="country_icons/' + obj.player1_country_code + '.png" width="16" height="11" title="' + obj.player1_country_name + '" alt="' + obj.player1_country_name + '"/ >');
jQuery('#player1_flag').html('<img src="country_icons/' + obj.player1_country.code + '.png" width="16" height="11" title="' + obj.player1_country.name + '" alt="' + obj.player1_country.name + '"/ >');
} else {
jQuery('#player1_flag').html('<img src="country_icons/' + obj.player1_country_code + '.png" width="16" height="11" title="' + obj.player1_country_name + '" alt="' + obj.player1_country_name + '"/ >');
jQuery('#player2_flag').html('<img src="country_icons/' + obj.player2_country_code + '.png" width="16" height="11" title="' + obj.player2_country_name + '" alt="' + obj.player2_country_name + '"/ >');
jQuery('#player1_flag').html('<img src="country_icons/' + obj.player1_country.code + '.png" width="16" height="11" title="' + obj.player1_country.name + '" alt="' + obj.player1_country.name + '"/ >');
jQuery('#player2_flag').html('<img src="country_icons/' + obj.player2_country.code + '.png" width="16" height="11" title="' + obj.player2_country.name + '" alt="' + obj.player2_country.name + '"/ >');
}
// and wait till 2nd user connects to the game
break;
Expand All @@ -252,7 +258,7 @@
if(jQuery('#player1_notice_audio')[0].play)
jQuery('#player1_notice_audio')[0].play();
jQuery('#player2_flag').html('<img src="country_icons/' + obj.country_code + '.png" alt="' + obj.country_name + '" width="16" height="11" alt="' + obj.country_name + '"/ >');
jQuery.facebox('Round could be started: please press spacebar to start!');
jQuery.facebox('Round could be started: you could press spacebar to start!');
setTimeout(function() { jQuery.facebox.close() }, 3000); // automatically close the alert after 3 seconds
}
break;
Expand Down
20 changes: 10 additions & 10 deletions room_module.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ exports.Room = function() {
, round_started = false /* we should maintain this*/
, player_id_having_the_ball = 1 /* */
, number_of_connected_players = 0
, first_player_country_hash = []
, second_player_country_hash = []
, first_player_country = {}
, second_player_country = {}
, session_ids = {player1: null, player2: null}
;

this.get_first_player_country_hash = function() {
return first_player_country_hash;
this.get_first_player_country = function() {
return first_player_country;
}

this.debug_session_ids = function() {
Expand All @@ -22,8 +22,8 @@ exports.Room = function() {
}
}

this.get_second_player_country_hash = function() {
return second_player_country_hash;
this.get_second_player_country = function() {
return second_player_country;
}

this.is_first_player_connected = function() {
Expand All @@ -32,8 +32,8 @@ exports.Room = function() {

this.first_player_connect = function(session_id, country_code, country_name) {
first_player_connected = true;
first_player_country_hash.push(country_code);
first_player_country_hash.push(country_name);
first_player_country['code'] = country_code;
first_player_country['name'] = country_name;
number_of_connected_players++;
session_ids['player1'] = session_id;
}
Expand All @@ -45,8 +45,8 @@ exports.Room = function() {

this.second_player_connect = function(session_id, country_code, country_name) {
second_player_connected = true;
second_player_country_hash.push(country_code);
second_player_country_hash.push(country_name);
second_player_country['code'] = country_code;
second_player_country['name'] = country_name;
number_of_connected_players++;
session_ids['player2'] = session_id;
}
Expand Down
14 changes: 8 additions & 6 deletions server.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ io.on('connection', function(client){
// if client just connected send him the list with all available rooms
var list_of_rooms = {type: 'list_of_rooms', number_of_rooms: number_of_rooms, rooms: []}
for(var i = 0; i < number_of_rooms; i++) {
list_of_rooms['rooms'].push({number_of_connected_players: rooms[i].get_number_of_connected_players()})
list_of_rooms['rooms'].push({number_of_connected_players: rooms[i].get_number_of_connected_players(), player1_country: rooms[i].get_first_player_country(), player2_country: rooms[i].get_second_player_country()})
}
console.log(list_of_rooms);
//console.log(list_of_rooms);
client.send(list_of_rooms);

//client.broadcast({ announcement: client.sessionId + ' connected' });
Expand All @@ -48,12 +48,12 @@ io.on('connection', function(client){
case 'connect':
if(!selected_room.is_first_player_connected()) {
selected_room.first_player_connect(client.sessionId, message.country_code, message.country_name);
client.send({type: 'player_connected', player_id: 1, player1_country_code: message.country_code, player1_country_name: message.country_name});
client.send({type: 'player_connected', player_id: 1, player1_country: {code: message.country_code, name: message.country_name}});
} else {
selected_room.second_player_connect(client.sessionId, message.country_code, message.country_name);
var player1_country = selected_room.get_first_player_country_hash();
var player2_country = selected_room.get_second_player_country_hash();
client.send({type: 'player_connected', player_id: 2, buffer: buffer, player1_country_code: player1_country[0], player1_country_name: player1_country[1], player2_country_code: player2_country[0], player2_country_name: player2_country[1] }); // when second player has connected, 1st player could had moved up or down his default position, so show him right cordinates in buffer variable

client.send({type: 'player_connected', player_id: 2, buffer: buffer, player1_country: selected_room.get_first_player_country(), player2_country: selected_room.get_second_player_country() }); // when second player has connected, 1st player could had moved up or down his default position, so show him right cordinates in buffer variable

client.broadcast({ type: 'round_could_be_started', room_id: message.room_id, country_code: message.country_code, country_name: message.country_name});
}
for(var i = 0; i < number_of_rooms; i++) {
Expand Down Expand Up @@ -89,6 +89,8 @@ io.on('connection', function(client){
});

client.on('disconnect', function(){
// TODO add a timeOut to disconnect

// 1. find in what room the client has disconnected and update rooms var
var room_id_with_disconnected_player = null;
for(var i = 0; i < number_of_rooms; i++) {
Expand Down

0 comments on commit 9f38838

Please sign in to comment.