-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.js
203 lines (155 loc) · 5.45 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// server.js
// where your node app starts
// init project
const express = require('express');
const bodyParser = require('body-parser');
const app = express();
app.use(bodyParser.urlencoded({ extended: true }));
//import Modules
const WinningGifs = require('./assets/gifs.js')
const GamePlay = require('./modules/game_play.js')
const {Database, Helper} = require('./modules/database.js')
const colors = ['white', 'red', 'black', 'orange', 'blue']
const numbers = [1,2,3,4,5]
const hintOptions = {}
hintOptions.color = colors
hintOptions.number = numbers
const cors = require('cors')
app.use(cors());
// http://expressjs.com/en/starter/static-files.html
app.use(express.static('public'));
// init sqlite db
var fs = require('fs');
var dbFile = './.data/sqlite.db';
var exists = fs.existsSync(dbFile);
var sqlite3 = require('sqlite3').verbose();
var db = new sqlite3.Database(dbFile);
db.serialize(() => {
/*
db.run('DROP TABLE IF EXISTS Messages', error => {
if (error) {
throw error;
}
})
db.run('DROP TABLE IF EXISTS Players', error => {
if (error) {
throw error;
}
})
db.run('DROP TABLE IF EXISTS HanabiGames', error => {
if (error) {
throw error;
}
})
db.run('DROP TABLE IF EXISTS OriginalDeck', error => {
if (error) {
throw error;
}
})
db.run('DROP TABLE IF EXISTS PlayingDeck', error => {
if (error) {
throw error;
}
})
db.run('DROP TABLE IF EXISTS DiscardedCards', error => {
if (error) {
throw error;
}
})
db.run('DROP TABLE IF EXISTS PlayedCards', error => {
if (error) {
throw error;
}
})
*/
db.run('CREATE TABLE IF NOT EXISTS OriginalDeck(id INTEGER PRIMARY KEY, gameId TEXT, '+Helper.createCardString(50)+')');
db.run('CREATE TABLE IF NOT EXISTS PlayingDeck(id INTEGER PRIMARY KEY, gameId TEXT, '+Helper.createCardString(50)+')');
db.run('CREATE TABLE IF NOT EXISTS DiscardedCards(id INTEGER PRIMARY KEY, gameId TEXT, '+Helper.createCardString(25)+')');
db.run('CREATE TABLE IF NOT EXISTS PlayedCards(id INTEGER PRIMARY KEY, gameId TEXT, '+Helper.createCardString(25)+')');
db.run('CREATE TABLE IF NOT EXISTS Messages(id INTEGER PRIMARY KEY, gameId INTEGER, Messages TEXT)')
db.run('CREATE TABLE IF NOT EXISTS Players(id INTEGER PRIMARY KEY, gameId TEXT, name TEXT, active INTEGER, '+Helper.createCardString(5)+')');
db.run('CREATE TABLE IF NOT EXISTS HanabiGames (id INTEGER PRIMARY KEY, numberOfPlayers INTEGER, dateCreated DATE, score INTEGER, hintsLeft INTEGER, livesLeft INTEGER, gameProgress TEXT)');
})
// http://expressjs.com/en/starter/basic-routing.html
app.get('/', function(request, response) {
response.sendFile(__dirname + '/views/index.html');
});
app.get('/winner', function(request, response){
var gif = WinningGifs[Math.floor(Math.random()*WinningGifs.length)]
response.json(gif)
});
app.get('/newgame/:numberOfPlayers', function(request, response) {
var numberOfPlayers = request.params.numberOfPlayers
var name = request.query.name
var newGame = GamePlay.newGame(numberOfPlayers, name)
if(numberOfPlayers == null || numberOfPlayers > 5){
response.send("Error: Must have 2-5 players")
}
console.log('<<CREATING A NEW GAME>>')
Database.insert(newGame)
.then(results => response.json(results))
});
app.get('/joingame/:gameid', function(request, response){
var name = request.query.name
var gameId = request.params.gameid
var gameObject = {}
Database.get(gameId)
.then(function(gameObject){
let playerId = GamePlay.joinGame(gameObject, name)
if(playerId){
Database.joinGame(gameObject, name, playerId)
.then(gameObject => Database.get(gameObject.id))
.then(results => response.json(results))
}else{
response.send("Sorry Game is Full")
}
})
});
app.get('/game/:gameid/:name', function(request, response){
var gameId = request.params.gameid
var name = request.params.name
Database.get(gameId)
.then(function(results){
response.json(results)
});
});
//== Playing Choices ==//
app.get('/game/:gameid/:name/playcard', function(request, response){
var name = request.params.name
var cardIndex = request.query.cardIndex
var gameId = request.params.gameid
Database.get(gameId).then(function(game){
console.log("PLAY CARD GAME:", game)
GamePlay.playCard(game, cardIndex, name)
Database.update(game)
.then(results => response.json(results))
})
});
app.get('/game/:gameid/:name/discard', function(request, response){
var name = request.params.name
var cardIndex = request.query.cardIndex
var gameId = request.params.gameid
Database.get(gameId).then(function(results){
GamePlay.discard(results, cardIndex, name)
Database.update(results)
.then(results => response.json(results))
})
});
app.get('/game/:gameid/:name/givehint', function(request, response){
var name = request.params.name
console.log(name)
var hint = request.query.hint
var player = request.query.player //The Player Receiving the Hint!
var gameId = request.params.gameid
//=== Getting Data from the DataBase==//
Database.get(gameId).then(function(results){
console.log(JSON.stringify(results))
GamePlay.giveHint(results, hint, player, name)
Database.update(results)
.then(results => response.json(results))
});
});
// listen for requests :)
var listener = app.listen(process.env.PORT, function() {
console.log('Your app is listening on port ' + listener.address().port);
});