ultimate_tictactoe/game_manager.js

317 lines
8.5 KiB
JavaScript
Raw Permalink Normal View History

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-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 15:51:08 +01:00
this.use_notification = false;
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())
{
2019-03-10 13:30:18 +01:00
this.status_change_listener("" + grid.get_won_player().get_name() + " has won.", "Game Over")
this.end_game();
}
else if (grid.is_complete())
{
this.status_change_listener("Draw. Everybody looses!", "Game Over");
2019-03-07 19:14:29 +01:00
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 13:30:18 +01:00
if (this.grid.subgrids[y][x].is_draw() || this.grid.subgrids[y][x].is_won())
2019-03-10 00:29:15 +01:00
{
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)
{
2019-03-10 13:30:18 +01:00
this.status_change_listener("Congratulation, you won!", "Game Over");
2019-03-10 00:29:15 +01:00
}
else
{
2019-03-10 13:30:18 +01:00
this.status_change_listener("You lost!", "Game Over");
2019-03-10 00:29:15 +01:00
}
this.end_game();
2019-03-07 19:14:29 +01:00
}
2019-03-10 13:30:18 +01:00
else if (grid.is_complete())
{
this.status_change_listener("Draw. Everybody looses!", "Game Over");
this.end_game();
}
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();
2019-03-10 13:30:18 +01:00
if (this.grid.subgrids[y][x].is_draw()|| this.grid.subgrids[y][x].is_won())
2019-03-10 00:29:15 +01:00
{
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();
this.grid.unblock_all();
2019-03-07 19:14:29 +01:00
this.is_local_player_a = false;
2019-03-10 13:30:18 +01:00
this.status_change_listener("", "local game")
2019-03-07 19:14:29 +01:00
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)
{
2019-03-10 13:30:18 +01:00
this.status_change_listener("Connected. Waiting for matchmaking...");
2019-03-10 00:29:15 +01:00
}
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;
2019-03-10 13:30:18 +01:00
var status_title = "" + this.remote_player.get_name() + " vs. " + opponent_name;
2019-03-10 14:06:55 +01:00
this.notify("Your game against " + opponent_name + " started");
2019-03-10 00:29:15 +01:00
if (is_first_move)
{
this.grid.deactivate_all();
this.grid.unblock_all();
this.grid.player_change_listener(this.remote_player);
2019-03-10 13:30:18 +01:00
this.status_change_listener("game aginst " + opponent_name + " started. It's your turn", status_title);
2019-03-10 00:29:15 +01:00
}
else
{
this.grid.deactivate_all();
this.grid.block_all();
this.grid.player_change_listener(this.remote_opponent);
2019-03-10 13:34:04 +01:00
this.status_change_listener("game started, it's " + opponent_name + " turn", status_title);
2019-03-10 00:29:15 +01:00
}
}
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;
}
2019-03-10 13:30:18 +01:00
this.status_change_listener("game was closed by server or opponent", "Game Over");
2019-03-10 13:49:55 +01:00
2019-03-10 14:06:55 +01:00
this.notify("Game is closed");
2019-03-10 13:49:55 +01:00
this.end_game();
2019-03-10 00:29:15 +01:00
}
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;
2019-03-10 13:30:18 +01:00
this.status_change_listener("" + "it's " + next_player.get_name() + "'s turn...");
2019-03-07 19:14:29 +01:00
this.grid.player_change_listener(next_player);
}
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);
2019-03-10 14:06:55 +01:00
this.notify("it's your turn");
2019-03-10 00:29:15 +01:00
}
else
{
2019-03-10 13:30:18 +01:00
this.status_change_listener("waiting for " + this.remote_opponent.get_name() + "'s move...");
2019-03-10 00:29:15 +01:00
this.grid.player_change_listener(this.remote_opponent);
}
}
2019-03-10 14:06:55 +01:00
end_game(clicked_by_local_user = false)
{
2019-03-10 00:29:15 +01:00
if (this.game_mode == "remote")
{
this.game_server_connection.close();
2019-03-10 14:06:55 +01:00
if (clicked_by_local_user)
{
this.status_change_listener("you closed the game", "Game Over");
}
2019-03-10 00:29:15 +01:00
}
2019-03-10 13:30:18 +01:00
else if (this.game_mode == "local")
{
this.status_change_listener("game is closed", "Game Over");
}
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()
{
2019-03-10 13:30:18 +01:00
this.status_change_listener("connection error", "Game Over");
2019-03-10 00:29:15 +01:00
this.end_game();
}
2019-03-10 14:06:55 +01:00
2019-03-10 15:51:08 +01:00
activate_notifications()
{
this.use_notification = true;
}
2019-03-10 14:06:55 +01:00
notify(text) {
2019-03-10 15:51:08 +01:00
Notification.requestPermission(function(result) {
if (result === 'granted') {
navigator.serviceWorker.ready.then(function(registration) {
registration.showNotification(text, {
icon: './icon.png',
vibrate: [200, 200],
tag: "tictactoe-notification"
});
});
}
});
2019-03-10 14:06:55 +01:00
2019-03-10 15:51:08 +01:00
}
2019-03-06 12:11:43 +01:00
}