summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorsanine <sanine.not@pm.me>2023-11-06 00:57:14 -0600
committersanine <sanine.not@pm.me>2023-11-06 00:57:14 -0600
commit541b103179cac2dc58962c52b5144b41f75316b0 (patch)
tree7a9dc091d84dfac183be905bdadcfa3798e7b9a2 /src
parent078a01a078dae15b5e8e3482b57f156ecd0c7fae (diff)
refactor: agent*.js => proposal*.js
Diffstat (limited to 'src')
-rw-r--r--src/world/proposal.js (renamed from src/world/agent.js)4
-rw-r--r--src/world/proposal.test.js (renamed from src/world/agent.test.js)46
2 files changed, 47 insertions, 3 deletions
diff --git a/src/world/agent.js b/src/world/proposal.js
index 95d869c..d1f2eb0 100644
--- a/src/world/agent.js
+++ b/src/world/proposal.js
@@ -46,6 +46,8 @@ import { pairs, deepEqual } from '../util.js';
*/
+// check that two flags objects are compatible
+// flags are considered compatible if they do not have any common keys with different values
function flags_compatible(a, b) {
const keys = [...new Set(Object.keys(a).concat(Object.keys(b)))];
return keys.reduce(
@@ -91,6 +93,7 @@ function pos_exists(a) {
}
+// check the equality of two objects with (x,y) keys
function pos_equal(a, b) {
if (a.x !== b.x) { return false; }
if (a.y !== b.y) { return false; }
@@ -134,6 +137,7 @@ function agent_change_conflict(a, b) {
}
+// combine world_change and agent_change conflict/merge tuples for a pair of proposals
function proposal_conflict_merge(a, b) {
const [world_conflict, world_merge] = pairs(a.world_changes || [], b.world_changes || []).reduce(
(acc, [a, b]) => {
diff --git a/src/world/agent.test.js b/src/world/proposal.test.js
index fe73b25..a1e1203 100644
--- a/src/world/agent.test.js
+++ b/src/world/proposal.test.js
@@ -1,10 +1,8 @@
import {
proposal_merge,
-} from './agent.js';
+} from './proposal.js';
-// --===== proposal conflicts =====--
-
// tile updates
test("proposals changing different tiles don't conflict", () => {
@@ -144,6 +142,19 @@ test("setting the same agent to compatible flags does not conflict", () => {
});
+test("setting the same agent to compatible object flags does not conflict", () => {
+ const a = {
+ agent_changes: [{ agent_id: 'aaa', flags: { emit: [0, 1, 1, 0] } }],
+ };
+
+ const b = {
+ agent_changes: [{ agent_id: 'aaa', flags: { emit: [0, 1, 1, 0], hi: 4 } }],
+ };
+
+ expect(proposal_merge([a], b)).toEqual([a, b]);
+});
+
+
test("setting the same agent to incompatible flags does conflict", () => {
const a = {
agent_changes: [{ agent_id: 'aaa', flags: { frozen: false } }],
@@ -155,3 +166,32 @@ test("setting the same agent to incompatible flags does conflict", () => {
expect(proposal_merge([a], b)).toEqual([]);
});
+
+
+test("setting the same agent to incompatible object flags does conflict", () => {
+ const a = {
+ agent_changes: [{ agent_id: 'aaa', flags: { emit: [0, 1, 1, 0] } }],
+ };
+
+ const b = {
+ agent_changes: [{ agent_id: 'aaa', flags: { emit: [0, 1, 1, 1], hi: 4 } }],
+ };
+
+ expect(proposal_merge([a], b)).toEqual([]);
+});
+
+
+test("setting the same agent to identical flags merges", () => {
+ const a = {
+ agent_changes: [{ agent_id: 'aaa', flags: { emit: [0, 1, 1, 0] } }],
+ };
+
+ const b = {
+ agent_changes: [{ agent_id: 'aaa', flags: { emit: [0, 1, 1, 0] } }],
+ };
+
+ expect(proposal_merge([a], b)).toEqual([a]);
+});
+
+
+