This repository has been archived by the owner on Jun 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
bingo-server.js
176 lines (149 loc) · 4.6 KB
/
bingo-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
function randomUUID() {
// http://ajaxian.com/archives/uuid-generator-in-javascript
var s = [], itoh = '0123456789ABCDEF';
// Make array of random hex digits. The UUID only has 32 digits in it, but we
// allocate an extra items to make room for the '-'s we'll be inserting.
for (var i = 0; i <36; i++) s[i] = Math.floor(Math.random()*0x10);
// Conform to RFC-4122, section 4.4
s[14] = 4; // Set 4 high bits of time_high field to version
s[19] = (s[19] & 0x3) | 0x8; // Specify 2 high bits of clock sequence
// Convert to hex chars
for (var i = 0; i <36; i++) s[i] = itoh[s[i]];
// Insert '-'s
s[8] = s[13] = s[18] = s[23] = '-';
return s.join('');
}
var jqtpl = require('jqtpl'),
express = require('express'),
app = express.createServer(),
fs = require('fs'),
io = require('socket.io'),
db = require('dirty'),
games = db('games.db');
wordlists = db('wordlists.db');
app.use(express.bodyDecoder());
app.use(express.staticProvider(__dirname + '/public'));
app.set( "view engine", "jqtpl" );
app.register( ".jqtpl", require( "jqtpl" ) );
// (1)
app.get('/games', function(req,res){
var list = [];
games.forEach(function(k,v){
w = wordlists.get(v.wordlist);
list.push({
'key': k,
'title': w.title,
'creator': v.creator,
'num_curr_playing': 0})
})
res.send(list);
});
// (2)
app.get('/wordlists', function(req,res){
var list = [];
wordlists.forEach(function(k, v) {
list.push({'key': k, 'title': v.title, 'creator': v.creator, 'words': v.words})
});
res.send(list);
});
// (3)
app.post('/new_wordlist', function(req,res){
var title = req.body.title,
creator = req.body.creator,
words = req.body.textarea.split("\n"),
uuid = randomUUID();
wordlists.set(uuid, {'title': title, 'creator': creator, 'words': words});
res.send(uuid + '\n');
});
// (4)
app.get('/play/:gameuid', function(req,res){
var game = req.params.gameuid,
words = wordlists.get(games.get(game)["wordlist"]).words;
shuffle = function(o){ //v1.0
for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
return o;
};
var words = shuffle(words);
var rows = [words.slice(0,4), words.slice(4,8), words.slice(8,12), words.slice(12,16)];
fs.readFile(__dirname + '/views/partials/bingo.jqtpl', "binary", function(error, file) {
var parshal = jqtpl.tmpl(file, {
"a": "hello", "rows": rows,
}, {
rowletter: function(j) {
return String.fromCharCode(97+j);
}
});
res.send(parshal)
});
})
// (5)
app.post('/new_game', function(req,res){
var wordlist = req.body.wordlist,
creator = req.body.creator,
uuid = randomUUID();
games.set(uuid, {'wordlist': wordlist, 'creator':creator});
res.send(uuid + '\n');
})
//app.get('/list', function(req, res) {
//var active = [];
//db.forEach(function(key,val) {
//if (key == "game") {
//active.push({title: val.title, creator:val.creator});
//}
//})
//res.send(active);
//});
//app.get('/listwords', function(req,res) {
//var wordlist = [];
//db.forEach(function(key, val){
//if (key == "wordlist") {
//wordlist.push({val.title})
//}
//}
//});
//app.post('/newgame', function(req,res) {
//var wordlist = req.body.wordlist,
//creator = req.body.creator;
//db.set("game", {
//wordlist:"wordlist",
//creator: "creator"
//});
//res.send({"a": 1});
//});
//app.post('/newwordlist', function(req, res) {
//var szavak = req.body.textarea.split("\n");
//db.set("wordlist", {
//"title": req.body.title,
//"creator": req.body.creator,
//"words": szavak
//})
//res.send({"a": 1});
//});
//app.get('/play/:game', function(req, res) {
//var game = db.get(req.params.game);
//});
//});
// új urlek
//
// régi
// NYUGODT URLEK
// /list
// -> get: [{name: "", creator: "", active: true,num_currently_playing},...]
// /new
// -> post: creator & name & words = [, , ,]
// /play/:name
// -> get: words=[] // 16, randomised from list
app.listen(8124);
var socket = io.listen(app);
var megnyert_jatekok = [];
socket.on('connection', function(client) {
client.broadcast({announcement: client.sessionId + ' connected, weee'});
client.on('message', function(data) {
console.log(data);
if (data.nyertem && megnyert_jatekok.indexOf(data.game) == -1) {
client.broadcast({ "event": "win", "game": data.game, "nick": data.nick});
megnyert_jatekok.push(data.game);
games.rm(data.game);
}
});
});