Skip to content
This repository was archived by the owner on Jan 18, 2022. It is now read-only.

Commit cef6e14

Browse files
author
Jamie Brynes
authored
Fix PlayerLifecycleHelper.IsOwningWorker (#1027)
1 parent 25fc8cc commit cef6e14

File tree

7 files changed

+98
-7
lines changed

7 files changed

+98
-7
lines changed

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,15 @@
1515
- Use the `buildTargetFilter` command line argument to pass in a comma delimited list of build targets to filter for. For example, `+buildTargetFilter win,macos`.
1616
- Added two new GDK packages: `io.improbable.worker.sdk` and `io.improbable.worker.sdk.mobile` which contain the underlying Worker SDK packages for Windows/MacOS/Linux and Android/iOS respectively.
1717

18+
19+
### Changed
20+
21+
- `PlayerLifecycleHelper.IsOwningWorker` will now return false instead of throwing an exception if the entity is not in your worker's view.
22+
23+
### Fixed
24+
25+
- Fixed a bug where `PlayerLifecycleHelper.IsOwningWorker` would throw an exception if the entity was in your worker's view.
26+
1827
### Internal
1928

2029
- Stopped throwing a `Test Exception` in playground.

test-project/Assets/EditmodeTests/Improbable.Gdk.EditModeTests.asmdef

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@
55
"Improbable.Gdk.Generated",
66
"Improbable.Gdk.Core",
77
"Unity.Entities",
8-
"Unity.Entities.Hybrid"
8+
"Unity.Entities.Hybrid",
9+
"Improbable.Gdk.PlayerLifecycle"
910
],
1011
"optionalUnityReferences": [
1112
"TestAssemblies"
@@ -18,5 +19,6 @@
1819
"overrideReferences": false,
1920
"precompiledReferences": [],
2021
"autoReferenced": true,
21-
"defineConstraints": []
22-
}
22+
"defineConstraints": [],
23+
"versionDefines": []
24+
}

test-project/Assets/EditmodeTests/PlayerLifecycle.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
using System;
2+
using Improbable.Gdk.Core;
3+
using Improbable.Gdk.PlayerLifecycle;
4+
using NUnit.Framework;
5+
using UnityEngine;
6+
7+
namespace Improbable.Gdk.EditmodeTests.PlayerLifecycle
8+
{
9+
[TestFixture]
10+
public class PlayerLifecycleHelperTests
11+
{
12+
private SpatialOSReceiveSystem receiveSystem;
13+
private MockConnectionHandler connectionHandler;
14+
private WorkerInWorld worker;
15+
16+
[SetUp]
17+
public void Setup()
18+
{
19+
var builder = new MockConnectionHandlerBuilder();
20+
connectionHandler = builder.ConnectionHandler;
21+
22+
worker = WorkerInWorld.CreateWorkerInWorldAsync(builder, "TestWorkerType", new LoggingDispatcher(),
23+
Vector3.zero).Result;
24+
25+
receiveSystem = worker.World.GetExistingSystem<SpatialOSReceiveSystem>();
26+
}
27+
28+
[Test]
29+
public void IsOwningWorker_should_return_false_if_entity_does_not_exist()
30+
{
31+
Assert.IsFalse(PlayerLifecycleHelper.IsOwningWorker(new EntityId(10), worker.World));
32+
}
33+
34+
[Test]
35+
public void IsOwningWorker_should_return_false_if_entity_doesnt_have_OwningWorker_component()
36+
{
37+
connectionHandler.CreateEntity(1, new EntityTemplate());
38+
receiveSystem.Update();
39+
40+
Assert.IsFalse(PlayerLifecycleHelper.IsOwningWorker(new EntityId(1), worker.World));
41+
}
42+
43+
[Test]
44+
public void IsOwningWorker_should_return_false_if_entity_isnt_owned_by_this_worker()
45+
{
46+
connectionHandler.CreateEntity(1, GetOwnedEntity("other-worker"));
47+
receiveSystem.Update();
48+
49+
Assert.IsFalse(PlayerLifecycleHelper.IsOwningWorker(new EntityId(1), worker.World));
50+
}
51+
52+
[Test]
53+
public void IsOwningWorker_should_return_true_if_OwningWorker_component_has_my_worker_id()
54+
{
55+
connectionHandler.CreateEntity(1, GetOwnedEntity(worker.WorkerId));
56+
receiveSystem.Update();
57+
58+
Assert.IsTrue(PlayerLifecycleHelper.IsOwningWorker(new EntityId(1), worker.World));
59+
}
60+
61+
[TearDown]
62+
public void TearDown()
63+
{
64+
worker.Dispose();
65+
}
66+
67+
private EntityTemplate GetOwnedEntity(string workerId)
68+
{
69+
var template = new EntityTemplate();
70+
template.AddComponent(new Position.Snapshot(), "worker");
71+
template.AddComponent(new OwningWorker.Snapshot(workerId), "worker");
72+
return template;
73+
}
74+
}
75+
}

test-project/Assets/EditmodeTests/PlayerLifecycle/PlayerLifecycleHelperTests.cs.meta

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test-project/Packages/manifest.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"io.improbable.gdk.testutils": "file:../../workers/unity/Packages/io.improbable.gdk.testutils",
88
"io.improbable.gdk.tools": "file:../../workers/unity/Packages/io.improbable.gdk.tools",
99
"io.improbable.worker.sdk": "file:../../workers/unity/Packages/io.improbable.worker.sdk",
10+
"io.improbable.gdk.playerlifecycle": "file:../../workers/unity/Packages/io.improbable.gdk.playerlifecycle",
1011
"com.unity.modules.physics": "1.0.0"
1112
},
1213
"registry": "https://staging-packages.unity.com"

workers/unity/Packages/io.improbable.gdk.playerlifecycle/PlayerLifecycleHelper.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,16 @@ public static bool IsOwningWorker(EntityId entityId, World workerWorld)
3333
throw new InvalidOperationException("Provided World does not have an associated worker");
3434
}
3535

36-
if (entitySystem.GetEntitiesInView().Contains(entityId))
36+
if (!entitySystem.GetEntitiesInView().Contains(entityId))
3737
{
38-
throw new InvalidOperationException(
39-
$"Entity with SpatialOS Entity ID {entityId.Id} is not in this worker's view");
38+
return false;
4039
}
4140

4241
if (!updateSystem.HasComponent(OwningWorker.ComponentId, entityId))
4342
{
4443
return false;
4544
}
4645

47-
4846
var ownerId = updateSystem.GetComponent<OwningWorker.Snapshot>(entityId).WorkerId;
4947
return worker.WorkerId == ownerId;
5048
}

0 commit comments

Comments
 (0)