Index: pages/api/poker/index.js
===================================================================
--- pages/api/poker/index.js	(revision 95ce58bf1e6a160d56dbf39a83e4ed466a89d982)
+++ pages/api/poker/index.js	(revision d0ef2592564bcd6d3691722843df4fc6a7397109)
@@ -256,5 +256,5 @@
         /**
          * /---------------------- GET ----------------------/
-         * Creates the table and enters the user inside
+         * Updates the state periodically
          * @action update_state
          * @param session_id
@@ -265,5 +265,5 @@
             const { success, table, player } = getTableAndPlayer(session_id);
 
-            if (table.started && !table.ended) {
+            if (success && table.started && !table.ended) {
                 const d = Date.now();
 
@@ -272,4 +272,5 @@
                         table.players[table.turnIdx].isFolded = true;
 
+                        table.lastActivity = Date.now();
                         setNextPlayerIdx(table.id);
                     }
Index: pages/api/roulette/index.js
===================================================================
--- pages/api/roulette/index.js	(revision 95ce58bf1e6a160d56dbf39a83e4ed466a89d982)
+++ pages/api/roulette/index.js	(revision d0ef2592564bcd6d3691722843df4fc6a7397109)
@@ -2,4 +2,20 @@
 
 require('dotenv').config();
+
+let samplePlayer = {
+    session_id: '',
+    name: '',
+    whichBets: [],
+    coinPlaced: {
+        x: -1,
+        y: -1,                
+    },
+    credits: -1,
+    betAmount: 0,
+    wonAmount: 0,
+    status: '_1_',
+    outcome: 'none',
+    gotResults: false,
+}
 
 function getWinningBets(magicNumber) {
@@ -36,6 +52,4 @@
 
 function updateGameWithWinners() {
-    const winningBets = getWinningBets(magicNumber);
-
     for (let i = 0; i < game.players.length; i++) {
         const player = game.players[i];
@@ -43,5 +57,5 @@
         let playerWon = false;
         player.whichBets.forEach(bet => {
-            if (winningBets.indexOf(bet) !== -1) {
+            if (game.winningBets.indexOf(bet) !== -1) {
                 playerWon = true;
             }
@@ -49,18 +63,36 @@
 
         if (playerWon) {
-            game.players[i].outcome = 'won';
+            player.outcome = 'won';
         }
         else {
-            game.players[i].outcome = 'lost';
-        }
-    }
-}
-
-function resetPlayers() {
+            player.outcome = 'lost';
+        }
+
+        player.wonAmount = calculateWinnings(player);
+
+        axios.get(`${process.env.HOME_URL}/api/postgre/?action=add_credits&session_id=${player.session_id}&credits=${player.wonAmount}&game=roulette&outcome=${player.outcome}`).then(postgreRes => {
+            if (postgreRes.data?.success) {
+                player.credits = postgreRes.data?.credits;
+            }
+        });
+    }
+}
+
+function resetGame() {
+    game.magicNumber = -1;
+    game.winningBets = [];
+    game.status = '_1_ongoing_timer';
+
     game.players.forEach(player => {
         player.whichBets = [];
         player.betAmount = 0;
+        player.wonAmount = 0;
+        player.coinPlaced = {
+            x: -1,
+            y: -1,                
+        },
         player.outcome = 'none';
         player.status = '_1_no_placed_bet';
+        player.gotResults = false;
     })
 }
@@ -74,5 +106,5 @@
     if (bets[0] === 'Even' || bets[0] === 'Odd') return 2 * bet;
     else if (bets[0] === 'Red' || bets[0] === 'Black') return 2 * bet;
-    else if (bets[0].contains('Remainder')) return 3 * bet;
+    else if (bets[0].includes('Remainder')) return 3 * bet;
     else if (bets[0] === '1-12' || bets[0] === '13-24' || bets[0] === '25-36') return 3 * bet;
     else if (bets[0] === '1-18' || bets[0] === '19-36') return 2 * bet;
@@ -84,9 +116,4 @@
 }
 
-const COUNTDOWN_FROM = 30;
-const WAIT_BEFORE = 20;
-
-let magicNumber = -1;
-
 (function() {
     setInterval(() => {
@@ -95,15 +122,18 @@
         // WAIT_BEFORE seconds is the time allocated for spinning the wheel and seeing the results.
         if (game.timeToStart == 0) {
-            game.timeToStart = COUNTDOWN_FROM + WAIT_BEFORE;
+            game.timeToStart = game.COUNTDOWN_FROM + game.WAIT_BEFORE;
+
+            game.magicNumber = Math.floor(Math.random() * 37);
+            game.winningBets = getWinningBets(game.magicNumber);
+            
+            setTimeout(() => {
+                updateGameWithWinners();
+            }, 6000)
         }
         else if (game.timeToStart == 10) {
-            magicNumber = Math.floor(Math.random() * 37);
             game.status = '_2_spinning';
         }
-        else if (game.timeToStart == COUNTDOWN_FROM) {
-            game.status = '_1_ongoing_timer';
-        }
-        else if (game.timeToStart == COUNTDOWN_FROM + 5) {
-            resetPlayers();
+        else if (game.timeToStart == game.COUNTDOWN_FROM) {
+            resetGame();
         }
 
@@ -113,6 +143,10 @@
 let game = {
     status: '_1_ongoing_timer',     // statuses: _1_ongoing_timer, _2_spinning,
-    timeToStart: COUNTDOWN_FROM,    // in seconds
-    players: [] ,                   // example player -> { session_id, name, whichBet, betAmount, status, outcome }  // statuses: _1_no_placed_bet, _2_placed_bet
+    timeToStart: 30,                // in seconds
+    COUNTDOWN_FROM: 30,
+    WAIT_BEFORE: 20,
+    magicNumber: -1,
+    winningBets: [],
+    players: [],
 }
 
@@ -123,8 +157,40 @@
             name: name,
             whichBets: [],
+            coinPlaced: {
+                x: -1,
+                y: -1,                
+            },
+            credits: -1,
             betAmount: 0,
+            wonAmount: 0,
             status: '_1_no_placed_bet',
             outcome: 'none',
+            gotResults: false,
         })
+    }
+}
+
+export function getPlayer(session_id) {
+    const playerIdx = game.players.map(e=>e.session_id).indexOf(session_id);
+
+    if (playerIdx !== -1) {
+        return {
+            success: true,
+            player: game.players[playerIdx],
+        }
+    }
+
+    return {
+        success: false,
+        player: {...samplePlayer},
+    };
+}
+
+export function restrictGameInfo() {
+    const restrictedPlayers = game.players.map(player=>({...player, session_id: ""}))
+
+    return {
+        ...game,
+        players: restrictedPlayers,
     }
 }
@@ -140,66 +206,4 @@
         /**
          * /---------------------- GET ----------------------/
-         * Return to the user info for starting a new game.
-         * @action reset_game
-         * @param session_id
-         */
-        if (req.query.action === 'reset_game' && req.query?.session_id) {
-            const session_id = req.query.session_id;
-            const playerIdx = game.players.map(e=>e.session_id).indexOf(session_id);
-
-            if (playerIdx !== -1) {
-                res.json({
-                    success: true,
-                    game: game,
-                })
-            }
-            else {
-                res.json({
-                    success: false,
-                })
-            }
-        }
-
-        /**
-         * /---------------------- GET ----------------------/
-         * Timer done on client side.
-         * @action timer_done
-         * @param session_id
-         */
-         if (req.query.action === 'timer_done' && req.query?.session_id) {
-            const session_id = req.query.session_id;
-            const playerIdx = game.players.map(e=>e.session_id).indexOf(session_id);
-
-            if (playerIdx !== -1 && game.status.substr(1, 1) === '2') {
-                updateGameWithWinners();
-
-                const playerWinnings = calculateWinnings(game.players[playerIdx]);
-
-                axios.get(`${process.env.HOME_URL}/api/postgre/?action=add_credits&session_id=${session_id}&credits=${playerWinnings}&game=roulette&outcome=${game.players[playerIdx].outcome}`).then(postgreRes => {
-                    if (postgreRes.data?.success) {
-                        res.json({
-                            success: true,
-                            game: game,
-                            magicNumber: magicNumber,
-                            winningBets: getWinningBets(magicNumber),
-                            credits: postgreRes.data?.credits,
-                        })
-                    }
-                    else {
-                        res.json({
-                            success: false,
-                        })
-                    }
-                });
-            }
-            else {
-                res.json({
-                    success: false,
-                })
-            }
-        }
-
-        /**
-         * /---------------------- GET ----------------------/
          * Place a bet.
          * @action place_bet
@@ -207,29 +211,65 @@
          * @param betAmount
          * @param whichBets
+         * @param coinPlacedX
+         * @param coinPlacedY
          */
