Changeset 95ce58b for pages/api/postgre
- Timestamp:
- 07/08/22 13:51:13 (2 years ago)
- Branches:
- main
- Children:
- d0ef259
- Parents:
- aac3b2b
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
pages/api/postgre/index.js
raac3b2b r95ce58b 6 6 7 7 const crypto = require('crypto'); 8 9 import { progressRoundTillTheEnd } from '../poker/tableSpecific'; 8 10 9 11 const Pool = require('pg').Pool … … 11 13 connectionString: `postgres://${process.env.POSTGRES_USER}:${process.env.POSTGRES_PASSWORD}@${process.env.POSTGRES_HOST}/${process.env.POSTGRES_DB}` 12 14 }); 13 14 const sessions = []15 // example session = { id, displayName, username, credits, lastActivity }16 15 17 16 export default function handler(req, res) { … … 97 96 }); 98 97 } 98 99 update_sessions_to_database(); 99 100 100 101 res.json({ … … 159 160 } 160 161 }); 162 163 update_sessions_to_database(); 161 164 162 165 res.json({ … … 253 256 254 257 axios.get(`${process.env.HOME_URL}/api/blackjack/?action=remove_room&session_id=${session_id}`); 258 259 update_sessions_to_database(); 255 260 } 256 261 … … 427 432 lastActivity: Date.now(), 428 433 } 429 434 430 435 sessions.push(session); 436 437 update_sessions_to_database(); 431 438 432 439 res.json({ … … 453 460 } 454 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();
Note:
See TracChangeset
for help on using the changeset viewer.