2019-03-21 12:13:46 +01:00
|
|
|
class OnlineMatchManager
|
|
|
|
{
|
|
|
|
constructor(grid, status_label, matches_container, control_container, game_server_connection, match_id, match_state, player_name)
|
|
|
|
{
|
|
|
|
this.grid = grid;
|
|
|
|
this.status_label = status_label;
|
|
|
|
|
|
|
|
this.match_id = match_id;
|
|
|
|
|
|
|
|
this.game_server_connection = game_server_connection;
|
|
|
|
|
|
|
|
|
|
|
|
this.match_state = match_state;
|
|
|
|
this.player_name = player_name
|
|
|
|
|
|
|
|
var player_a = this.match_state.player_a;
|
|
|
|
var player_b = this.match_state.player_b;
|
|
|
|
|
|
|
|
this.online_opponent = new Player(player_a == this.player_name ? player_b : player_a, 255,0,0);
|
|
|
|
this.local_player = new Player(this.player_name, 0,255,0);
|
|
|
|
|
|
|
|
// create match button in match list:
|
|
|
|
this.control_container = control_container;
|
2019-03-24 16:43:29 +01:00
|
|
|
this.matches_container = matches_container;
|
|
|
|
|
|
|
|
var tmp = matches_container.create_double_button("" + this.online_opponent.get_name(), "+")
|
|
|
|
|
|
|
|
this.match_button = tmp[1];
|
|
|
|
this.match_button_div = tmp[0];
|
|
|
|
this.match_button_option = tmp[2];
|
|
|
|
|
|
|
|
if (this.game_server_connection.is_friend(this.online_opponent.get_name()))
|
|
|
|
{
|
|
|
|
this.match_button_option.disabled = true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.match_button_option.addEventListener("click", () => {
|
|
|
|
this.game_server_connection.send_friend_request(this.online_opponent.get_name());
|
|
|
|
this.match_button_option.disabled = true;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-03-21 12:13:46 +01:00
|
|
|
this.match_button.addEventListener("click", () => this.open_match());
|
|
|
|
|
2019-03-22 22:05:30 +01:00
|
|
|
if (this.online_opponent.get_name() == this.match_state.active_player)
|
|
|
|
{
|
|
|
|
this.match_button.className = "infobar-button-red";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.match_button.className = "infobar-button-green"
|
|
|
|
}
|
2019-03-21 12:13:46 +01:00
|
|
|
|
|
|
|
this.is_closed = false;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
click_listener(sub_x, sub_y, x,y)
|
|
|
|
{
|
|
|
|
this.grid.block_all();
|
|
|
|
|
|
|
|
this.game_server_connection.send_move(sub_x, sub_y, x, y, this.match_id);
|
|
|
|
|
|
|
|
this.status_label.innerHTML = "waiting for " + this.online_opponent.get_name() + "'s move";
|
|
|
|
}
|
|
|
|
|
|
|
|
on_user_close()
|
|
|
|
{
|
|
|
|
|
|
|
|
// send end match message:
|
|
|
|
if (!this.match_state.game_over)
|
|
|
|
{
|
|
|
|
this.game_server_connection.send_end_match(this.match_id);
|
|
|
|
}
|
|
|
|
|
2019-03-24 16:43:29 +01:00
|
|
|
if (this.match_button_div != null)
|
|
|
|
{
|
|
|
|
clearInner(this.match_button_div)
|
|
|
|
this.matches_container.container.removeChild(this.match_button_div);
|
|
|
|
this.match_button_div = null;
|
|
|
|
}
|
2019-03-21 12:13:46 +01:00
|
|
|
this.status_label.innerHTML = "match is closed";
|
|
|
|
this.control_container.hide();
|
|
|
|
this.is_closed = true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
remove_match()
|
|
|
|
{
|
2019-03-24 16:43:29 +01:00
|
|
|
if (this.match_button_div != null)
|
2019-03-21 12:13:46 +01:00
|
|
|
{
|
2019-03-24 16:43:29 +01:00
|
|
|
clearInner(this.match_button_div)
|
|
|
|
this.matches_container.container.removeChild(this.match_button_div);
|
|
|
|
this.match_button_div = null;
|
2019-03-21 12:13:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
update_match_state(match_state)
|
|
|
|
{
|
|
|
|
this.match_state = match_state;
|
2019-03-22 22:05:30 +01:00
|
|
|
|
|
|
|
if (this.online_opponent.get_name() == this.match_state.active_player)
|
|
|
|
{
|
|
|
|
this.match_button.className = "infobar-button-red";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.match_button.className = "infobar-button-green"
|
|
|
|
}
|
2019-03-21 12:13:46 +01:00
|
|
|
|
2019-03-21 14:15:00 +01:00
|
|
|
if (this.match_state.active_player == this.local_player.get_name())
|
2019-03-21 12:13:46 +01:00
|
|
|
{
|
2019-03-21 14:15:00 +01:00
|
|
|
this.game_server_connection.notify("your turn against " + this.online_opponent.get_name());
|
2019-03-21 12:13:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-22 22:05:30 +01:00
|
|
|
on_focus_loose()
|
|
|
|
{
|
|
|
|
if (this.online_opponent.get_name() == this.match_state.active_player)
|
|
|
|
{
|
|
|
|
this.match_button.className = "infobar-button-red";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.match_button.className = "infobar-button-green"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-21 12:13:46 +01:00
|
|
|
open_match()
|
|
|
|
{
|
|
|
|
this.grid.register_click_callback(null);
|
|
|
|
|
|
|
|
this.grid.deactivate_all();
|
|
|
|
this.grid.unblock_all();
|
|
|
|
|
2019-03-22 22:05:30 +01:00
|
|
|
this.match_button.className = "infobar-button-active";
|
2019-03-21 12:13:46 +01:00
|
|
|
|
|
|
|
this.game_server_connection.set_active_match(this.match_id);
|
|
|
|
|
|
|
|
this.control_container.show();
|
|
|
|
|
|
|
|
var complete_field = this.match_state.complete_field;
|
|
|
|
var global_field = this.match_state.global_field;
|
|
|
|
|
|
|
|
var sub_x = null;
|
|
|
|
var sub_y = null;
|
|
|
|
var x = null;
|
|
|
|
var y = null;
|
|
|
|
|
|
|
|
if (this.match_state.last_move != null)
|
|
|
|
{
|
|
|
|
sub_x = this.match_state.last_move.sub_x;
|
|
|
|
sub_y = this.match_state.last_move.sub_y;
|
|
|
|
x = this.match_state.last_move.x;
|
|
|
|
y = this.match_state.last_move.y;
|
|
|
|
}
|
|
|
|
|
|
|
|
var game_over = this.match_state.game_over;
|
|
|
|
var player_won = this.match_state.player_won;
|
|
|
|
var current_player_name = this.match_state.active_player;
|
|
|
|
|
|
|
|
var player_a = this.match_state.player_a;
|
|
|
|
var player_b = this.match_state.player_b;
|
|
|
|
|
|
|
|
console.log(game_over);
|
|
|
|
console.log(current_player);
|
|
|
|
|
|
|
|
var FIELD_EMPTY = 0
|
|
|
|
var FIELD_USER = player_a == this.player_name ? 1 : 2;
|
|
|
|
var FIELD_OPPONENT = player_a == this.player_name ? 2 : 1;
|
|
|
|
var FIELD_DRAW = 3
|
|
|
|
|
|
|
|
this.online_opponent = new Player(player_a == this.player_name ? player_b : player_a, 255,0,0);
|
|
|
|
this.local_player = new Player(this.player_name, 0,255,0);
|
|
|
|
|
|
|
|
var i,j;
|
|
|
|
for(j = 0; j < 9; j++)
|
|
|
|
{
|
|
|
|
for(i = 0; i < 9; i++)
|
|
|
|
{
|
|
|
|
var si = Math.floor(i / 3);
|
|
|
|
var sj = Math.floor(j / 3);
|
|
|
|
if (complete_field[j][i] == FIELD_USER)
|
|
|
|
{
|
|
|
|
this.grid.subgrids[sj][si].player_change_listener(this.local_player);
|
|
|
|
this.grid.subgrids[sj][si].cells[j % 3][i % 3].on_click();
|
|
|
|
}
|
|
|
|
if (complete_field[j][i] == FIELD_OPPONENT)
|
|
|
|
{
|
|
|
|
this.grid.subgrids[sj][si].player_change_listener(this.online_opponent);
|
|
|
|
this.grid.subgrids[sj][si].cells[j % 3][i % 3].on_click();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.grid.block_all();
|
|
|
|
this.grid.register_click_callback((i,j,k,l) => this.click_listener(i,j,k,l));
|
|
|
|
|
|
|
|
if (game_over && player_won != null)
|
|
|
|
{
|
|
|
|
if (player_won == this.player_name)
|
|
|
|
{
|
|
|
|
this.status_label.innerHTML = "Congratulation, you won!";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.status_label.innerHTML = "Game over, you lost!";
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else if(game_over)
|
|
|
|
{
|
2019-03-22 22:05:30 +01:00
|
|
|
if (this.grid.is_complete())
|
|
|
|
{
|
|
|
|
this.status_label.innerHTML = "Draw. Everyone looses!";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.status_label.innerHTML = "Game was closed by server or opponent";
|
|
|
|
}
|
2019-03-21 12:13:46 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
var current_player = this.player_name == current_player_name ? this.local_player : this.online_opponent;
|
|
|
|
this.grid.player_change_listener(current_player);
|
|
|
|
|
|
|
|
|
|
|
|
if (this.player_name == current_player_name)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (this.match_state.last_move != null)
|
|
|
|
{
|
|
|
|
if (this.grid.subgrids[y][x].is_won() || this.grid.subgrids[y][x].is_draw())
|
|
|
|
{
|
|
|
|
this.grid.unblock_all_non_completed();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.grid.unblock(x,y);
|
|
|
|
}
|
|
|
|
this.status_label.innerHTML = "It's your turn!";
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.grid.unblock_all();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.status_label.innerHTML = "waiting for " + this.online_opponent.get_name() + "'s move";
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|