1 | import { v4 as uuidv4 } from 'uuid';
|
---|
2 |
|
---|
3 | import axios from 'axios';
|
---|
4 |
|
---|
5 | require('dotenv').config();
|
---|
6 |
|
---|
7 | const crypto = require('crypto');
|
---|
8 |
|
---|
9 | import { progressRoundTillTheEnd } from '../poker/tableSpecific';
|
---|
10 |
|
---|
11 | const Pool = require('pg').Pool
|
---|
12 | const pool = new Pool({
|
---|
13 | connectionString: `postgres://${process.env.POSTGRES_USER}:${process.env.POSTGRES_PASSWORD}@${process.env.POSTGRES_HOST}/${process.env.POSTGRES_DB}`
|
---|
14 | });
|
---|
15 |
|
---|
16 | export default function handler(req, res) {
|
---|
17 | /**
|
---|
18 | * GET method
|
---|
19 | */
|
---|
20 | if (req.method === 'GET') {
|
---|
21 | /**
|
---|
22 | * /---------------------- GET ----------------------/
|
---|
23 | * If the player won credits, update them in the database.
|
---|
24 | * Also, update the stats in the database.
|
---|
25 | * @action give_credits
|
---|
26 | * @param session_id
|
---|
27 | * @param credits
|
---|
28 | */
|
---|
29 | if (req.query?.action === 'add_credits' && req.query?.session_id && req.query?.credits) {
|
---|
30 | const session_id = req.query.session_id
|
---|
31 | const session = sessions.find(session => session.id === session_id)
|
---|
32 |
|
---|
33 | if (session) {
|
---|
34 | session.lastActivity = Date.now();
|
---|
35 |
|
---|
36 | if (parseInt(req.query.credits) > 0) {
|
---|
37 | session.credits = session.credits + parseInt(req.query.credits)
|
---|
38 |
|
---|
39 | pool.query('UPDATE players SET credits = $1 WHERE username = $2', [session.credits, session.username], (error, results) => {
|
---|
40 | if (error) throw error;
|
---|
41 | });
|
---|
42 | }
|
---|
43 |
|
---|
44 | if (req.query?.dont_update_stats) {
|
---|
45 | // continue
|
---|
46 | } else {
|
---|
47 | pool.query('SELECT * FROM stats WHERE username = $1', [session.username], (error, results) => {
|
---|
48 | if (error) throw error;
|
---|
49 |
|
---|
50 | if (results.rows.length > 0) {
|
---|
51 | const stats = results.rows[0]
|
---|
52 |
|
---|
53 | if (parseInt(req.query.credits) > 0) {
|
---|
54 | pool.query('UPDATE stats SET money_earned = $1 WHERE username = $2', [parseInt(stats.money_earned) + parseInt(req.query.credits), session.username], (error, results) => {
|
---|
55 | if (error) throw error;
|
---|
56 | });
|
---|
57 | }
|
---|
58 |
|
---|
59 | if (req.query?.game === 'blackjack') {
|
---|
60 | if (req.query?.outcome === 'player_busted' || req.query?.outcome === 'player_lost') {
|
---|
61 | pool.query('UPDATE stats SET blackjack_games = $1 WHERE username = $2', [parseInt(stats.blackjack_games) + 1, session.username], (error, results) => {
|
---|
62 | if (error) throw error;
|
---|
63 | });
|
---|
64 | }
|
---|
65 | else if (req.query?.outcome === 'dealer_busted' || req.query?.outcome === 'player_won') {
|
---|
66 | pool.query('UPDATE stats SET blackjack_games = $1, blackjack_won_games = $2 WHERE username = $3', [parseInt(stats.blackjack_games) + 1, parseInt(stats.blackjack_won_games) + 1, session.username], (error, results) => {
|
---|
67 | if (error) throw error;
|
---|
68 | });
|
---|
69 | }
|
---|
70 | }
|
---|
71 | else if (req.query?.game === 'roulette') {
|
---|
72 | if (req.query?.outcome === 'lost') {
|
---|
73 | pool.query('UPDATE stats SET roulette_games = $1 WHERE username = $2', [parseInt(stats.roulette_games) + 1, session.username], (error, results) => {
|
---|
74 | if (error) throw error;
|
---|
75 | });
|
---|
76 | }
|
---|
77 | else if (req.query?.outcome === 'won') {
|
---|
78 | pool.query('UPDATE stats SET roulette_games = $1, roulette_won_games = $2 WHERE username = $3', [parseInt(stats.roulette_games) + 1, parseInt(stats.roulette_won_games) + 1, session.username], (error, results) => {
|
---|
79 | if (error) throw error;
|
---|
80 | });
|
---|
81 | }
|
---|
82 | }
|
---|
83 | else if (req.query?.game === 'poker') {
|
---|
84 | if (req.query?.outcome === 'lost') {
|
---|
85 | pool.query('UPDATE stats SET poker_games = $1 WHERE username = $2', [parseInt(stats.poker_games) + 1, session.username], (error, results) => {
|
---|
86 | if (error) throw error;
|
---|
87 | });
|
---|
88 | }
|
---|
89 | else if (req.query?.outcome === 'won') {
|
---|
90 | pool.query('UPDATE stats SET poker_games = $1, poker_won_games = $2 WHERE username = $3', [parseInt(stats.poker_games) + 1, parseInt(stats.poker_won_games) + 1, session.username], (error, results) => {
|
---|
91 | if (error) throw error;
|
---|
92 | });
|
---|
93 | }
|
---|
94 | }
|
---|
95 | }
|
---|
96 | });
|
---|
97 | }
|
---|
98 |
|
---|
99 | update_sessions_to_database();
|
---|
100 |
|
---|
101 | res.json({
|
---|
102 | success: true,
|
---|
103 | credits: session.credits,
|
---|
104 | })
|
---|
105 |
|
---|
106 | return ;
|
---|
107 | }
|
---|
108 |
|
---|
109 | res.json({
|
---|
110 | success: false,
|
---|
111 | })
|
---|
112 | }
|
---|
113 |
|
---|
114 | /**
|
---|
115 | * /---------------------- GET ----------------------/
|
---|
116 | * The player lost credits, update this in the database.
|
---|
117 | * @action take_credits
|
---|
118 | * @param session_id
|
---|
119 | * @param credits
|
---|
120 | */
|
---|
121 | if (req.query?.action === 'take_credits' && req.query?.session_id && req.query?.credits) {
|
---|
122 | const session_id = req.query.session_id
|
---|
123 | const session = sessions.find(session => session.id === session_id)
|
---|
124 |
|
---|
125 | let takeWhatYouCan = false;
|
---|
126 | if (req.query?.takeWhatYouCan === "true") takeWhatYouCan = true;
|
---|
127 |
|
---|
128 | if (session) {
|
---|
129 | session.lastActivity = Date.now();
|
---|
130 |
|
---|
131 | if (session.credits < parseInt(req.query.credits)) {
|
---|
132 | if (takeWhatYouCan) {
|
---|
133 | session.credits = 0;
|
---|
134 | }
|
---|
135 | else {
|
---|
136 | res.json({
|
---|
137 | success: false,
|
---|
138 | });
|
---|
139 |
|
---|
140 | return ;
|
---|
141 | }
|
---|
142 | }
|
---|
143 | else {
|
---|
144 | session.credits = session.credits - parseInt(req.query.credits)
|
---|
145 | }
|
---|
146 |
|
---|
147 | pool.query('UPDATE players SET credits = $1 WHERE username = $2', [session.credits, session.username], (error, results) => {
|
---|
148 | if (error) throw error;
|
---|
149 | });
|
---|
150 |
|
---|
151 | pool.query('SELECT * FROM stats WHERE username = $1', [session.username], (error, results) => {
|
---|
152 | if (error) throw error;
|
---|
153 |
|
---|
154 | if (results.rows.length > 0) {
|
---|
155 | const stats = results.rows[0]
|
---|
156 |
|
---|
157 | pool.query('UPDATE stats SET money_bet = $1 WHERE username = $2', [parseInt(stats.money_bet) + parseInt(req.query.credits), session.username], (error, results) => {
|
---|
158 | if (error) throw error;
|
---|
159 | });
|
---|
160 | }
|
---|
161 | });
|
---|
162 |
|
---|
163 | update_sessions_to_database();
|
---|
164 |
|
---|
165 | res.json({
|
---|
166 | success: true,
|
---|
167 | credits: session.credits,
|
---|
168 | })
|
---|
169 | return ;
|
---|
170 | }
|
---|
171 |
|
---|
172 | res.json({
|
---|
173 | success: false,
|
---|
174 | })
|
---|
175 | }
|
---|
176 |
|
---|
177 | /**
|
---|
178 | * /---------------------- GET ----------------------/
|
---|
179 | * Get stats for the player, so we can display them in the front end.
|
---|
180 | * @action get_stats
|
---|
181 | * @param session_id
|
---|
182 | */
|
---|
183 | if (req.query?.action === 'get_stats' && req.query?.session_id) {
|
---|
184 | const session_id = req.query.session_id
|
---|
185 | const session = sessions.find(session => session.id === session_id)
|
---|
186 |
|
---|
187 | if (session) {
|
---|
188 | session.lastActivity = Date.now();
|
---|
189 |
|
---|
190 | pool.query('SELECT * FROM stats WHERE username = $1', [session.username], (error, results) => {
|
---|
191 | if (error) throw error;
|
---|
192 |
|
---|
193 | if (results.rows.length > 0) {
|
---|
194 | res.json({
|
---|
195 | success: true,
|
---|
196 | stats: results.rows[0],
|
---|
197 | })
|
---|
198 | }
|
---|
199 | else {
|
---|
200 | res.json({
|
---|
201 | success: false,
|
---|
202 | })
|
---|
203 | }
|
---|
204 | });
|
---|
205 |
|
---|
206 | return ;
|
---|
207 | }
|
---|
208 |
|
---|
209 | res.json({
|
---|
210 | success: false,
|
---|
211 | })
|
---|
212 | }
|
---|
213 |
|
---|
214 | /**
|
---|
215 | * /---------------------- GET ----------------------/
|
---|
216 | * Checks if the player is logged in, and returns his session if so.
|
---|
217 | * @action check_if_logged_in
|
---|
218 | * @param session_id
|
---|
219 | */
|
---|
220 | if (req.query?.action === 'check_if_logged_in' && req.query?.session_id) {
|
---|
221 | const session_id = req.query.session_id
|
---|
222 | const session = sessions.find(session => session.id === session_id)
|
---|
223 |
|
---|
224 | if (session) {
|
---|
225 | res.json({
|
---|
226 | success: true,
|
---|
227 | displayName: session.displayName,
|
---|
228 | session_id: session.id,
|
---|
229 | credits: session.credits,
|
---|
230 | })
|
---|
231 | return ;
|
---|
232 | }
|
---|
233 |
|
---|
234 | res.json({
|
---|
235 | success: false,
|
---|
236 | })
|
---|
237 | }
|
---|
238 |
|
---|
239 | /**
|
---|
240 | * /---------------------- GET ----------------------/
|
---|
241 | * Takes the credits in the player's session, and updates the database.
|
---|
242 | * Logs the player out and kills the session.
|
---|
243 | * @action logout
|
---|
244 | * @param session_id
|
---|
245 | */
|
---|
246 | if (req.query?.action === 'logout' && req.query?.session_id) {
|
---|
247 | const session_id = req.query.session_id
|
---|
248 | const session = sessions.find(session => session.id === session_id)
|
---|
249 |
|
---|
250 | if (session) {
|
---|
251 | pool.query('UPDATE players SET credits = $1 WHERE username = $2', [session.credits, session.username], (error, results) => {
|
---|
252 | if (error) throw error;
|
---|
253 | });
|
---|
254 |
|
---|
255 | sessions.splice(sessions.indexOf(session), 1);
|
---|
256 |
|
---|
257 | axios.get(`${process.env.HOME_URL}/api/blackjack/?action=remove_room&session_id=${session_id}`);
|
---|
258 |
|
---|
259 | update_sessions_to_database();
|
---|
260 | }
|
---|
261 |
|
---|
262 | res.json({
|
---|
263 | success: true,
|
---|
264 | message: 'Successfully logged out',
|
---|
265 | })
|
---|
266 | }
|
---|
267 | }
|
---|
268 |
|
---|
269 | /**
|
---|
270 | * POST method
|
---|
271 | */
|
---|
272 | if (req.method === 'POST') {
|
---|
273 | const { body } = req;
|
---|
274 |
|
---|
275 | /**
|
---|
276 | * /---------------------- POST ----------------------/
|
---|
277 | * Checks if the entered account info is good, and registers a new user in the database if so.
|
---|
278 | * @action register
|
---|
279 | * @param username
|
---|
280 | * @param displayName
|
---|
281 | * @param password
|
---|
282 | */
|
---|
283 | if (body?.action === 'register') {
|
---|
284 | // checks
|
---|
285 | if (body?.username == "undefined" || body?.username == "null" || body?.username == "") {
|
---|
286 | res.json({
|
---|
287 | success: false,
|
---|
288 | message: 'Username is required',
|
---|
289 | });
|
---|
290 | return ;
|
---|
291 | }
|
---|
292 | if (/[^a-zA-Z]/g.test(body?.username)) {
|
---|
293 | res.json({
|
---|
294 | success: false,
|
---|
295 | message: 'Username must contain only letters',
|
---|
296 | })
|
---|
297 | return ;
|
---|
298 | }
|
---|
299 | if (body?.displayName == "undefined" || body?.displayName == "null" || body?.displayName == "") {
|
---|
300 | res.json({
|
---|
301 | success: false,
|
---|
302 | message: 'Display name is required',
|
---|
303 | });
|
---|
304 | return ;
|
---|
305 | }
|
---|
306 | if (body?.displayName?.toLowerCase() === "guest") {
|
---|
307 | res.json({
|
---|
308 | success: false,
|
---|
309 | message: 'Display name cannot be guest',
|
---|
310 | });
|
---|
311 | return ;
|
---|
312 | }
|
---|
313 | if (body?.password == "undefined" || body?.password == "null" || body?.password == "") {
|
---|
314 | res.json({
|
---|
315 | success: false,
|
---|
316 | message: 'Password is required',
|
---|
317 | });
|
---|
318 | return ;
|
---|
319 | }
|
---|
320 |
|
---|
321 | // everything's okay
|
---|
322 | body.username = body.username.toLowerCase()
|
---|
323 |
|
---|
324 | // hash password
|
---|
325 | const salt = crypto.randomBytes(16).toString('hex');
|
---|
326 | const hashedPassword = crypto.pbkdf2Sync(body.password, salt, 1000, 64, 'sha512').toString('hex');
|
---|
327 |
|
---|
328 | // check if user already exists
|
---|
329 | pool.query('SELECT * FROM users WHERE username = $1', [body.username], (error, results) => {
|
---|
330 | if (error) throw error;
|
---|
331 |
|
---|
332 | if (results.rows.length > 0) {
|
---|
333 | res.json({
|
---|
334 | success: false,
|
---|
335 | message: 'Username already exists',
|
---|
336 | });
|
---|
337 | return ;
|
---|
338 | }
|
---|
339 |
|
---|
340 | // store user in database
|
---|
341 | pool.query('INSERT INTO users (username, password, salt) VALUES ($1, $2, $3)', [body.username, hashedPassword, salt], (error, usersResults) => {
|
---|
342 | if (error) throw error;
|
---|
343 |
|
---|
344 | pool.query('INSERT INTO players (username, display_name, credits) VALUES ($1, $2, $3)', [body.username, body.displayName, 1000], (error, playersResults) => {
|
---|
345 | if (error) throw error;
|
---|
346 |
|
---|
347 | pool.query('INSERT INTO stats (username, blackjack_games, roulette_games, poker_games, blackjack_won_games, roulette_won_games, poker_won_games, money_bet, money_earned) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)', [body.username, 0, 0, 0, 0, 0, 0, 0, 0], (error, statsResults) => {
|
---|
348 | if (error) throw error;
|
---|
349 |
|
---|
350 | res.json({
|
---|
351 | success: true,
|
---|
352 | message: 'Registration successful',
|
---|
353 | });
|
---|
354 | return ;
|
---|
355 | });
|
---|
356 | });
|
---|
357 | });
|
---|
358 | });
|
---|
359 | }
|
---|
360 |
|
---|
361 | /**
|
---|
362 | * /---------------------- POST ----------------------/
|
---|
363 | * Checks if the entered account info is good, and logs the user in if so.
|
---|
364 | * @action login
|
---|
365 | * @param username
|
---|
366 | * @param password
|
---|
367 | */
|
---|
368 | if (body?.action === 'login') {
|
---|
369 | // checks
|
---|
370 | if (body?.username == "undefined" || body?.username == "null" || body?.username == "") {
|
---|
371 | res.json({
|
---|
372 | success: false,
|
---|
373 | message: 'Username is required',
|
---|
374 | });
|
---|
375 | return ;
|
---|
376 | }
|
---|
377 | if (/[^a-zA-Z]/g.test(body?.username)) {
|
---|
378 | res.json({
|
---|
379 | success: false,
|
---|
380 | message: 'Username must contain only letters',
|
---|
381 | })
|
---|
382 | return ;
|
---|
383 | }
|
---|
384 | if (body?.password == "undefined" || body?.password == "null" || body?.password == "") {
|
---|
385 | res.json({
|
---|
386 | success: false,
|
---|
387 | message: 'Password is required',
|
---|
388 | });
|
---|
389 | return ;
|
---|
390 | }
|
---|
391 |
|
---|
392 | // everything's okay
|
---|
393 | body.username = body.username.toLowerCase();
|
---|
394 |
|
---|
395 | // check if user exists
|
---|
396 | pool.query('SELECT * FROM users WHERE username = $1', [body.username], (error, usersResults) => {
|
---|
397 | if (error) throw error;
|
---|
398 |
|
---|
399 | if (usersResults.rows.length === 0) {
|
---|
400 | res.json({
|
---|
401 | success: false,
|
---|
402 | message: 'User does not exist. Try Registering instead.',
|
---|
403 | });
|
---|
404 | return ;
|
---|
405 | }
|
---|
406 | else {
|
---|
407 | if (usersResults.rows.length > 0) {
|
---|
408 | const user = usersResults.rows[0];
|
---|
409 | const salt = user.salt;
|
---|
410 | const hashedPassword = crypto.pbkdf2Sync(body.password, salt, 1000, 64, 'sha512').toString('hex');
|
---|
411 |
|
---|
412 | if (hashedPassword === user.password) {
|
---|
413 | pool.query('SELECT * FROM players WHERE username = $1', [body.username], (error, playersResults) => {
|
---|
414 | if (playersResults.rows.length > 0) {
|
---|
415 | let session = sessions.find(session => session.username === playersResults.rows[0].username)
|
---|
416 |
|
---|
417 | if (session) {
|
---|
418 | // Already logged in
|
---|
419 | res.json({
|
---|
420 | success: true,
|
---|
421 | message: 'Login successful',
|
---|
422 | session: session,
|
---|
423 | })
|
---|
424 | }
|
---|
425 | else {
|
---|
426 | // create a session
|
---|
427 | session = {
|
---|
428 | id: uuidv4(),
|
---|
429 | displayName: playersResults.rows[0].display_name,
|
---|
430 | username: playersResults.rows[0].username,
|
---|
431 | credits: playersResults.rows[0].credits,
|
---|
432 | lastActivity: Date.now(),
|
---|
433 | }
|
---|
434 |
|
---|
435 | sessions.push(session);
|
---|
436 |
|
---|
437 | update_sessions_to_database();
|
---|
438 |
|
---|
439 | res.json({
|
---|
440 | success: true,
|
---|
441 | message: 'Login successful',
|
---|
442 | session: session,
|
---|
443 | })
|
---|
444 | }
|
---|
445 |
|
---|
446 | return ;
|
---|
447 | }
|
---|
448 | });
|
---|
449 | }
|
---|
450 | else {
|
---|
451 | res.json({
|
---|
452 | success: false,
|
---|
453 | message: 'Username and password do not match.',
|
---|
454 | });
|
---|
455 | }
|
---|
456 | }
|
---|
457 | }
|
---|
458 | });
|
---|
459 | }
|
---|
460 | }
|
---|
461 | }
|
---|
462 |
|
---|
463 |
|
---|
464 | /**
|
---|
465 | * User session data
|
---|
466 | */
|
---|
467 | export var sessions = []
|
---|
468 |
|
---|
469 | export function update_sessions_to_database() {
|
---|
470 | pool.query('UPDATE sessions SET data = $1 WHERE identifier = $2', [JSON.stringify(sessions), 'sessions_data'], (error, results) => {
|
---|
471 | if (error) throw error;
|
---|
472 | });
|
---|
473 | }
|
---|
474 |
|
---|
475 | export function load_sessions_from_database() {
|
---|
476 | pool.query('SELECT data FROM sessions WHERE identifier = $1', ['sessions_data'], (error, results) => {
|
---|
477 | if (error) throw error;
|
---|
478 |
|
---|
479 | sessions = JSON.parse(results?.rows[0]?.data || []);
|
---|
480 | });
|
---|
481 | }
|
---|
482 | load_sessions_from_database();
|
---|
483 |
|
---|
484 | /**
|
---|
485 | * Poker game data
|
---|
486 | */
|
---|
487 | export var tables = []
|
---|
488 |
|
---|
489 | export function cleanTables() {
|
---|
490 | tables = [];
|
---|
491 | }
|
---|
492 |
|
---|
493 | export function update_tables_to_database() {
|
---|
494 | tables = tables.map(table => ({...table, turnTimeout: null}));
|
---|
495 |
|
---|
496 | pool.query('UPDATE poker SET data = $1 WHERE identifier = $2', [JSON.stringify(tables), 'poker_data'], (error, results) => {
|
---|
497 | if (error) throw error;
|
---|
498 | });
|
---|
499 | }
|
---|
500 |
|
---|
501 | export async function load_tables_from_database() {
|
---|
502 | pool.query('SELECT data FROM poker WHERE identifier = $1', ['poker_data'], (error, results) => {
|
---|
503 | if (error) throw error;
|
---|
504 |
|
---|
505 | tables = JSON.parse(results?.rows[0]?.data || []);
|
---|
506 |
|
---|
507 | tables.forEach(table => {
|
---|
508 | if (table.started) {
|
---|
509 | progressRoundTillTheEnd(table.id);
|
---|
510 | }
|
---|
511 | })
|
---|
512 |
|
---|
513 | cleanTables();
|
---|
514 |
|
---|
515 | update_tables_to_database();
|
---|
516 | });
|
---|
517 | }
|
---|
518 | load_tables_from_database();
|
---|
519 |
|
---|
520 | /**
|
---|
521 | * Roulette game data
|
---|
522 | */
|
---|
523 | export var game = {}
|
---|
524 |
|
---|
525 | export function update_game_to_database() {
|
---|
526 | pool.query('UPDATE roulette SET data = $1 WHERE identifier = $2', [JSON.stringify(game), 'roulette_data'], (error, results) => {
|
---|
527 | if (error) throw error;
|
---|
528 | });
|
---|
529 | }
|
---|
530 |
|
---|
531 | export async function load_game_from_database() {
|
---|
532 | pool.query('SELECT data FROM roulette WHERE identifier = $1', ['roulette_data'], (error, results) => {
|
---|
533 | if (error) throw error;
|
---|
534 |
|
---|
535 | game = JSON.parse(results?.rows[0]?.data || []);
|
---|
536 |
|
---|
537 | game.loaded = true;
|
---|
538 | console.log(game);
|
---|
539 | });
|
---|
540 | }
|
---|
541 | load_game_from_database();
|
---|
542 | |
---|