-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.js
179 lines (155 loc) · 4.98 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
var usersById = {};
var nextUserId = 0;
var usersByTwitId = {};
var express = require('express');
var ejs = require('ejs');
var app = module.exports = express.createServer()
,io = require('socket.io').listen(app)
,RedisStore = require('connect-redis')(express)
, nko = require('nko')('YIjnA93TUs1ZJBym');
io.enable('browser client minification');
io.enable('browser client etag');
io.set('log level', 1);
function getCurrentTime() {
return new Date();
}
var fs = require('fs');
//var fd = fs.openSync('./node_neko.json','w');
var neko_auto = true;
var neko_auto_json;
if(neko_auto){
neko_auto_json = JSON.parse(fs.readFileSync(__dirname + '/node_neko.json', "utf8"));
}
var neko_auto_playing = false;
var rat_auto = true;
var emit_time;
var now;
io.sockets.on('connection', function (socket) {
if(neko_auto && !neko_auto_playing){
neko_auto_playing = true;
var i = 0;
var tmout = setInterval(function(){
socket.json.broadcast.send(neko_auto_json[i]);
i++;
if(i > neko_auto_json.length){
i = 0;
}
},1000/10);
}
var rat_mintime = 5;
var rat_maxtime = 30;
if(rat_auto) {
var time_interval = (Math.random()*(rat_maxtime-rat_mintime)+rat_mintime)*1000;
setInterval(function(){
now = getCurrentTime();
if(!emit_time){
emit_time = now;
}
if((now - emit_time) > time_interval){
var offset = 100;
var rat_x = parseInt(Math.random()*(1200-offset*2))+offset;
var rat_y = parseInt(Math.random()*(600-offset*2))+offset;
var rat_json = {"Rat": [rat_x,rat_y]};
socket.json.broadcast.send(rat_json);
emit_time = now;
time_interval = (Math.random()*(rat_maxtime-rat_mintime)+rat_mintime)*1000;
}
}, 1000);
}
socket.on('connect', function(){
console.log("user connected");
});
socket.on('message', function (msg) {
if("nekoData" in msg) {
socket.json.broadcast.send(msg);
// fs.writeSync(fd, JSON.stringify(msg), encoding='utf8')
}
});
socket.on('disconnect', function () {
console.log('user disconnected');
});
});
function addUser (source, sourceUser) {
var user;
user = usersById[++nextUserId] = {id: nextUserId};
user[source] = sourceUser;
return user;
}
var everyauth = require('everyauth');
var consumerKey,consumerSecret;
if( process.env.NODE_ENV === 'production' ){
consumerKey = "lBVc0hFWaAoDCGbXVe3JRg";
consumerSecret = 'UQtjm72v9Kz3mt9AVzjqFr7KIpN2025eC5dkkZ1UUCQ';
} else {
consumerKey = 'S6rnCD5fZqx8T9LXPoISbA';
consumerSecret = '3SKffY2ZSrfd6o3jQRhGADRYF29MBzkz9i8LJWPo';
}
everyauth.twitter
.consumerKey(consumerKey)
.consumerSecret(consumerSecret)
.findOrCreateUser(function (sess, accessToken, accessSecret, twitUser) {
return usersByTwitId[twitUser.id] || (usersByTwitId[twitUser.id] = addUser('twitter', twitUser));
})
.redirectPath('/');
everyauth.everymodule.findUserById( function (id, callback) {
callback(null, usersById[id]);
});
// Configuration
app.configure(function(){
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.set('view options', { layout: false });
app.use(express.logger());
app.use(express.favicon(__dirname + "/public/images/favicon.png"));
app.use(express.bodyParser());
app.use(express.cookieParser());
app.use(express.session({secret: '20110827teamiij'
,store: new RedisStore
,cookie: { maxAge: 30 * 86400 * 1000 }}));
app.use(everyauth.middleware());
app.use(express.methodOverride());
app.use(app.router);
app.use(express.static(__dirname + '/public'));
});
app.configure('development', function(){
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});
app.configure('production', function(){
app.use(express.errorHandler());
});
// Routes
everyauth.helpExpress(app);
app.get('/', function (req, res) {
if(req.session.auth){
req.session.authed = true;
}
if(req.user && req.user.twitter && req.user.twitter.screen_name){
req.session.screen_name = req.user.twitter.screen_name;
}
// console.log(req.session);
if(req.session.authed && req.session.screen_name){
res.render('index.ejs', {locals : {screen_name: req.session.screen_name}});
} else {
res.render('index.ejs', {locals : {screen_name: null}});
}
});
app.get('/byebye', function(req, res, next){
delete(req.session.authed);
delete(req.session.screen_name);
req.session.destroy(function() {
res.redirect('/');
});
});
app.listen(process.env.NODE_ENV === 'production' ? 80 : 8080, function() {
console.log('Ready');
// if run as root, downgrade to the owner of this file
if (process.getuid() === 0)
require('fs').stat(__filename, function(err, stats) {
if (err) return console.log(err)
process.setuid(stats.uid);
});
});
console.log("Express server listening on port %d in %s mode", app.address().port, app.settings.env);
process.on('uncaughtException', function (err) {
console.log('Caught exception: ' + err);
});