Skip to content

Commit

Permalink
Merge pull request #40 from claustra01/feat/view_ranking
Browse files Browse the repository at this point in the history
ランキング表示コマンド
  • Loading branch information
claustra01 authored Dec 17, 2023
2 parents 5819771 + 1c4f47a commit da8be7c
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 0 deletions.
42 changes: 42 additions & 0 deletions src/adapter/commands/ranking.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { Reply, ReplyType } from '../../usecase/types/reply';
import { playerController } from '../queries/player';

export const commandRanking = async (args: string[]): Promise<Reply> => {
if (args.length !== 2) {
return {
type: ReplyType.Error,
errorText: `Invalid Arguments: maxRange`,
};
}
const maxRange = parseInt(args[1]);
if (isNaN(maxRange)) {
return {
type: ReplyType.Error,
errorText: `Error: maxRange is not a number`,
};
}
if (maxRange < 1 || maxRange > 10) {
return {
type: ReplyType.Error,
errorText: `Error: maxRange must be between 1 and 10`,
};
}
try {
const players = await playerController.readRanking(maxRange);
let replyText = '';
players.forEach((player, index) => {
replyText += `${index + 1}. ${
player.discordId ? player.discordId : player.playerName
}: ${player.currentRate}pt\n`;
});
return {
type: ReplyType.Text,
contentText: replyText,
};
} catch (error) {
return {
type: ReplyType.Error,
errorText: `${error}`,
};
}
};
14 changes: 14 additions & 0 deletions src/adapter/queries/player.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,20 @@ export class PlayerController implements IController<Player, NewPlayer> {
return parsePlayerList(result);
}

async readRanking(maxCount: number): Promise<Player[]> {
const result = await this.pool
.query(`SELECT * FROM players ORDER BY current_rate DESC LIMIT $1`, [
maxCount.toString(),
])
.catch((error) => {
throw error;
});
if (result.length === 0) {
throw new Error(`Player Not Found`);
}
return parsePlayerList(result);
}

async readByDiscordOrName(id: string): Promise<Player> {
const result = await this.pool
.query(`SELECT * FROM players WHERE discord_id = $1`, [id])
Expand Down
10 changes: 10 additions & 0 deletions src/infrastructure/discord.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Client, GatewayIntentBits, Message, Partials } from 'discord.js';
import { commandLink } from '../adapter/commands/link';
import { commandPing } from '../adapter/commands/ping';
import { commandRanking } from '../adapter/commands/ranking';
import { commandRate } from '../adapter/commands/rate';
import { commandRegister } from '../adapter/commands/register';
import { config } from '../config/config';
Expand Down Expand Up @@ -88,6 +89,15 @@ export const runDiscordBot = () => {
message.reply(replyText);
break;
}
// ranking
case commands.ranking.name: {
if (!checkPermission(message, line, commands.ping.requirePermission))
break;
const reply = await commandRanking(commandText);
const replyText = generateReply(reply, line);
message.reply(replyText);
break;
}
// register
case commands.register.name: {
if (
Expand Down
5 changes: 5 additions & 0 deletions src/usecase/types/commands.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export interface Command {
export const enum CommandList {
ping = 'ping',
rate = 'rate',
ranking = 'ranking',
register = 'register',
link = 'link',
}
Expand All @@ -19,6 +20,10 @@ export const commands: Record<CommandList, Command> = {
name: 'rate',
requirePermission: false,
},
ranking: {
name: 'ranking',
requirePermission: false,
},
register: {
name: 'register',
requirePermission: true,
Expand Down

0 comments on commit da8be7c

Please sign in to comment.