[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 | }
|
---|
[3a783f2] | 84 | else if (req.query?.game === 'poker') {
|
---|
| 85 | if (req.query?.outcome === 'lost') {
|
---|
| 86 | pool.query('UPDATE stats SET poker_games = $1 WHERE username = $2', [parseInt(stats.poker_games) + 1, session.username], (error, results) => {
|
---|
| 87 | if (error) throw error;
|
---|
| 88 | });
|
---|
| 89 | }
|
---|
| 90 | else if (req.query?.outcome === 'won') {
|
---|
| 91 | 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) => {
|
---|
| 92 | if (error) throw error;
|
---|
| 93 | });
|
---|
| 94 | }
|
---|
| 95 | }
|
---|
[87614a5] | 96 | }
|
---|
| 97 | });
|
---|
| 98 | }
|
---|
| 99 |
|
---|
| 100 | res.json({
|
---|
| 101 | success: true,
|
---|
| 102 | credits: session.credits,
|
---|
| 103 | })
|
---|
| 104 |
|
---|
| 105 | return ;
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | res.json({
|
---|
| 109 | success: false,
|
---|
| 110 | })
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 | /**
|
---|
| 114 | * /---------------------- GET ----------------------/
|
---|
[ebf5e04] | 115 | * The player lost credits, update this in the database.
|
---|
[87614a5] | 116 | * @action take_credits
|
---|
| 117 | * @param session_id
|
---|
| 118 | * @param credits
|
---|
| 119 | */
|
---|
| 120 | if (req.query?.action === 'take_credits' && req.query?.session_id && req.query?.credits) {
|
---|
| 121 | const session_id = req.query.session_id
|
---|
| 122 | const session = sessions.find(session => session.id === session_id)
|
---|
| 123 |
|
---|
[3a783f2] | 124 | let takeWhatYouCan = false;
|
---|
| 125 | if (req.query?.takeWhatYouCan === "true") takeWhatYouCan = true;
|
---|
| 126 |
|
---|
[87614a5] | 127 | if (session) {
|
---|
| 128 | session.lastActivity = Date.now();
|
---|
| 129 |
|
---|
[3a783f2] | 130 | if (session.credits < parseInt(req.query.credits)) {
|
---|
| 131 | if (takeWhatYouCan) {
|
---|
| 132 | session.credits = 0;
|
---|
| 133 | }
|
---|
| 134 | else {
|
---|
| 135 | res.json({
|
---|
| 136 | success: false,
|
---|
| 137 | });
|
---|
| 138 |
|
---|
| 139 | return ;
|
---|
| 140 | }
|
---|
| 141 | }
|
---|
| 142 | else {
|
---|
| 143 | session.credits = session.credits - parseInt(req.query.credits)
|
---|
| 144 | }
|
---|
[87614a5] | 145 |
|
---|
| 146 | pool.query('UPDATE players SET credits = $1 WHERE username = $2', [session.credits, session.username], (error, results) => {
|
---|
| 147 | if (error) throw error;
|
---|
| 148 | });
|
---|
| 149 |
|
---|
| 150 | pool.query('SELECT * FROM stats WHERE username = $1', [session.username], (error, results) => {
|
---|
| 151 | if (error) throw error;
|
---|
| 152 |
|
---|
| 153 | if (results.rows.length > 0) {
|
---|
| 154 | const stats = results.rows[0]
|
---|
| 155 |
|
---|
| 156 | pool.query('UPDATE stats SET money_bet = $1 WHERE username = $2', [parseInt(stats.money_bet) + parseInt(req.query.credits), session.username], (error, results) => {
|
---|
| 157 | if (error) throw error;
|
---|
| 158 | });
|
---|
| 159 | }
|
---|
| 160 | });
|
---|
| 161 |
|
---|
| 162 | res.json({
|
---|
| 163 | success: true,
|
---|
| 164 | credits: session.credits,
|
---|
| 165 | })
|
---|
| 166 | return ;
|
---|
| 167 | }
|
---|
| 168 |
|
---|
| 169 | res.json({
|
---|
| 170 | success: false,
|
---|
| 171 | })
|
---|
| 172 | }
|
---|
| 173 |
|
---|
| 174 | /**
|
---|
| 175 | * /---------------------- GET ----------------------/
|
---|
[ebf5e04] | 176 | * Get stats for the player, so we can display them in the front end.
|
---|
[87614a5] | 177 | * @action get_stats
|
---|
| 178 | * @param session_id
|
---|
| 179 | */
|
---|
| 180 | if (req.query?.action === 'get_stats' && req.query?.session_id) {
|
---|
| 181 | const session_id = req.query.session_id
|
---|
| 182 | const session = sessions.find(session => session.id === session_id)
|
---|
| 183 |
|
---|
| 184 | if (session) {
|
---|
| 185 | session.lastActivity = Date.now();
|
---|
| 186 |
|
---|
| 187 | pool.query('SELECT * FROM stats WHERE username = $1', [session.username], (error, results) => {
|
---|
| 188 | if (error) throw error;
|
---|
| 189 |
|
---|
| 190 | if (results.rows.length > 0) {
|
---|
| 191 | res.json({
|
---|
| 192 | success: true,
|
---|
| 193 | stats: results.rows[0],
|
---|
| 194 | })
|
---|
| 195 | }
|
---|
| 196 | else {
|
---|
| 197 | res.json({
|
---|
| 198 | success: false,
|
---|
| 199 | })
|
---|
| 200 | }
|
---|
| 201 | });
|
---|
| 202 |
|
---|
| 203 | return ;
|
---|
| 204 | }
|
---|
| 205 |
|
---|
| 206 | res.json({
|
---|
| 207 | success: false,
|
---|
| 208 | })
|
---|
| 209 | }
|
---|
| 210 |
|
---|
| 211 | /**
|
---|
| 212 | * /---------------------- GET ----------------------/
|
---|
[285c3cc] | 213 | * Checks if the player is logged in, and returns his session if so.
|
---|
[87614a5] | 214 | * @action check_if_logged_in
|
---|
| 215 | * @param session_id
|
---|
| 216 | */
|
---|
| 217 | if (req.query?.action === 'check_if_logged_in' && req.query?.session_id) {
|
---|
| 218 | const session_id = req.query.session_id
|
---|
| 219 | const session = sessions.find(session => session.id === session_id)
|
---|
| 220 |
|
---|
| 221 | if (session) {
|
---|
| 222 | res.json({
|
---|
| 223 | success: true,
|
---|
| 224 | displayName: session.displayName,
|
---|
| 225 | session_id: session.id,
|
---|
| 226 | credits: session.credits,
|
---|
| 227 | })
|
---|
| 228 | return ;
|
---|
| 229 | }
|
---|
| 230 |
|
---|
| 231 | res.json({
|
---|
| 232 | success: false,
|
---|
| 233 | })
|
---|
| 234 | }
|
---|
| 235 |
|
---|
| 236 | /**
|
---|
| 237 | * /---------------------- GET ----------------------/
|
---|
[ebf5e04] | 238 | * Takes the credits in the player's session, and updates the database.
|
---|
| 239 | * Logs the player out and kills the session.
|
---|
[87614a5] | 240 | * @action logout
|
---|
| 241 | * @param session_id
|
---|
| 242 | */
|
---|
| 243 | if (req.query?.action === 'logout' && req.query?.session_id) {
|
---|
| 244 | const session_id = req.query.session_id
|
---|
| 245 | const session = sessions.find(session => session.id === session_id)
|
---|
| 246 |
|
---|
| 247 | if (session) {
|
---|
| 248 | pool.query('UPDATE players SET credits = $1 WHERE username = $2', [session.credits, session.username], (error, results) => {
|
---|
| 249 | if (error) throw error;
|
---|
| 250 | });
|
---|
| 251 |
|
---|
| 252 | sessions.splice(sessions.indexOf(session), 1);
|
---|
| 253 |
|
---|
| 254 | axios.get(`${process.env.HOME_URL}/api/blackjack/?action=remove_room&session_id=${session_id}`);
|
---|
| 255 | }
|
---|
| 256 |
|
---|
| 257 | res.json({
|
---|
| 258 | success: true,
|
---|
| 259 | message: 'Successfully logged out',
|
---|
| 260 | })
|
---|
| 261 | }
|
---|
| 262 | }
|
---|
| 263 |
|
---|
| 264 | /**
|
---|
| 265 | * POST method
|
---|
| 266 | */
|
---|
| 267 | if (req.method === 'POST') {
|
---|
| 268 | const { body } = req;
|
---|
| 269 |
|
---|
| 270 | /**
|
---|
| 271 | * /---------------------- POST ----------------------/
|
---|
[ebf5e04] | 272 | * Checks if the entered account info is good, and registers a new user in the database if so.
|
---|
[87614a5] | 273 | * @action register
|
---|
| 274 | * @param username
|
---|
| 275 | * @param displayName
|
---|
| 276 | * @param password
|
---|
| 277 | */
|
---|
| 278 | if (body?.action === 'register') {
|
---|
| 279 | // checks
|
---|
| 280 | if (body?.username == "undefined" || body?.username == "null" || body?.username == "") {
|
---|
| 281 | res.json({
|
---|
| 282 | success: false,
|
---|
| 283 | message: 'Username is required',
|
---|
| 284 | });
|
---|
| 285 | return ;
|
---|
| 286 | }
|
---|
| 287 | if (/[^a-zA-Z]/g.test(body?.username)) {
|
---|
| 288 | res.json({
|
---|
| 289 | success: false,
|
---|
| 290 | message: 'Username must contain only letters',
|
---|
| 291 | })
|
---|
| 292 | return ;
|
---|
| 293 | }
|
---|
| 294 | if (body?.displayName == "undefined" || body?.displayName == "null" || body?.displayName == "") {
|
---|
| 295 | res.json({
|
---|
| 296 | success: false,
|
---|
| 297 | message: 'Display name is required',
|
---|
| 298 | });
|
---|
| 299 | return ;
|
---|
| 300 | }
|
---|
| 301 | if (body?.displayName?.toLowerCase() === "guest") {
|
---|
| 302 | res.json({
|
---|
| 303 | success: false,
|
---|
| 304 | message: 'Display name cannot be guest',
|
---|
| 305 | });
|
---|
| 306 | return ;
|
---|
| 307 | }
|
---|
| 308 | if (body?.password == "undefined" || body?.password == "null" || body?.password == "") {
|
---|
| 309 | res.json({
|
---|
| 310 | success: false,
|
---|
| 311 | message: 'Password is required',
|
---|
| 312 | });
|
---|
| 313 | return ;
|
---|
| 314 | }
|
---|
| 315 |
|
---|
| 316 | // everything's okay
|
---|
| 317 | body.username = body.username.toLowerCase()
|
---|
| 318 |
|
---|
| 319 | // hash password
|
---|
| 320 | const salt = crypto.randomBytes(16).toString('hex');
|
---|
| 321 | const hashedPassword = crypto.pbkdf2Sync(body.password, salt, 1000, 64, 'sha512').toString('hex');
|
---|
| 322 |
|
---|
| 323 | // check if user already exists
|
---|
| 324 | pool.query('SELECT * FROM users WHERE username = $1', [body.username], (error, results) => {
|
---|
| 325 | if (error) throw error;
|
---|
| 326 |
|
---|
| 327 | if (results.rows.length > 0) {
|
---|
| 328 | res.json({
|
---|
| 329 | success: false,
|
---|
| 330 | message: 'Username already exists',
|
---|
| 331 | });
|
---|
| 332 | return ;
|
---|
| 333 | }
|
---|
| 334 |
|
---|
| 335 | // store user in database
|
---|
| 336 | pool.query('INSERT INTO users (username, password, salt) VALUES ($1, $2, $3)', [body.username, hashedPassword, salt], (error, usersResults) => {
|
---|
| 337 | if (error) throw error;
|
---|
| 338 |
|
---|
| 339 | pool.query('INSERT INTO players (username, display_name, credits) VALUES ($1, $2, $3)', [body.username, body.displayName, 1000], (error, playersResults) => {
|
---|
| 340 | if (error) throw error;
|
---|
| 341 |
|
---|
| 342 | 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) => {
|
---|
| 343 | if (error) throw error;
|
---|
| 344 |
|
---|
| 345 | res.json({
|
---|
| 346 | success: true,
|
---|
| 347 | message: 'Registration successful',
|
---|
| 348 | });
|
---|
| 349 | return ;
|
---|
| 350 | });
|
---|
| 351 | });
|
---|
| 352 | });
|
---|
| 353 | });
|
---|
| 354 | }
|
---|
| 355 |
|
---|
| 356 | /**
|
---|
| 357 | * /---------------------- POST ----------------------/
|
---|
[ebf5e04] | 358 | * Checks if the entered account info is good, and logs the user in if so.
|
---|
[87614a5] | 359 | * @action login
|
---|
| 360 | * @param username
|
---|
| 361 | * @param password
|
---|
| 362 | */
|
---|
| 363 | if (body?.action === 'login') {
|
---|
| 364 | // checks
|
---|
| 365 | if (body?.username == "undefined" || body?.username == "null" || body?.username == "") {
|
---|
| 366 | res.json({
|
---|
| 367 | success: false,
|
---|
| 368 | message: 'Username is required',
|
---|
| 369 | });
|
---|
| 370 | return ;
|
---|
| 371 | }
|
---|
| 372 | if (/[^a-zA-Z]/g.test(body?.username)) {
|
---|
| 373 | res.json({
|
---|
| 374 | success: false,
|
---|
| 375 | message: 'Username must contain only letters',
|
---|
| 376 | })
|
---|
| 377 | return ;
|
---|
| 378 | }
|
---|
| 379 | if (body?.password == "undefined" || body?.password == "null" || body?.password == "") {
|
---|
| 380 | res.json({
|
---|
| 381 | success: false,
|
---|
| 382 | message: 'Password is required',
|
---|
| 383 | });
|
---|
| 384 | return ;
|
---|
| 385 | }
|
---|
| 386 |
|
---|
| 387 | // everything's okay
|
---|
| 388 | body.username = body.username.toLowerCase();
|
---|
| 389 |
|
---|
| 390 | // check if user exists
|
---|
| 391 | pool.query('SELECT * FROM users WHERE username = $1', [body.username], (error, usersResults) => {
|
---|
| 392 | if (error) throw error;
|
---|
| 393 |
|
---|
| 394 | if (usersResults.rows.length === 0) {
|
---|
| 395 | res.json({
|
---|
| 396 | success: false,
|
---|
| 397 | message: 'User does not exist. Try Registering instead.',
|
---|
| 398 | });
|
---|
| 399 | return ;
|
---|
| 400 | }
|
---|
| 401 | else {
|
---|
| 402 | if (usersResults.rows.length > 0) {
|
---|
| 403 | const user = usersResults.rows[0];
|
---|
| 404 | const salt = user.salt;
|
---|
| 405 | const hashedPassword = crypto.pbkdf2Sync(body.password, salt, 1000, 64, 'sha512').toString('hex');
|
---|
| 406 |
|
---|
| 407 | if (hashedPassword === user.password) {
|
---|
| 408 | pool.query('SELECT * FROM players WHERE username = $1', [body.username], (error, playersResults) => {
|
---|
| 409 | if (playersResults.rows.length > 0) {
|
---|
| 410 | let session = sessions.find(session => session.username === playersResults.rows[0].username)
|
---|
| 411 |
|
---|
| 412 | if (session) {
|
---|
| 413 | // Already logged in
|
---|
| 414 | res.json({
|
---|
[aac3b2b] | 415 | success: true,
|
---|
| 416 | message: 'Login successful',
|
---|
| 417 | session: session,
|
---|
[87614a5] | 418 | })
|
---|
| 419 | }
|
---|
| 420 | else {
|
---|
| 421 | // create a session
|
---|
| 422 | session = {
|
---|
| 423 | id: uuidv4(),
|
---|
| 424 | displayName: playersResults.rows[0].display_name,
|
---|
| 425 | username: playersResults.rows[0].username,
|
---|
| 426 | credits: playersResults.rows[0].credits,
|
---|
| 427 | lastActivity: Date.now(),
|
---|
| 428 | }
|
---|
| 429 |
|
---|
| 430 | sessions.push(session);
|
---|
| 431 |
|
---|
| 432 | res.json({
|
---|
| 433 | success: true,
|
---|
| 434 | message: 'Login successful',
|
---|
| 435 | session: session,
|
---|
| 436 | })
|
---|
| 437 | }
|
---|
| 438 |
|
---|
| 439 | return ;
|
---|
| 440 | }
|
---|
| 441 | });
|
---|
| 442 | }
|
---|
| 443 | else {
|
---|
| 444 | res.json({
|
---|
| 445 | success: false,
|
---|
| 446 | message: 'Username and password do not match.',
|
---|
| 447 | });
|
---|
| 448 | }
|
---|
| 449 | }
|
---|
| 450 | }
|
---|
| 451 | });
|
---|
| 452 | }
|
---|
| 453 | }
|
---|
| 454 | } |
---|