Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Next step developments #8

Merged
merged 6 commits into from
Jan 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions src/database/queries.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import {col, fn, Op} from 'sequelize';
import {Statistic} from './models/statistic';

export async function getDailyAverageStats(userId: number) {
const oneMonthAgo = new Date();
oneMonthAgo.setMonth(oneMonthAgo.getMonth() - 1);
const results = await Statistic.findAll({
attributes: [
[fn('DATE_TRUNC', 'day', col('createdAt')), 'date'],
[fn('AVG', col('money')), 'avgMoney'],
[fn('AVG', col('winCount')), 'avgWinCount'],
[fn('AVG', col('loseCount')), 'avgLoseCount'],
],
where: {
userId: userId,
createdAt: {
[Op.gte]: oneMonthAgo,
},
},
group: [fn('DATE_TRUNC', 'day', col('createdAt'))],
order: [fn('DATE_TRUNC', 'day', col('createdAt'))],
raw: true,
});

const labels: string[] = [];
const averageMoney: number[] = [];
const averageWinCount: number[] = [];
const averageLoseCount: number[] = [];

results.forEach((row: any) => {
const date = new Date(row.date).toLocaleDateString('en-US', {
month: 'short',
day: 'numeric',
}); // Format date as 'Jan 1', 'Jan 2', etc.
labels.push(date);
averageMoney.push(parseFloat(parseFloat(row.avgMoney).toFixed(2)));
averageWinCount.push(parseFloat(parseFloat(row.avgWinCount).toFixed(2)));
averageLoseCount.push(parseFloat(parseFloat(row.avgLoseCount).toFixed(2)));
});

return {labels, averageMoney, averageWinCount, averageLoseCount};
}
2 changes: 1 addition & 1 deletion src/games/fiveCardDraw/fiveCardDrawBot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export class FiveCardDrawBot implements BotInterface {
}

performAction(): { action: string; amount: number; cardsToDiscard: string[] } {
if (this.playerMoney <= this.tableMinBet + 500) {
if (this.playerMoney <= this.tableMinBet + 100) {
this.resultsSet.action = BOT_REMOVE;
} else if (this.isCallSituation && this.checkAmount > this.playerMoney) {
this.resultsSet.action = BOT_FOLD;
Expand Down
1 change: 1 addition & 0 deletions src/games/fiveCardDraw/fiveCardDrawTable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1180,6 +1180,7 @@ export class FiveCardDrawTable {
removeBotFromTable(currentPlayerTurn: number): void {
this.eventEmitter.emit(NEW_BOT_EVENT_KEY, this.tableId, gameConfig.games.fiveCardDraw.startMoney);
this.players[currentPlayerTurn].socket = null;
this.players[currentPlayerTurn].selectedTableId = -1;
}

getTableBotCount(): number {
Expand Down
7 changes: 5 additions & 2 deletions src/games/gameHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import EventEmitter from 'events';
import {NEW_BOT_EVENT_KEY, NEW_PLAYER_STARTING_FUNDS} from '../constants';
import {Achievement} from '../database/models/achievement';
import {FiveCardDrawBot} from './fiveCardDraw/fiveCardDrawBot';
import {getDailyAverageStats} from '../database/queries';

let playerIdIncrement = 0;
const players = new Map<WebSocket, Player>();
Expand Down Expand Up @@ -419,6 +420,7 @@ class GameHandler implements GameHandlerInterface {
const user = await User.findOne({where: {id: auth.userId}});
if (player && user) {
const achievements: Achievement[] = await Achievement.findAll({where: {userId: auth.userId}});
const dailyAverageStats = await getDailyAverageStats(auth.userId);
const response: ClientResponse = {
key: 'userStatistics',
data: {
Expand All @@ -431,6 +433,7 @@ class GameHandler implements GameHandlerInterface {
achievements: achievements.map(({id, achievementType}) => ({
id, achievementType,
})),
dailyAverageStats: dailyAverageStats,
},
success: true,
}
Expand Down Expand Up @@ -573,7 +576,7 @@ class GameHandler implements GameHandlerInterface {
amount: action.amount,
}
};
logger.info(`Sending player ${player.playerId} auto play action ${action.action}`);
logger.info(`🤖 Sending player ${player.playerId} auto play action ${action.action}`);
player.socket?.send(JSON.stringify(responseArray));
} else if (table instanceof FiveCardDrawTable) {
const check_amount = table.currentHighestBet === 0 ?
Expand All @@ -597,7 +600,7 @@ class GameHandler implements GameHandlerInterface {
cards: action.cardsToDiscard,
}
};
logger.info(`Sending player ${player.playerId} auto play action ${action.action}`);
logger.info(`🤖 Sending player ${player.playerId} auto play action ${action.action}`);
player.socket?.send(JSON.stringify(responseArray));
} else {
logger.warn('No auto play handler defined for selected game');
Expand Down
2 changes: 1 addition & 1 deletion src/games/holdem/holdemBot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ export class HoldemBot implements BotInterface {
}

performAction(): { action: string; amount: number } {
if (this.playerMoney <= this.tableMinBet + 500) {
if (this.playerMoney <= this.tableMinBet + 100) {
this.resultsSet.action = BOT_REMOVE;
} else if (this.isCallSituation && this.checkAmount > this.playerMoney) {
this.resultsSet.action = BOT_FOLD;
Expand Down
1 change: 1 addition & 0 deletions src/games/holdem/holdemTable.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1182,6 +1182,7 @@ export class HoldemTable implements HoldemTableInterface {
removeBotFromTable(currentPlayerTurn: number): void {
this.eventEmitter.emit(NEW_BOT_EVENT_KEY, this.tableId, gameConfig.games.holdEm.startMoney);
this.players[currentPlayerTurn].socket = null;
this.players[currentPlayerTurn].selectedTableId = -1;
}

getTableBotCount(): number {
Expand Down
8 changes: 8 additions & 0 deletions src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,13 @@ export interface AuthInterface {
userId: number;
}

export interface DailyAverageStatsInterface {
labels: string[];
averageMoney: number[];
averageWinCount: number[];
averageLoseCount: number[];
}

export interface UserStatsInterface {
username: string;
money: number;
Expand All @@ -250,4 +257,5 @@ export interface UserStatsInterface {
id: number;
achievementType: string;
}[];
dailyAverageStats: DailyAverageStatsInterface;
}
Loading