Fix bugs and add leave

This commit is contained in:
Aslan 2026-01-23 09:26:48 -05:00
parent 5f168fc067
commit a14be66890
2 changed files with 31 additions and 11 deletions

View file

@ -69,15 +69,7 @@ const getLevel = (experience: number): ILevel => {
}; };
const getUserName = (user: IUser): string => { const getUserName = (user: IUser): string => {
const userPattern = /@[a-zA-Z0-9]*/; return getUserNameById(user.id);
const match = user.id.match(userPattern)?.at(0);
if (!match) {
return "";
}
let username = match.replaceAll("@", "");
username = username.charAt(0).toUpperCase() + username.slice(1);
return username;
}; };
const getUserNameById = (userId: string): string => { const getUserNameById = (userId: string): string => {

View file

@ -32,6 +32,7 @@ import {
type IStat, type IStat,
type Location, type Location,
} from "../../services/game/index.js"; } from "../../services/game/index.js";
import { getUserNameById } from "../../helpers.js";
let client: MatrixClient; let client: MatrixClient;
const gamePrefix = `${config.app.triggerPrefix}game`; const gamePrefix = `${config.app.triggerPrefix}game`;
@ -66,6 +67,10 @@ const registerModuleGame = (
startConditions: [`${gamePrefix} travel `], startConditions: [`${gamePrefix} travel `],
callbackFunc: onTravel, callbackFunc: onTravel,
}); });
callbackStore.messageCallbacks.push({
startConditions: [`${gamePrefix} leave`],
callbackFunc: onLeave,
});
callbackStore.messageCallbacks.push({ callbackStore.messageCallbacks.push({
startConditions: [`${gamePrefix} entities`], startConditions: [`${gamePrefix} entities`],
callbackFunc: onEntities, callbackFunc: onEntities,
@ -88,6 +93,7 @@ const onHelp = (_text: string, roomId: string) => {
<li><b>!game location</b> - Shows information about your current location</li> <li><b>!game location</b> - Shows information about your current location</li>
<li><b>!game nearby</b> - Shows nearby locations</li> <li><b>!game nearby</b> - Shows nearby locations</li>
<li><b>!game travel {location}</b> - Travel to a location</li> <li><b>!game travel {location}</b> - Travel to a location</li>
<li><b>!game leave</b> - Leave your current location</li>
<li><b>!game entities</b> - Shows entities at your location</li> <li><b>!game entities</b> - Shows entities at your location</li>
<li><b>!game entity {entity}</b> - Shows information about an entity</li> <li><b>!game entity {entity}</b> - Shows information about an entity</li>
<li><b>(WIP) !game talk {entity}</b> - Talk to an entity</li> <li><b>(WIP) !game talk {entity}</b> - Talk to an entity</li>
@ -98,7 +104,11 @@ const onHelp = (_text: string, roomId: string) => {
}; };
const onStatus = (_text: string, roomId: string, sender: string) => { const onStatus = (_text: string, roomId: string, sender: string) => {
const entity = getEntityByName(sender); onStatusName(roomId, getUserNameById(sender));
};
const onStatusName = (roomId: string, name: string) => {
const entity = getEntityByName(name);
const race = getRace(entity.race); const race = getRace(entity.race);
const location = getLocation(entity.location); const location = getLocation(entity.location);
@ -275,6 +285,24 @@ const onTravel = (text: string, roomId: string, sender: string) => {
startTravel(player, travelLocation.id, client, roomId); startTravel(player, travelLocation.id, client, roomId);
}; };
const onLeave = (_text: string, roomId: string, sender: string) => {
const player = getPlayer(sender);
const parentLocation = getParentLocation(player.location);
const travelTime = getTravelTimeInHours(
player,
getLocation(player.location),
parentLocation,
);
client.sendTextMessage(
roomId,
`You're now travelling to ${parentLocation.name}. It will take you ${travelTime.toFixed(1)} hours`,
);
startTravel(player, parentLocation.id, client, roomId);
};
const onEntities = (_text: string, roomId: string, sender: string) => { const onEntities = (_text: string, roomId: string, sender: string) => {
const player = getPlayer(sender); const player = getPlayer(sender);
const location = getLocation(player.location); const location = getLocation(player.location);
@ -312,7 +340,7 @@ const onEntity = (text: string, roomId: string, sender: string) => {
return; return;
} }
onStatus("", roomId, entity.name); onStatusName(roomId, entity.name);
}; };
export { registerModuleGame }; export { registerModuleGame };