forked from SubratThakur/sequence
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
151 lines (145 loc) · 4.86 KB
/
index.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
const express = require('express');
const cors = require('cors');
const morgan = require('morgan');
const bodyParser = require('body-parser');
const app = express();
const port = process.env.PORT || 3001;
const server = app.listen(port, () => console.log("Server Started!"));
const io = require('socket.io')(server);
const game = require("./game");
const client = require("./db_connection");
const {board, colors, chest, chance} = require("./monopoly");
const ObjectId = require('mongodb').ObjectID;
var allowCrossDomain = function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With');
// intercept OPTIONS method
if ('OPTIONS' == req.method) {
res.send(200);
}
else {
next();
}
};
app.use(allowCrossDomain)
app.use(morgan('short'));
app.use(bodyParser.urlencoded({extended: true}));
app.use(bodyParser.json());
//app.use(cors());
if (process.env.NODE_ENV !== 'production') {
require('dotenv').config();
}
const shuffle = (input_array) => {
const a = [...input_array];
for (let i = a.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[a[i], a[j]] = [a[j], a[i]];
}
return a;
};
app.get('/', async (req, res) => {
res.json({asd: "asd", db: "hi"});
});
app.get('/search_for_games', async (req, res) => {
const game_name = req.query.game_name;
const games = await (await client).find(
{"auth.game_name": game_name},
{fields: {auth: 1, player_info: 1, game_state: 1}}
).sort([['_id', -1]]).toArray();
res.json({games});
});
app.post('/create_game', async (req, res) => {
const newBoard = board.map(tile => {
return {
...tile,
owned: false,
player: null,
mortgaged: false,
upgrades: 0,
}
});
const game = await (await client).insertOne(
{
auth: {
game_name: req.body.game_name,
game_password: req.body.game_password,
},
player_info: [{
username: req.body.username,
password: req.body.password,
position: 0,
money: 1500,
id: 0,
state: "START_TURN",
jail_state: false,
jail_turns: 0,
doubles_rolled: 0,
dice: [0, 0],
pay_multiplier: 1,
color: colors[0],
}],
animated_players_move: {player: -1, moves: []},
game_state: "INVITING_PLAYERS",
auction: false,
auction_tile: 0,
trades: [],
logs: [],
board: newBoard,
chance: shuffle(chance),
chest: shuffle(chest),
last_chance_card: -1,
last_chest_card: -1,
current_player: 0,
}
);
res.json({success: true, game_id: game.ops[0]._id});
});
app.post('/join_game', async (req, res) => {
const game = await (await client).findOne(
{_id: new ObjectId(req.body.game_id)},
{fields: {player_info: 1, auth: 1, game_state: 1}},
);
if (req.body.game_password !== game.auth.game_password) {
res.json({error: "incorrect_game_pw"});
return;
}
const playerIndex = game.player_info.findIndex(player => player.username === req.body.username);
if (playerIndex === -1 && game.game_state !== "INVITING_PLAYERS") { // can't join anymore
res.json({error: "player_not_in_game"});
return;
}
if (playerIndex === -1 && game.game_state === "INVITING_PLAYERS") { // can still join
await (await client).updateOne(
{_id: new ObjectId(req.body.game_id)},
{
$push: {
[`player_info`]: {
username: req.body.username,
password: req.body.password,
position: 0,
money: 1500,
id: Math.max(...game.player_info.map(el => el.id)) + 1,
state: "NOT_TURN",
jail_state: false,
jail_turns: 0,
doubles_rolled: 0,
dice: [0, 0],
pay_multiplier: 1,
color: colors[Math.max(...game.player_info.map(el => el.id)) + 1],
},
}
},
);
res.json({success: true});
return;
}
if (game.player_info[playerIndex].password !== req.body.password) {
res.json({error: "incorrect_player_pw"});
return;
}
res.json({success: true});
});
io.on('connection', (socket) => {
game.game(socket, io);
});