-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathour_server.js
More file actions
138 lines (124 loc) · 3.51 KB
/
our_server.js
File metadata and controls
138 lines (124 loc) · 3.51 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
// This is the server-side combined server for handling both the static files
// and the API
// It is maintained by the API team
// Load the Engine teams code
var engine = require('./engine/our_engine');
// Load the Real-time signalling code
var our_socket = require('./socket/our_socket');
// Set up the http server
var port = 8080;
var express = require('express');
var app = express();
var http = require('http').Server(app);
//URLs that start with /ui/ get files returned from the public subdirectory
app.get('/ui/*',function(req,res){
var dictionary = require('url').parse(req.url, true);
var pathname = dictionary.pathname.substring(3);
console.log("Static: requested: "+pathname);
res.sendFile(__dirname + '/ui'+pathname);
});
//URLs that start with /api/ get data returned from the engine module
app.get('/api/*',function(req,res){
var dictionary = require('url').parse(req.url, true);
var method = dictionary.pathname.substring(4);
console.log("API : called : "+method);
console.log(" data : "+JSON.stringify(dictionary.query));
var response;
if(method == "/api_ajax_create_game"){
if(dictionary.query.test == "true"){
response = { error:false,
errors:[],
game_id: "42",
called_with:dictionary.query
};
}
else{
response = engine.from_api_ajax_create_game(dictionary.query);
if((response === undefined) || (response == null)){
response = {
error:true,
errors:['Not implemented'],
called_with:dictionary.query
};
}
}
}
else if(method === "/api_ajax_join_game"){
if(dictionary.query.test == "true"){
response = { error:false,
errors:[],
players: ["Rachel","Austin","Stefan",null],
called_with:dictionary.query
};
}
else{
response = engine.from_api_ajax_join_game(dictionary.query);
if((response === undefined) || (response == null)){
response = {
error:true,
errors:['Not implemented'],
called_with:dictionary.query
};
}
}
}
else if(method === "/api_start_game"){
if(dictionary.query.test == "true"){
var d = new Date();
response = { error:false,
errors:[],
time_started: d.getTime(),
called_with:dictionary.query
};
}
else{
response = engine.from_api_start_game(dictionary.query);
if(response === undefined){
response = {
error:true,
errors:['Not implemented'],
called_with:dictionary.query
};
}
}
}
else if(method === "/api_ajax_get_time"){
if(dictionary.query.test == "true"){
response = { error:false,
errors:[],
time: "10101010",
called_with:dictionary.query
};
}
else{
response = engine.from_api_ajax_get_time(dictionary.query);
if(response === undefined){
response = {
error:true,
errors:['Not implemented'],
called_with:dictionary.query
};
}
}
}
else{
response = { error:true,
errors:['Unknown method call to our_server: '+method],
called_with:dictionary.query
};
}
//Generic response code
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end(JSON.stringify(response));
console.log(" returned : "+JSON.stringify(response));
});
//Basic redirect for launching if anythin else is accessed
app.get('/*',function(req,res){
res.redirect('/ui/Home.html');
});
// This sets up the websocket connections
our_socket.set_up_socket(http);
// This turns on the webserver
http.listen(port,function(){
console.log('Web/API/Socket Server running on port '+port+'.');
});