Changeset e9f11ac


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

Location:
pages/api
Files:
3 edited

Legend:

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

    rd0ef259 re9f11ac  
    274274                        table.lastActivity = Date.now();
    275275                        setNextPlayerIdx(table.id);
     276                       
     277                        update_tables_to_database();
    276278                    }
    277279                }
  • 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();
  • pages/api/roulette/index.js

    rd0ef259 re9f11ac  
    11import axios from 'axios';
     2
     3import { game, update_game_to_database } from '../postgre/index'
    24
    35require('dotenv').config();
     
    141143})();
    142144
    143 let game = {
    144     status: '_1_ongoing_timer',     // statuses: _1_ongoing_timer, _2_spinning,
    145     timeToStart: 30,                // in seconds
    146     COUNTDOWN_FROM: 30,
    147     WAIT_BEFORE: 20,
    148     magicNumber: -1,
    149     winningBets: [],
    150     players: [],
     145if (game.status === undefined) {
     146    game.status = '_1_ongoing_timer';     // statuses: _1_ongoing_timer, _2_spinning
     147    game.timeToStart = 30;                // in seconds
     148    game.COUNTDOWN_FROM = 30;
     149    game.WAIT_BEFORE = 20;
     150    game.magicNumber = -1;
     151    game.winningBets = [];
     152    game.players = [];
    151153}
    152154
     
    262264            }
    263265
     266            if (game.loaded !== undefined && game.loaded) {
     267                update_game_to_database();
     268            }
     269
    264270            res.json({
    265271                success: true,
Note: See TracChangeset for help on using the changeset viewer.