Changeset 95ce58b for pages/api/postgre


Ignore:
Timestamp:
07/08/22 13:51:13 (2 years ago)
Author:
anastasovv <simon@…>
Branches:
main
Children:
d0ef259
Parents:
aac3b2b
Message:

Saving sessions data and poker data to database

File:
1 edited

Legend:

Unmodified
Added
Removed
  • pages/api/postgre/index.js

    raac3b2b r95ce58b  
    66
    77const crypto = require('crypto');
     8
     9import { progressRoundTillTheEnd } from '../poker/tableSpecific';
    810
    911const Pool = require('pg').Pool
     
    1113  connectionString: `postgres://${process.env.POSTGRES_USER}:${process.env.POSTGRES_PASSWORD}@${process.env.POSTGRES_HOST}/${process.env.POSTGRES_DB}`
    1214});
    13 
    14 const sessions = []
    15 // example session = { id, displayName, username, credits, lastActivity }
    1615
    1716export default function handler(req, res) {
     
    9796          });
    9897        }
     98
     99        update_sessions_to_database();
    99100         
    100101        res.json({
     
    159160          }
    160161        });
     162
     163        update_sessions_to_database();
    161164
    162165        res.json({
     
    253256
    254257        axios.get(`${process.env.HOME_URL}/api/blackjack/?action=remove_room&session_id=${session_id}`);
     258
     259        update_sessions_to_database();
    255260      }
    256261
     
    427432                      lastActivity: Date.now(),
    428433                    }
    429                    
     434
    430435                    sessions.push(session);
     436
     437                    update_sessions_to_database();
    431438   
    432439                    res.json({
     
    453460  }
    454461}
     462
     463
     464/**
     465 * User session data
     466 */
     467export var sessions = []
     468
     469export 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   
     475export 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}
     482load_sessions_from_database();
     483 
     484 /**
     485  * Poker game data
     486  */
     487export var tables = []
     488 
     489export function cleanTables() {
     490   tables = [];
     491}
     492 
     493export 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   
     501export 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}
     518load_tables_from_database();
Note: See TracChangeset for help on using the changeset viewer.