/
server.js
169 lines (151 loc) · 4.09 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
var net = require('net'),
http = require('http'),
url = require('url'),
fs = require('fs'),
sys = require('sys'),
formidable = require('formidable'),
express = require('express'),
io = require('socket.io'),
game = require('./game'),
util = require('./util'),
cmd = require('./cmd');
var SRC = {
html: './src/web/',
css: './src/web/',
js: './src/web/'
};
var HEAD_INFO = {
html: {encoding: 'utf8', contentType: 'text/html'},
css: {encoding: 'utf8', contentType: 'text/css'},
js: {encoding: 'utf8', contentType: 'text/javascript'},
png: {encoding: 'binary', contentType: 'images/png'},
swf: {encoding: 'binary', contentType: 'application/x-shockwave-flash'}
};
var index = fs.readFileSync(SRC.html + "index.html");
var server = http.createServer(function(req, res) {
var path = url.parse(req.url).pathname;
console.log('Request path: ' + path);
var pieces = path.split('/');
if(path === '/') {
res.writeHead(200, {'Content-Type': 'text/html'});
/* res.write(index);
res.end();
*/
fs.readFile(SRC.html + 'index.html', function(err, data) {
res.write(data);
res.end();
});
} else if(path === '/upload') {
handleUpload(req, res);
} else if(path === '/upload.html') {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write(fs.readFileSync(SRC.html + 'upload.html'));
res.end();
} else if(pieces[1] != null) {
dispatchURL(path, pieces, res);
} else {
res.end();
console.log('Request null: ' + path);
}
});
function handleUpload(req, res) {
var form = new formidable.IncomingForm(),
deck;
form.uploadDir = './upload/';
form.addListener('file', function(field, file) {
deck = file;
});
form.addListener('end', function() {
res.writeHead(200, {'content-type': 'text/html'});
res.write('<h1>Deck Upload\'d</h1>');
res.end();
openFile(deck);
});
form.parse(req);
}
function openFile(file) {
/* TODO: Write the parser, add to game.decks!
*/
sys.log(sys.inspect(file));
var rs = fs.createReadStream(file.path);
var deck = "";
rs.on('data', function(data) {
deck += data;
});
rs.on('end', function() {
parseDeck(file.filename, deck);
});
}
function parseDeck(deckName, deck) {
game.decks[deckName] = deck.split('\n').map(function(line) { return line.split(' '); });
}
function dispatchURL(path, pieces, res) {
var head = HEAD_INFO[path.split('.').pop()],
type = pieces[1];
res.writeHead(200, {'Content-Type': head.contentType});
fs.readFile('./src/web' + path, head.encoding, function(err, data) {
if(err) {
res.end();
throw err;
} else {
res.write(data, head.encoding);
res.end();
}
});
}
var app, s2;
exports.startServer = function(port) {
port = port || 80;
app = express.createServer();
app.listen(port);
s2 = io.listen(app);
s2.sockets.on('connection', function(client) {
client.toString = function() { return client.sessionId; };
client.on('message', function(message) {
console.log('Socket ' + client + ': ' + message);
var json = JSON.parse(message);
cmd.dispatch(client, json);
});
});
app.get('*', function(req, res) {
var path = url.parse(req.url).pathname;
console.log('Request path: ' + path);
var pieces = path.split('/');
if(path === '/') {
res.writeHead(200, {'Content-Type': 'text/html'});
/* res.write(index);
res.end();
*/
fs.readFile(SRC.html + 'index.html', function(err, data) {
res.write(data);
res.end();
});
} else if(path === '/upload') {
handleUpload(req, res);
} else if(path === '/upload.html') {
res.writeHead(200, {'Content-Type': 'text/html'});
res.write(fs.readFileSync(SRC.html + 'upload.html'));
res.end();
} else if(pieces[1] != null) {
dispatchURL(path, pieces, res);
} else {
res.end();
console.log('Request null: ' + path);
}
});
}
/*
exports.startServer = function(port) {
port = port || 80;
server.listen(port);
var socket = io.listen(server);
socket.on('connection', function(client) {
client.toString = function() { return client.sessionId; };
client.on('message', function(message) {
console.log('Socket ' + client + ': ' + message);
var json = JSON.parse(message);
cmd.dispatch(client, json);
});
});
}
*/