Index: pages/api/poker/gameStates.js
===================================================================
--- pages/api/poker/gameStates.js	(revision aac3b2b897972cd33d5372848c2cbac2e4e97ccc)
+++ pages/api/poker/gameStates.js	(revision 95ce58bf1e6a160d56dbf39a83e4ed466a89d982)
@@ -1,8 +1,10 @@
-export let tables = []
+import { tables } from "../postgre/index";
+
+import { v4 as uuidv4 } from "uuid";
 
 export const singleDeck = ["SA", "S2", "S3", "S4", "S5", "S6", "S7", "S8", "S9", "SX", "SJ", "SQ", "SK",
-                    "HA", "H2", "H3", "H4", "H5", "H6", "H7", "H8", "H9", "HX", "HJ", "HQ", "HK",
-                    "CA", "C2", "C3", "C4", "C5", "C6", "C7", "C8", "C9", "CX", "CJ", "CQ", "CK",
-                    "DA", "D2", "D3", "D4", "D5", "D6", "D7", "D8", "D9", "DX", "DJ", "DQ", "DK"    ];
+                           "HA", "H2", "H3", "H4", "H5", "H6", "H7", "H8", "H9", "HX", "HJ", "HQ", "HK",
+                           "CA", "C2", "C3", "C4", "C5", "C6", "C7", "C8", "C9", "CX", "CJ", "CQ", "CK",
+                           "DA", "D2", "D3", "D4", "D5", "D6", "D7", "D8", "D9", "DX", "DJ", "DQ", "DK"    ];
 
 export const deck = [...singleDeck];
@@ -17,4 +19,5 @@
     round: 0,
     turnIdx: -1,
+    lastActivity: 0,
     pot: 0,
     lastBet: 20,
@@ -45,2 +48,182 @@
     },
 }
