Add travel

This commit is contained in:
Aslan 2026-01-21 15:45:41 -05:00
parent eea6031423
commit 1c0a2124f0
2 changed files with 28 additions and 7 deletions

View file

@ -7,6 +7,7 @@ import {
getLocationByName,
getLocationDistance,
getParentLocation,
getTravelTimeInHours,
hasLocation,
startTravel,
} from "../../services/game/location.js";
@ -210,25 +211,36 @@ const onTravel = (text: string, roomId: string, sender: string) => {
const player = getPlayerById(sender);
const locationName = text.replace(`${gamePrefix} travel `, "").trim();
const location = getLocationByName(locationName);
if (!location) {
const travelLocation = getLocationByName(locationName);
if (!travelLocation) {
client.sendTextMessage(roomId, "No such location exists");
return;
}
const currentLocationHasLocation = hasLocation(
player.location,
location.id,
travelLocation.id,
);
const parentLocationHasLocation = hasLocation(
getParentLocation(player.location).id,
location.id,
travelLocation.id,
);
if (!currentLocationHasLocation && !parentLocationHasLocation) {
client.sendTextMessage(roomId, "No such location found nearby");
return;
}
const travelTime = getTravelTimeInHours(
player,
getLocation(player.location),
travelLocation,
);
client.sendTextMessage(
roomId,
`You're now travelling to ${travelLocation.name}. It will take you ${travelTime.toFixed(1)} hours`,
);
startTravel(player, location.id, client, roomId);
};

View file

@ -51,6 +51,16 @@ const hasLocation = (
return childLocations.includes(locationChild);
};
const getTravelTimeInHours = (
player: IPlayer,
locationA: ILocation,
locationB: ILocation,
) => {
const distance = getLocationDistance(locationA, locationB);
return (distance / getSpeed(player)) * 3600000;
};
const startTravel = (
player: IPlayer,
location: Location,
@ -60,13 +70,11 @@ const startTravel = (
const currentLocation = getLocation(player.location);
const newLocation = getLocation(location);
const distance = getLocationDistance(currentLocation, newLocation);
setTimeout(
() => {
finishTravel(player, newLocation, client, roomId);
},
(distance / getSpeed(player)) * 3600000,
getTravelTimeInHours(player, currentLocation, newLocation),
);
};
@ -90,6 +98,7 @@ export {
getLocationDistance,
getParentLocation,
hasLocation,
getTravelTimeInHours,
startTravel,
finishTravel,
};