-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
63 lines (57 loc) · 1.61 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
var net = require("net");
var _ = require("lodash");
global.socket = new net.Socket();
var port = global.PORT || 18632;
global.socket.connect(port, global.IP);
function getNick() {
var nick;
do {
nick = prompt("Nickname: ") || "";
} while(nick.trim().length == 0);
return nick;
}
var commands = {
"nick_ok": function () {
global.socket.write("list_lobbies\n");
},
"list_lobbies": function (params) {
global.lobbies = [];
if (+params[0] === 0) global.rewriteLobbies();
},
"lobby": function (params) {
global.lobbies.push({
id: params[0],
playerCount: params[1],
players: [],
name: params.slice(2).join(" ")
});
global.rewriteLobbies();
},
"my_lobby": function (params) {
global.currentLobby = _.find(global.lobbies, function (l) { return l.id === params[0]; });
},
"player": function (params) {
_.find(global.lobbies, function (l) { return l.id === params[0]; }).players.push(params[1]);
},
"start_game": function (params) {
otherPlayer = params[0];
document.location.href = "../game.html";
},
"error": function (params) {
alert("Error: \"" + params.join(" ") + "\"");
}
};
$(function () {
global.socket.on("connect", function () {
global.socket.on("data", function (b) {
var result = b.toString();
var data = result.trim().split("\n");
for (var i = 0; i < data.length; i++){
console.log(data[i]);
if((val = _(commands).keys().find(function (c) { return data[i].indexOf(c) === 0; })) != null)
commands[val](data[i].split(" ").slice(1));
}
});
global.socket.write("nick " + getNick() + "\n");
});
});