diff --git a/src/modules/game/game.ts b/src/modules/game/game.ts index 02cbe82..c36b58e 100644 --- a/src/modules/game/game.ts +++ b/src/modules/game/game.ts @@ -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); }; diff --git a/src/services/game/location.ts b/src/services/game/location.ts index b30e890..0e734e1 100644 --- a/src/services/game/location.ts +++ b/src/services/game/location.ts @@ -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, };