-        if (req.query.action === 'place_bet' && req.query?.session_id && req.query?.betAmount && req.query?.whichBets) {
+        if (req.query.action === 'place_bet' && req.query?.session_id && req.query?.betAmount && req.query?.whichBets && req.query?.coinPlacedX && req.query?.coinPlacedY) {
             const session_id = req.query.session_id;
-            const playerIdx = game.players.map(e=>e.session_id).indexOf(session_id);
-
-            if (playerIdx !== -1 && game.status.substr(1, 1) === '1' && game.players[playerIdx].status.substr(1, 1) === '1') {
-                game.players[playerIdx].betAmount = parseInt(req.query.betAmount);
-                game.players[playerIdx].whichBets = req.query.whichBets.split(',');
-                game.players[playerIdx].status = '_2_placed_bet';
-    
+
+            const { success, player } = getPlayer(session_id);
+
+            if (success && game.status.includes('_1_') && player.status.includes('_1_')) {
                 axios.get(`${process.env.HOME_URL}/api/postgre?action=take_credits&session_id=${session_id}&credits=${req.query.betAmount}`).then(postgreRes => {
                     if (postgreRes.data?.success) {
-                        res.json({
-                            success: true,
-                            game: game,
-                            credits: postgreRes.data?.credits,
-                        })
-                    }
-                    else {
-                        res.json({
-                            success: false,
-                        })
+                        player.betAmount = parseInt(req.query.betAmount);
+                        player.whichBets = req.query.whichBets.split(',');
+                        player.status = '_2_placed_bet';
+                        player.coinPlaced = {
+                            x: req.query.coinPlacedX,
+                            y: req.query.coinPlacedY,
+                        },
+                        player.credits = postgreRes.data?.credits;
                     }
                 });
             }
+
+            res.end();
+        }
+
+        /**
+         * /---------------------- GET ----------------------/
+         * Updates the state periodically
+         * @action update_state
+         * @param session_id
+         */
+         if (req.query.action === 'update_state' && req.query?.session_id) {
+            const session_id = req.query.session_id;
+
+            const { success, player } = getPlayer(session_id);
+
+            let extraAction = "";
+            let magicNumber = -1;
+            let winningBets = [];
+
+            if (success) {
+                if (game.timeToStart > game.COUNTDOWN_FROM && !player.gotResults) {
+                    extraAction = "spin_wheel";
+                    magicNumber = game.magicNumber;
+                    winningBets = game.winningBets;
+
+                    player.gotResults = true;
+                }
+            }
+
+            res.json({
+                success: true,
+                rouletteGame: {
+                    game: restrictGameInfo(),
+                    player: player,
+                },
+                extraAction: extraAction,
+                magicNumber: magicNumber,
+                winningBets: winningBets,
+            })
         }
 
