public Task SetRide(RideInfo rideInfo) { CurrentRide = rideInfo; // this user is no longer looking for a match - clear location this.UpdateLocationAndNotify(null); return(Task.CompletedTask); }
public static async Task <bool> TryFinalizeMatch(string initiator, string candidate, IDurableOrchestrationContext context) { var initiatorEntity = new EntityId(nameof(UserEntity), initiator); var candidateEntity = new EntityId(nameof(UserEntity), candidate); // Check if both users are still available and close enough. // To prevent race conditions, we do this in a critical section // that locks both users. using (await context.LockAsync(initiatorEntity, candidateEntity)) { var initiatorProxy = context.CreateEntityProxy <IUserEntity>(initiatorEntity); var candidateProxy = context.CreateEntityProxy <IUserEntity>(candidateEntity); var initiatorInfo = await initiatorProxy.GetState(); var candidateInfo = await candidateProxy.GetState(); if (initiatorInfo.Location == null) { // initiator is no longer trying to find a match! No need to keep trying. return(true); } if (candidateInfo.Location == null || !ZipCodes.GetProximityList(initiatorInfo.Location.Value).Contains(candidateInfo.Location.Value)) { // candidate is no longer eligible return(false); } // match was successful. Create a new ride. var driver = initiator.StartsWith("D") ? initiatorInfo : candidateInfo; var rider = initiator.StartsWith("R") ? initiatorInfo : candidateInfo; var rideInfo = new RideInfo() { RideId = context.NewGuid(), DriverId = driver.UserId, DriverLocation = driver.Location.Value, RiderId = rider.UserId, RiderLocation = rider.Location.Value, }; // assign both users to the new ride. // (this is happening within the critical section) await Task.WhenAll(initiatorProxy.SetRide(rideInfo), candidateProxy.SetRide(rideInfo)); return(true); } }
public void ClearRide(Guid rideId) { if (CurrentRide != null && CurrentRide.RideId == rideId) { if (Entity.Current.EntityKey == CurrentRide.DriverId) { // forward signal to rider Entity.Current.SignalEntity <IUserEntity>(CurrentRide.RiderId, x => x.ClearRide(rideId)); } CurrentRide = null; } }