-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
23bd323
commit 431674e
Showing
17 changed files
with
255 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import test, { Mock } from "node:test"; | ||
import { localStorage } from "./globals"; | ||
import { savePartialComponent, loadPartialComponent } from "./Component"; | ||
import assert from "node:assert"; | ||
import { executeFilterQuery, registerEntity } from "./Query"; | ||
import { peekNextEntityId } from "./Entity"; | ||
|
||
function testSave<T>( | ||
key: string, | ||
data: ReadonlyArray<T>, | ||
selectedEntities: ReadonlyArray<number>, | ||
expectedData: ReadonlyArray<T>, | ||
) { | ||
savePartialComponent(key, data, selectedEntities); | ||
const setItemMock = ( | ||
localStorage.setItem as Mock<typeof Storage.prototype.setItem> | ||
).mock; | ||
|
||
const lastCall = setItemMock.calls[setItemMock.calls.length - 1]; | ||
assert(lastCall.arguments[0] === key); | ||
assert(lastCall.arguments[1] === JSON.stringify(expectedData)); | ||
} | ||
|
||
test("savePartialComponent", () => { | ||
testSave("Color", [8, 6, 7, 5, 3], [2, 3, 4], [7, 5, 3]); | ||
testSave("Mass", [8, 6, 7, 5, 3], [2, 3, 4], [7, 5, 3]); | ||
}); | ||
|
||
function testLoad<T>( | ||
key: string, | ||
data: T[], | ||
loadedData: ReadonlyArray<T>, | ||
nextEntityId: number, | ||
expectedData: ReadonlyArray<T>, | ||
expectedQueryResult: ReadonlyArray<number>, | ||
) { | ||
const getItemMock = ( | ||
localStorage.getItem as Mock<typeof Storage.prototype.getItem> | ||
).mock; | ||
getItemMock.mockImplementation(() => JSON.stringify(loadedData)); | ||
loadPartialComponent(key, data, nextEntityId); | ||
|
||
assert.deepEqual(data, expectedData); | ||
|
||
for (let i = 0; i < data.length; i++) { | ||
registerEntity(i); | ||
} | ||
const queryResult = executeFilterQuery((_id) => true, []); | ||
assert.deepEqual(queryResult, expectedQueryResult); | ||
assert.equal(peekNextEntityId(), expectedQueryResult.length); | ||
} | ||
|
||
test("loadPartialComponent", () => { | ||
testLoad("Color", [], [7, 5, 3], 3, [, , , 7, 5, 3], [0, 1, 2, 3, 4, 5]); | ||
testLoad("Mass", [], [7, 5, 3], 3, [, , , 7, 5, 3], [0, 1, 2, 3, 4, 5]); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { peekNextEntityId, setNextEntityId } from "./Entity"; | ||
import { registerEntity } from "./Query"; | ||
import { localStorage } from "./globals"; | ||
|
||
export function savePartialComponent<T>( | ||
key: string, | ||
data: ReadonlyArray<T>, | ||
selectedEntities: ReadonlyArray<number>, | ||
) { | ||
const selectedData = selectedEntities.map((entity) => data[entity]); | ||
const item = JSON.stringify(selectedData); | ||
localStorage.setItem(key, item); | ||
} | ||
|
||
export function loadPartialComponent<T>( | ||
key: string, | ||
data: T[], | ||
nextEntityId: number, | ||
) { | ||
const item = localStorage.getItem(key); | ||
if (item) { | ||
const selectedData = JSON.parse(item) as T[]; | ||
for ( | ||
let i = 0, entityId = nextEntityId; | ||
i < selectedData.length; | ||
i++, entityId++ | ||
) { | ||
data[entityId] = selectedData[i]; | ||
registerEntity(entityId); | ||
} | ||
if (peekNextEntityId() < nextEntityId + selectedData.length) { | ||
setNextEntityId(nextEntityId + selectedData.length); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ export const enum Key { | |
l = "l", | ||
r = "r", | ||
w = "w", | ||
W = "W", | ||
p = "p", | ||
c = "c", | ||
Space = " ", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { loadPartialComponent, savePartialComponent } from "../Component"; | ||
import { invariant } from "../Error"; | ||
const STORAGE_KEY = "Component:ShouldSave"; | ||
const DATA: Array<boolean> = []; | ||
|
||
export function setShouldSave(entityId: number, value: boolean) { | ||
DATA[entityId] = value; | ||
} | ||
|
||
export function hasShouldSave(entityId: number): boolean { | ||
return DATA[entityId] !== undefined; | ||
} | ||
|
||
export function getShouldSave(entityId: number): boolean { | ||
invariant( | ||
hasShouldSave(entityId), | ||
`Entity ${entityId} does not have a ShouldSave`, | ||
); | ||
return DATA[entityId]; | ||
} | ||
|
||
export function saveShouldSave(selectedEntities: ReadonlyArray<number>) { | ||
savePartialComponent(STORAGE_KEY, DATA, selectedEntities); | ||
} | ||
|
||
export function loadShouldSave(nextEntityId: number) { | ||
loadPartialComponent(STORAGE_KEY, DATA, nextEntityId); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { peekNextEntityId } from "../Entity"; | ||
import { loadActLike } from "../components/ActLike"; | ||
import { loadLayer } from "../components/Layer"; | ||
import { loadLookLike } from "../components/LookLike"; | ||
import { loadPixiAppId } from "../components/PixiAppId"; | ||
import { loadPositionX } from "../components/PositionX"; | ||
import { loadPositionY } from "../components/PositionY"; | ||
import { loadShouldSave } from "../components/ShouldSave"; | ||
|
||
export function loadComponents() { | ||
const nextEntityId = peekNextEntityId(); | ||
loadShouldSave(nextEntityId); | ||
loadLayer(nextEntityId); | ||
loadPositionX(nextEntityId); | ||
loadPositionY(nextEntityId); | ||
loadLookLike(nextEntityId); | ||
loadActLike(nextEntityId); | ||
loadPixiAppId(nextEntityId); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { executeFilterQuery } from "../Query"; | ||
import { saveActLike } from "../components/ActLike"; | ||
import { saveLayer } from "../components/Layer"; | ||
import { saveLookLike } from "../components/LookLike"; | ||
import { savePixiAppId } from "../components/PixiAppId"; | ||
import { savePositionX } from "../components/PositionX"; | ||
import { savePositionY } from "../components/PositionY"; | ||
import { hasShouldSave, saveShouldSave } from "../components/ShouldSave"; | ||
|
||
const entityIds: number[] = []; | ||
function getSelectedEntities(): ReadonlyArray<number> { | ||
entityIds.length = 0; | ||
return executeFilterQuery(hasShouldSave, entityIds); | ||
} | ||
|
||
export function saveComponents() { | ||
const selectedEntities = getSelectedEntities(); | ||
saveShouldSave(selectedEntities); | ||
saveLayer(selectedEntities); | ||
savePositionX(selectedEntities); | ||
savePositionY(selectedEntities); | ||
saveLookLike(selectedEntities); | ||
saveActLike(selectedEntities); | ||
savePixiAppId(selectedEntities); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.