public void MoveToLocation(LocationId locationId) { if (_roadMovementComponent != null) { throw new InvalidOperationException($"Player {_playerId} is at road {_roadMovementComponent.Road.RoadId}. Cannot enter location from here."); } if (_locationId == locationId) { throw new InvalidOperationException($"Player {_playerId} is already at location {_locationId}. Cannot move to the same location."); } var currentLocation = _locationStore.Find(_locationId); if (currentLocation == null) { throw new InvalidOperationException($"Player {_playerId} is currently at not existing location {_locationId}. Cannot move to location {locationId}."); } if (_locationStore.Find(locationId) == null) { throw new InvalidOperationException($"Location {locationId} doesn't exist."); } if (!currentLocation.Locations.Contains(locationId)) { throw new InvalidOperationException($"Cannot move the player {_playerId} from {_locationId} to {locationId}. There is no path to this location from the current location."); } _locationId = locationId; _updateMessagingGroup($"location_{locationId}"); }
public ValueTask HandleAsync(ConnectedClient sender, MoveToLocation message, CancellationToken cancellationToken) { var characterId = sender.ClientId; var location = _locationStore.FindLocationForCharacter(characterId); if (location == null) { // Character has never connected yet. throw new InvalidOperationException("Cannot move between locations. Did not join the world yet."); } // Consider using sender.Group to get current location. if (sender.Group != location.LocationId) { throw new InvalidOperationException("Current group of player is not equal to location. Synchronization mismatch."); } if (!location.Locations.Contains(message.LocationId)) { throw new InvalidOperationException("Cannot move to this location from another location."); } var newLocation = _locationStore.Find(message.LocationId); if (newLocation == null) { throw new InvalidOperationException("Location does not exist."); } // TODO: Transaction? location.Characters.Remove(characterId); _locationStore.Save(location); newLocation.Characters.Add(characterId); _locationStore.Save(newLocation); sender.Group = newLocation.LocationId; return(default);