Changeset e9f11ac for pages/api/postgre


Ignore:
Timestamp:
07/12/22 17:03:03 (2 years ago)
Author:
anastasovv <simon@…>
Branches:
main
Children:
1df3fde
Parents:
d0ef259
Message:

Saving roulette data to database

File:
1 edited

Legend:

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

    rd0ef259 re9f11ac  
    468468
    469469export 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    });
     470  pool.query('UPDATE sessions SET data = $1 WHERE identifier = $2', [JSON.stringify(sessions), 'sessions_data'], (error, results) => {
     471    if (error) throw error;
     472  });
    473473}
    474474   
    475475export 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    });
     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  });
    481481}
    482482load_sessions_from_database();
    483483 
    484  /**
    485   * Poker game data
    486   */
     484/**
     485 * Poker game data
     486 */
    487487export var tables = []
    488488 
     
    492492 
    493493export 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    });
     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  });
    499499}
    500500   
    501501export 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;
     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();
     519
     520/**
     521 *  Roulette game data
     522 */
     523export var game = {}
     524 
     525export function update_game_to_database() {
     526  pool.query('UPDATE roulette SET data = $1 WHERE identifier = $2', [JSON.stringify(game), 'roulette_data'], (error, results) => {
     527    if (error) throw error;
     528  });
     529}
     530   
     531export async function load_game_from_database() {
     532  pool.query('SELECT data FROM roulette WHERE identifier = $1', ['roulette_data'], (error, results) => {
     533    if (error) throw error;
     534
     535    game = JSON.parse(results?.rows[0]?.data || []);
     536
     537    game.loaded = true;
     538    console.log(game);
     539  });
     540}
     541load_game_from_database();
    504542 
    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.