[87614a5] | 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 | const Pool = require('pg').Pool
|
---|
| 10 | const pool = new Pool({
|
---|
[64dc53b] | 11 | connectionString: `postgres://${process.env.POSTGRES_USER}:${process.env.POSTGRES_PASSWORD}@${process.env.POSTGRES_HOST}/${process.env.POSTGRES_DB}`
|
---|
[87614a5] | 12 | });
|
---|
| 13 |
|
---|
| 14 | const sessions = []
|
---|
[ebf5e04] | 15 | // example session = { id, displayName, username, credits, lastActivity }
|
---|
[87614a5] | 16 |
|
---|
| 17 | export default function handler(req, res) {
|
---|
| 18 | /**
|
---|
| 19 | * GET method
|
---|
| 20 | */
|
---|
| 21 | if (req.method === 'GET') {
|
---|
| 22 | /**
|
---|
| 23 | * /---------------------- GET ----------------------/
|
---|
[ebf5e04] | 24 | * If the player won credits, update them in the database.
|
---|
| 25 | * Also, update the stats in the database.
|
---|
[87614a5] | 26 | * @action give_credits
|
---|
| 27 | * @param session_id
|
---|
| 28 | * @param credits
|
---|
| 29 | */
|
---|
| 30 | if (req.query?.action === 'add_credits' && req.query?.session_id && req.query?.credits) {
|
---|
| 31 | const session_id = req.query.session_id
|
---|
| 32 | const session = sessions.find(session => session.id === session_id)
|
---|
| 33 |
|
---|
| 34 | if (session) {
|
---|
| 35 | session.lastActivity = Date.now();
|
---|
| 36 |
|
---|
| 37 | if (parseInt(req.query.credits) > 0) {
|
---|
| 38 | session.credits = session.credits + parseInt(req.query.credits)
|
---|
| 39 |
|
---|
| 40 | pool.query('UPDATE players SET credits = $1 WHERE username = $2', [session.credits, session.username], (error, results) => {
|
---|
| 41 | if (error) throw error;
|
---|
| 42 | });
|
---|
| 43 | }
|
---|
| 44 |
|
---|
| 45 | if (req.query?.dont_update_stats) {
|
---|
| 46 | // continue
|
---|
| 47 | } else {
|
---|
| 48 | pool.query('SELECT * FROM stats WHERE username = $1', [session.username], (error, results) => {
|
---|
| 49 | if (error) throw error;
|
---|
| 50 |
|
---|
| 51 | if (results.rows.length > 0) {
|
---|
| 52 | const stats = results.rows[0]
|
---|
| 53 |
|
---|
| 54 | if (parseInt(req.query.credits) > 0) {
|
---|
| 55 | pool.query('UPDATE stats SET money_earned = $1 WHERE username = $2', [parseInt(stats.money_earned) + parseInt(req.query.credits), session.username], (error, results) => {
|
---|
| 56 | if (error) throw error;
|
---|
| 57 | });
|
---|
| 58 | }
|
---|
| 59 |
|
---|
| 60 | if (req.query?.game === 'blackjack') {
|
---|
| 61 | if (req.query?.outcome === 'player_busted' || req.query?.outcome === 'player_lost') {
|
---|
| 62 | pool.query('UPDATE stats SET blackjack_games = $1 WHERE username = $2', [parseInt(stats.blackjack_games) + 1, session.username], (error, results) => {
|
---|
| 63 | if (error) throw error;
|
---|
| 64 | });
|
---|
| 65 | }
|
---|
| 66 | else if (req.query?.outcome === 'dealer_busted' || req.query?.outcome === 'player_won') {
|
---|
| 67 | 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) => {
|
---|
| 68 | if (error) throw error;
|
---|
| 69 | });
|
---|
| 70 | }
|
---|
| 71 | }
|
---|
[ace7865] | 72 | else if (req.query?.game === 'roulette') {
|
---|
| 73 | if (req.query?.outcome === 'lost') {
|
---|
| 74 | pool.query('UPDATE stats SET roulette_games = $1 WHERE username = $2', [parseInt(stats.roulette_games) + 1, session.username], (error, results) => {
|
---|
| 75 | if (error) throw error;
|
---|
| 76 | });
|
---|
| 77 | }
|
---|
| 78 | else if (req.query?.outcome === 'won') {
|
---|
| 79 | 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) => {
|
---|
| 80 | if (error) throw error;
|
---|
| 81 | });
|
---|
| 82 | }
|
---|
| 83 | }
|
---|
[87614a5] | 84 | }
|
---|
| 85 | });
|
---|
| 86 | }
|
---|
| 87 |
|
---|
| 88 | res.json({
|
---|
| 89 | success: true,
|
---|
| 90 | credits: session.credits,
|
---|
| 91 | })
|
---|
| 92 |
|
---|
| 93 | return ;
|
---|
| 94 | }
|
---|
| 95 |
|
---|
| 96 | res.json({
|
---|
| 97 | success: false,
|
---|
| 98 | })
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 | /**
|
---|
| 102 | * /---------------------- GET ----------------------/
|
---|
[ebf5e04] | 103 | * The player lost credits, update this in the database.
|
---|
[87614a5] | 104 | * @action take_credits
|
---|
| 105 | * @param session_id
|
---|
| 106 | * @param credits
|
---|
| 107 | */
|
---|
| 108 | if (req.query?.action === 'take_credits' && req.query?.session_id && req.query?.credits) {
|
---|
| 109 | const session_id = req.query.session_id
|
---|
| 110 | const session = sessions.find(session => session.id === session_id)
|
---|
| 111 |
|
---|
| 112 | if (session) {
|
---|
| 113 | session.lastActivity = Date.now();
|
---|
| 114 |
|
---|
| 115 | session.credits = session.credits - parseInt(req.query.credits)
|
---|
| 116 |
|
---|
| 117 | pool.query('UPDATE players SET credits = $1 WHERE username = $2', [session.credits, session.username], (error, results) => {
|
---|
| 118 | if (error) throw error;
|
---|
| 119 | });
|
---|
| 120 |
|
---|
| 121 | pool.query('SELECT * FROM stats WHERE username = $1', [session.username], (error, results) => {
|
---|
| 122 | if (error) throw error;
|
---|
| 123 |
|
---|
| 124 | if (results.rows.length > 0) {
|
---|
| 125 | const stats = results.rows[0]
|
---|
| 126 |
|
---|
| 127 | pool.query('UPDATE stats SET money_bet = $1 WHERE username = $2', [parseInt(stats.money_bet) + parseInt(req.query.credits), session.username], (error, results) => {
|
---|
| 128 | if (error) throw error;
|
---|
| 129 | });
|
---|
| 130 | }
|
---|
| 131 | });
|
---|
| 132 |
|
---|
| 133 | res.json({
|
---|
| 134 | success: true,
|
---|
| 135 | credits: session.credits,
|
---|
| 136 | })
|
---|
| 137 | return ;
|
---|
| 138 | }
|
---|
| 139 |
|
---|
| 140 | res.json({
|
---|
| 141 | success: false,
|
---|
| 142 | })
|
---|
| 143 | }
|
---|
| 144 |
|
---|
| 145 | /**
|
---|
| 146 | * /---------------------- GET ----------------------/
|
---|
[ebf5e04] | 147 | * Get stats for the player, so we can display them in the front end.
|
---|
[87614a5] | 148 | * @action get_stats
|
---|
| 149 | * @param session_id
|
---|
| 150 | */
|
---|
| 151 | if (req.query?.action === 'get_stats' && req.query?.session_id) {
|
---|
| 152 | const session_id = req.query.session_id
|
---|
| 153 | const session = sessions.find(session => session.id === session_id)
|
---|
| 154 |
|
---|
| 155 | if (session) {
|
---|
| 156 | session.lastActivity = Date.now();
|
---|
| 157 |
|
---|
| 158 | pool.query('SELECT * FROM stats WHERE username = $1', [session.username], (error, results) => {
|
---|
| 159 | if (error) throw error;
|
---|
| 160 |
|
---|
| 161 | if (results.rows.length > 0) {
|
---|
| 162 | res.json({
|
---|
| 163 | success: true,
|
---|
| 164 | stats: results.rows[0],
|
---|
| 165 | })
|
---|
| 166 | }
|
---|
| 167 | else {
|
---|
| 168 | res.json({
|
---|
| 169 | success: false,
|
---|
| 170 | })
|
---|
| 171 | }
|
---|
| 172 | });
|
---|
| 173 |
|
---|
| 174 | return ;
|
---|
| 175 | }
|
---|
| 176 |
|
---|
| 177 | res.json({
|
---|
| 178 | success: false,
|
---|
| 179 | })
|
---|
| 180 | }
|
---|
| 181 |
|
---|
| 182 | /**
|
---|
| 183 | * /---------------------- GET ----------------------/
|
---|
[285c3cc] | 184 | * Checks if the player is logged in, and returns his session if so.
|
---|
[87614a5] | 185 | * @action check_if_logged_in
|
---|
| 186 | * @param session_id
|
---|
| 187 | */
|
---|
| 188 | if (req.query?.action === 'check_if_logged_in' && req.query?.session_id) {
|
---|
| 189 | const session_id = req.query.session_id
|
---|
| 190 | const session = sessions.find(session => session.id === session_id)
|
---|
| 191 |
|
---|
| 192 | if (session) {
|
---|
| 193 | res.json({
|
---|
| 194 | success: true,
|
---|
| 195 | displayName: session.displayName,
|
---|
| 196 | session_id: session.id,
|
---|
| 197 | credits: session.credits,
|
---|
| 198 | })
|
---|
| 199 | return ;
|
---|
| 200 | }
|
---|
| 201 |
|
---|
| 202 | res.json({
|
---|
| 203 | success: false,
|
---|
| 204 | })
|
---|
| 205 | }
|
---|
| 206 |
|
---|
| 207 | /**
|
---|
| 208 | * /---------------------- GET ----------------------/
|
---|
[ebf5e04] | 209 | * Takes the credits in the player's session, and updates the database.
|
---|
| 210 | * Logs the player out and kills the session.
|
---|
[87614a5] | 211 | * @action logout
|
---|
| 212 | * @param session_id
|
---|
| 213 | */
|
---|
| 214 | if (req.query?.action === 'logout' && req.query?.session_id) {
|
---|
| 215 | const session_id = req.query.session_id
|
---|
| 216 | const session = sessions.find(session => session.id === session_id)
|
---|
| 217 |
|
---|
| 218 | if (session) {
|
---|
| 219 | pool.query('UPDATE players SET credits = $1 WHERE username = $2', [session.credits, session.username], (error, results) => {
|
---|
| 220 | if (error) throw error;
|
---|
| 221 | });
|
---|
| 222 |
|
---|
| 223 | sessions.splice(sessions.indexOf(session), 1);
|
---|
| 224 |
|
---|
| 225 | axios.get(`${process.env.HOME_URL}/api/blackjack/?action=remove_room&session_id=${session_id}`);
|
---|
| 226 | }
|
---|
| 227 |
|
---|
| 228 | res.json({
|
---|
| 229 | success: true,
|
---|
| 230 | message: 'Successfully logged out',
|
---|
| 231 | })
|
---|
| 232 | }
|
---|
| 233 | }
|
---|
| 234 |
|
---|
| 235 | /**
|
---|
| 236 | * POST method
|
---|
| 237 | */
|
---|
| 238 | if (req.method === 'POST') {
|
---|
| 239 | const { body } = req;
|
---|
| 240 |
|
---|
| 241 | /**
|
---|
| 242 | * /---------------------- POST ----------------------/
|
---|
[ebf5e04] | 243 | * Checks if the entered account info is good, and registers a new user in the database if so.
|
---|
[87614a5] | 244 | * @action register
|
---|
| 245 | * @param username
|
---|
| 246 | * @param displayName
|
---|
| 247 | * @param password
|
---|
| 248 | */
|
---|
| 249 | if (body?.action === 'register') {
|
---|
| 250 | // checks
|
---|
| 251 | if (body?.username == "undefined" || body?.username == "null" || body?.username == "") {
|
---|
| 252 | res.json({
|
---|
| 253 | success: false,
|
---|
| 254 | message: 'Username is required',
|
---|
| 255 | });
|
---|
| 256 | return ;
|
---|
| 257 | }
|
---|
| 258 | if (/[^a-zA-Z]/g.test(body?.username)) {
|
---|
| 259 | res.json({
|
---|
| 260 | success: false,
|
---|
| 261 | message: 'Username must contain only letters',
|
---|
| 262 | })
|
---|
| 263 | return ;
|
---|
| 264 | }
|
---|
| 265 | if (body?.displayName == "undefined" || body?.displayName == "null" || body?.displayName == "") {
|
---|
| 266 | res.json({
|
---|
| 267 | success: false,
|
---|
| 268 | message: 'Display name is required',
|
---|
| 269 | });
|
---|
| 270 | return ;
|
---|
| 271 | }
|
---|
| 272 | if (body?.displayName?.toLowerCase() === "guest") {
|
---|
| 273 | res.json({
|
---|
| 274 | success: false,
|
---|
| 275 | message: 'Display name cannot be guest',
|
---|
| 276 | });
|
---|
| 277 | return ;
|
---|
| 278 | }
|
---|
| 279 | if (body?.password == "undefined" || body?.password == "null" || body?.password == "") {
|
---|
| 280 | res.json({
|
---|
| 281 | success: false,
|
---|
| 282 | message: 'Password is required',
|
---|
| 283 | });
|
---|
| 284 | return ;
|
---|
| 285 | }
|
---|
| 286 |
|
---|
| 287 | // everything's okay
|
---|
| 288 | body.username = body.username.toLowerCase()
|
---|
| 289 |
|
---|
| 290 | // hash password
|
---|
| 291 | const salt = crypto.randomBytes(16).toString('hex');
|
---|
| 292 | const hashedPassword = crypto.pbkdf2Sync(body.password, salt, 1000, 64, 'sha512').toString('hex');
|
---|
| 293 |
|
---|
| 294 | // check if user already exists
|
---|
| 295 | pool.query('SELECT * FROM users WHERE username = $1', [body.username], (error, results) => {
|
---|
| 296 | if (error) throw error;
|
---|
| 297 |
|
---|
| 298 | if (results.rows.length > 0) {
|
---|
| 299 | res.json({
|
---|
| 300 | success: false,
|
---|
| 301 | message: 'Username already exists',
|
---|
| 302 | });
|
---|
| 303 | return ;
|
---|
| 304 | }
|
---|
| 305 |
|
---|
| 306 | // store user in database
|
---|
| 307 | pool.query('INSERT INTO users (username, password, salt) VALUES ($1, $2, $3)', [body.username, hashedPassword, salt], (error, usersResults) => {
|
---|
| 308 | if (error) throw error;
|
---|
| 309 |
|
---|
| 310 | pool.query('INSERT INTO players (username, display_name, credits) VALUES ($1, $2, $3)', [body.username, body.displayName, 1000], (error, playersResults) => {
|
---|
| 311 | if (error) throw error;
|
---|
| 312 |
|
---|
| 313 | 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) => {
|
---|
| 314 | if (error) throw error;
|
---|
| 315 |
|
---|
| 316 | res.json({
|
---|
| 317 | success: true,
|
---|
| 318 | message: 'Registration successful',
|
---|
| 319 | });
|
---|
| 320 | return ;
|
---|
| 321 | });
|
---|
| 322 | });
|
---|
| 323 | });
|
---|
| 324 | });
|
---|
| 325 | }
|
---|
| 326 |
|
---|
| 327 | /**
|
---|
| 328 | * /---------------------- POST ----------------------/
|
---|
[ebf5e04] | 329 | * Checks if the entered account info is good, and logs the user in if so.
|
---|
[87614a5] | 330 | * @action login
|
---|
| 331 | * @param username
|
---|
| 332 | * @param password
|
---|
| 333 | */
|
---|
| 334 | if (body?.action === 'login') {
|
---|
| 335 | // checks
|
---|
| 336 | if (body?.username == "undefined" || body?.username == "null" || body?.username == "") {
|
---|
| 337 | res.json({
|
---|
| 338 | success: false,
|
---|
| 339 | message: 'Username is required',
|
---|
| 340 | });
|
---|
| 341 | return ;
|
---|
| 342 | }
|
---|
| 343 | if (/[^a-zA-Z]/g.test(body?.username)) {
|
---|
| 344 | res.json({
|
---|
| 345 | success: false,
|
---|
| 346 | message: 'Username must contain only letters',
|
---|
| 347 | })
|
---|
| 348 | return ;
|
---|
| 349 | }
|
---|
| 350 | if (body?.password == "undefined" || body?.password == "null" || body?.password == "") {
|
---|
| 351 | res.json({
|
---|
| 352 | success: false,
|
---|
| 353 | message: 'Password is required',
|
---|
| 354 | });
|
---|
| 355 | return ;
|
---|
| 356 | }
|
---|
| 357 |
|
---|
| 358 | // everything's okay
|
---|
| 359 | body.username = body.username.toLowerCase();
|
---|
| 360 |
|
---|
| 361 | // check if user exists
|
---|
| 362 | pool.query('SELECT * FROM users WHERE username = $1', [body.username], (error, usersResults) => {
|
---|
| 363 | if (error) throw error;
|
---|
| 364 |
|
---|
| 365 | if (usersResults.rows.length === 0) {
|
---|
| 366 | res.json({
|
---|
| 367 | success: false,
|
---|
| 368 | message: 'User does not exist. Try Registering instead.',
|
---|
| 369 | });
|
---|
| 370 | return ;
|
---|
| 371 | }
|
---|
| 372 | else {
|
---|
| 373 | if (usersResults.rows.length > 0) {
|
---|
| 374 | const user = usersResults.rows[0];
|
---|
| 375 | const salt = user.salt;
|
---|
| 376 | const hashedPassword = crypto.pbkdf2Sync(body.password, salt, 1000, 64, 'sha512').toString('hex');
|
---|
| 377 |
|
---|
| 378 | if (hashedPassword === user.password) {
|
---|
| 379 | pool.query('SELECT * FROM players WHERE username = $1', [body.username], (error, playersResults) => {
|
---|
| 380 | if (playersResults.rows.length > 0) {
|
---|
| 381 | let session = sessions.find(session => session.username === playersResults.rows[0].username)
|
---|
| 382 |
|
---|
| 383 | if (session) {
|
---|
| 384 | // Already logged in
|
---|
| 385 | res.json({
|
---|
| 386 | success: false,
|
---|
| 387 | message: 'You are already logged in',
|
---|
| 388 | })
|
---|
| 389 | }
|
---|
| 390 | else {
|
---|
| 391 | // create a session
|
---|
| 392 | session = {
|
---|
| 393 | id: uuidv4(),
|
---|
| 394 | displayName: playersResults.rows[0].display_name,
|
---|
| 395 | username: playersResults.rows[0].username,
|
---|
| 396 | credits: playersResults.rows[0].credits,
|
---|
| 397 | lastActivity: Date.now(),
|
---|
| 398 | }
|
---|
| 399 |
|
---|
| 400 | sessions.push(session);
|
---|
| 401 |
|
---|
| 402 | res.json({
|
---|
| 403 | success: true,
|
---|
| 404 | message: 'Login successful',
|
---|
| 405 | session: session,
|
---|
| 406 | })
|
---|
| 407 | }
|
---|
| 408 |
|
---|
| 409 | return ;
|
---|
| 410 | }
|
---|
| 411 | });
|
---|
| 412 | }
|
---|
| 413 | else {
|
---|
| 414 | res.json({
|
---|
| 415 | success: false,
|
---|
| 416 | message: 'Username and password do not match.',
|
---|
| 417 | });
|
---|
| 418 | }
|
---|
| 419 | }
|
---|
| 420 | }
|
---|
| 421 | });
|
---|
| 422 | }
|
---|
| 423 | }
|
---|
| 424 | } |
---|