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

main
Last change on this file since e903234 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
Line 
1import axios from 'axios';
2
3require('dotenv').config();
4
5import { createTable, getRestrictedTablesArray, getRestrictedTableArray, getTable, getTableAndPlayer } from './gameStates';
6
7import { drawASingleCard, setNextPlayerIdx, progressRoundIfNeeded } from './tableSpecific'
8
9import { update_tables_to_database } from '../postgre/index'
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
31 if (success && table.started && !table.ended && player.isSatDown && !player.isFolded) {
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') {
40 if (table.lastBet === 0) {
41 table.turnsSinceLastBet++;
42 okayToGo = true;
43
44 progressRoundIfNeeded(table.id);
45 }
46 }
47 else if (req.query.specificAction === 'call') {
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 });
64 }
65 else if (req.query.specificAction === 'raise') {
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 }
82 }
83 else if (req.query.specificAction === 'fold') {
84 player.isFolded = true;
85 okayToGo = true;
86
87 progressRoundIfNeeded(table.id);
88 }
89
90 if (okayToGo) {
91 table.lastActivity = Date.now();
92 setNextPlayerIdx(table.id);
93 }
94 }
95
96 update_tables_to_database();
97
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
110 if (success && !table.started && !table.ended && table.players.filter(e=>e.isSatDown===true).length >= 2) {
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
119 table.lastActivity = Date.now();
120 table.started = true;
121 table.round = 1;
122
123 table.turnIdx = Math.floor(Math.random(0, table.players.length))
124 setNextPlayerIdx(table.id);
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 }
138
139 update_tables_to_database();
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
158 update_tables_to_database();
159
160 res.end();
161 }
162
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) {
173 table.lastActivity = Date.now();
174
175 player.isGhost = true;
176 player.isFolded = true;
177
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);
185 }
186 }
187
188 update_tables_to_database();
189
190 res.end();
191 }
192
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 * @param username
201 */
202 if (req.query.action === 'join_a_table' && req.query?.session_id && req.query?.tableId && req.query?.displayName && req.query?.username) {
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
209 if (table !== undefined && !table.started) {
210 table.players.push({
211 id: req.query.session_id,
212 table: req.query.tableId,
213 username: req.query.username,
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 },
228 })
229 }
230 }
231 }
232
233 update_tables_to_database();
234
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
245 * @param username
246 */
247 if (req.query.action === 'create_a_table' && req.query?.session_id && req.query?.displayName && req.query?.tableName && req.query?.username) {
248 const { success } = getTableAndPlayer(req.query.session_id);
249
250 if (!success) {
251 createTable(req.query.session_id, req.query.displayName, req.query.tableName, req.query.username);
252 }
253
254 update_tables_to_database();
255
256 res.end();
257 }
258
259 /**
260 * /---------------------- GET ----------------------/
261 * Updates the state periodically
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
268 const { success, table, player } = getTableAndPlayer(session_id);
269
270 if (success && table.started && !table.ended) {
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
277 table.lastActivity = Date.now();
278 setNextPlayerIdx(table.id);
279
280 update_tables_to_database();
281 }
282 }
283 }
284
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,
310 username: postgreRes.data?.username,
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.