-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.js
More file actions
184 lines (147 loc) · 4.48 KB
/
app.js
File metadata and controls
184 lines (147 loc) · 4.48 KB
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
// Noun and adj list are fixtures for now
var nouns = ['speakers', 'oranges', 'sweaters', 'lamps', 'writing a blog', 'semicolons'];
var adjectives = ['loud', 'fun', 'nerdy'];
// Dependencies
var express = require('express'),
io = require('socket.io'),
ar = require('couch-ar'),
jade = require('jade');
// Connect to database and load models from /models dir
ar.init({
dbName: 'apples',
root: __dirname + '/models'
}, function(db){
// Ensure enough time for models to load
setTimeout(1000);
});
// Setup express
var app = express.createServer(express.bodyParser());
app.set('view engine', 'jade');
app.set('view options', {layout: false});
app.use(express.static(__dirname + '/public'));
app.use(express.logger());
app.get('/', function(req, res){
res.render('index');
});
app.listen(3009);
// Helper functions
function event_obj(event_name, data_obj) {
return { event: event_name,
data: data_obj };
}
var adj, win_noun; // Adjective that is in play
// Assign user a noun card
var deal_noun = function(user_id, client) {
var word = nouns[Math.floor(Math.random()*nouns.length)];
var new_noun = ar.Noun.create({word: word, user_id: user_id});
new_noun.save(function(err, db_res){
// Notify client of each card after save
client.send(event_obj('new_card', {
id: new_noun.id,
word: new_noun.word,
status: 'in_hand'
}));
});
};
// Assign user 7 cards
var deal_nouns = function(user_id, client) {
for(var ndx = 0; ndx < 7; ndx++) {
deal_noun(user_id, client);
}
};
// Setup Socket.IO
var socket = io.listen(app);
// Handle socket events
socket.on('connection', function(client) {
var user = ar.User.create({
name: client.sessionId,
status: 'active'
});
// Add user to database, send relavent data to client
user.save(function(err, db_res) {
client.send(event_obj('init', {
id: user.id,
}));
// Deal 7 cards
deal_nouns(user.id, client);
// Notify this client of other active clients
ar.User.findAllByStatus('active', function(users) {
users.forEach(function(other_user){
client.send(event_obj('add_user', {
id: other_user.id,
name: other_user.name
}));
});
});
// Notify other clients of this client
client.broadcast(event_obj('add_user', {
id: user.id,
name: user.name
}));
});
// Update client status in db, alert other clients
client.on('disconnect', function() {
user.status = 'disconnected';
user.save(function(err, db_res) {
socket.broadcast(event_obj('remove_user', user.id));
});
});
// Respond to updates from client
client.on('message', function(msg) {
// User picks a noun card
if(msg.event == 'play_card') {
// Update noun card
ar.Noun.findById(msg.data, function(card) {
card.status = 'played';
card.adj_id = adj.id;
card.save(function(err,res){ });
client.send(event_obj('remove_card', card.id));
// Place card in pile
socket.broadcast(event_obj('add_to_pile', {id: card.id, word: card.word}));
});
deal_noun(user.id, client);
// Judge picks winning card
} else if(msg.event == 'pick_card') {
console.log('picked: ' + msg.data);
win_noun = ar.Noun.findById(msg.data, function(noun){
adj.win_noun_id = msg.data;
adj.save(function(err,res){
socket.broadcast(event_obj('winning_word', noun.word));
});
});
} else { console.log(msg) }
});
});
// Game processes
var playInt, judgeInt;
function start_judge_timer(){
judgeInt = setInterval(function(){
// Create an adjective(game) object - choose adj and judge
var word = adjectives[Math.floor(Math.random()*adjectives.length)];
ar.User.findAllByStatus('active', function(users) {
var user = users[Math.floor(Math.random()*users.length)];
adj = ar.Adj.create({
word: word,
judge_id: user.id,
status: 'playing'
});
adj.save(function(err, db_res){ });
socket.broadcast(event_obj('mode_playing',{
judge_name: user.name,
judge_id: user.id,
word: word
}));
});
start_play_timer();
clearInterval(judgeInt);
}, 10000);
}
function start_play_timer(){
playInt = setInterval(function(){
console.log("MODE CHANGED TO JUDGING");
socket.broadcast(event_obj('mode_judging',''));
start_judge_timer();
clearInterval(playInt);
}, 10000);
}
start_play_timer();