+
+
+/**
+ * ********************* BEGIN OF FUNCTIONS *********************
+ */
+
+export function createTable(playerId, playerName, tableName) {
+    const tableId = uuidv4();
+
+    const table = {
+        id: tableId,
+        name: tableName,
+        status: '_1_just_created',
+        creator: playerName,
+        started: false,
+        ended: false,
+        round: 0,
+        turnIdx: -1,
+        lastActivity: 0,
+        prevTurnIdx: -2,
+        pot: 0,
+        lastBet: 20,
+        turnsSinceLastBet: 0,
+        deck: [...deck],
+        players: [{
+            id: playerId,
+            table: tableId,
+            credits: 0,
+            status: '_1_just_entered',
+            displayName: playerName,
+            cards: [],
+            betAmount: 0,
+            wonAmount: 0,
+            isSatDown: false,
+            isCoordinator: true,
+            isFolded: false,
+            isGhost: false,
+            hand: {
+                hand: '',
+                highCard: 0,
+            },
+        }],
+        onlyOnePlayerLeft: false,
+        winners: [],
+        splitWinners: false,
+        cards: [],
+    }
+
+    tables.push(table)
+
+    return table;
+}
+
+export function getRestrictedTablesArray() {
+    let result = [];
+
+    tables.forEach(table => {
+        let tmpPlayers = [];
+        table.players.forEach(player => {
+            tmpPlayers.push({
+                ...player,
+                id: '',
+                table: '',
+                cards: '',
+            })
+        });
+
+        let tmpWinners = [];
+        table.winners.forEach(winner => {
+            tmpWinners.push({
+                ...winner,
+                id: '',
+                table: '',
+                cards: '',
+            })
+        });
+
+        let tmp = {
+            ...table,
+            deck: [],
+            players: tmpPlayers,
+            winners: tmpWinners,
+            turnTimeout: null,
+        }
+
+        result.push({...tmp});
+    })
+
+    return result;
+}
+
+export function getRestrictedTableArray(tableId, session_id) {
+    let result = undefined;
+
+    let tableIdx = tables.map(e=>e.id).indexOf(tableId);
+
+    if (tableIdx !== -1) {
+        let table = tables[tableIdx];
+
+        let tmpPlayers = [];
+        table.players.forEach(player => {
+            if (player.id === session_id) {
+                tmpPlayers.push({
+                    ...player,
+                    id: '',
+                    table: '',
+                })
+            }
+            else {
+                tmpPlayers.push({
+                    ...player,
+                    id: '',
+                    table: '',
+                    cards: table.ended ? player.cards : player.cards.length > 0 ? ['back', 'back'] : '',
+                })
+            }
+        });
+
+        let tmpWinners = [];
+        table.winners.forEach(winner => {
+            if (winner.id === session_id) {
+                tmpWinners.push({
+                    ...winner,
+                    id: '',
+                    table: '',
+                })
+            }
+            else {
+                tmpWinners.push({
+                    ...winner,
+                    id: '',
+                    table: '',
+                    cards: table.ended ? winner.cards : winner.cards.length > 0 ? ['back', 'back'] : '',
+                })
+            }
+        });
+        result = {
+            ...table,
+            players: tmpPlayers,
+            winners: tmpWinners,
+            turnTimeout: null,
+        }
+    }
+
+    return result;
+}
+
+export function getTable(tableId) {
+    const tableIdx = tables.map(e=>e.id).indexOf(tableId);
+
+    if (tableIdx !== -1) {
+        return tables[tableIdx];
+    }
+
+    return undefined;
+}
+
+export function getTableAndPlayer(session_id) {
+    for (let tableIdx = 0; tableIdx < tables.length; tableIdx++) {
+        const playerIdx = tables[tableIdx].players.filter(e=>e.isGhost === false).map(e=>e.id).indexOf(session_id);
+
+        if (playerIdx !== -1) {
+            return {
+                success: true,
+                table: tables[tableIdx],
+                player: tables[tableIdx].players[playerIdx],
+            }
+        }
+    }
+
+    return {
+        success: false,
+        table: {...sampleTable},
+        player: {...samplePlayer},
+    };
+}
+
+/**
+ * ********************* END OF FUNCTIONS *********************
+ */
Index: pages/api/poker/index.js
===================================================================
--- pages/api/poker/index.js	(revision aac3b2b897972cd33d5372848c2cbac2e4e97ccc)
+++ pages/api/poker/index.js	(revision 95ce58bf1e6a160d56dbf39a83e4ed466a89d982)
@@ -3,186 +3,9 @@
 require('dotenv').config();
 
