forked from eduardo-elizondo/llama
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.js
183 lines (161 loc) · 4.16 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
'use strict';
var express = require('express');
var app = express();
var port = 4000;
// var request = require('request');
// var XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
//var fs = require('fs');
//var vm = require('vm');
//include("./dataStructures.js");
//function include(path) {
// var code = fs.readFileSync(path, 'utf-8');
// vm.runInThisContext(code, path);
//}
var usersLang = {};
var messageList = {};
var messageUnseen = {};
/* Message */
var Message = function (username, from, msg) {
this.username = username;
this.from = from;
this.msg = msg;
this.time = new Date().getTime();
};
function addMessage(msg) {
var user = msg.username;
if (!messageList[user]) {
messageList[user] = [];
}
messageList[user].push(msg);
}
function getMessages(username) {
var list = messageList[username];
if (list) {
// send(list);
// for (var i=0; i<messageList.length; i++) {
// TODO: Finish sending
// }
delete messageList[username];
return list;
}
}
/* UserList */
var UserList = function () {
this.users = {};
}
/* User */
var User = function (username, from, to) {
this.username = username;
this.from = from || "en-US";
this.to = to || "zh-CN";
};
/* UserList */
var UserList = function () {
this.users = {};
}
UserList.prototype.addUser = function(username, from, to) {
var exists = this.has(username);
if (!exists) {
var user = new User(username, from, to);
this.users[username] = user;
var key = langKey(from, to);
if (!usersLang[key]) {
usersLang[key] = [];
}
usersLang[key].push(user);
console.log("Registered: " + username);
}
return !exists;
}
UserList.prototype.randomUser = function(username) {
var has = this.has(username);
if (has) {
var user = this.get(username);
var from = user.from;
var to = user.to;
var key = langKey(to, from);
var list = usersLang[key];
var pos = Math.floor(Math.random() * list.length);
var res = list[pos];
console.log("Chose: " + res.username);
return res.username;
}
}
UserList.prototype.has = function(username) {
return this.users[username] !== undefined;
}
UserList.prototype.get = function(username) {
return this.users[username];
}
function langKey(from, to) {
return from + "_" + to;
}
function randomUser(username) {
return userList.randomUser(username);
}
/* Register User */
function registerUser(username, from, to) {
return userList.addUser(username, from, to);
}
function sendMessage(msg, from, to) {
if (msg && userList.has(from) && userList.has(to)) {
var userFrom = userList.get(from);
var userTo = userList.get(to);
var message = new Message(userTo.username, userFrom.username, msg);
addMessage(message);
}
}
var userList = new UserList();
function init() {
registerUser("a", "en-US", "zh-CN");
registerUser("b", "zh-CN", "en-US");
registerUser("c", "zh-CN", "en-US");
registerUser("d", "zh-CN", "en-US");
registerUser("e", "zh-CN", "en-US");
}
init();
app.get('/send/:from/:to/:msg', function(req, res) {
res.set("Connection", "close");
res.end();
var msg = req.params.msg;
var from = req.params.from;
var to = req.params.to;
console.log("Sent " + msg + " from " + from + " to " + to);
sendMessage(msg, from, to);
});
// 104.236.28.245:4730/register/aaa
app.get('/register/:user/:from/:to', function(req, res) {
res.type('application/json');
var user = req.params.user;
var from = req.params.from || "en-US";
var to = req.params.to || "zh-CN";
if (!user || !registerUser(user, from, to)) {
res.send({status:"failed"});
}
else {
console.log("Registered: " + user);
res.send({status:"success"});
}
});
// 104.236.28.245:4730/get/Gibolt
app.get('/get/:user', function(req, res) {
res.type('application/json');
var user = req.params.user;
var msgs = getMessages(user) || [];
console.log(msgs);
res.send(msgs);
});
app.get('/random/:user', function(req, res) {
res.type('application/json');
var user = req.params.user;
var random = randomUser(user);
if (random) {
res.send({user:random});
}
else {
console.log("No user: ");
res.send({status:"failed"});
}
});
console.log("Connecting to port: " + port);
app.listen(process.env.PORT || port, '0.0.0.0');