[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 |
|
---|
[e007fcd] | 9 | const nodemailer = require('nodemailer');
|
---|
| 10 |
|
---|
[95ce58b] | 11 | import { progressRoundTillTheEnd } from '../poker/tableSpecific';
|
---|
| 12 |
|
---|
[87614a5] | 13 | const Pool = require('pg').Pool
|
---|
| 14 | const pool = new Pool({
|
---|
[64dc53b] | 15 | connectionString: `postgres://${process.env.POSTGRES_USER}:${process.env.POSTGRES_PASSWORD}@${process.env.POSTGRES_HOST}/${process.env.POSTGRES_DB}`
|
---|
[87614a5] | 16 | });
|
---|
| 17 |
|
---|
| 18 | export default function handler(req, res) {
|
---|
| 19 | /**
|
---|
| 20 | * GET method
|
---|
| 21 | */
|
---|
| 22 | if (req.method === 'GET') {
|
---|
| 23 | /**
|
---|
| 24 | * /---------------------- GET ----------------------/
|
---|
[ebf5e04] | 25 | * If the player won credits, update them in the database.
|
---|
| 26 | * Also, update the stats in the database.
|
---|
[87614a5] | 27 | * @action give_credits
|
---|
| 28 | * @param session_id
|
---|
| 29 | * @param credits
|
---|
| 30 | */
|
---|
| 31 | if (req.query?.action === 'add_credits' && req.query?.session_id && req.query?.credits) {
|
---|
| 32 | const session_id = req.query.session_id
|
---|
| 33 | const session = sessions.find(session => session.id === session_id)
|
---|
| 34 |
|
---|
| 35 | if (session) {
|
---|
| 36 | session.lastActivity = Date.now();
|
---|
| 37 |
|
---|
| 38 | if (parseInt(req.query.credits) > 0) {
|
---|
| 39 | session.credits = session.credits + parseInt(req.query.credits)
|
---|
| 40 |
|
---|
| 41 | pool.query('UPDATE players SET credits = $1 WHERE username = $2', [session.credits, session.username], (error, results) => {
|
---|
| 42 | if (error) throw error;
|
---|
| 43 | });
|
---|
| 44 | }
|
---|
| 45 |
|
---|
| 46 | if (req.query?.dont_update_stats) {
|
---|
| 47 | // continue
|
---|
| 48 | } else {
|
---|
| 49 | pool.query('SELECT * FROM stats WHERE username = $1', [session.username], (error, results) => {
|
---|
| 50 | if (error) throw error;
|
---|
| 51 |
|
---|
| 52 | if (results.rows.length > 0) {
|
---|
| 53 | const stats = results.rows[0]
|
---|
| 54 |
|
---|
| 55 | if (parseInt(req.query.credits) > 0) {
|
---|
| 56 | pool.query('UPDATE stats SET money_earned = $1 WHERE username = $2', [parseInt(stats.money_earned) + parseInt(req.query.credits), session.username], (error, results) => {
|
---|
| 57 | if (error) throw error;
|
---|
| 58 | });
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | if (req.query?.game === 'blackjack') {
|
---|
| 62 | if (req.query?.outcome === 'player_busted' || req.query?.outcome === 'player_lost') {
|
---|
| 63 | pool.query('UPDATE stats SET blackjack_games = $1 WHERE username = $2', [parseInt(stats.blackjack_games) + 1, session.username], (error, results) => {
|
---|
| 64 | if (error) throw error;
|
---|
| 65 | });
|
---|
| 66 | }
|
---|
| 67 | else if (req.query?.outcome === 'dealer_busted' || req.query?.outcome === 'player_won') {
|
---|
| 68 | 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) => {
|
---|
| 69 | if (error) throw error;
|
---|
| 70 | });
|
---|
| 71 | }
|
---|
| 72 | }
|
---|
[ace7865] | 73 | else if (req.query?.game === 'roulette') {
|
---|
| 74 | if (req.query?.outcome === 'lost') {
|
---|
| 75 | pool.query('UPDATE stats SET roulette_games = $1 WHERE username = $2', [parseInt(stats.roulette_games) + 1, session.username], (error, results) => {
|
---|
| 76 | if (error) throw error;
|
---|
| 77 | });
|
---|
| 78 | }
|
---|
| 79 | else if (req.query?.outcome === 'won') {
|
---|
| 80 | 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) => {
|
---|
| 81 | if (error) throw error;
|
---|
| 82 | });
|
---|
| 83 | }
|
---|
| 84 | }
|
---|
[3a783f2] | 85 | else if (req.query?.game === 'poker') {
|
---|
| 86 | if (req.query?.outcome === 'lost') {
|
---|
| 87 | pool.query('UPDATE stats SET poker_games = $1 WHERE username = $2', [parseInt(stats.poker_games) + 1, session.username], (error, results) => {
|
---|
| 88 | if (error) throw error;
|
---|
| 89 | });
|
---|
| 90 | }
|
---|
| 91 | else if (req.query?.outcome === 'won') {
|
---|
| 92 | 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) => {
|
---|
| 93 | if (error) throw error;
|
---|
| 94 | });
|
---|
| 95 | }
|
---|
| 96 | }
|
---|
[87614a5] | 97 | }
|
---|
| 98 | });
|
---|
| 99 | }
|
---|
[95ce58b] | 100 |
|
---|
| 101 | update_sessions_to_database();
|
---|
[87614a5] | 102 |
|
---|
| 103 | res.json({
|
---|
| 104 | success: true,
|
---|
| 105 | credits: session.credits,
|
---|
| 106 | })
|
---|
| 107 |
|
---|
| 108 | return ;
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | res.json({
|
---|
| 112 | success: false,
|
---|
| 113 | })
|
---|
| 114 | }
|
---|
| 115 |
|
---|
| 116 | /**
|
---|
| 117 | * /---------------------- GET ----------------------/
|
---|
[ebf5e04] | 118 | * The player lost credits, update this in the database.
|
---|
[87614a5] | 119 | * @action take_credits
|
---|
| 120 | * @param session_id
|
---|
| 121 | * @param credits
|
---|
| 122 | */
|
---|
| 123 | if (req.query?.action === 'take_credits' && req.query?.session_id && req.query?.credits) {
|
---|
| 124 | const session_id = req.query.session_id
|
---|
| 125 | const session = sessions.find(session => session.id === session_id)
|
---|
| 126 |
|
---|
[3a783f2] | 127 | let takeWhatYouCan = false;
|
---|
| 128 | if (req.query?.takeWhatYouCan === "true") takeWhatYouCan = true;
|
---|
| 129 |
|
---|
[87614a5] | 130 | if (session) {
|
---|
| 131 | session.lastActivity = Date.now();
|
---|
| 132 |
|
---|
[3a783f2] | 133 | if (session.credits < parseInt(req.query.credits)) {
|
---|
| 134 | if (takeWhatYouCan) {
|
---|
| 135 | session.credits = 0;
|
---|
| 136 | }
|
---|
| 137 | else {
|
---|
| 138 | res.json({
|
---|
| 139 | success: false,
|
---|
| 140 | });
|
---|
| 141 |
|
---|
| 142 | return ;
|
---|
| 143 | }
|
---|
| 144 | }
|
---|
| 145 | else {
|
---|
| 146 | session.credits = session.credits - parseInt(req.query.credits)
|
---|
| 147 | }
|
---|
[87614a5] | 148 |
|
---|
| 149 | pool.query('UPDATE players SET credits = $1 WHERE username = $2', [session.credits, session.username], (error, results) => {
|
---|
| 150 | if (error) throw error;
|
---|
| 151 | });
|
---|
| 152 |
|
---|
| 153 | pool.query('SELECT * FROM stats WHERE username = $1', [session.username], (error, results) => {
|
---|
| 154 | if (error) throw error;
|
---|
| 155 |
|
---|
| 156 | if (results.rows.length > 0) {
|
---|
| 157 | const stats = results.rows[0]
|
---|
| 158 |
|
---|
| 159 | pool.query('UPDATE stats SET money_bet = $1 WHERE username = $2', [parseInt(stats.money_bet) + parseInt(req.query.credits), session.username], (error, results) => {
|
---|
| 160 | if (error) throw error;
|
---|
| 161 | });
|
---|
| 162 | }
|
---|
| 163 | });
|
---|
| 164 |
|
---|
[95ce58b] | 165 | update_sessions_to_database();
|
---|
| 166 |
|
---|
[87614a5] | 167 | res.json({
|
---|
| 168 | success: true,
|
---|
| 169 | credits: session.credits,
|
---|
| 170 | })
|
---|
| 171 | return ;
|
---|
| 172 | }
|
---|
| 173 |
|
---|
| 174 | res.json({
|
---|
| 175 | success: false,
|
---|
| 176 | })
|
---|
| 177 | }
|
---|
| 178 |
|
---|
| 179 | /**
|
---|
| 180 | * /---------------------- GET ----------------------/
|
---|
[ebf5e04] | 181 | * Get stats for the player, so we can display them in the front end.
|
---|
[87614a5] | 182 | * @action get_stats
|
---|
| 183 | * @param session_id
|
---|
| 184 | */
|
---|
| 185 | if (req.query?.action === 'get_stats' && req.query?.session_id) {
|
---|
| 186 | const session_id = req.query.session_id
|
---|
| 187 | const session = sessions.find(session => session.id === session_id)
|
---|
| 188 |
|
---|
| 189 | if (session) {
|
---|
| 190 | session.lastActivity = Date.now();
|
---|
| 191 |
|
---|
| 192 | pool.query('SELECT * FROM stats WHERE username = $1', [session.username], (error, results) => {
|
---|
| 193 | if (error) throw error;
|
---|
| 194 |
|
---|
| 195 | if (results.rows.length > 0) {
|
---|
| 196 | res.json({
|
---|
| 197 | success: true,
|
---|
| 198 | stats: results.rows[0],
|
---|
| 199 | })
|
---|
| 200 | }
|
---|
| 201 | else {
|
---|
| 202 | res.json({
|
---|
| 203 | success: false,
|
---|
| 204 | })
|
---|
| 205 | }
|
---|
| 206 | });
|
---|
| 207 |
|
---|
| 208 | return ;
|
---|
| 209 | }
|
---|
| 210 |
|
---|
| 211 | res.json({
|
---|
| 212 | success: false,
|
---|
| 213 | })
|
---|
| 214 | }
|
---|
| 215 |
|
---|
[e007fcd] | 216 | /**
|
---|
| 217 | * /---------------------- GET ----------------------/
|
---|
| 218 | * Activates an user account if not activated.
|
---|
| 219 | * @action activate_account
|
---|
| 220 | * @param emailActivationId
|
---|
| 221 | */
|
---|
| 222 | if (req.query?.action === 'activate_account' && req.query?.emailActivationId) {
|
---|
| 223 | pool.query('SELECT * FROM users WHERE email_activation_id = $1', [req.query.emailActivationId], (error, results) => {
|
---|
| 224 | if (error) throw error;
|
---|
| 225 |
|
---|
| 226 | if (results.rows.length > 0) {
|
---|
| 227 | pool.query('UPDATE users SET activated = $1 WHERE email_activation_id = $2', [true, req.query.emailActivationId], (error, results) => {
|
---|
| 228 | if (error) throw error;
|
---|
| 229 |
|
---|
| 230 | res.json({
|
---|
| 231 | success: true,
|
---|
| 232 | })
|
---|
| 233 | });
|
---|
| 234 | }
|
---|
| 235 | else {
|
---|
| 236 | res.json({
|
---|
| 237 | success: false,
|
---|
| 238 | })
|
---|
| 239 | }
|
---|
| 240 | });
|
---|
| 241 | }
|
---|
| 242 |
|
---|
[87614a5] | 243 | /**
|
---|
| 244 | * /---------------------- GET ----------------------/
|
---|
[285c3cc] | 245 | * Checks if the player is logged in, and returns his session if so.
|
---|
[87614a5] | 246 | * @action check_if_logged_in
|
---|
| 247 | * @param session_id
|
---|
| 248 | */
|
---|
| 249 | if (req.query?.action === 'check_if_logged_in' && req.query?.session_id) {
|
---|
| 250 | const session_id = req.query.session_id
|
---|
| 251 | const session = sessions.find(session => session.id === session_id)
|
---|
| 252 |
|
---|
| 253 | if (session) {
|
---|
| 254 | res.json({
|
---|
| 255 | success: true,
|
---|
| 256 | displayName: session.displayName,
|
---|
[e007fcd] | 257 | username: session.username,
|
---|
[87614a5] | 258 | session_id: session.id,
|
---|
| 259 | credits: session.credits,
|
---|
| 260 | })
|
---|
| 261 | return ;
|
---|
| 262 | }
|
---|
| 263 |
|
---|
| 264 | res.json({
|
---|
| 265 | success: false,
|
---|
| 266 | })
|
---|
| 267 | }
|
---|
| 268 |
|
---|
| 269 | /**
|
---|
| 270 | * /---------------------- GET ----------------------/
|
---|
[ebf5e04] | 271 | * Takes the credits in the player's session, and updates the database.
|
---|
| 272 | * Logs the player out and kills the session.
|
---|
[87614a5] | 273 | * @action logout
|
---|
| 274 | * @param session_id
|
---|
| 275 | */
|
---|
| 276 | if (req.query?.action === 'logout' && req.query?.session_id) {
|
---|
| 277 | const session_id = req.query.session_id
|
---|
| 278 | const session = sessions.find(session => session.id === session_id)
|
---|
| 279 |
|
---|
| 280 | if (session) {
|
---|
| 281 | pool.query('UPDATE players SET credits = $1 WHERE username = $2', [session.credits, session.username], (error, results) => {
|
---|
| 282 | if (error) throw error;
|
---|
| 283 | });
|
---|
| 284 |
|
---|
| 285 | sessions.splice(sessions.indexOf(session), 1);
|
---|
[e007fcd] | 286 | update_sessions_to_database();
|
---|
[87614a5] | 287 |
|
---|
[e007fcd] | 288 | // remove player from games:
|
---|
| 289 | if (rooms[session_id] !== undefined) {
|
---|
| 290 | delete rooms[session_id];
|
---|
| 291 | update_rooms_to_database();
|
---|
| 292 | }
|
---|
[95ce58b] | 293 |
|
---|
[e007fcd] | 294 | if (game.players?.map(e=>e.session_id).indexOf(session_id) !== -1) {
|
---|
| 295 | game.players?.splice(game.players?.map(e=>e.session_id).indexOf(session_id), 1);
|
---|
| 296 | update_game_to_database();
|
---|
| 297 | }
|
---|
| 298 |
|
---|
| 299 | tables.forEach(table => {
|
---|
| 300 | table.players?.forEach(player => {
|
---|
| 301 | if (player.id === session_id) {
|
---|
| 302 | player.isGhost = true;
|
---|
| 303 | }
|
---|
| 304 | })
|
---|
| 305 | })
|
---|
| 306 | update_tables_to_database();
|
---|
[87614a5] | 307 | }
|
---|
| 308 |
|
---|
| 309 | res.json({
|
---|
| 310 | success: true,
|
---|
| 311 | message: 'Successfully logged out',
|
---|
| 312 | })
|
---|
| 313 | }
|
---|
| 314 | }
|
---|
| 315 |
|
---|
| 316 | /**
|
---|
| 317 | * POST method
|
---|
| 318 | */
|
---|
| 319 | if (req.method === 'POST') {
|
---|
| 320 | const { body } = req;
|
---|
| 321 |
|
---|
[433e0c5] | 322 | /**
|
---|
| 323 | * /---------------------- POST ----------------------/
|
---|
| 324 | * Deposits money from credit card to game account.
|
---|
| 325 | * @action register
|
---|
| 326 | * @param session_id
|
---|
| 327 | * @param data
|
---|
| 328 | */
|
---|
| 329 | if (body?.action === 'deposit') {
|
---|
| 330 | // checks
|
---|
| 331 | if (body?.session_id == "undefined" || body?.session_id == "null" || body?.session_id == "") {
|
---|
| 332 | res.json({
|
---|
| 333 | success: false,
|
---|
| 334 | message: 'You are not logged in. Please log in first.',
|
---|
| 335 | });
|
---|
| 336 | return ;
|
---|
| 337 | }
|
---|
| 338 | if (body?.data?.name == "undefined" || body?.data?.name == "null" || body?.data?.name == "") {
|
---|
| 339 | res.json({
|
---|
| 340 | success: false,
|
---|
| 341 | message: 'Name field cannot be empty',
|
---|
| 342 | });
|
---|
| 343 | return ;
|
---|
| 344 | }
|
---|
| 345 | if (body?.data?.card == "undefined" || body?.data?.card == "null" || body?.data?.card == "") {
|
---|
| 346 | res.json({
|
---|
| 347 | success: false,
|
---|
| 348 | message: 'Card numbers field cannot be empty',
|
---|
| 349 | });
|
---|
| 350 | return ;
|
---|
| 351 | }
|
---|
| 352 | if (body?.data?.expire == "undefined" || body?.data?.expire == "null" || body?.data?.expire == "") {
|
---|
| 353 | res.json({
|
---|
| 354 | success: false,
|
---|
| 355 | message: 'Expiration date field cannot be empty',
|
---|
| 356 | });
|
---|
| 357 | return ;
|
---|
| 358 | }
|
---|
| 359 | if (body?.data?.ccv == "undefined" || body?.data?.ccv == "null" || body?.data?.ccv == "") {
|
---|
| 360 | res.json({
|
---|
| 361 | success: false,
|
---|
| 362 | message: 'CCV field cannot be empty',
|
---|
| 363 | });
|
---|
| 364 | return ;
|
---|
| 365 | }
|
---|
| 366 | if (body?.data?.amount == "undefined" || body?.data?.amount == "null" || body?.data?.amount == "") {
|
---|
| 367 | res.json({
|
---|
| 368 | success: false,
|
---|
| 369 | message: 'Amount field cannot be empty',
|
---|
| 370 | });
|
---|
| 371 | return ;
|
---|
| 372 | }
|
---|
[55701f0] | 373 | if (parseInt(body?.data?.amount) > 5000) {
|
---|
| 374 | res.json({
|
---|
| 375 | success: false,
|
---|
| 376 | message: 'Failed to deposit. Insufficient credit on card.',
|
---|
| 377 | });
|
---|
| 378 | return ;
|
---|
| 379 | }
|
---|
[433e0c5] | 380 |
|
---|
| 381 | let session = sessions.find(session => session.id === body?.session_id)
|
---|
| 382 |
|
---|
| 383 | if (session) {
|
---|
| 384 | if (parseInt(body.data.amount) > 0) {
|
---|
| 385 | session.credits = session.credits + parseInt(body.data.amount)
|
---|
| 386 |
|
---|
| 387 | pool.query('UPDATE players SET credits = $1 WHERE username = $2', [session.credits, session.username], (error, results) => {
|
---|
| 388 | if (error) throw error;
|
---|
| 389 |
|
---|
| 390 | res.json({
|
---|
| 391 | success: true,
|
---|
| 392 | credits: session.credits
|
---|
| 393 | })
|
---|
[55701f0] | 394 |
|
---|
| 395 | update_sessions_to_database();
|
---|
[433e0c5] | 396 | });
|
---|
| 397 | }
|
---|
| 398 | }
|
---|
| 399 | }
|
---|
| 400 |
|
---|
| 401 | /**
|
---|
| 402 | * /---------------------- POST ----------------------/
|
---|
| 403 | * Withdraws money from game account to personal account.
|
---|
| 404 | * @action register
|
---|
| 405 | * @param session_id
|
---|
| 406 | * @param data
|
---|
| 407 | */
|
---|
| 408 | if (body?.action === 'withdraw') {
|
---|
| 409 | // checks
|
---|
| 410 | if (body?.session_id == "undefined" || body?.session_id == "null" || body?.session_id == "") {
|
---|
| 411 | res.json({
|
---|
| 412 | success: false,
|
---|
| 413 | message: 'You are not logged in. Please log in first.',
|
---|
| 414 | });
|
---|
| 415 | return ;
|
---|
| 416 | }
|
---|
| 417 | if (body?.data?.citibank == "undefined" || body?.data?.citibank == "null" || body?.data?.citibank == "") {
|
---|
| 418 | res.json({
|
---|
| 419 | success: false,
|
---|
| 420 | message: 'Bank name field cannot be empty',
|
---|
| 421 | });
|
---|
| 422 | return ;
|
---|
| 423 | }
|
---|
| 424 | if (body?.data?.iban == "undefined" || body?.data?.iban == "null" || body?.data?.iban == "") {
|
---|
| 425 | res.json({
|
---|
| 426 | success: false,
|
---|
| 427 | message: 'IBAN code field cannot be empty',
|
---|
| 428 | });
|
---|
| 429 | return ;
|
---|
| 430 | }
|
---|
| 431 | if (body?.data?.bic == "undefined" || body?.data?.bic == "null" || body?.data?.bic == "") {
|
---|
| 432 | res.json({
|
---|
| 433 | success: false,
|
---|
| 434 | message: 'BIC code field cannot be empty',
|
---|
| 435 | });
|
---|
| 436 | return ;
|
---|
| 437 | }
|
---|
| 438 | if (body?.data?.beneficiary == "undefined" || body?.data?.beneficiary == "null" || body?.data?.beneficiary == "") {
|
---|
| 439 | res.json({
|
---|
| 440 | success: false,
|
---|
| 441 | message: 'Beneficiary name field cannot be empty',
|
---|
| 442 | });
|
---|
| 443 | return ;
|
---|
| 444 | }
|
---|
| 445 | if (body?.data?.address == "undefined" || body?.data?.address == "null" || body?.data?.address == "") {
|
---|
| 446 | res.json({
|
---|
| 447 | success: false,
|
---|
| 448 | message: 'Bank address field cannot be empty',
|
---|
| 449 | });
|
---|
| 450 | return ;
|
---|
| 451 | }
|
---|
| 452 | if (body?.data?.amount == "undefined" || body?.data?.amount == "null" || body?.data?.amount == "") {
|
---|
| 453 | res.json({
|
---|
| 454 | success: false,
|
---|
| 455 | message: 'Amount field cannot be empty',
|
---|
| 456 | });
|
---|
| 457 | return ;
|
---|
| 458 | }
|
---|
| 459 |
|
---|
| 460 | let session = sessions.find(session => session.id === body?.session_id)
|
---|
| 461 |
|
---|
| 462 | if (session) {
|
---|
| 463 | if (parseInt(body.data.amount) > 0) {
|
---|
| 464 | session.credits = Math.max(session.credits - parseInt(body.data.amount), 0)
|
---|
| 465 |
|
---|
| 466 | pool.query('UPDATE players SET credits = $1 WHERE username = $2', [session.credits, session.username], (error, results) => {
|
---|
| 467 | if (error) throw error;
|
---|
| 468 |
|
---|
| 469 | res.json({
|
---|
| 470 | success: true,
|
---|
| 471 | credits: session.credits
|
---|
| 472 | })
|
---|
[55701f0] | 473 |
|
---|
| 474 | update_sessions_to_database();
|
---|
[433e0c5] | 475 | });
|
---|
| 476 | }
|
---|
| 477 | }
|
---|
| 478 | }
|
---|
| 479 |
|
---|
| 480 | /**
|
---|
| 481 | * /---------------------- POST ----------------------/
|
---|
| 482 | * Sends a complaint.
|
---|
| 483 | * @action complain
|
---|
| 484 | * @param session_id
|
---|
| 485 | * @param description
|
---|
| 486 | */
|
---|
| 487 | if (body?.action === 'complain') {
|
---|
| 488 | // checks
|
---|
| 489 | if (body?.session_id == "undefined" || body?.session_id == "null" || body?.session_id == "") {
|
---|
| 490 | res.json({
|
---|
| 491 | success: false,
|
---|
| 492 | message: 'You are not logged in. Please log in first.',
|
---|
| 493 | });
|
---|
| 494 | return ;
|
---|
| 495 | }
|
---|
| 496 | if (body?.description == "undefined" || body?.description == "null" || body?.description == "") {
|
---|
| 497 | res.json({
|
---|
| 498 | success: false,
|
---|
| 499 | message: 'You cannot submit an empty complaint.',
|
---|
| 500 | });
|
---|
| 501 | return ;
|
---|
| 502 | }
|
---|
| 503 |
|
---|
| 504 | let session = sessions.find(session => session.id === body.session_id)
|
---|
| 505 |
|
---|
| 506 | if (session) {
|
---|
| 507 | // date, by, description, answered
|
---|
| 508 | const date = new Date();
|
---|
| 509 | pool.query('INSERT INTO complaints (date, by, description, answered) VALUES ($1, $2, $3, $4)', [date, session.username, body.description, false], (error, complaintResults) => {
|
---|
| 510 | if (error) throw error;
|
---|
| 511 |
|
---|
| 512 | res.json({
|
---|
| 513 | success: true,
|
---|
| 514 | })
|
---|
| 515 | });
|
---|
| 516 | }
|
---|
| 517 | }
|
---|
| 518 |
|
---|
[87614a5] | 519 | /**
|
---|
| 520 | * /---------------------- POST ----------------------/
|
---|
[ebf5e04] | 521 | * Checks if the entered account info is good, and registers a new user in the database if so.
|
---|
[87614a5] | 522 | * @action register
|
---|
| 523 | * @param username
|
---|
| 524 | * @param displayName
|
---|
| 525 | * @param password
|
---|
| 526 | */
|
---|
| 527 | if (body?.action === 'register') {
|
---|
| 528 | // checks
|
---|
[e007fcd] | 529 | if (body?.email == "undefined" || body?.email == "null" || body?.email == "") {
|
---|
| 530 | res.json({
|
---|
| 531 | success: false,
|
---|
| 532 | message: 'Email is required',
|
---|
| 533 | });
|
---|
| 534 | return ;
|
---|
| 535 | }
|
---|
| 536 | if (!body?.email?.includes('@') || body?.email?.indexOf('@', body?.email?.indexOf('@')+1) !== -1) {
|
---|
| 537 | res.json({
|
---|
| 538 | success: false,
|
---|
| 539 | message: 'Not a valid email',
|
---|
| 540 | });
|
---|
| 541 | return ;
|
---|
| 542 | }
|
---|
[87614a5] | 543 | if (body?.username == "undefined" || body?.username == "null" || body?.username == "") {
|
---|
| 544 | res.json({
|
---|
| 545 | success: false,
|
---|
| 546 | message: 'Username is required',
|
---|
| 547 | });
|
---|
| 548 | return ;
|
---|
| 549 | }
|
---|
| 550 | if (/[^a-zA-Z]/g.test(body?.username)) {
|
---|
| 551 | res.json({
|
---|
| 552 | success: false,
|
---|
| 553 | message: 'Username must contain only letters',
|
---|
| 554 | })
|
---|
| 555 | return ;
|
---|
| 556 | }
|
---|
| 557 | if (body?.displayName == "undefined" || body?.displayName == "null" || body?.displayName == "") {
|
---|
| 558 | res.json({
|
---|
| 559 | success: false,
|
---|
| 560 | message: 'Display name is required',
|
---|
| 561 | });
|
---|
| 562 | return ;
|
---|
| 563 | }
|
---|
| 564 | if (body?.displayName?.toLowerCase() === "guest") {
|
---|
| 565 | res.json({
|
---|
| 566 | success: false,
|
---|
| 567 | message: 'Display name cannot be guest',
|
---|
| 568 | });
|
---|
| 569 | return ;
|
---|
| 570 | }
|
---|
| 571 | if (body?.password == "undefined" || body?.password == "null" || body?.password == "") {
|
---|
| 572 | res.json({
|
---|
| 573 | success: false,
|
---|
| 574 | message: 'Password is required',
|
---|
| 575 | });
|
---|
| 576 | return ;
|
---|
| 577 | }
|
---|
| 578 |
|
---|
| 579 | // everything's okay
|
---|
| 580 | body.username = body.username.toLowerCase()
|
---|
| 581 |
|
---|
| 582 | // hash password
|
---|
| 583 | const salt = crypto.randomBytes(16).toString('hex');
|
---|
| 584 | const hashedPassword = crypto.pbkdf2Sync(body.password, salt, 1000, 64, 'sha512').toString('hex');
|
---|
| 585 |
|
---|
| 586 | // check if user already exists
|
---|
| 587 | pool.query('SELECT * FROM users WHERE username = $1', [body.username], (error, results) => {
|
---|
| 588 | if (error) throw error;
|
---|
| 589 |
|
---|
| 590 | if (results.rows.length > 0) {
|
---|
| 591 | res.json({
|
---|
| 592 | success: false,
|
---|
| 593 | message: 'Username already exists',
|
---|
| 594 | });
|
---|
| 595 | return ;
|
---|
| 596 | }
|
---|
| 597 |
|
---|
[e007fcd] | 598 | const emailActivationId = uuidv4();
|
---|
| 599 |
|
---|
[87614a5] | 600 | // store user in database
|
---|
[e007fcd] | 601 | pool.query('INSERT INTO users (username, password, salt, email, email_activation_id, activated) VALUES ($1, $2, $3, $4, $5, $6)', [body.username, hashedPassword, salt, body.email, emailActivationId, false], (error, usersResults) => {
|
---|
[87614a5] | 602 | if (error) throw error;
|
---|
| 603 |
|
---|
| 604 | pool.query('INSERT INTO players (username, display_name, credits) VALUES ($1, $2, $3)', [body.username, body.displayName, 1000], (error, playersResults) => {
|
---|
| 605 | if (error) throw error;
|
---|
| 606 |
|
---|
| 607 | 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) => {
|
---|
| 608 | if (error) throw error;
|
---|
| 609 |
|
---|
[e007fcd] | 610 | sendMailForActivation(body.displayName, body.email, emailActivationId);
|
---|
| 611 |
|
---|
[87614a5] | 612 | res.json({
|
---|
| 613 | success: true,
|
---|
| 614 | message: 'Registration successful',
|
---|
| 615 | });
|
---|
| 616 | return ;
|
---|
| 617 | });
|
---|
| 618 | });
|
---|
| 619 | });
|
---|
| 620 | });
|
---|
| 621 | }
|
---|
| 622 |
|
---|
| 623 | /**
|
---|
| 624 | * /---------------------- POST ----------------------/
|
---|
[ebf5e04] | 625 | * Checks if the entered account info is good, and logs the user in if so.
|
---|
[87614a5] | 626 | * @action login
|
---|
| 627 | * @param username
|
---|
| 628 | * @param password
|
---|
| 629 | */
|
---|
| 630 | if (body?.action === 'login') {
|
---|
| 631 | // checks
|
---|
| 632 | if (body?.username == "undefined" || body?.username == "null" || body?.username == "") {
|
---|
| 633 | res.json({
|
---|
| 634 | success: false,
|
---|
| 635 | message: 'Username is required',
|
---|
| 636 | });
|
---|
| 637 | return ;
|
---|
| 638 | }
|
---|
| 639 | if (/[^a-zA-Z]/g.test(body?.username)) {
|
---|
| 640 | res.json({
|
---|
| 641 | success: false,
|
---|
| 642 | message: 'Username must contain only letters',
|
---|
| 643 | })
|
---|
| 644 | return ;
|
---|
| 645 | }
|
---|
| 646 | if (body?.password == "undefined" || body?.password == "null" || body?.password == "") {
|
---|
| 647 | res.json({
|
---|
| 648 | success: false,
|
---|
| 649 | message: 'Password is required',
|
---|
| 650 | });
|
---|
| 651 | return ;
|
---|
| 652 | }
|
---|
| 653 |
|
---|
| 654 | // everything's okay
|
---|
| 655 | body.username = body.username.toLowerCase();
|
---|
| 656 |
|
---|
| 657 | // check if user exists
|
---|
| 658 | pool.query('SELECT * FROM users WHERE username = $1', [body.username], (error, usersResults) => {
|
---|
| 659 | if (error) throw error;
|
---|
| 660 |
|
---|
| 661 | if (usersResults.rows.length === 0) {
|
---|
| 662 | res.json({
|
---|
| 663 | success: false,
|
---|
| 664 | message: 'User does not exist. Try Registering instead.',
|
---|
| 665 | });
|
---|
| 666 | return ;
|
---|
| 667 | }
|
---|
| 668 | else {
|
---|
| 669 | if (usersResults.rows.length > 0) {
|
---|
| 670 | const user = usersResults.rows[0];
|
---|
[e007fcd] | 671 |
|
---|
[87614a5] | 672 | const salt = user.salt;
|
---|
| 673 | const hashedPassword = crypto.pbkdf2Sync(body.password, salt, 1000, 64, 'sha512').toString('hex');
|
---|
| 674 |
|
---|
| 675 | if (hashedPassword === user.password) {
|
---|
[e007fcd] | 676 | if (user.activated === "false") {
|
---|
| 677 | res.json({
|
---|
| 678 | success: false,
|
---|
| 679 | message: 'Account not activated. Check your email.',
|
---|
| 680 | })
|
---|
| 681 |
|
---|
| 682 | return ;
|
---|
| 683 | }
|
---|
| 684 |
|
---|
[87614a5] | 685 | pool.query('SELECT * FROM players WHERE username = $1', [body.username], (error, playersResults) => {
|
---|
| 686 | if (playersResults.rows.length > 0) {
|
---|
| 687 | let session = sessions.find(session => session.username === playersResults.rows[0].username)
|
---|
| 688 |
|
---|
| 689 | if (session) {
|
---|
| 690 | // Already logged in
|
---|
| 691 | res.json({
|
---|
[aac3b2b] | 692 | success: true,
|
---|
| 693 | message: 'Login successful',
|
---|
| 694 | session: session,
|
---|
[87614a5] | 695 | })
|
---|
| 696 | }
|
---|
| 697 | else {
|
---|
| 698 | // create a session
|
---|
| 699 | session = {
|
---|
| 700 | id: uuidv4(),
|
---|
| 701 | displayName: playersResults.rows[0].display_name,
|
---|
| 702 | username: playersResults.rows[0].username,
|
---|
| 703 | credits: playersResults.rows[0].credits,
|
---|
| 704 | lastActivity: Date.now(),
|
---|
| 705 | }
|
---|
[95ce58b] | 706 |
|
---|
[87614a5] | 707 | sessions.push(session);
|
---|
[95ce58b] | 708 |
|
---|
| 709 | update_sessions_to_database();
|
---|
[87614a5] | 710 |
|
---|
| 711 | res.json({
|
---|
| 712 | success: true,
|
---|
| 713 | message: 'Login successful',
|
---|
| 714 | session: session,
|
---|
| 715 | })
|
---|
| 716 | }
|
---|
| 717 |
|
---|
| 718 | return ;
|
---|
| 719 | }
|
---|
| 720 | });
|
---|
| 721 | }
|
---|
| 722 | else {
|
---|
| 723 | res.json({
|
---|
| 724 | success: false,
|
---|
| 725 | message: 'Username and password do not match.',
|
---|
| 726 | });
|
---|
| 727 | }
|
---|
| 728 | }
|
---|
| 729 | }
|
---|
| 730 | });
|
---|
| 731 | }
|
---|
| 732 | }
|
---|
[95ce58b] | 733 | }
|
---|
| 734 |
|
---|
[e007fcd] | 735 | // Mailing
|
---|
| 736 | const transporter = nodemailer.createTransport({
|
---|
| 737 | service: 'gmail',
|
---|
| 738 | auth: {
|
---|
| 739 | user: process.env.GOOGLE_EMAIL,
|
---|
| 740 | pass: process.env.GOOGLE_APP_PASSWORD,
|
---|
| 741 | }
|
---|
| 742 | })
|
---|
| 743 |
|
---|
| 744 | function sendMailForActivation(displayName, userEmail, emailActivationId) {
|
---|
| 745 | const message = {
|
---|
| 746 | from: process.env.GOOGLE_EMAIL,
|
---|
| 747 | to: userEmail,
|
---|
| 748 | subject: "Caessino - Activate your account",
|
---|
| 749 | html: `
|
---|
| 750 | <h4>Hello, ${displayName}</h4>
|
---|
| 751 | <p>Thank you for creating an account at Caessino. Just one more step and you can start enjoying the games!</p>
|
---|
| 752 | <p>To activate your account please follow this link: <a target="_blank" href="${process.env.HOME_URL}/activate/${emailActivationId}">Activate account</a>
|
---|
| 753 | <br/>
|
---|
| 754 | <p>Cheers and happy playing,</p>
|
---|
| 755 | <p>The Team ESS</p>
|
---|
| 756 | `
|
---|
| 757 | }
|
---|
| 758 |
|
---|
| 759 | transporter.sendMail(message, (err, data) => {
|
---|
| 760 | if (err) {
|
---|
| 761 | console.log(err);
|
---|
| 762 | }
|
---|
| 763 | })
|
---|
| 764 | }
|
---|
| 765 |
|
---|
| 766 | let mailSentTo = {
|
---|
| 767 | poker: [],
|
---|
| 768 | roulette: [],
|
---|
| 769 | blackjack: [],
|
---|
| 770 | }
|
---|
| 771 | function sendMailForGameCompletition(game, username, displayName) {
|
---|
| 772 | return ;
|
---|
| 773 |
|
---|
| 774 | const msgPoker = 'Your game was played to the end by the computer with the following rules:<br/>1. No more bets were made by any player;<br/>2. Cards were dealt normally like they would be under normal circumstances;<br/>3. Credits were given to the winners and taken from the losers.';
|
---|
| 775 | const msgRoulette = 'If you reconnect immediately, you can catch this ongoing game. But don\'t worry if you can\'t! If you win, credits will be awarded to you.';
|
---|
| 776 | const msgBlackjack = 'You can now continue playing your game.';
|
---|
| 777 |
|
---|
| 778 | pool.query('SELECT * FROM users WHERE username = $1', [username], (error, results) => {
|
---|
| 779 | if (error) throw error;
|
---|
| 780 |
|
---|
| 781 | if (results.rows.length > 0) {
|
---|
| 782 | const userEmail = results.rows[0].email;
|
---|
| 783 |
|
---|
| 784 | if ((game === 'poker' && mailSentTo.poker.indexOf(userEmail) === -1) ||
|
---|
| 785 | (game === 'roulette' && mailSentTo.roulette.indexOf(userEmail) === -1) ||
|
---|
| 786 | (game === 'blackjack' && mailSentTo.blackjack.indexOf(userEmail) === -1))
|
---|
| 787 | {
|
---|
| 788 | const message = {
|
---|
| 789 | from: process.env.GOOGLE_EMAIL,
|
---|
| 790 | to: userEmail,
|
---|
| 791 | subject: "Caessino - Server is back online",
|
---|
| 792 | html: `
|
---|
| 793 | <h4>Hello, ${displayName}</h4>
|
---|
| 794 | <p>We are writing to inform you that the server is back online.</p>
|
---|
| 795 | <p>We know that you were in the middle of playing ${game}, and we apologize for the interrupt.</p>
|
---|
| 796 | <p>${game === 'poker' ? msgPoker : game === 'roulette' ? msgRoulette : msgBlackjack}</p>
|
---|
| 797 | <br/>
|
---|
| 798 | <p>All the best,</p>
|
---|
| 799 | <p>The Team ESS</p>
|
---|
| 800 | `
|
---|
| 801 | }
|
---|
| 802 |
|
---|
| 803 | transporter.sendMail(message, (err, data) => {
|
---|
| 804 | if (err) {
|
---|
| 805 | console.log(err);
|
---|
| 806 | }
|
---|
| 807 | })
|
---|
| 808 |
|
---|
| 809 | mailSentTo[game].push(userEmail)
|
---|
| 810 | }
|
---|
| 811 | }
|
---|
| 812 | });
|
---|
| 813 | }
|
---|
[95ce58b] | 814 |
|
---|
| 815 | /**
|
---|
| 816 | * User session data
|
---|
| 817 | */
|
---|
| 818 | export var sessions = []
|
---|
| 819 |
|
---|
| 820 | export function update_sessions_to_database() {
|
---|
[e9f11ac] | 821 | pool.query('UPDATE sessions SET data = $1 WHERE identifier = $2', [JSON.stringify(sessions), 'sessions_data'], (error, results) => {
|
---|
| 822 | if (error) throw error;
|
---|
| 823 | });
|
---|
[95ce58b] | 824 | }
|
---|
| 825 |
|
---|
| 826 | export function load_sessions_from_database() {
|
---|
[e9f11ac] | 827 | pool.query('SELECT data FROM sessions WHERE identifier = $1', ['sessions_data'], (error, results) => {
|
---|
| 828 | if (error) throw error;
|
---|
| 829 |
|
---|
| 830 | sessions = JSON.parse(results?.rows[0]?.data || []);
|
---|
| 831 | });
|
---|
[95ce58b] | 832 | }
|
---|
| 833 | load_sessions_from_database();
|
---|
| 834 |
|
---|
[e9f11ac] | 835 | /**
|
---|
| 836 | * Poker game data
|
---|
| 837 | */
|
---|
[95ce58b] | 838 | export var tables = []
|
---|
| 839 |
|
---|
| 840 | export function cleanTables() {
|
---|
| 841 | tables = [];
|
---|
| 842 | }
|
---|
| 843 |
|
---|
| 844 | export function update_tables_to_database() {
|
---|
[e9f11ac] | 845 | tables = tables.map(table => ({...table, turnTimeout: null}));
|
---|
| 846 |
|
---|
| 847 | pool.query('UPDATE poker SET data = $1 WHERE identifier = $2', [JSON.stringify(tables), 'poker_data'], (error, results) => {
|
---|
| 848 | if (error) throw error;
|
---|
| 849 | });
|
---|
[95ce58b] | 850 | }
|
---|
| 851 |
|
---|
| 852 | export async function load_tables_from_database() {
|
---|
[e9f11ac] | 853 | pool.query('SELECT data FROM poker WHERE identifier = $1', ['poker_data'], (error, results) => {
|
---|
| 854 | if (error) throw error;
|
---|
| 855 |
|
---|
| 856 | tables = JSON.parse(results?.rows[0]?.data || []);
|
---|
| 857 |
|
---|
| 858 | tables.forEach(table => {
|
---|
| 859 | if (table.started) {
|
---|
| 860 | progressRoundTillTheEnd(table.id);
|
---|
| 861 | }
|
---|
| 862 | })
|
---|
| 863 |
|
---|
[e007fcd] | 864 | tables.forEach(table => {
|
---|
| 865 | if (table.ended) {
|
---|
| 866 | table.players?.forEach(player => {
|
---|
| 867 | if (!player.isGhost) {
|
---|
| 868 | sendMailForGameCompletition('poker', player.username, player.displayName);
|
---|
| 869 | }
|
---|
| 870 | })
|
---|
| 871 | }
|
---|
| 872 | })
|
---|
| 873 |
|
---|
[e9f11ac] | 874 | cleanTables();
|
---|
| 875 |
|
---|
| 876 | update_tables_to_database();
|
---|
| 877 | });
|
---|
[95ce58b] | 878 | }
|
---|
| 879 | load_tables_from_database();
|
---|
[e9f11ac] | 880 |
|
---|
| 881 | /**
|
---|
| 882 | * Roulette game data
|
---|
| 883 | */
|
---|
| 884 | export var game = {}
|
---|
| 885 |
|
---|
| 886 | export function update_game_to_database() {
|
---|
| 887 | pool.query('UPDATE roulette SET data = $1 WHERE identifier = $2', [JSON.stringify(game), 'roulette_data'], (error, results) => {
|
---|
| 888 | if (error) throw error;
|
---|
| 889 | });
|
---|
| 890 | }
|
---|
| 891 |
|
---|
| 892 | export async function load_game_from_database() {
|
---|
| 893 | pool.query('SELECT data FROM roulette WHERE identifier = $1', ['roulette_data'], (error, results) => {
|
---|
| 894 | if (error) throw error;
|
---|
| 895 |
|
---|
| 896 | game = JSON.parse(results?.rows[0]?.data || []);
|
---|
| 897 |
|
---|
[e007fcd] | 898 | game.players?.forEach(player => {
|
---|
| 899 | sendMailForGameCompletition('roulette', player.username, player.name);
|
---|
| 900 | })
|
---|
| 901 |
|
---|
[e9f11ac] | 902 | game.loaded = true;
|
---|
| 903 | });
|
---|
| 904 | }
|
---|
| 905 | load_game_from_database();
|
---|
[433e0c5] | 906 |
|
---|
| 907 | /**
|
---|
| 908 | * Blackjack game data
|
---|
| 909 | */
|
---|
| 910 | export var rooms = []
|
---|
| 911 |
|
---|
| 912 | export function update_rooms_to_database() {
|
---|
| 913 | let tmpRooms = [];
|
---|
| 914 |
|
---|
| 915 | for (let key in rooms) {
|
---|
| 916 | if (key === "loaded") continue ;
|
---|
| 917 |
|
---|
| 918 | tmpRooms.push(rooms[key]);
|
---|
| 919 | tmpRooms[tmpRooms.length - 1].id = key;
|
---|
| 920 | }
|
---|
| 921 |
|
---|
| 922 | pool.query('UPDATE blackjack SET data = $1 WHERE identifier = $2', [JSON.stringify(tmpRooms), 'blackjack_data'], (error, results) => {
|
---|
| 923 | if (error) throw error;
|
---|
| 924 | });
|
---|
| 925 | }
|
---|
| 926 |
|
---|
| 927 | export async function load_rooms_from_database() {
|
---|
| 928 | pool.query('SELECT data FROM blackjack WHERE identifier = $1', ['blackjack_data'], (error, results) => {
|
---|
| 929 | if (error) throw error;
|
---|
| 930 |
|
---|
| 931 | if (results?.rows[0]?.data) {
|
---|
| 932 | const tmpRooms = JSON.parse(results.rows[0].data);
|
---|
| 933 |
|
---|
| 934 | tmpRooms.forEach(room => {
|
---|
| 935 | rooms[room.id] = {...room, id: ''}
|
---|
| 936 | })
|
---|
[e007fcd] | 937 |
|
---|
| 938 | tmpRooms.forEach(room => {
|
---|
| 939 | sendMailForGameCompletition('blackjack', room.username, room.displayName);
|
---|
| 940 | })
|
---|
[433e0c5] | 941 |
|
---|
| 942 | rooms["loaded"] = true;
|
---|
| 943 | }
|
---|
| 944 | });
|
---|
| 945 | }
|
---|
| 946 | load_rooms_from_database();
|
---|