2019-03-21 12:13:46 +01:00
|
|
|
class WebsocketConnection
|
|
|
|
{
|
2019-03-29 12:47:49 +01:00
|
|
|
constructor(ip, port, grid, info_func, err_func,matches_container, control_container, end_button, logout_container, search_container, login_callback_func, error_callback_func)
|
2019-03-21 12:13:46 +01:00
|
|
|
{
|
|
|
|
this.ip = ip;
|
|
|
|
this.port = port;
|
|
|
|
this.session_id = null;
|
|
|
|
this.player = new Player("player", 128, 0,0);
|
|
|
|
this.socket = null;
|
|
|
|
this.registered = false;
|
|
|
|
|
2019-04-04 14:58:29 +02:00
|
|
|
this.temporary_session = false;
|
|
|
|
|
2019-03-29 12:47:49 +01:00
|
|
|
this.info_func = info_func;
|
|
|
|
this.err_func = err_func;
|
|
|
|
|
2019-03-21 12:13:46 +01:00
|
|
|
this.grid = grid;
|
|
|
|
this.matches_container = matches_container;
|
|
|
|
this.control_container = control_container;
|
2019-03-29 12:47:49 +01:00
|
|
|
this.logout_container = logout_container;
|
2019-03-24 16:43:29 +01:00
|
|
|
this.search_container = search_container;
|
2019-03-21 12:13:46 +01:00
|
|
|
|
2019-03-29 12:47:49 +01:00
|
|
|
this.end_button = end_button;
|
|
|
|
|
2019-04-01 16:17:20 +02:00
|
|
|
this.l_matches_container = null;
|
|
|
|
|
2019-03-21 12:13:46 +01:00
|
|
|
this.active_match = null;
|
|
|
|
|
|
|
|
this.connected = false;
|
|
|
|
|
|
|
|
this.login_callback_func = login_callback_func;
|
|
|
|
|
|
|
|
this.openmatches = {};
|
2019-03-21 15:41:38 +01:00
|
|
|
this.error_callback_func = error_callback_func;
|
2019-03-21 12:13:46 +01:00
|
|
|
|
2019-03-21 15:41:38 +01:00
|
|
|
this.closed_by_user = false;
|
2019-03-21 12:13:46 +01:00
|
|
|
|
|
|
|
|
2019-03-24 16:43:29 +01:00
|
|
|
this.friends = [];
|
2019-03-29 12:47:49 +01:00
|
|
|
this.elos = [];
|
2019-03-24 16:43:29 +01:00
|
|
|
|
2019-03-29 15:11:25 +01:00
|
|
|
this.top_names = [];
|
|
|
|
this.top_elos = [];
|
|
|
|
|
2019-03-24 16:43:29 +01:00
|
|
|
this.friend_name_divs = [];
|
|
|
|
|
2019-04-04 14:58:29 +02:00
|
|
|
|
|
|
|
|
2019-03-24 16:43:29 +01:00
|
|
|
matches_container.hide();
|
2019-03-21 12:13:46 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
set_player(p)
|
|
|
|
{
|
|
|
|
this.player = p;
|
|
|
|
}
|
|
|
|
|
|
|
|
set_game_manager(gm)
|
|
|
|
{
|
|
|
|
this.game_manager = gm;
|
|
|
|
}
|
|
|
|
|
|
|
|
is_registered()
|
|
|
|
{
|
|
|
|
return this.registered;
|
|
|
|
}
|
|
|
|
|
|
|
|
is_connected()
|
|
|
|
{
|
|
|
|
return this.connected;
|
|
|
|
}
|
|
|
|
|
2019-03-24 16:43:29 +01:00
|
|
|
is_friend(friend)
|
|
|
|
{
|
|
|
|
return this.friends.includes(friend);
|
|
|
|
}
|
|
|
|
|
2019-03-21 12:13:46 +01:00
|
|
|
on_open(username, pw)
|
|
|
|
{
|
2019-03-24 19:10:46 +01:00
|
|
|
|
2019-03-21 12:13:46 +01:00
|
|
|
this.connected = true;
|
|
|
|
this.login(username, pw)
|
|
|
|
}
|
|
|
|
|
2019-03-21 15:41:38 +01:00
|
|
|
on_reopen(session_id)
|
|
|
|
{
|
2019-03-24 19:10:46 +01:00
|
|
|
|
2019-03-21 15:41:38 +01:00
|
|
|
this.connected = true;
|
|
|
|
this.relogin(session_id);
|
|
|
|
}
|
|
|
|
|
2019-03-22 08:59:41 +01:00
|
|
|
on_close(login_failed=false)
|
2019-03-21 12:13:46 +01:00
|
|
|
{
|
2019-03-24 19:10:46 +01:00
|
|
|
for (var key in this.openmatches)
|
|
|
|
{
|
|
|
|
this.openmatches[key].remove_match();
|
|
|
|
}
|
|
|
|
this.openmatches = {};
|
2019-04-01 16:17:20 +02:00
|
|
|
// remove match info label if exists:
|
|
|
|
if (this.l_matches_container != null)
|
|
|
|
{
|
|
|
|
this.matches_container.container.removeChild(this.l_matches_container);
|
|
|
|
clearInner(this.l_matches_container);
|
|
|
|
this.l_matches_container = null;
|
|
|
|
}
|
2019-03-24 19:10:46 +01:00
|
|
|
|
|
|
|
// remove complete friend list:
|
|
|
|
var n = this.friend_name_divs.length;
|
|
|
|
|
|
|
|
var i;
|
|
|
|
|
|
|
|
for (i = 0; i < n; i++)
|
|
|
|
{
|
|
|
|
clearInner(this.friend_name_divs[i]);
|
|
|
|
this.search_container.container.removeChild(this.friend_name_divs[i]);
|
|
|
|
this.friend_name_divs[i] = null;
|
|
|
|
}
|
|
|
|
this.friend_name_divs = [];
|
|
|
|
this.friends = [];
|
|
|
|
|
2019-03-21 20:12:39 +01:00
|
|
|
var login_failed = !this.registered;
|
2019-03-21 12:13:46 +01:00
|
|
|
this.registered = false;
|
|
|
|
this.connected = false;
|
2019-03-21 20:12:39 +01:00
|
|
|
if (!this.closed_by_user && !login_failed)
|
2019-03-21 15:41:38 +01:00
|
|
|
{
|
2019-03-29 12:47:49 +01:00
|
|
|
this.err_func("connection to server closed");
|
2019-03-21 15:41:38 +01:00
|
|
|
this.error_callback_func();
|
|
|
|
}
|
|
|
|
|
2019-03-21 12:13:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
on_error()
|
|
|
|
{
|
2019-03-24 19:10:46 +01:00
|
|
|
for (var key in this.openmatches)
|
|
|
|
{
|
|
|
|
this.openmatches[key].remove_match();
|
|
|
|
}
|
|
|
|
this.openmatches = {};
|
2019-04-01 16:17:20 +02:00
|
|
|
// remove match info label if exists:
|
|
|
|
if (this.l_matches_container != null)
|
|
|
|
{
|
|
|
|
this.matches_container.container.removeChild(this.l_matches_container);
|
|
|
|
clearInner(this.l_matches_container);
|
|
|
|
this.l_matches_container = null;
|
|
|
|
}
|
2019-03-24 19:10:46 +01:00
|
|
|
|
|
|
|
// remove complete friend list:
|
|
|
|
var n = this.friend_name_divs.length;
|
|
|
|
|
|
|
|
var i;
|
|
|
|
|
|
|
|
for (i = 0; i < n; i++)
|
|
|
|
{
|
|
|
|
clearInner(this.friend_name_divs[i]);
|
|
|
|
this.search_container.container.removeChild(this.friend_name_divs[i]);
|
|
|
|
this.friend_name_divs[i] = null;
|
|
|
|
}
|
|
|
|
this.friend_name_divs = [];
|
|
|
|
this.friends = [];
|
|
|
|
|
2019-03-21 12:13:46 +01:00
|
|
|
console.log("error in websocket connection");
|
|
|
|
this.registered = false;
|
|
|
|
this.connected = false;
|
2019-03-21 15:41:38 +01:00
|
|
|
if (!this.closed_by_user)
|
|
|
|
{
|
2019-03-29 12:47:49 +01:00
|
|
|
this.err_func("connection to server lost");
|
2019-03-21 15:41:38 +01:00
|
|
|
this.error_callback_func();
|
|
|
|
}
|
2019-03-21 12:13:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
on_message(event)
|
|
|
|
{
|
|
|
|
var json_msg = event.data;
|
|
|
|
var msg = JSON.parse(json_msg);
|
|
|
|
|
|
|
|
if (!msg.hasOwnProperty("type") || !msg.hasOwnProperty("data"))
|
|
|
|
{
|
|
|
|
console.log("received wrong formatted message");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log("raw_msg: " + json_msg);
|
|
|
|
|
|
|
|
switch(msg.type)
|
|
|
|
{
|
|
|
|
case "login_response":
|
|
|
|
this.on_register_response(msg.data);
|
|
|
|
break;
|
|
|
|
|
2019-03-21 15:41:38 +01:00
|
|
|
case "reconnect_response":
|
|
|
|
this.on_reconnect_response(msg.data);
|
2019-03-21 12:13:46 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
case "match_update":
|
|
|
|
this.on_match_update(msg.data);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "match_request_response":
|
|
|
|
this.on_match_request_response(msg.data);
|
|
|
|
break;
|
2019-03-24 16:43:29 +01:00
|
|
|
|
|
|
|
case "friend_request_response":
|
|
|
|
this.on_friend_request_response(msg.data);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "unfriend_request_response":
|
|
|
|
this.on_unfriend_request_response(msg.data);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "friends_update":
|
|
|
|
this.on_friends_update(msg.data);
|
|
|
|
break;
|
2019-03-29 12:47:49 +01:00
|
|
|
|
|
|
|
case "elo_update":
|
|
|
|
this.on_elo_update(msg.data);
|
|
|
|
break;
|
|
|
|
|
2019-03-21 12:13:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
set_active_match(id)
|
|
|
|
{
|
2019-03-22 22:05:30 +01:00
|
|
|
for (var key in this.openmatches)
|
|
|
|
{
|
|
|
|
if (key == id)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
this.openmatches[key].on_focus_loose();
|
|
|
|
}
|
2019-03-21 12:13:46 +01:00
|
|
|
this.active_match = id;
|
|
|
|
}
|
|
|
|
|
|
|
|
on_close_click()
|
|
|
|
{
|
|
|
|
if (this.active_match != null && (this.active_match in this.openmatches))
|
|
|
|
{
|
|
|
|
this.openmatches[this.active_match].on_user_close();
|
|
|
|
|
|
|
|
|
2019-03-21 15:41:38 +01:00
|
|
|
}
|
2019-03-21 12:13:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
on_match_update(data)
|
|
|
|
{
|
|
|
|
var id = data.id
|
|
|
|
var match_state = data.match_state;
|
2019-04-01 16:17:20 +02:00
|
|
|
var revoke_time = data.revoke_time;
|
2019-04-04 14:58:29 +02:00
|
|
|
var ranked = data.ranked;
|
2019-03-21 12:13:46 +01:00
|
|
|
|
|
|
|
if (match_state == null)
|
|
|
|
{
|
|
|
|
// checking whether we can delete our dict object
|
|
|
|
if (id in this.openmatches)
|
|
|
|
{
|
|
|
|
delete this.openmatches[id];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (id in this.openmatches)
|
|
|
|
{
|
2019-04-01 16:17:20 +02:00
|
|
|
this.openmatches[id].update_match_state(match_state, revoke_time)
|
2019-03-21 12:13:46 +01:00
|
|
|
if (this.active_match == id)
|
|
|
|
{
|
|
|
|
this.openmatches[id].open_match();
|
|
|
|
}
|
2019-03-29 12:47:49 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
this.matches_container.blink(theme_color_highlight);
|
|
|
|
}
|
2019-03-21 12:13:46 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
if (!match_state.game_over)
|
|
|
|
{
|
2019-04-01 16:17:20 +02:00
|
|
|
|
|
|
|
// remove match info label if exists:
|
|
|
|
if (this.l_matches_container != null)
|
|
|
|
{
|
|
|
|
this.matches_container.container.removeChild(this.l_matches_container);
|
|
|
|
clearInner(this.l_matches_container);
|
|
|
|
this.l_matches_container = null;
|
|
|
|
}
|
|
|
|
|
2019-04-04 14:58:29 +02:00
|
|
|
this.openmatches[id] = new OnlineMatchManager(this.grid, this.info_func, this.matches_container, this.control_container, this.end_button, this, id, match_state, revoke_time, this.player.get_name(), ranked);
|
2019-03-21 16:06:18 +01:00
|
|
|
if (match_state.last_move == null)
|
|
|
|
{
|
2019-03-29 20:41:15 +01:00
|
|
|
this.notify("new Game against " + encodeHTML(this.openmatches[id].online_opponent.get_name()));
|
2019-03-21 16:06:18 +01:00
|
|
|
}
|
2019-03-29 12:47:49 +01:00
|
|
|
this.matches_container.blink(theme_color_highlight);
|
2019-04-01 16:17:20 +02:00
|
|
|
|
|
|
|
// create new info:
|
|
|
|
this.l_matches_container = this.matches_container.create_label("click on name to open,<br>click '+' to add as friend");
|
2019-03-21 12:13:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
on_register_response(data)
|
|
|
|
{
|
|
|
|
if (data.success)
|
|
|
|
{
|
|
|
|
this.registered = true;
|
|
|
|
this.session_id = data.id;
|
2019-04-04 14:58:29 +02:00
|
|
|
this.temporary_session = !data.registered;
|
2019-03-21 12:13:46 +01:00
|
|
|
this.login_callback_func();
|
2019-03-29 12:47:49 +01:00
|
|
|
this.info_func(data.msg);
|
|
|
|
return;
|
2019-03-21 12:13:46 +01:00
|
|
|
}
|
2019-03-29 12:47:49 +01:00
|
|
|
this.err_func(data.msg);
|
2019-03-21 12:13:46 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2019-03-21 15:41:38 +01:00
|
|
|
on_reconnect_response(data)
|
2019-03-21 12:13:46 +01:00
|
|
|
{
|
2019-03-21 15:41:38 +01:00
|
|
|
if (data.success)
|
|
|
|
{
|
|
|
|
this.registered = true;
|
|
|
|
this.session_id = data.id;
|
|
|
|
this.player.set_name(data.user);
|
|
|
|
this.login_callback_func();
|
2019-03-29 12:47:49 +01:00
|
|
|
this.info_func(data.msg);
|
|
|
|
return;
|
2019-03-21 15:41:38 +01:00
|
|
|
}
|
2019-03-29 12:47:49 +01:00
|
|
|
this.err_func(data.msg);
|
2019-03-21 12:13:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
on_match_request_response(data)
|
|
|
|
{
|
|
|
|
if (data.success)
|
|
|
|
{
|
2019-03-29 12:47:49 +01:00
|
|
|
this.info_func("match request sent");
|
2019-03-21 12:13:46 +01:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2019-03-29 20:41:15 +01:00
|
|
|
this.err_func("could not send request: " + encodeHTML(data.msg));
|
2019-03-21 12:13:46 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-24 16:43:29 +01:00
|
|
|
on_friend_request_response(data)
|
|
|
|
{
|
2019-03-29 12:47:49 +01:00
|
|
|
if (data.success)
|
|
|
|
{
|
|
|
|
this.info_func(data.msg);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.err_func(data.msg);
|
2019-03-24 16:43:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
on_unfriend_request_response(data)
|
|
|
|
{
|
2019-03-29 12:47:49 +01:00
|
|
|
if (data.success)
|
|
|
|
{
|
|
|
|
this.info_func(data.msg);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.err_func(data.msg);
|
2019-03-24 16:43:29 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
on_friends_update(data)
|
|
|
|
{
|
|
|
|
this.friends = data.friends;
|
2019-03-29 12:47:49 +01:00
|
|
|
this.elos = data.elos;
|
2019-03-24 16:43:29 +01:00
|
|
|
|
|
|
|
// remove complete friend list:
|
|
|
|
var n = this.friend_name_divs.length;
|
|
|
|
|
|
|
|
var i;
|
|
|
|
|
|
|
|
for (i = 0; i < n; i++)
|
|
|
|
{
|
|
|
|
clearInner(this.friend_name_divs[i]);
|
|
|
|
this.search_container.container.removeChild(this.friend_name_divs[i]);
|
|
|
|
this.friend_name_divs[i] = null;
|
|
|
|
}
|
|
|
|
this.friend_name_divs = [];
|
|
|
|
// rebuild friend list:
|
|
|
|
n = this.friends.length;
|
|
|
|
|
|
|
|
for (i = 0; i < n; i++)
|
|
|
|
{
|
2019-03-29 12:47:49 +01:00
|
|
|
var display_name = this.friends[i];
|
|
|
|
if (display_name.length > 10)
|
|
|
|
{
|
|
|
|
display_name = display_name.substr(0,8) + "…";
|
|
|
|
}
|
|
|
|
|
2019-03-29 20:41:15 +01:00
|
|
|
var tmp = this.search_container.create_double_button("" + encodeHTML(display_name) + "<sup><span style=\"font-size: 0.5em\">" + encodeHTML(""+this.elos[i]) + "</span></sup>", "-");
|
2019-03-24 18:55:06 +01:00
|
|
|
|
|
|
|
tmp[1].name = this.friends[i];
|
|
|
|
tmp[2].name = this.friends[i];
|
|
|
|
tmp[1].connection = this;
|
|
|
|
tmp[2].connection = this;
|
|
|
|
|
2019-03-24 16:43:29 +01:00
|
|
|
|
2019-03-24 18:55:06 +01:00
|
|
|
tmp[1].addEventListener("click", function() {
|
|
|
|
this.connection.send_match_request(this.name);
|
2019-03-24 16:43:29 +01:00
|
|
|
});
|
|
|
|
|
2019-03-24 18:55:06 +01:00
|
|
|
tmp[2].addEventListener("click", function (){
|
|
|
|
this.connection.send_unfriend_request(this.name);
|
2019-03-24 16:43:29 +01:00
|
|
|
});
|
|
|
|
|
|
|
|
this.friend_name_divs.push(tmp[0])
|
|
|
|
}
|
|
|
|
}
|
2019-03-21 12:13:46 +01:00
|
|
|
|
2019-03-29 12:47:49 +01:00
|
|
|
on_elo_update(data)
|
|
|
|
{
|
|
|
|
console.log("received elo update: " + data.elo);
|
2019-04-04 14:58:29 +02:00
|
|
|
if (data.elo != null && data.rank != null)
|
|
|
|
{
|
|
|
|
this.logout_container.update_head("logged in as: " + encodeHTML(connection.player.get_name()) + "<br>Score: " + encodeHTML("" + data.elo) + "<br>Rank: " + encodeHTML(""+data.rank));
|
|
|
|
}
|
2019-03-29 15:11:25 +01:00
|
|
|
this.top_names = data.top_names;
|
|
|
|
this.top_elos = data.top_elos;
|
2019-03-29 12:47:49 +01:00
|
|
|
this.logout_container.blink(theme_color_highlight);
|
2019-03-29 15:11:25 +01:00
|
|
|
|
|
|
|
|
2019-03-29 12:47:49 +01:00
|
|
|
// TODO
|
|
|
|
}
|
|
|
|
|
2019-03-24 18:55:06 +01:00
|
|
|
|
|
|
|
|
2019-03-21 12:13:46 +01:00
|
|
|
connect(username, pw)
|
|
|
|
{
|
2019-03-21 15:41:38 +01:00
|
|
|
this.socket = new WebSocket(server_protocol + this.ip + ":" + this.port);
|
2019-03-21 12:13:46 +01:00
|
|
|
this.socket.onmessage = (e => this.on_message(e));
|
|
|
|
this.socket.onopen = (() => this.on_open(username, pw));
|
|
|
|
this.socket.onerror = (() => this.on_error());
|
|
|
|
this.socket.onclose = (() => this.on_close());
|
|
|
|
}
|
|
|
|
|
2019-03-21 15:41:38 +01:00
|
|
|
reconnect(session_id)
|
|
|
|
{
|
2019-03-22 22:05:30 +01:00
|
|
|
for (var key in this.openmatches)
|
|
|
|
{
|
|
|
|
this.openmatches[key].remove_match();
|
|
|
|
}
|
|
|
|
this.openmatches = {};
|
2019-03-21 15:41:38 +01:00
|
|
|
this.socket = new WebSocket(server_protocol + this.ip + ":" + this.port);
|
|
|
|
this.socket.onmessage = (e => this.on_message(e));
|
|
|
|
this.socket.onopen = (() => this.on_reopen(session_id));
|
|
|
|
this.socket.onerror = (() => this.on_error());
|
|
|
|
this.socket.onclose = (() => this.on_close());
|
|
|
|
}
|
|
|
|
|
2019-03-21 12:13:46 +01:00
|
|
|
send_move(sub_x, sub_y, x, y, match_id)
|
|
|
|
{
|
|
|
|
var msg_object = {
|
|
|
|
type: "move",
|
|
|
|
data: {
|
|
|
|
id: match_id,
|
|
|
|
sub_x: "" + sub_x,
|
|
|
|
sub_y: "" + sub_y,
|
|
|
|
x: "" + x,
|
|
|
|
y: "" + y
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
this.socket.send(JSON.stringify(msg_object));
|
|
|
|
}
|
|
|
|
|
|
|
|
send_end_match(match_id)
|
|
|
|
{
|
|
|
|
var msg_object = {
|
|
|
|
type: "end_match",
|
|
|
|
data: {
|
|
|
|
id: match_id,
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
this.socket.send(JSON.stringify(msg_object));
|
2019-03-21 14:15:00 +01:00
|
|
|
|
2019-03-21 12:13:46 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
send_match_request(player_name)
|
|
|
|
{
|
|
|
|
var msg_object = {
|
|
|
|
type: "match_request",
|
|
|
|
data: {
|
|
|
|
player: player_name
|
|
|
|
}
|
|
|
|
};
|
|
|
|
this.socket.send(JSON.stringify(msg_object));
|
|
|
|
}
|
|
|
|
|
2019-03-24 16:43:29 +01:00
|
|
|
send_friend_request(friend_name)
|
|
|
|
{
|
|
|
|
var msg_object = {
|
|
|
|
type: "friend_request",
|
|
|
|
data: {
|
|
|
|
user: friend_name
|
|
|
|
}
|
|
|
|
};
|
|
|
|
this.socket.send(JSON.stringify(msg_object));
|
|
|
|
}
|
|
|
|
|
|
|
|
send_unfriend_request(friend_name)
|
|
|
|
{
|
|
|
|
var msg_object = {
|
|
|
|
type: "unfriend_request",
|
|
|
|
data: {
|
|
|
|
user: friend_name
|
|
|
|
}
|
|
|
|
};
|
|
|
|
this.socket.send(JSON.stringify(msg_object));
|
|
|
|
}
|
|
|
|
|
2019-03-21 12:13:46 +01:00
|
|
|
login(username, pw)
|
|
|
|
{
|
|
|
|
this.player.set_name(username);
|
|
|
|
// register for game queue
|
|
|
|
var msg_object = {
|
|
|
|
type: "login",
|
|
|
|
data: {
|
|
|
|
name: this.player.get_name(),
|
|
|
|
pw: pw
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
this.socket.send(JSON.stringify(msg_object));
|
|
|
|
}
|
|
|
|
|
2019-03-21 15:41:38 +01:00
|
|
|
relogin(session_id)
|
|
|
|
{
|
2019-03-24 16:43:29 +01:00
|
|
|
for (var key in this.openmatches)
|
|
|
|
{
|
|
|
|
this.openmatches[key].remove_match();
|
|
|
|
}
|
2019-03-21 15:41:38 +01:00
|
|
|
// register for game queue
|
|
|
|
var msg_object = {
|
|
|
|
type: "reconnect",
|
|
|
|
data: {
|
|
|
|
id: session_id
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
this.socket.send(JSON.stringify(msg_object));
|
|
|
|
}
|
|
|
|
|
2019-03-21 12:13:46 +01:00
|
|
|
send_disconnect()
|
|
|
|
{
|
|
|
|
if (!this.is_connected)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var msg_object = {
|
|
|
|
type: "end_game",
|
|
|
|
data: {
|
|
|
|
msg: ""
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
this.socket.send(JSON.stringify(msg_object));
|
|
|
|
}
|
|
|
|
|
|
|
|
close()
|
|
|
|
{
|
2019-03-29 12:47:49 +01:00
|
|
|
this.info_func("logged out");
|
2019-03-21 15:41:38 +01:00
|
|
|
this.closed_by_user = true;
|
2019-03-21 12:13:46 +01:00
|
|
|
this.socket.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
notify(text) {
|
2019-03-21 17:17:42 +01:00
|
|
|
|
|
|
|
if (document.hasFocus())
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2019-03-21 12:13:46 +01:00
|
|
|
|
|
|
|
Notification.requestPermission(function(result) {
|
|
|
|
if (result === 'granted') {
|
2019-03-31 15:55:54 +02:00
|
|
|
if (navigator.serviceWorker == undefined)
|
|
|
|
{
|
|
|
|
var notification = new Notification(text);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-03-21 12:13:46 +01:00
|
|
|
navigator.serviceWorker.ready.then(function(registration) {
|
|
|
|
registration.showNotification(text, {
|
|
|
|
icon: './icon.png',
|
|
|
|
vibrate: [200, 200],
|
|
|
|
tag: "tictactoe-notification"
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|