logs prune and minor changes

This commit is contained in:
Milo
2025-06-18 15:07:11 +02:00
parent 76f011f995
commit 325c3bfaba
2 changed files with 35 additions and 3 deletions

View File

@@ -19,7 +19,7 @@ import {
postAPOBuy,
initialShuffledCards,
getFirstActivePlayerAfterDealer,
getNextActivePlayer, checkEndOfBettingRound, initialCards, checkRoomWinners
getNextActivePlayer, checkEndOfBettingRound, initialCards, checkRoomWinners, pruneOldLogs
} from './utils.js';
import {channelPointsHandler, eloHandler, pokerTest, slowmodesHandler} from './game.js';
import { Client, GatewayIntentBits, EmbedBuilder, ActionRowBuilder, ButtonBuilder, ButtonStyle } from 'discord.js';
@@ -812,7 +812,7 @@ app.post('/interactions', verifyKeyMiddleware(process.env.PUBLIC_KEY), async fun
const guildId = req.body.guild_id;
const roleId = process.env.VOTING_ROLE_ID; // Set this in your .env file
const onlineEligibleUsers = await getOnlineUsersWithRole(guildId, roleId);
const requiredMajority = Math.max(parseInt(process.env.MIN_VOTES), Math.floor(onlineEligibleUsers.size / 2) + 1);
const requiredMajority = Math.max(parseInt(process.env.MIN_VOTES), Math.floor(onlineEligibleUsers.size / (time >= 21600 ? 2 : 3)) + 1);
const votesNeeded = Math.max(0, requiredMajority - activePolls[id].for);
activePolls[id].endTime = Date.now() + process.env.POLL_TIME * 1000;
@@ -2660,7 +2660,10 @@ app.get('/users/by-elo', (req, res) => {
res.json(users);
})
app.get('/logs', (req, res) => {
app.get('/logs', async (req, res) => {
// purge old logs
await pruneOldLogs()
return res.status(200).json(getLogs.all())
})

View File

@@ -3,6 +3,7 @@ import OpenAI from "openai";
import {GoogleGenAI} from "@google/genai";
import {Mistral} from '@mistralai/mistralai';
import pkg from 'pokersolver';
import {flopoDB, getAllUsers} from "./init_database.js";
const { Hand } = pkg;
@@ -282,4 +283,32 @@ export function checkRoomWinners(room) {
);
return winningIndices.map(index => playerSolutions[index].id);
}
export async function pruneOldLogs() {
const users = flopoDB.prepare(`
SELECT user_id
FROM logs
GROUP BY user_id
HAVING COUNT(*) > ${process.env.LOGS_BY_USER}
`).all();
const transaction = flopoDB.transaction(() => {
for (const { user_id } of users) {
flopoDB.prepare(`
DELETE FROM logs
WHERE id IN (
SELECT id FROM (
SELECT id,
ROW_NUMBER() OVER (ORDER BY id DESC) AS rn
FROM logs
WHERE user_id = ?
)
WHERE rn > ${process.env.LOGS_BY_USER}
)
`).run(user_id);
}
});
transaction()
}