Index: pages/api/roulette/index.js
===================================================================
--- pages/api/roulette/index.js	(revision 9bd09b003e0450668a5ceb182fb4d9dfb5a2d704)
+++ pages/api/roulette/index.js	(revision ace78655f6db8dbc9acd8ded9cb43af6a42152ce)
@@ -3,16 +3,108 @@
 require('dotenv').config();
 
-const COUNTDOWN_FROM = 90;
-const WAIT_BEFORE = 30;
+function getWinningBets(magicNumber) {
+    let winningBets = [];
+    winningBets.push(magicNumber);
+    
+    if (magicNumber != 0) {
+        if ((magicNumber <= 9 && magicNumber % 2 == 1) || (magicNumber > 10 && magicNumber <= 18 && magicNumber % 2 == 0) ||
+            (magicNumber > 19 && magicNumber <= 27 && magicNumber % 2 == 1) || (magicNumber > 27 && magicNumber % 2 == 0)) {
+
+            winningBets.push('Red');
+        }
+        else {
+            winningBets.push('Black');
+        }
+
+        if (magicNumber % 2 === 0)      winningBets.push('Even');
+        else                            winningBets.push('Odd');
+
+        if (magicNumber <= 12)          winningBets.push('1-12');
+        else if (magicNumber <= 24)     winningBets.push('13-24');
+        else                            winningBets.push('25-36');
+
+        if (magicNumber <= 18)          winningBets.push('1-18');
+        else                            winningBets.push('19-36');
+
+        if (magicNumber % 3 === 0)      winningBets.push('Remainder3');
+        else if (magicNumber % 3 === 2) winningBets.push('Remainder2');
+        else                            winningBets.push('Remainder1');
+    }
+
+    return winningBets;
+}
+
+function updateGameWithWinners() {
+    const winningBets = getWinningBets(magicNumber);
+
+    for (let i = 0; i < game.players.length; i++) {
+        const player = game.players[i];
+
+        let playerWon = false;
+        player.whichBets.forEach(bet => {
+            if (winningBets.indexOf(bet) !== -1) {
+                playerWon = true;
+            }
+        })
+
+        if (playerWon) {
+            game.players[i].outcome = 'won';
+        }
+        else {
+            game.players[i].outcome = 'lost';
+        }
+    }
+}
+
+function resetPlayers() {
+    game.players.forEach(player => {
+        player.whichBets = [];
+        player.betAmount = 0;
+        player.outcome = 'none';
+        player.status = '_1_no_placed_bet';
+    })
+}
+
+function calculateWinnings(player) {
+    if (player.outcome === 'lost') return 0;
+
+    let bets = player.whichBets;
+    let bet = player.betAmount;
+
+    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] === '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;
+    else if (bets.length === 4) return 9 * bet;
+    else if (bets.length === 2) return 18 * bet;
+    else if (bets.length === 1) return 36 * bet;
+
+    return 0;
+}
+
+const COUNTDOWN_FROM = 30;
+const WAIT_BEFORE = 20;
+
+let magicNumber = -1;
 
 (function() {
     setInterval(() => {
-
         game.timeToStart--;
 
-        // 30 seconds is the time to spin and see the results.
+        // 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;
         }
+        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();
+        }
 
     }, 1000);
@@ -20,7 +112,7 @@
 
 let game = {
-    status: '_1_ongoing_timer',     // statuses: _1_ongoing_timer, _2_spinning, _3_results
+    status: '_1_ongoing_timer',     // statuses: _1_ongoing_timer, _2_spinning,
     timeToStart: COUNTDOWN_FROM,    // in seconds
-    players: [] ,                   // example player -> { session_id, name, whichBet, betAmount, status }  // statuses: _1_no_placed_bet, _2_placed_bet
+    players: [] ,                   // example player -> { session_id, name, whichBet, betAmount, status, outcome }  // statuses: _1_no_placed_bet, _2_placed_bet
 }
 
@@ -33,4 +125,5 @@
             betAmount: 0,
             status: '_1_no_placed_bet',
+            outcome: 'none',
         })
     }
@@ -45,4 +138,67 @@
      */
     if (req.method === 'GET') {
+        
+        /**
+         * /---------------------- 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 ----------------------/
@@ -57,5 +213,5 @@
             const playerIdx = game.players.map(e=>e.session_id).indexOf(session_id);
 
-            if (playerIdx !== -1 && game.players[playerIdx].status.substr(1, 1) === '1') {
+            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(',');
