Skip to content

Commit

Permalink
Fixed drop issue
Browse files Browse the repository at this point in the history
When an item was dropped and the player left the scope of it, this wasn't notified to the client. The issue was, that a Droppeditem doesn't implement IObservable, which it doesn't have to.
  • Loading branch information
sven-n committed Jun 24, 2024
1 parent 0df54b4 commit 7f9001d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 20 deletions.
42 changes: 24 additions & 18 deletions src/GameLogic/ObserverToWorldViewAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,23 +140,28 @@ public async ValueTask LocateablesOutOfScopeAsync(IEnumerable<ILocateable> oldOb
return;
}

IEnumerable<IObservable> oldItems;
IEnumerable<ILocateable> oldItems;
using (await this._observingLock.WriterLockAsync())
{
oldItems = oldObjects.OfType<IObservable>().Where(item => this._observingObjects.Contains(item) && this.ObjectWillBeOutOfScope(item)).ToList();
oldItems.ForEach(item => this._observingObjects.Remove(item));
oldItems = oldObjects.Where(this.ObjectWillBeOutOfScope).ToList();
oldItems
.OfType<IObservable>()
.ForEach(item => this._observingObjects.Remove(item));
}

await oldItems.ForEachAsync(item => item.RemoveObserverAsync(this._adaptee)).ConfigureAwait(false);
await oldItems
.OfType<IObservable>()
.ForEachAsync(item => item.RemoveObserverAsync(this._adaptee))
.ConfigureAwait(false);

if (this._adaptee is IHasBucketInformation { NewBucket: null })
{
// adaptee (player) left the map or disconnected; it's not required to update the view
}
else
{
var droppedItems = oldItems.OfType<ILocateable>().Where(item => item is DroppedItem || item is DroppedMoney);
var nonItems = oldItems.OfType<ILocateable>().Except(droppedItems).WhereActive();
var droppedItems = oldItems.Where(item => item is DroppedItem || item is DroppedMoney);
var nonItems = oldItems.Except(droppedItems).WhereActive();
if (nonItems.Any())
{
await this._adaptee.InvokeViewPlugInAsync<IObjectsOutOfScopePlugIn>(p => p.ObjectsOutOfScopeAsync(nonItems)).ConfigureAwait(false);
Expand Down Expand Up @@ -239,7 +244,7 @@ protected override async ValueTask DisposeAsyncCore()
await base.DisposeAsyncCore().ConfigureAwait(false);
}

private bool ObjectWillBeOutOfScope(IObservable observable)
private bool ObjectWillBeOutOfScope(ILocateable locateable)
{
var myBucketInformations = this._adaptee as IHasBucketInformation;
if (myBucketInformations is null)
Expand All @@ -253,20 +258,21 @@ private bool ObjectWillBeOutOfScope(IObservable observable)
return true;
}

var locateableBucketInformations = observable as IHasBucketInformation;
if (locateableBucketInformations is null)
if (locateable is IHasBucketInformation locateableBucketInformations)
{
// We have to assume that this observable will be out of scope since it can't tell us where it goes
return true;
}
if (locateableBucketInformations.NewBucket is null)
{
// It's leaving the map, so will be out of scope
return true;
}

if (locateableBucketInformations.NewBucket is null)
{
// It's leaving the map, so will be out of scope
return true;
// If we observe the target bucket of the observable, it will be in our range
return !this.ObservingBuckets.Contains(locateableBucketInformations.NewBucket);
}

// If we observe the target bucket of the observable, it will be in our range
return !this.ObservingBuckets.Contains(locateableBucketInformations.NewBucket);
// We have to assume that this observable will be
// out of scope since it can't tell us where it goes.
// That's okay, because otherwise, we wouldn't get here.
return true;
}
}
4 changes: 2 additions & 2 deletions src/SharedAssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,5 @@
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
[assembly: AssemblyVersion("0.8.4.0")]
[assembly: AssemblyFileVersion("0.8.4.0")]
[assembly: AssemblyVersion("0.8.5.0")]
[assembly: AssemblyFileVersion("0.8.5.0")]

0 comments on commit 7f9001d

Please sign in to comment.