source: pages/api/poker/index.js@ faff334

main
Last change on this file since faff334 was e007fcd, checked in by anastasovv <simon@…>, 2 years ago

Now you need to activate your account via email & also added mail sending after server crash

  • Property mode set to 100644
File size: 11.9 KB
RevLine 
[b13f93b]1import axios from 'axios';
2
3require('dotenv').config();
4
[95ce58b]5import { createTable, getRestrictedTablesArray, getRestrictedTableArray, getTable, getTableAndPlayer } from './gameStates';
[b13f93b]6
[55701f0]7import { drawASingleCard, setNextPlayerIdx, progressRoundIfNeeded } from './tableSpecific'
[b13f93b]8
[55701f0]9import { update_tables_to_database } from '../postgre/index'
[b13f93b]10
11/**
12 * ********************* BEGIN OF REQUEST HANDLER *********************
13 */
14export 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
[e007fcd]200 * @param username
[b13f93b]201 */
[e007fcd]202 if (req.query.action === 'join_a_table' && req.query?.session_id && req.query?.tableId && req.query?.displayName && req.query?.username) {
[b13f93b]203 if (req.query.tableId.length > 0) {
204 const { success } = getTableAndPlayer(req.query.session_id);
205
206 if (!success) {
207 const table = getTable(req.query.tableId)
208
[3a783f2]209 if (table !== undefined && !table.started) {
[b13f93b]210 table.players.push({
211 id: req.query.session_id,
212 table: req.query.tableId,
[e007fcd]213 username: req.query.username,
[189cd8f]214 credits: 0,
215 status: '_1_just_entered',
216 displayName: req.query.displayName,
217 cards: [],
218 betAmount: 0,
219 wonAmount: 0,
220 isSatDown: false,
221 isCoordinator: false,
222 isFolded: false,
223 isGhost: false,
224 hand: {
225 hand: '',
226 highCard: 0,
227 },
[b13f93b]228 })
229 }
230 }
231 }
232
[95ce58b]233 update_tables_to_database();
234
[b13f93b]235 res.end();
236 }
237
238 /**
239 * /---------------------- GET ----------------------/
240 * Creates the table and enters the user inside
241 * @action create_a_table
242 * @param session_id
243 * @param displayName
244 * @param tableName
[e007fcd]245 * @param username
[b13f93b]246 */
[e007fcd]247 if (req.query.action === 'create_a_table' && req.query?.session_id && req.query?.displayName && req.query?.tableName && req.query?.username) {
[b13f93b]248 const { success } = getTableAndPlayer(req.query.session_id);
249
250 if (!success) {
[e007fcd]251 createTable(req.query.session_id, req.query.displayName, req.query.tableName, req.query.username);
[b13f93b]252 }
253
[95ce58b]254 update_tables_to_database();
255
[b13f93b]256 res.end();
257 }
258
259 /**
260 * /---------------------- GET ----------------------/
[d0ef259]261 * Updates the state periodically
[b13f93b]262 * @action update_state
263 * @param session_id
264 */
265 if (req.query.action === 'update_state' && req.query?.session_id) {
266 const session_id = req.query.session_id;
267
[189cd8f]268 const { success, table, player } = getTableAndPlayer(session_id);
[b13f93b]269
[d0ef259]270 if (success && table.started && !table.ended) {
[95ce58b]271 const d = Date.now();
272
273 if (d - table.lastActivity > 30000) {
274 if (table.players[table.turnIdx] !== undefined) {
275 table.players[table.turnIdx].isFolded = true;
276
[d0ef259]277 table.lastActivity = Date.now();
[95ce58b]278 setNextPlayerIdx(table.id);
[e9f11ac]279
280 update_tables_to_database();
[95ce58b]281 }
282 }
283 }
284
[b13f93b]285 res.json({
286 success: true,
287 pokerGame: {
288 tables: getRestrictedTablesArray(),
289 table: getRestrictedTableArray(table.id, req.query.session_id),
290 player: player,
291 }
292 })
293 }
294
295 /**
296 * /---------------------- GET ----------------------/
297 * If the player is not in an existing room, create a room for them.
298 * If they are reconnecting, get the room they were in.
299 * @action get_player_info_on_enter
300 * @param session_id
301 */
302 if (req.query.action === 'get_player_info_on_enter' && req.query?.session_id) {
303 const session_id = req.query.session_id;
304
305 axios.get(`${process.env.HOME_URL}/api/postgre?action=check_if_logged_in&session_id=${session_id}`).then(postgreRes => {
306 if (postgreRes.data?.success) {
307 res.json({
308 success: true,
309 displayName: postgreRes.data?.displayName,
[e007fcd]310 username: postgreRes.data?.username,
[b13f93b]311 session_id: postgreRes.data?.session_id,
312 credits: postgreRes.data?.credits,
313 })
314 }
315 else {
316 res.json({
317 success: false,
318 })
319 }
320 });
321 }
322 }
323}
324/**
325 * ********************* END OF REQUEST HANDLER *********************
326 */
Note: See TracBrowser for help on using the repository browser.