-import { v4 as uuidv4 } from 'uuid';
-
-import { tables, deck, sampleTable, samplePlayer } from './gameStates'
-
-import { drawASingleCard, setNextPlayerIdx, progressRoundIfNeeded } from './tableSpecific'
-
-/**
- * ********************* BEGIN OF FUNCTIONS *********************
- */
-
-function createTable(playerId, playerName, tableName) {
-    const tableId = uuidv4();
-
-    const table = {
-        id: tableId,
-        name: tableName,
-        status: '_1_just_created',
-        creator: playerName,
-        started: false,
-        ended: false,
-        round: 0,
-        turnIdx: -1,
-        pot: 0,
-        lastBet: 20,
-        turnsSinceLastBet: 0,
-        deck: [...deck],
-        players: [{
-            id: playerId,
-            table: tableId,
-            credits: 0,
-            status: '_1_just_entered',
-            displayName: playerName,
-            cards: [],
-            betAmount: 0,
-            wonAmount: 0,
-            isSatDown: false,
-            isCoordinator: true,
-            isFolded: false,
-            isGhost: false,
-            hand: {
-                hand: '',
-                highCard: 0,
-            },
-        }],
-        onlyOnePlayerLeft: false,
-        winners: [],
-        splitWinners: false,
-        cards: [],
-    }
-
-    tables.push(table)
-
-    return table;
-}
-
-function getRestrictedTablesArray() {
-    let result = [];
-
-    tables.forEach(table => {
-        let tmpPlayers = [];
-        table.players.forEach(player => {
-            tmpPlayers.push({
-                ...player,
-                id: '',
-                table: '',
-                cards: '',
-            })
-        });
-
-        let tmpWinners = [];
-        table.winners.forEach(winner => {
-            tmpWinners.push({
-                ...winner,
-                id: '',
-                table: '',
-                cards: '',
-            })
-        });
-
-        let tmp = {
-            ...table,
-            deck: [],
-            players: tmpPlayers,
-            winners: tmpWinners,
-            turnTimeout: null,
-        }
-
-        result.push({...tmp});
-    })
-
-    return result;
-}
-
-function getRestrictedTableArray(tableId, session_id) {
-    let result = undefined;
-
-    let tableIdx = tables.map(e=>e.id).indexOf(tableId);
-
-    if (tableIdx !== -1) {
-        let table = tables[tableIdx];
-
-        let tmpPlayers = [];
-        table.players.forEach(player => {
-            if (player.id === session_id) {
-                tmpPlayers.push({
-                    ...player,
-                    id: '',
-                    table: '',
-                })
-            }
-            else {
-                tmpPlayers.push({
-                    ...player,
-                    id: '',
-                    table: '',
-                    cards: table.ended ? player.cards : player.cards.length > 0 ? ['back', 'back'] : '',
-                })
-            }
-        });
-
-        let tmpWinners = [];
-        table.winners.forEach(winner => {
-            if (winner.id === session_id) {
-                tmpWinners.push({
-                    ...winner,
-                    id: '',
-                    table: '',
-                })
-            }
-            else {
-                tmpWinners.push({
-                    ...winner,
-                    id: '',
-                    table: '',
-                    cards: table.ended ? winner.cards : winner.cards.length > 0 ? ['back', 'back'] : '',
-                })
-            }
-        });
-        result = {
-            ...table,
-            players: tmpPlayers,
-            winners: tmpWinners,
-            turnTimeout: null,
-        }
-    }
-
-    return result;
-}
-
-function getTable(tableId) {
-    const tableIdx = tables.map(e=>e.id).indexOf(tableId);
-
-    if (tableIdx !== -1) {
-        return tables[tableIdx];
-    }
-
-    return undefined;
-}
-
-function getTableAndPlayer(session_id) {
-    for (let tableIdx = 0; tableIdx < tables.length; tableIdx++) {
-        const playerIdx = tables[tableIdx].players.filter(e=>e.isGhost === false).map(e=>e.id).indexOf(session_id);
-
-        if (playerIdx !== -1) {
-            return {
-                success: true,
-                table: tables[tableIdx],
-                player: tables[tableIdx].players[playerIdx],
-            }
-        }
-    }
-
-    return {
-        success: false,
-        table: {...sampleTable},
-        player: {...samplePlayer},
-    };
-}
-
-/**
- * ********************* END OF FUNCTIONS *********************
- */
+import { createTable, getRestrictedTablesArray, getRestrictedTableArray, getTable, getTableAndPlayer } from './gameStates';
+
+import { drawASingleCard, setNextPlayerIdx, progressRoundIfNeeded, progressRoundTillTheEnd } from './tableSpecific'
+
+import { tables, cleanTables, update_tables_to_database, load_tables_from_database } from '../postgre/index'
 
 /**
@@ -266,8 +89,11 @@
 
                 if (okayToGo) {
+                    table.lastActivity = Date.now();
                     setNextPlayerIdx(table.id);
                 }
             }
             
+            update_tables_to_database();
+
             res.end();
         }
@@ -291,4 +117,5 @@
                 })
 
+                table.lastActivity = Date.now();
                 table.started = true;
                 table.round = 1;
@@ -309,4 +136,6 @@
                 })
             }
+
+            update_tables_to_database();
             
             res.end();
@@ -327,4 +156,6 @@
             }
 
+            update_tables_to_database();
+
             res.end();
         }
@@ -340,4 +171,6 @@
 
             if (success) {
+                table.lastActivity = Date.now();
+                
                 player.isGhost = true;
                 player.isFolded = true;
@@ -352,4 +185,6 @@
                 }
             }
+
+            update_tables_to_database();
 
             res.end();
@@ -394,4 +229,6 @@
             }
 
+            update_tables_to_database();
+
             res.end();
         }
@@ -412,4 +249,6 @@
             }
 
+            update_tables_to_database();
+
             res.end();
         }
@@ -425,4 +264,16 @@
 
             const { success, table, player } = getTableAndPlayer(session_id);
+
+            if (table.started && !table.ended) {
+                const d = Date.now();
+
+                if (d - table.lastActivity > 30000) {
+                    if (table.players[table.turnIdx] !== undefined) {
+                        table.players[table.turnIdx].isFolded = true;
+
+                        setNextPlayerIdx(table.id);
+                    }
+                }
+            }
 
             res.json({
Index: pages/api/poker/tableSpecific.js
===================================================================
--- pages/api/poker/tableSpecific.js	(revision aac3b2b897972cd33d5372848c2cbac2e4e97ccc)
+++ pages/api/poker/tableSpecific.js	(revision 95ce58bf1e6a160d56dbf39a83e4ed466a89d982)
@@ -1,3 +1,5 @@
-import { tables, deck } from './gameStates'
+import { tables } from '../postgre/index';
+
+import { deck } from './gameStates'
 
 import { hands, getBestHandDetails } from './handEvaluations';
@@ -62,5 +64,5 @@
     const tableIdx = tables.map(e=>e.id).indexOf(tableId);
 
-    if (tables[tableIdx] !== undefined && !tables[tableIdx].ended) {
+    if (tables[tableIdx] !== undefined && tables[tableIdx].started && !tables[tableIdx].ended) {
         const table = tables[tableIdx];
 
@@ -80,4 +82,5 @@
 
         if (table.turnTimeout !== null) clearTimeout(table.turnTimeout);
+        table.prevTurnIdx = -2;
 
         let counter = 10;
@@ -91,15 +94,4 @@
             if (table.players[table.turnIdx] !== undefined && table.players[table.turnIdx].isSatDown && !table.players[table.turnIdx].isFolded) {
                 if (table.round >= 2 && table.players[table.turnIdx].credits === 0) continue;
-
-                let prevTurnIdx = table.turnIdx;
-                table.turnTimeout = setTimeout(() => {
-                    if (prevTurnIdx === table.turnIdx) {
-                        if (table.players[table.turnIdx] !== undefined) {
-                            table.players[table.turnIdx].isFolded = true;
-                            
-                            setNextPlayerIdx(table.id);
-                        }
-                    }
-                }, 30000);
 
                 table.lastBet = getMaxBet(table.id) - table.players[table.turnIdx].betAmount;
@@ -151,4 +143,5 @@
         table.round = 0;
         table.turnIdx = -1;
+        table.lastActivity = 0;
         table.turnTimeout = null;
         table.pot = 0;
@@ -233,4 +226,6 @@
         table.started = false;
         table.ended = true;
+        if (table.turnTimeout !== null) clearTimeout(table.turnTimeout);
+        table.turnTimeout = null;
 
         table.onlyOnePlayerLeft = true;
@@ -301,4 +296,6 @@
         table.started = false;
         table.ended = true;
+        if (table.turnTimeout !== null) clearTimeout(table.turnTimeout);
+        table.turnTimeout = null;
         if (table.ended && table.winners.length === 0) {
             setWinner(table.id);
@@ -327,4 +324,6 @@
                 table.started = false;
                 table.ended = true;
+                if (table.turnTimeout !== null) clearTimeout(table.turnTimeout);
+                table.turnTimeout = null;
             }
 
