-
Notifications
You must be signed in to change notification settings - Fork 2
/
server.js
199 lines (180 loc) · 5.94 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/*global console*/
function servStart() {
router.apply(this, arguments);
}
require("./commons");
require("./choreographer");
// HTTP Server
var //connect = require("connect"),
fs = require("fs"),
web = (typeof connect !== "undefined" ?
connect(connect.cookieParser(),
connect.session({
secret: 'keyboard cat',
cookie: {maxAge: 60000}
}),
connect.favicon(),
servStart) :
require("http").createServer(servStart)),
newMajiang = require("./majiang").newMajiang,
$ = require("./domizer").$,
then,
server;
server = (function (router) {
var that = {},
games = [],
request,
response,
next;
function handleParams(callBack) {
return function (req, res, inNext) {
request = req;
response = res;
next = inNext;
callBack.apply(that, arguments);
};
}
$.gameLink = function gameLink(game, state, states) {
return state === states.FINISHED ?
"Game Finished" :
$.div($.linkTo(joinSlashes(["/majiang", game, "stats"]),
"Game ", game),
" - ", state,
" [", game.players.getLength(), " player(s)]",
" - ",
$.linkTo(joinSlashes([
"/majiang",
game,
(state === states.OPEN ?
"join" : "specjoin")
]), (state === states.OPEN ?
"JOIN" : "SPECTATE")));
};
routes = {
"leave": function handleLeave(game) {
delete games[game];
},
"games": function handleGames() {
writePlainResponse(response, games.join("\n"));
},
"majiang": function handleMajiang() {
var game = newMajiang(that);
game.add("join");
games[game] = game;
writePlainResponse(response, [
"Game Created, ID:" + game,
"You can join with /majiang/" + game + "/join"
]);
},
"home": function handleHome() {
var html =
$.standardHead("Welcome to SiChuan Mahjong",
$.body((function () {
if (games.length) {
return games.reduce(function (res, game) {
if (game !== undefined) {
res.push($.gameLink(game, game.getState(), game.STATES));
}
return res;
}, []).join("");
} else {
return $.div("No games");
}
}()),
$.br(),
$.linkTo("/majiang", "New Game"),
$.javascript("/client.js")));
// console.log(html);
writeHtmlResponse(response, html);
}
};
function remove(route) {
router.get.remove(["", route]);
}
function add(route) {
router.get.add(["", route],
handleParams(routes[route]));
}
function on(event, req, res, inNext, args) {
request = req;
response = res;
next = inNext;
routes[event].apply(this, args);
}
(function construct() {
add("home");
add("majiang");
add("games");
}());
return that.merge({
// properties
"games": games,
// accessors
// methods
// routing
"on": on
});
}(router));
// static js and css files server
router.get.add(/^\/([^\.]+)\.([^\.]+$)/,
function handleFiles(req, res, /*next, */file, ext) {
try {
var type;
switch (ext) {
case "js":
type = "application/x-javascript";
break;
case "css":
type = "text/css";
break;
default:
throw "not authorized";
}
fs.readFile("./static/" + file + "." + ext, function (err, content) {
try {
if (err) {
throw err;
}
if (content) {
// console.log("GET", file + "." + ext + ": OK");
writeResponse(res, type, content);
}
} catch (e) {
// console.log("GET", file + "." + ext + ": Not authorized");
router.notFound(req, res);
}
});
} catch (e) {
// console.log("GET", file + "." + ext + ": Not authorized");
router.notFound(req, res);
}
});
router.get.add("/",
function handleUI(req, res, next) {
var html =
$.standardHead("Welcome to SiChuan Mahjong",
$.body(
$.form({id: "responseForm"},
$.input({
id: "newGameButton",
type: "button",
value: "New Game"
}, ""),
$.input({
id: "getGamesButton",
type: "button",
value: "List Games"
}, ""),
$.select({
id: "responseSelect",
value: "Select one..."
}, "")),
$.textarea({
id: "serverResponse",
style: "width:90%; height:90%;"
}, ""),
$.javascript({}, "/client.js")
));
writeHtmlResponse(res, html);
});
web.listen(8080);