[b13f93b] | 1 | import axios from 'axios';
|
---|
| 2 |
|
---|
| 3 | require('dotenv').config();
|
---|
| 4 |
|
---|
[95ce58b] | 5 | import { createTable, getRestrictedTablesArray, getRestrictedTableArray, getTable, getTableAndPlayer } from './gameStates';
|
---|
[b13f93b] | 6 |
|
---|
[95ce58b] | 7 | import { drawASingleCard, setNextPlayerIdx, progressRoundIfNeeded, progressRoundTillTheEnd } from './tableSpecific'
|
---|
[b13f93b] | 8 |
|
---|
[95ce58b] | 9 | import { tables, cleanTables, update_tables_to_database, load_tables_from_database } from '../postgre/index'
|
---|
[b13f93b] | 10 |
|
---|
| 11 | /**
|
---|
| 12 | * ********************* BEGIN OF REQUEST HANDLER *********************
|
---|
| 13 | */
|
---|
| 14 | export default async function handler(req, res) {
|
---|
| 15 | /**
|
---|
| 16 | * GET method
|
---|
| 17 | */
|
---|
| 18 | if (req.method === 'GET') {
|
---|
| 19 |
|
---|
| 20 | /**
|
---|
| 21 | * /---------------------- GET ----------------------/
|
---|
| 22 | * Creates the table and enters the user inside
|
---|
| 23 | * @action game_action
|
---|
| 24 | * @param session_id
|
---|
| 25 | * @param specificAction
|
---|
| 26 | * @param betAmount
|
---|
| 27 | */
|
---|
| 28 | if (req.query.action === 'game_action' && req.query?.session_id && req.query?.specificAction && req.query?.betAmount) {
|
---|
| 29 | const { success, table, player } = getTableAndPlayer(req.query.session_id)
|
---|
| 30 |
|
---|
[3a783f2] | 31 | if (success && table.started && !table.ended && player.isSatDown && !player.isFolded) {
|
---|
[b13f93b] | 32 | if (table.players.map(e=>e.id).indexOf(req.query.session_id) !== table.turnIdx) {
|
---|
| 33 | res.end();
|
---|
| 34 | return ;
|
---|
| 35 | }
|
---|
| 36 |
|
---|
| 37 | let okayToGo = false;
|
---|
| 38 |
|
---|
| 39 | if (req.query.specificAction === 'check') {
|
---|
[3a783f2] | 40 | if (table.lastBet === 0) {
|
---|
| 41 | table.turnsSinceLastBet++;
|
---|
| 42 | okayToGo = true;
|
---|
| 43 |
|
---|
| 44 | progressRoundIfNeeded(table.id);
|
---|
| 45 | }
|
---|
[b13f93b] | 46 | }
|
---|
| 47 | else if (req.query.specificAction === 'call') {
|
---|
[3a783f2] | 48 | await axios.get(`${process.env.HOME_URL}/api/postgre/?action=take_credits&session_id=${req.query.session_id}&credits=${table.lastBet}&takeWhatYouCan=true`).then(postgreRes => {
|
---|
| 49 | if (postgreRes.data?.success) {
|
---|
| 50 | player.credits = postgreRes.data?.credits;
|
---|
| 51 |
|
---|
| 52 | if (player.credits >= table.lastBet)
|
---|
| 53 | player.betAmount += table.lastBet;
|
---|
| 54 | else
|
---|
| 55 | player.betAmount += player.credits;
|
---|
| 56 |
|
---|
| 57 | table.pot += table.lastBet;
|
---|
| 58 | table.turnsSinceLastBet++;
|
---|
| 59 | okayToGo = true;
|
---|
| 60 |
|
---|
| 61 | progressRoundIfNeeded(table.id);
|
---|
| 62 | }
|
---|
| 63 | });
|
---|
[b13f93b] | 64 | }
|
---|
| 65 | else if (req.query.specificAction === 'raise') {
|
---|
[3a783f2] | 66 | const betAmount = parseInt(req.query.betAmount);
|
---|
| 67 |
|
---|
| 68 | if (betAmount >= table.lastBet) {
|
---|
| 69 | await axios.get(`${process.env.HOME_URL}/api/postgre/?action=take_credits&session_id=${req.query.session_id}&credits=${betAmount}&takeWhatYouCan=true`).then(postgreRes => {
|
---|
| 70 | if (postgreRes.data?.success) {
|
---|
| 71 | player.credits = postgreRes.data?.credits;
|
---|
| 72 |
|
---|
| 73 | player.betAmount += betAmount;
|
---|
| 74 | table.pot += betAmount;
|
---|
| 75 | table.turnsSinceLastBet = 1;
|
---|
| 76 | okayToGo = true;
|
---|
| 77 |
|
---|
| 78 | progressRoundIfNeeded(table.id);
|
---|
| 79 | }
|
---|
| 80 | });
|
---|
| 81 | }
|
---|
[b13f93b] | 82 | }
|
---|
| 83 | else if (req.query.specificAction === 'fold') {
|
---|
[3a783f2] | 84 | player.isFolded = true;
|
---|
| 85 | okayToGo = true;
|
---|
| 86 |
|
---|
| 87 | progressRoundIfNeeded(table.id);
|
---|
[b13f93b] | 88 | }
|
---|
| 89 |
|
---|
| 90 | if (okayToGo) {
|
---|
[95ce58b] | 91 | table.lastActivity = Date.now();
|
---|
[b13f93b] | 92 | setNextPlayerIdx(table.id);
|
---|
| 93 | }
|
---|
| 94 | }
|
---|
| 95 |
|
---|
[95ce58b] | 96 | update_tables_to_database();
|
---|
| 97 |
|
---|
[b13f93b] | 98 | res.end();
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 | /**
|
---|
| 102 | * /---------------------- GET ----------------------/
|
---|
| 103 | * Creates the table and enters the user inside
|
---|
| 104 | * @action start_game
|
---|
| 105 | * @param session_id
|
---|
| 106 | */
|
---|
| 107 | if (req.query.action === 'start_game' && req.query?.session_id) {
|
---|
| 108 | const { success, table } = getTableAndPlayer(req.query.session_id)
|
---|
| 109 |
|
---|
[aac3b2b] | 110 | if (success && !table.started && !table.ended && table.players.filter(e=>e.isSatDown===true).length >= 2) {
|
---|
[3a783f2] | 111 | table.players.forEach(player => {
|
---|
| 112 | axios.get(`${process.env.HOME_URL}/api/postgre/?action=check_if_logged_in&session_id=${player.id}`).then(postgreRes => {
|
---|
| 113 | if (postgreRes.data?.success) {
|
---|
| 114 | player.credits = postgreRes.data?.credits;
|
---|
| 115 | }
|
---|
| 116 | });
|
---|
| 117 | })
|
---|
| 118 |
|
---|
[95ce58b] | 119 | table.lastActivity = Date.now();
|
---|
[b13f93b] | 120 | table.started = true;
|
---|
| 121 | table.round = 1;
|
---|
| 122 |
|
---|
[3a783f2] | 123 | table.turnIdx = Math.floor(Math.random(0, table.players.length))
|
---|
| 124 | setNextPlayerIdx(table.id);
|
---|
[b13f93b] | 125 |
|
---|
| 126 | table.players.forEach(player => {
|
---|
| 127 | if (player.isSatDown) {
|
---|
| 128 | for (let i = 0; i < 2; i++) {
|
---|
| 129 | const card = drawASingleCard(table.id);
|
---|
| 130 |
|
---|
| 131 | if (card !== undefined) {
|
---|
| 132 | player.cards.push(card);
|
---|
| 133 | }
|
---|
| 134 | }
|
---|
| 135 | }
|
---|
| 136 | })
|
---|
| 137 | }
|
---|
[95ce58b] | 138 |
|
---|
| 139 | update_tables_to_database();
|
---|
[b13f93b] | 140 |
|
---|
| 141 | res.end();
|
---|
| 142 | }
|
---|
| 143 |
|
---|
| 144 | /**
|
---|
| 145 | * /---------------------- GET ----------------------/
|
---|
| 146 | * Creates the table and enters the user inside
|
---|
| 147 | * @action sit_down
|
---|
| 148 | * @param session_id
|
---|
| 149 | * @param tableId
|
---|
| 150 | */
|
---|
| 151 | if (req.query.action === 'sit_down' && req.query?.session_id && req.query?.tableId) {
|
---|
| 152 | const { success, table, player } = getTableAndPlayer(req.query.session_id)
|
---|
| 153 |
|
---|
| 154 | if (success && !table.started) {
|
---|
| 155 | player.isSatDown = true;
|
---|
| 156 | }
|
---|
| 157 |
|
---|
[95ce58b] | 158 | update_tables_to_database();
|
---|
| 159 |
|
---|
[b13f93b] | 160 | res.end();
|
---|
| 161 | }
|
---|
| 162 |
|
---|
[3a783f2] | 163 | /**
|
---|
| 164 | * /---------------------- GET ----------------------/
|
---|
| 165 | * Creates the table and enters the user inside
|
---|
| 166 | * @action leave_table
|
---|
| 167 | * @param session_id
|
---|
| 168 | */
|
---|
| 169 | if (req.query.action === 'leave_table' && req.query?.session_id) {
|
---|
| 170 | const { success, table, player } = getTableAndPlayer(req.query.session_id);
|
---|
| 171 |
|
---|
| 172 | if (success) {
|
---|
[95ce58b] | 173 | table.lastActivity = Date.now();
|
---|
| 174 |
|
---|
[3a783f2] | 175 | player.isGhost = true;
|
---|
| 176 | player.isFolded = true;
|
---|
| 177 |
|
---|
[aac3b2b] | 178 | if (table.started) {
|
---|
| 179 | if (table.players[table.turnIdx] !== undefined && table.players[table.turnIdx] === player) {
|
---|
| 180 | setNextPlayerIdx(table.id);
|
---|
| 181 | }
|
---|
| 182 | }
|
---|
| 183 | else {
|
---|
| 184 | table.players = table.players.filter(e=>e.isGhost === false);
|
---|
[3a783f2] | 185 | }
|
---|
| 186 | }
|
---|
| 187 |
|
---|
[95ce58b] | 188 | update_tables_to_database();
|
---|
| 189 |
|
---|
[3a783f2] | 190 | res.end();
|
---|
| 191 | }
|
---|
| 192 |
|
---|
[b13f93b] | 193 | /**
|
---|
| 194 | * /---------------------- GET ----------------------/
|
---|
| 195 | * Creates the table and enters the user inside
|
---|
| 196 | * @action join_a_table
|
---|
| 197 | * @param session_id
|
---|
| 198 | * @param tableId
|
---|
| 199 | * @param displayName
|
---|
| 200 | */
|
---|
| 201 | if (req.query.action === 'join_a_table' && req.query?.session_id && req.query?.tableId && req.query?.displayName) {
|
---|
| 202 | if (req.query.tableId.length > 0) {
|
---|
| 203 | const { success } = getTableAndPlayer(req.query.session_id);
|
---|
| 204 |
|
---|
| 205 | if (!success) {
|
---|
| 206 | const table = getTable(req.query.tableId)
|
---|
| 207 |
|
---|
[3a783f2] | 208 | if (table !== undefined && !table.started) {
|
---|
[b13f93b] | 209 | table.players.push({
|
---|
| 210 | id: req.query.session_id,
|
---|
| 211 | table: req.query.tableId,
|
---|
[189cd8f] | 212 | credits: 0,
|
---|
| 213 | status: '_1_just_entered',
|
---|
| 214 | displayName: req.query.displayName,
|
---|
| 215 | cards: [],
|
---|
| 216 | betAmount: 0,
|
---|
| 217 | wonAmount: 0,
|
---|
| 218 | isSatDown: false,
|
---|
| 219 | isCoordinator: false,
|
---|
| 220 | isFolded: false,
|
---|
| 221 | isGhost: false,
|
---|
| 222 | hand: {
|
---|
| 223 | hand: '',
|
---|
| 224 | highCard: 0,
|
---|
| 225 | },
|
---|
[b13f93b] | 226 | })
|
---|
| 227 | }
|
---|
| 228 | }
|
---|
| 229 | }
|
---|
| 230 |
|
---|
[95ce58b] | 231 | update_tables_to_database();
|
---|
| 232 |
|
---|
[b13f93b] | 233 | res.end();
|
---|
| 234 | }
|
---|
| 235 |
|
---|
| 236 | /**
|
---|
| 237 | * /---------------------- GET ----------------------/
|
---|
| 238 | * Creates the table and enters the user inside
|
---|
| 239 | * @action create_a_table
|
---|
| 240 | * @param session_id
|
---|
| 241 | * @param displayName
|
---|
| 242 | * @param tableName
|
---|
| 243 | */
|
---|
| 244 | if (req.query.action === 'create_a_table' && req.query?.session_id && req.query?.displayName && req.query?.tableName) {
|
---|
| 245 | const { success } = getTableAndPlayer(req.query.session_id);
|
---|
| 246 |
|
---|
| 247 | if (!success) {
|
---|
| 248 | createTable(req.query.session_id, req.query.displayName, req.query.tableName);
|
---|
| 249 | }
|
---|
| 250 |
|
---|
[95ce58b] | 251 | update_tables_to_database();
|
---|
| 252 |
|
---|
[b13f93b] | 253 | res.end();
|
---|
| 254 | }
|
---|
| 255 |
|
---|
| 256 | /**
|
---|
| 257 | * /---------------------- GET ----------------------/
|
---|
[d0ef259] | 258 | * Updates the state periodically
|
---|
[b13f93b] | 259 | * @action update_state
|
---|
| 260 | * @param session_id
|
---|
| 261 | */
|
---|
| 262 | if (req.query.action === 'update_state' && req.query?.session_id) {
|
---|
| 263 | const session_id = req.query.session_id;
|
---|
| 264 |
|
---|
[189cd8f] | 265 | const { success, table, player } = getTableAndPlayer(session_id);
|
---|
[b13f93b] | 266 |
|
---|
[d0ef259] | 267 | if (success && table.started && !table.ended) {
|
---|
[95ce58b] | 268 | const d = Date.now();
|
---|
| 269 |
|
---|
| 270 | if (d - table.lastActivity > 30000) {
|
---|
| 271 | if (table.players[table.turnIdx] !== undefined) {
|
---|
| 272 | table.players[table.turnIdx].isFolded = true;
|
---|
| 273 |
|
---|
[d0ef259] | 274 | table.lastActivity = Date.now();
|
---|
[95ce58b] | 275 | setNextPlayerIdx(table.id);
|
---|
| 276 | }
|
---|
| 277 | }
|
---|
| 278 | }
|
---|
| 279 |
|
---|
[b13f93b] | 280 | res.json({
|
---|
| 281 | success: true,
|
---|
| 282 | pokerGame: {
|
---|
| 283 | tables: getRestrictedTablesArray(),
|
---|
| 284 | table: getRestrictedTableArray(table.id, req.query.session_id),
|
---|
| 285 | player: player,
|
---|
| 286 | }
|
---|
| 287 | })
|
---|
| 288 | }
|
---|
| 289 |
|
---|
| 290 | /**
|
---|
| 291 | * /---------------------- GET ----------------------/
|
---|
| 292 | * If the player is not in an existing room, create a room for them.
|
---|
| 293 | * If they are reconnecting, get the room they were in.
|
---|
| 294 | * @action get_player_info_on_enter
|
---|
| 295 | * @param session_id
|
---|
| 296 | */
|
---|
| 297 | if (req.query.action === 'get_player_info_on_enter' && req.query?.session_id) {
|
---|
| 298 | const session_id = req.query.session_id;
|
---|
| 299 |
|
---|
| 300 | axios.get(`${process.env.HOME_URL}/api/postgre?action=check_if_logged_in&session_id=${session_id}`).then(postgreRes => {
|
---|
| 301 | if (postgreRes.data?.success) {
|
---|
| 302 | res.json({
|
---|
| 303 | success: true,
|
---|
| 304 | displayName: postgreRes.data?.displayName,
|
---|
| 305 | session_id: postgreRes.data?.session_id,
|
---|
| 306 | credits: postgreRes.data?.credits,
|
---|
| 307 | })
|
---|
| 308 | }
|
---|
| 309 | else {
|
---|
| 310 | res.json({
|
---|
| 311 | success: false,
|
---|
| 312 | })
|
---|
| 313 | }
|
---|
| 314 | });
|
---|
| 315 | }
|
---|
| 316 | }
|
---|
| 317 | }
|
---|
| 318 | /**
|
---|
| 319 | * ********************* END OF REQUEST HANDLER *********************
|
---|
| 320 | */
|
---|