Changeset faff334 for pages/api/postgre
- Timestamp:
- 07/18/22 13:59:55 (2 years ago)
- Branches:
- main
- Children:
- 22367db
- Parents:
- e903234
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
pages/api/postgre/index.js
re903234 rfaff334 179 179 /** 180 180 * /---------------------- GET ----------------------/ 181 * Get all the games played in the past. 182 * @action get_games_history 183 * @param session_id 184 */ 185 if (req.query?.action === 'get_games_history' && req.query?.session_id) { 186 const session_id = req.query.session_id 187 const session = sessions.find(session => session.id === session_id) 188 189 let blackjackHistory = []; 190 let rouletteHistory = []; 191 let pokerHistory = []; 192 193 if (session) { 194 // work 195 pool.query('SELECT * FROM blackjack_history WHERE username = $1', [session.username], (error, blackjackResults) => { 196 if (error) throw error; 197 198 if (blackjackResults.rows.length > 0) { 199 blackjackHistory = blackjackResults.rows[0]; 200 } 201 202 pool.query('SELECT * FROM roulette_history WHERE username = $1', [session.username], (error, rouletteResults) => { 203 if (error) throw error; 204 205 if (rouletteResults.rows.length > 0) { 206 rouletteHistory = rouletteResults.rows[0]; 207 } 208 209 pool.query('SELECT * FROM poker_history WHERE username = $1', [session.username], (error, pokerResults) => { 210 if (error) throw error; 211 212 if (pokerResults.rows.length > 0) { 213 pokerHistory = pokerResults.rows[0]; 214 } 215 216 res.json({ 217 success: true, 218 blackjack: JSON.parse(blackjackHistory.history ?? "[]"), 219 roulette: JSON.parse(rouletteHistory.history ?? "[]"), 220 poker: JSON.parse(pokerHistory.history ?? "[]"), 221 }) 222 }); 223 }); 224 }); 225 226 return ; 227 } 228 else { 229 res.json({ 230 success: false, 231 }) 232 } 233 } 234 235 /** 236 * /---------------------- GET ----------------------/ 237 * /--------------------- ADMIN ----------------------/ 238 * Get all the games currently played. 239 * @action get_live_games_as_admin 240 * @param admin_id 241 */ 242 if (req.query?.action === 'get_live_games_as_admin' && req.query?.admin_id) { 243 const admin_id = req.query.admin_id 244 const adminSession = adminSessions.find(adminSession => adminSession.id === admin_id) 245 246 if (adminSession) { 247 let tmpRooms = []; 248 249 for (let key in rooms) { 250 if (key === "loaded") continue ; 251 252 tmpRooms.push(rooms[key]); 253 tmpRooms[tmpRooms.length - 1].id = key; 254 } 255 256 res.json({ 257 success: true, 258 blackjack: tmpRooms, 259 roulette: game, 260 poker: tables, 261 }) 262 263 return ; 264 } 265 else { 266 res.json({ 267 success: false, 268 }) 269 } 270 } 271 272 /** 273 * /---------------------- GET ----------------------/ 181 274 * /--------------------- ADMIN ----------------------/ 182 275 * Get complaints from the players and show them to the admin … … 207 300 return ; 208 301 } 209 210 res.json({ 211 success: false, 212 }) 302 else { 303 res.json({ 304 success: false, 305 }) 306 } 213 307 } 214 308 … … 1171 1265 } 1172 1266 load_rooms_from_database(); 1267 1268 export async function saveGameInHistory(gameType, game, username) { 1269 if (!username || !game || !gameType) return ; 1270 1271 if (gameType === 'blackjack') { 1272 pool.query('SELECT * FROM blackjack_history WHERE username = $1', [username], (error, results) => { 1273 if (error) throw error; 1274 1275 if (results.rows.length > 0) { 1276 let games = JSON.parse(results?.rows[0]?.history ?? []); 1277 1278 if (games.indexOf(game) === -1) 1279 games.push(game); 1280 1281 pool.query('UPDATE blackjack_history SET history = $1 WHERE username = $2', [JSON.stringify(games), username], (error, newResults) => { 1282 if (error) throw error; 1283 }); 1284 } 1285 else { 1286 pool.query('INSERT INTO blackjack_history (username, history) VALUES ($1, $2)', [username, JSON.stringify([game])], (error, newResults) => { 1287 if (error) throw error; 1288 }); 1289 } 1290 }); 1291 } 1292 1293 if (gameType === 'roulette') { 1294 pool.query('SELECT * FROM roulette_history WHERE username = $1', [username], (error, results) => { 1295 if (error) throw error; 1296 1297 if (results.rows.length > 0) { 1298 let games = JSON.parse(results?.rows[0]?.history ?? []); 1299 1300 if (games.indexOf(game) === -1) 1301 games.push(game); 1302 1303 pool.query('UPDATE roulette_history SET history = $1 WHERE username = $2', [JSON.stringify(games), username], (error, newResults) => { 1304 if (error) throw error; 1305 }); 1306 } 1307 else { 1308 pool.query('INSERT INTO roulette_history (username, history) VALUES ($1, $2)', [username, JSON.stringify([game])], (error, newResults) => { 1309 if (error) throw error; 1310 }); 1311 } 1312 }); 1313 } 1314 1315 if (gameType === 'poker') { 1316 pool.query('SELECT * FROM poker_history WHERE username = $1', [username], (error, results) => { 1317 if (error) throw error; 1318 1319 if (results.rows.length > 0) { 1320 let games = JSON.parse(results?.rows[0]?.history ?? []); 1321 1322 if (games.indexOf(game) === -1) 1323 games.push(game); 1324 1325 pool.query('UPDATE poker_history SET history = $1 WHERE username = $2', [JSON.stringify(games), username], (error, newResults) => { 1326 if (error) throw error; 1327 }); 1328 } 1329 else { 1330 pool.query('INSERT INTO poker_history (username, history) VALUES ($1, $2)', [username, JSON.stringify([game])], (error, newResults) => { 1331 if (error) throw error; 1332 }); 1333 } 1334 }); 1335 } 1336 }
Note:
See TracChangeset
for help on using the changeset viewer.