Skip to content

Commit

Permalink
Send event for member_watch so leaderboard can get members
Browse files Browse the repository at this point in the history
  • Loading branch information
ttbowen committed Nov 5, 2023
1 parent a93bec9 commit 0777d0b
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ export class ChatManager extends events.EventEmitter {
return channel.join().receive("ok", (response: { room: Partial<Room> }) => {
this.roomChannels[roomId] = channel;
this.activeRooms[roomId] = new Room(response.room);
channel
.push(Events.MEMBER_WATCH, {})
.receive("ok", (response: { members: User[] }) => {
this.activeRooms[roomId].members = response.members;
});
});
}

Expand Down
23 changes: 12 additions & 11 deletions packages/mrwhale-gamejolt/src/commands/level/leaderboard.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { getLevelFromExp, codeBlock } from "@mrwhale-io/core";
import { Message, User } from "@mrwhale-io/gamejolt-client";
import * as sequelize from "sequelize";
import * as AsciiTable from "ascii-table";
import { User as GameUser } from "joltite.js";

import { getLevelFromExp, codeBlock } from "@mrwhale-io/core";
import { Message, User } from "@mrwhale-io/gamejolt-client";
import { GameJoltCommand } from "../../client/command/gamejolt-command";
import { Score } from "../../database/models/score";

Expand Down Expand Up @@ -35,8 +35,8 @@ export default class extends GameJoltCommand {
});
}

private createTable(mappedScores: MappedScores[]) {
const table = new AsciiTable("LEADERBOARD");
private createTable(name: string, mappedScores: MappedScores[]) {
const table = new AsciiTable(name);
table.setHeading("#", "Member", "Experience", "Level");

for (let i = 0; i < mappedScores.length; i++) {
Expand All @@ -60,9 +60,8 @@ export default class extends GameJoltCommand {
limit: 10,
});

const room = this.botClient.client.grid.chat.activeRooms[roomId];
const room = this.botClient.chat.activeRooms[roomId];
const mappedMembers = mapUsers(room.members);

const filteredScores = scores.filter(({ userId }) =>
Object.keys(mappedMembers).includes(userId.toString())
);
Expand All @@ -79,9 +78,9 @@ export default class extends GameJoltCommand {
private async getGlobalScores() {
const sum: any = sequelize.fn("sum", sequelize.col("exp"));
const scores = await Score.findAll({
attributes: ["userId", sum, "exp"],
order: [["exp", "DESC"]],
attributes: ["userId", [sum, "total"]],
group: ["Score.userId"],
order: [[sum, "DESC"]],
limit: 10,
});

Expand All @@ -90,9 +89,9 @@ export default class extends GameJoltCommand {
const mappedMembers = mapUsers(members.users);

const mappedScores: MappedScores[] = scores.map((score) => ({
exp: score.exp,
exp: score.getDataValue("total"),
user: mappedMembers[score.userId],
level: getLevelFromExp(score.exp),
level: getLevelFromExp(score.getDataValue("total")),
}));

return mappedScores;
Expand All @@ -101,8 +100,10 @@ export default class extends GameJoltCommand {
async action(message: Message, [command]: [string]): Promise<Message> {
try {
let mappedScores: MappedScores[] = [];
let name = "LEADERBOARD";
if (command && command.toLowerCase().trim() === "global") {
mappedScores = await this.getGlobalScores();
name = "GLOBAL LEADERBOARD";

if (mappedScores.length < 1) {
return message.reply("No one is ranked.");
Expand All @@ -115,7 +116,7 @@ export default class extends GameJoltCommand {
}
}

const table = this.createTable(mappedScores);
const table = this.createTable(name, mappedScores);

return message.reply(codeBlock(table.toString()));
} catch {
Expand Down

0 comments on commit 0777d0b

Please sign in to comment.