118 lines
3.3 KiB
TypeScript
118 lines
3.3 KiB
TypeScript
import { MatrixClient } from "matrix-js-sdk";
|
|
import type { ICallbackStore } from "../types.js";
|
|
import { config } from "../../config.js";
|
|
import {
|
|
getLevel,
|
|
getUserById,
|
|
getUserFromMention,
|
|
getUserName,
|
|
} from "../../helpers.js";
|
|
import { state } from "../../store/store.js";
|
|
import type { IUser } from "../../store/types.js";
|
|
let client: MatrixClient;
|
|
|
|
const registerModuleUser = (
|
|
matrixClient: MatrixClient,
|
|
callbackStore: ICallbackStore,
|
|
) => {
|
|
client = matrixClient;
|
|
|
|
callbackStore.messageCallbacks.push({
|
|
startConditions: [`${config.app.triggerPrefix}me`],
|
|
callbackFunc: onMe,
|
|
});
|
|
callbackStore.messageCallbacks.push({
|
|
startConditions: [`${config.app.triggerPrefix}myinfo `],
|
|
callbackFunc: onMyInfo,
|
|
});
|
|
callbackStore.messageCallbacks.push({
|
|
startConditions: [`${config.app.triggerPrefix}leaderboard`],
|
|
callbackFunc: onLeaderboard,
|
|
});
|
|
callbackStore.messageCallbacks.push({
|
|
startConditions: [`${config.app.triggerPrefix}aileaderboard`],
|
|
callbackFunc: onAILeaderboard,
|
|
});
|
|
};
|
|
|
|
const onMe = (text: string, roomId: string, sender: string) => {
|
|
const mention = text.split(" ")[1];
|
|
|
|
const user = mention ? getUserFromMention(mention) : getUserById(sender);
|
|
if (!user) {
|
|
return;
|
|
}
|
|
const level = getLevel(user.experience);
|
|
|
|
client.sendHtmlMessage(
|
|
roomId,
|
|
"",
|
|
`<p>Your Role: <b>${user.role}</b></p></br>
|
|
<p>Your Money: <b>${user.money}</b></p></br>
|
|
<p>Your AI Cost: <b>${user.aiCost.toFixed(2)}$</b></p></br>
|
|
<p>Your Level: <b>${level.level}</b></p></br>
|
|
<i>Next level progress: <b>${level.experienceInLevel}/${level.expToNextLevel}xp</b></i>`,
|
|
);
|
|
};
|
|
|
|
const onMyInfo = (text: string, roomId: string, sender: string) => {
|
|
const user = getUserById(sender);
|
|
const newInformation = text.replace(
|
|
`${config.app.triggerPrefix}myinfo `,
|
|
"",
|
|
);
|
|
|
|
if (!user || newInformation.length < 3) {
|
|
return;
|
|
}
|
|
|
|
user.information = newInformation;
|
|
|
|
client.sendTextMessage(roomId, "Information updated");
|
|
};
|
|
|
|
const onLeaderboard = (_text: string, roomId: string) => {
|
|
const mapUsersToLeaderboard = (user: IUser): string => {
|
|
const level = getLevel(user.experience);
|
|
const username = getUserName(user);
|
|
|
|
return `<li>${username}: level <b>${level.level}</b> (${level.experienceInLevel}/${level.expToNextLevel}xp)</li>`;
|
|
};
|
|
|
|
const users = state.users.sort(
|
|
(userA, userB) => userB.experience - userA.experience,
|
|
);
|
|
|
|
client.sendHtmlMessage(
|
|
roomId,
|
|
"",
|
|
`<h3>Leaderboard</h3>
|
|
<ul>
|
|
${users.map(mapUsersToLeaderboard).join("")}
|
|
</ul>`,
|
|
);
|
|
};
|
|
|
|
const onAILeaderboard = (_text: string, roomId: string) => {
|
|
const mapUsersToLeaderboard = (user: IUser): string => {
|
|
const cost = user.aiCost;
|
|
const username = getUserName(user);
|
|
|
|
return `<li>${username}: <b>${cost.toFixed(2)}$</b></li>`;
|
|
};
|
|
|
|
const users = state.users.sort(
|
|
(userA, userB) => userB.aiCost - userA.aiCost,
|
|
);
|
|
|
|
client.sendHtmlMessage(
|
|
roomId,
|
|
"",
|
|
`<h3>AI Cost Leaderboard</h3>
|
|
<ul>
|
|
${users.map(mapUsersToLeaderboard).join("")}
|
|
</ul>`,
|
|
);
|
|
};
|
|
|
|
export { registerModuleUser };
|