Fix bugs and add leave
This commit is contained in:
parent
5f168fc067
commit
a14be66890
2 changed files with 31 additions and 11 deletions
|
|
@ -69,15 +69,7 @@ const getLevel = (experience: number): ILevel => {
|
|||
};
|
||||
|
||||
const getUserName = (user: IUser): string => {
|
||||
const userPattern = /@[a-zA-Z0-9]*/;
|
||||
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;
|
||||
return getUserNameById(user.id);
|
||||
};
|
||||
|
||||
const getUserNameById = (userId: string): string => {
|
||||
|
|
|
|||
|
|
@ -32,6 +32,7 @@ import {
|
|||
type IStat,
|
||||
type Location,
|
||||
} from "../../services/game/index.js";
|
||||
import { getUserNameById } from "../../helpers.js";
|
||||
let client: MatrixClient;
|
||||
|
||||
const gamePrefix = `${config.app.triggerPrefix}game`;
|
||||
|
|
@ -66,6 +67,10 @@ const registerModuleGame = (
|
|||
startConditions: [`${gamePrefix} travel `],
|
||||
callbackFunc: onTravel,
|
||||
});
|
||||
callbackStore.messageCallbacks.push({
|
||||
startConditions: [`${gamePrefix} leave`],
|
||||
callbackFunc: onLeave,
|
||||
});
|
||||
callbackStore.messageCallbacks.push({
|
||||
startConditions: [`${gamePrefix} entities`],
|
||||
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 nearby</b> - Shows nearby locations</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 entity {entity}</b> - Shows information about 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 entity = getEntityByName(sender);
|
||||
onStatusName(roomId, getUserNameById(sender));
|
||||
};
|
||||
|
||||
const onStatusName = (roomId: string, name: string) => {
|
||||
const entity = getEntityByName(name);
|
||||
const race = getRace(entity.race);
|
||||
const location = getLocation(entity.location);
|
||||
|
||||
|
|
@ -275,6 +285,24 @@ const onTravel = (text: string, roomId: string, sender: string) => {
|
|||
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 player = getPlayer(sender);
|
||||
const location = getLocation(player.location);
|
||||
|
|
@ -312,7 +340,7 @@ const onEntity = (text: string, roomId: string, sender: string) => {
|
|||
return;
|
||||
}
|
||||
|
||||
onStatus("", roomId, entity.name);
|
||||
onStatusName(roomId, entity.name);
|
||||
};
|
||||
|
||||
export { registerModuleGame };
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue