2019-03-06 12:11:43 +01:00
|
|
|
class GameManager
|
|
|
|
{
|
2019-03-10 00:29:15 +01:00
|
|
|
constructor(grid, game_server_connection)
|
2019-03-06 12:11:43 +01:00
|
|
|
{
|
|
|
|
this.grid = grid;
|
|
|
|
|
2019-03-10 00:29:15 +01:00
|
|
|
this.dummy_player = new Player("test_player", 0,255,0);
|
2019-03-07 13:07:51 +01:00
|
|
|
|
2019-03-10 00:29:15 +01:00
|
|
|
this.local_player_a = new Player("red player", 255,0,0);
|
|
|
|
this.local_player_b = new Player("green player", 0,255,0);
|
|
|
|
this.is_local_player_a = false;
|
2019-03-06 12:11:43 +01:00
|
|
|
|
2019-03-10 00:29:15 +01:00
|
|
|
this.remote_player = new Player("you", 0,255,0);
|
|
|
|
this.remote_opponent = new Player("remote player", 255,0,0);
|
|
|
|
this.remote_is_local_turn = false;
|
2019-03-07 17:51:19 +01:00
|
|
|
|
2019-03-06 12:11:43 +01:00
|
|
|
this.grid.player_change_listener(this.dummy_player);
|
2019-03-07 10:38:08 +01:00
|
|
|
|
|
|
|
// possible modes:
|
|
|
|
// -- none
|
|
|
|
// -- local
|
2019-03-10 00:29:15 +01:00
|
|
|
// -- remote
|
2019-03-07 10:38:08 +01:00
|
|
|
|
|
|
|
this.game_mode = "none";
|
2019-03-07 13:07:51 +01:00
|
|
|
|
2019-03-10 00:29:15 +01:00
|
|
|
this.game_server_connection = game_server_connection;
|
|
|
|
game_server_connection.set_game_manager(this);
|
|
|
|
|
2019-03-07 13:07:51 +01:00
|
|
|
this.grid.register_click_callback((i,j,k,l) => this.click_listener(i,j,k,l));
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
click_listener(sub_x, sub_y, x,y)
|
|
|
|
{
|
2019-03-07 19:14:29 +01:00
|
|
|
if (this.game_mode == "local")
|
|
|
|
{
|
|
|
|
// check whether the game is over:
|
|
|
|
if (grid.is_won())
|
|
|
|
{
|
|
|
|
this.status_change_listener("" + grid.get_won_player().get_name() + " has won.")
|
|
|
|
this.end_game();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.toggle_local_player();
|
2019-03-08 10:40:16 +01:00
|
|
|
this.grid.block_all()
|
|
|
|
this.grid.subgrids[y][x].unblock();
|
2019-03-10 00:29:15 +01:00
|
|
|
if (this.grid.subgrids[y][x].is_draw())
|
|
|
|
{
|
|
|
|
this.grid.unblock_all();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if(this.game_mode == "remote")
|
|
|
|
{
|
|
|
|
|
|
|
|
this.grid.block_all();
|
|
|
|
|
|
|
|
if (this.remote_is_local_turn)
|
|
|
|
{
|
|
|
|
this.game_server_connection.send_move(sub_x, sub_y, x, y);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (grid.is_won())
|
|
|
|
{
|
|
|
|
if (this.remote_is_local_turn)
|
|
|
|
{
|
|
|
|
this.status_change_listener("Congratulation, you won!");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.status_change_listener("Game over, you lost!");
|
|
|
|
}
|
|
|
|
this.end_game();
|
2019-03-07 19:14:29 +01:00
|
|
|
}
|
2019-03-10 00:29:15 +01:00
|
|
|
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.toggle_remote_player();
|
|
|
|
|
|
|
|
if (this.remote_is_local_turn)
|
|
|
|
{
|
|
|
|
this.grid.block_all();
|
|
|
|
|
|
|
|
this.grid.subgrids[y][x].unblock();
|
|
|
|
|
|
|
|
if (this.grid.subgrids[y][x].is_draw())
|
|
|
|
{
|
|
|
|
this.grid.unblock_all();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-07 19:14:29 +01:00
|
|
|
}
|
2019-03-07 10:38:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
register_game_mode_change_listener(func)
|
|
|
|
{
|
|
|
|
this.game_mode_change_listener = func;
|
|
|
|
}
|
|
|
|
|
2019-03-07 19:14:29 +01:00
|
|
|
register_status_change_listener(func)
|
|
|
|
{
|
|
|
|
this.status_change_listener = func;
|
|
|
|
}
|
|
|
|
|
2019-03-07 10:38:08 +01:00
|
|
|
set_game_mode(mode)
|
|
|
|
{
|
|
|
|
this.game_mode = mode;
|
|
|
|
this.game_mode_change_listener(mode);
|
|
|
|
console.log("changed gamemode to " + mode);
|
|
|
|
}
|
|
|
|
|
|
|
|
start_local_game()
|
|
|
|
{
|
|
|
|
this.set_game_mode("local");
|
2019-03-07 13:07:51 +01:00
|
|
|
this.grid.deactivate_all();
|
2019-03-07 17:51:19 +01:00
|
|
|
this.grid.unblock_all();
|
2019-03-07 19:14:29 +01:00
|
|
|
|
|
|
|
this.is_local_player_a = false;
|
|
|
|
|
|
|
|
this.toggle_local_player();
|
|
|
|
|
2019-03-07 10:38:08 +01:00
|
|
|
}
|
|
|
|
|
2019-03-10 00:29:15 +01:00
|
|
|
register_remote_game(player_name)
|
|
|
|
{
|
|
|
|
console.log("set player name to: " + player_name);
|
|
|
|
if (player_name == "")
|
|
|
|
{
|
|
|
|
this.status_change_listener("enter your name first!");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.set_game_mode("remote");
|
|
|
|
this.grid.deactivate_all();
|
|
|
|
this.grid.block_all();
|
|
|
|
|
|
|
|
this.remote_player.set_name(player_name);
|
|
|
|
|
|
|
|
this.status_change_listener("connect to server...");
|
|
|
|
|
|
|
|
this.game_server_connection.connect(() => {
|
|
|
|
this.game_server_connection.set_player(this.remote_player);
|
|
|
|
this.game_server_connection.register();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
register_response_listener(is_accepted)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (this.game_mode != "remote")
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (is_accepted)
|
|
|
|
{
|
|
|
|
this.status_change_listener("Waiting for matchmaking");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.status_change_listener("could not register for matchmaking. Maybe the name is already in use?");
|
|
|
|
this.set_game_mode("none");
|
|
|
|
this.grid.deactivate_all();
|
|
|
|
this.grid.block_all();
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
start_game_listener(opponent_name, is_first_move)
|
|
|
|
{
|
|
|
|
if (this.game_mode != "remote")
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.remote_opponent.set_name(opponent_name);
|
|
|
|
this.remote_is_local_turn = is_first_move;
|
|
|
|
|
|
|
|
if (is_first_move)
|
|
|
|
{
|
|
|
|
this.grid.deactivate_all();
|
|
|
|
this.grid.unblock_all();
|
|
|
|
|
|
|
|
this.grid.player_change_listener(this.remote_player);
|
|
|
|
|
|
|
|
this.status_change_listener("game aginst " + opponent_name + " started. It's your turn");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.grid.deactivate_all();
|
|
|
|
this.grid.block_all();
|
|
|
|
|
|
|
|
this.grid.player_change_listener(this.remote_opponent);
|
|
|
|
|
|
|
|
this.status_change_listener("game started, it's " + opponent_name + " turn");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
move_listener(sub_x, sub_y, x, y)
|
|
|
|
{
|
|
|
|
if (this.game_mode != "remote" || this.remote_is_local_turn)
|
|
|
|
{
|
|
|
|
console.log("received unexpected move");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
this.grid.block_all();
|
|
|
|
this.grid.subgrids[sub_y][sub_x].unblock();
|
|
|
|
this.grid.subgrids[sub_y][sub_x].cells[y][x].on_click();
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
end_game_listener()
|
|
|
|
{
|
|
|
|
|
|
|
|
if (this.game_mode != "remote")
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.set_game_mode("none");
|
|
|
|
this.grid.block_all();
|
|
|
|
|
|
|
|
this.status_change_listener("game was closed by server or opponent");
|
|
|
|
}
|
|
|
|
|
2019-03-07 19:14:29 +01:00
|
|
|
toggle_local_player()
|
2019-03-07 10:38:08 +01:00
|
|
|
{
|
2019-03-07 19:14:29 +01:00
|
|
|
this.is_local_player_a = !this.is_local_player_a;
|
|
|
|
var next_player = this.is_local_player_a ? this.local_player_a : this.local_player_b;
|
|
|
|
|
|
|
|
this.status_change_listener("" + "it's " + next_player.get_name() + "s turn...");
|
|
|
|
this.grid.player_change_listener(next_player);
|
2019-03-07 17:51:19 +01:00
|
|
|
}
|
|
|
|
|
2019-03-10 00:29:15 +01:00
|
|
|
toggle_remote_player()
|
|
|
|
{
|
|
|
|
this.remote_is_local_turn = !this.remote_is_local_turn;
|
|
|
|
if (this.remote_is_local_turn)
|
|
|
|
{
|
|
|
|
this.status_change_listener("your turn");
|
|
|
|
this.grid.player_change_listener(this.remote_player);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
this.status_change_listener("waiting for opponents turn...");
|
|
|
|
this.grid.player_change_listener(this.remote_opponent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-07 19:14:29 +01:00
|
|
|
end_game()
|
2019-03-07 17:51:19 +01:00
|
|
|
{
|
2019-03-10 00:29:15 +01:00
|
|
|
if (this.game_mode == "remote")
|
|
|
|
{
|
|
|
|
this.game_server_connection.close();
|
|
|
|
}
|
2019-03-07 19:14:29 +01:00
|
|
|
this.set_game_mode("none");
|
|
|
|
this.grid.block_all();
|
2019-03-06 12:11:43 +01:00
|
|
|
}
|
2019-03-10 00:29:15 +01:00
|
|
|
|
|
|
|
connection_error_listener()
|
|
|
|
{
|
|
|
|
this.status_change_listener("connection error");
|
|
|
|
this.end_game();
|
|
|
|
}
|
2019-03-06 12:11:43 +01:00
|
|
|
}
|