-
Notifications
You must be signed in to change notification settings - Fork 3
/
server.js
121 lines (104 loc) · 3.04 KB
/
server.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
var sys = require("sys"),
crypto = require("crypto"),
// npm dependencies
connect = require("connect"),
express = require("express"),
io = require("socket.io"),
// local
gm = require(__dirname+"/lib/gamemanager");
var dreadnode = express.createServer(),
port = parseInt(process.env.PORT) || 80;
// Express Configuration
dreadnode.configure(function() {
dreadnode.set("views", __dirname+"/views");
dreadnode.use(connect.bodyDecoder());
dreadnode.use(connect.methodOverride());
dreadnode.use(dreadnode.router);
dreadnode.use(connect.staticProvider(__dirname+"/public"));
});
// Express Routes
dreadnode.get("/game", function(req, res) {
res.render("game.jade", {
locals: {
title: "HMS Dreadnode"
}
});
});
// Game Manager
var manager = gm.createGameManager();
// Utility Objects
var response = {
type: "response",
msg: "Your message was garbage"
};
var messenger = {
shot : function(clientId, msg) {
return msg;
}
};
var dispatch = {
chat : function() {
response.msg = "Received your chat message";
},
username : function(client, message) {
if(message.msg === "no") {
response.type = "no";
response.msg = "NOOOOOOOOOOO!";
} else {
var username = message.msg;
var couldAddUser = manager.addUser(client, username);
if (couldAddUser["success"]) {
response.type = "auth";
response.status = "success";
response.msg = "Welcome "+username;
} else {
response.type = "auth";
response.status = "ufail";
response.msg = couldAddUser["reason"];
}
}
client.send(JSON.stringify(response));
},
ready : function(client, message) {
manager.placeShips(client, message.msg);
},
shot : function(client, message) {
manager.fireShot(client, message.msg);
},
gravatar : function(client, message) {
response.type = "gravatar";
response.status = "success";
response.msg = crypto.createHash("md5").update(message.msg).digest("hex");
client.send(JSON.stringify(response));
}
};
// Socket.IO
var io = io.listen(dreadnode);
io.on("connection", function(client) {
console.log("Socket.IO Client Connected");
//client.broadcast("New user connected. Welcome.");
client.on("message", function(message) {
console.log("Incoming message: " + sys.inspect(message));
try {
message = JSON.parse(message);
if ("type" in message) {
dispatch[message.type](client, message)
}
} catch (e) {
console.log("Couldn't parse message: " + sys.inspect(message));
console.log(sys.inspect(e));
}
});
client.on("disconnect", function() {
console.log("Socket.IO Client Disconnected");
manager.removeUser(client);
});
manager.on("winner", function (connection) {
var game = this;
console.log(sys.inspect(this, connection));
userlist = { type: "userlist", msg: manager.getUsers() };
client.send(JSON.stringify(userlist));
});
});
dreadnode.listen(port);
console.log("Dreadnode server listening on :"+port);