Skip to content

Commit

Permalink
Merge pull request #90 from frondeus/85-module-prevents-saving-change…
Browse files Browse the repository at this point in the history
…s-in-the-grid-configuration-tool

Fixes #85
  • Loading branch information
frondeus authored Apr 7, 2023
2 parents 7899e4c + 2e8fcbd commit 21c713e
Show file tree
Hide file tree
Showing 12 changed files with 222 additions and 67 deletions.
2 changes: 1 addition & 1 deletion jest.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { Config } from 'jest';
import { pathsToModuleNameMapper } from 'ts-jest';

const config: Config = {
collectCoverageFrom: ['src/**/*'],
collectCoverageFrom: ['src/**/*.{svelte,ts,js}'],
coveragePathIgnorePatterns: [
'<rootDir>/src/main.ts', // Because execution part is hard to test
'<rootDir>/src/services/game.ts', // Because its an implementation detail of foundry, non-testable
Expand Down
33 changes: 33 additions & 0 deletions jest.setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,39 @@ Object.defineProperty(global, 'Hooks', {
}
});

Object.defineProperty(global, 'foundry', {
value: {
utils: {
flattenObject: (obj: any, _d: number = 0) => {
return flattenObject(obj, _d);
}
}
}
});

function flattenObject(ob: any, _d: number = 0) {
var toReturn: any = {};
if (_d > 100) {
throw new Error('Maximum depth exceeded');
}

for (var i in ob) {
if (!ob.hasOwnProperty(i)) continue;

if (typeof ob[i] == 'object' && ob[i] !== null) {
var flatObject = flattenObject(ob[i], _d + 1);
for (var x in flatObject) {
if (!flatObject.hasOwnProperty(x)) continue;

toReturn[i + '.' + x] = flatObject[x];
}
} else {
toReturn[i] = ob[i];
}
}
return toReturn;
}

configure({
testIdAttribute: 'data-test'
});
26 changes: 15 additions & 11 deletions src/proxies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@ function populateContext(ctx: any): Context {
ctx = {};
}
if (ctx.ctx === undefined || ctx.ctx === null) {
ctx.ctx = container.resolve(Context);
ctx.ctx.utils.warn('Context was undefined. Fixing it!');
let context = container.resolve(Context);
ctx.syrinCtx = () => context;
context.utils.warn('Context was undefined. Fixing it!');
}
return ctx.ctx;
return ctx.syrinCtx();
}

class SyrinAmbientSound extends AmbientSound {
Expand All @@ -26,10 +27,11 @@ class SyrinAmbientSound extends AmbientSound {

const provider = {
id: () => this.id,
radius: () => this.radius
radius: () => this.radius,
ctx: () => ctx
};

this.syrin = new AmbientSoundController(data, ctx, provider);
this.syrin = new AmbientSoundController(data, provider);
}

override _createSound(): null {
Expand Down Expand Up @@ -59,10 +61,11 @@ class SyrinPlaylistSound extends PlaylistSound {
const provider = {
playing: () => this.playing,
id: () => this.id,
update: (playing: boolean) => this.update({ playing })
update: (playing: boolean) => this.update({ playing }),
ctx: () => ctx
};

this.syrin = new PlaylistSoundController(data, ctx, provider);
this.syrin = new PlaylistSoundController(data, provider);
}

override async sync(): Promise<void> {
Expand Down Expand Up @@ -92,9 +95,10 @@ class SyrinPlaylist extends Playlist {

const provider = {
id: () => this.id,
update: (playing: boolean) => this.update({ playing })
update: (playing: boolean) => this.update({ playing }),
ctx: () => ctx
};
this.syrin = new PlaylistController(data, ctx, provider);
this.syrin = new PlaylistController(data, provider);
}

override async playAll(): Promise<undefined> {
Expand Down Expand Up @@ -125,10 +129,10 @@ function handler<
return new t(...args);
}

if (args[1] === undefined) {
if (args[1] === undefined || args[1] === null) {
args[1] = {};
}
(args[1] as any).ctx = ctx;
(args[1] as any).syrinCtx = () => ctx;
return new s(...args);
}
};
Expand Down
11 changes: 11 additions & 0 deletions src/sounds/__snapshots__/ambient-sound.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Ambient sound controller bugs can be flattened 1`] = `
{
"flags.mood": 4321,
"flags.type": "mood",
"provider.ctx": [Function],
"provider.id": [Function],
"provider.radius": [Function],
}
`;
26 changes: 26 additions & 0 deletions src/sounds/__snapshots__/playlist-sound.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Ambient sound controller bugs can be flattened 1`] = `
{
"flags.mood": 4321,
"flags.type": "mood",
"provider.ctx": [Function],
"provider.id": [Function],
"provider.playing": [MockFunction],
"provider.update": [MockFunction] {
"calls": [
[
false,
],
],
"results": [
{
"type": "return",
"value": undefined,
},
],
},
"unsubscriber": [Function],
"wasPlaying": false,
}
`;
23 changes: 23 additions & 0 deletions src/sounds/__snapshots__/playlist.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Ambient sound controller bugs can be flattened 1`] = `
{
"flags.soundset": "1234",
"provider.ctx": [Function],
"provider.id": [Function],
"provider.update": [MockFunction] {
"calls": [
[
false,
],
],
"results": [
{
"type": "return",
"value": undefined,
},
],
},
"unsubscriber": [Function],
}
`;
45 changes: 33 additions & 12 deletions src/sounds/ambient-sound.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@ describe('Ambient sound controller', () => {
it('does nothing when player is not active', async () => {
const provider = {
id: () => '1',
radius: () => 10
radius: () => 10,
ctx: () => mock.ctx
};
const sound = new AmbientSound({ flags: { syrinscape: flags } }, mock.ctx, provider);
const sound = new AmbientSound({ flags: { syrinscape: flags } }, provider);

mock.raw.getState = jest.fn(() => 'inactive');

Expand All @@ -34,9 +35,10 @@ describe('Ambient sound controller', () => {
it('also does nothing when id is unknown', async () => {
const provider = {
id: () => null,
radius: () => 10
radius: () => 10,
ctx: () => mock.ctx
};
const sound = new AmbientSound({ flags: { syrinscape: flags } }, mock.ctx, provider);
const sound = new AmbientSound({ flags: { syrinscape: flags } }, provider);

await sound.sync(true, 1);

Expand All @@ -47,9 +49,10 @@ describe('Ambient sound controller', () => {
const spy = jest.spyOn(mock.ctx.syrin, 'stopAmbientSound');
const provider = {
id: () => '1',
radius: () => 10
radius: () => 10,
ctx: () => mock.ctx
};
const sound = new AmbientSound({ flags: { syrinscape: flags } }, mock.ctx, provider);
const sound = new AmbientSound({ flags: { syrinscape: flags } }, provider);

await sound.sync(false, 1);

Expand All @@ -60,9 +63,10 @@ describe('Ambient sound controller', () => {
const spy = jest.spyOn(mock.ctx.syrin, 'playAmbientSound');
const provider = {
id: () => '1',
radius: () => 10
radius: () => 10,
ctx: () => mock.ctx
};
const sound = new AmbientSound({ flags: { syrinscape: flags } }, mock.ctx, provider);
const sound = new AmbientSound({ flags: { syrinscape: flags } }, provider);

await sound.sync(true, 1);

Expand All @@ -78,13 +82,14 @@ describe('Ambient sound controller', () => {
const spy = jest.spyOn(mock.ctx.syrin, 'playAmbientSound');
const provider = {
id: () => '1',
radius: () => 10
radius: () => 10,
ctx: () => mock.ctx
};
flags = {
type: 'element',
element: 4321
};
const sound = new AmbientSound({ flags: { syrinscape: flags } }, mock.ctx, provider);
const sound = new AmbientSound({ flags: { syrinscape: flags } }, provider);

await sound.sync(true, 1);

Expand All @@ -101,9 +106,10 @@ describe('Ambient sound controller', () => {
const spy = jest.spyOn(mock.ctx.syrin, 'playAmbientSound');
const provider = {
id: () => '1',
radius: () => 10
radius: () => 10,
ctx: () => mock.ctx
};
const sound = new AmbientSound({ flags: { syrinscape: flags } }, mock.ctx, provider);
const sound = new AmbientSound({ flags: { syrinscape: flags } }, provider);

await sound.sync(true, 1);

Expand All @@ -114,4 +120,19 @@ describe('Ambient sound controller', () => {
moodId: 4321
});
});

describe('bugs', () => {
it('can be flattened', () => {
const provider = {
id: () => '1',
radius: () => 10,
ctx: () => mock.ctx
};
const sound = new AmbientSound({ flags: { syrinscape: flags } }, provider);

const flattened = foundry.utils.flattenObject(sound);

expect(flattened).toMatchSnapshot();
});
});
});
17 changes: 8 additions & 9 deletions src/sounds/ambient-sound.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,20 @@ export type SyrinAmbientSoundFlags = ElementSoundFlags | MoodSoundFlags;
export interface AmbientSoundProvider {
id(): string | null;
radius(): number;
ctx(): Context;
}
export class AmbientSound {
ctx: Context;
flags: SyrinAmbientSoundFlags;
provider: AmbientSoundProvider;

constructor(data: any, ctx: Context, provider: AmbientSoundProvider) {
this.ctx = ctx;
constructor(data: any, provider: AmbientSoundProvider) {
this.flags = data.flags.syrinscape;
this.provider = provider;
this.ctx.utils.trace('Creating an ambient sound', { data, flags: this.flags });
this.provider.ctx().utils.trace('Creating an ambient sound', { data, flags: this.flags });
}

async sync(isAudible: boolean, volume: number): Promise<void> {
if (!this.ctx.api.isPlayerActive()) {
if (!this.provider.ctx().api.isPlayerActive()) {
return;
}

Expand All @@ -30,13 +29,13 @@ export class AmbientSound {
}

const power = (1.0 - volume) * this.provider.radius();
const userId = this.ctx.game.userId() ?? '';
const userId = this.provider.ctx().game.userId() ?? '';

if (isAudible) {
switch (this.flags.type) {
case 'mood': {
const moodId = this.flags.mood;
this.ctx.syrin.playAmbientSound(id, {
this.provider.ctx().syrin.playAmbientSound(id, {
kind: 'mood',
volume: power,
moodId,
Expand All @@ -46,7 +45,7 @@ export class AmbientSound {
}
case 'element': {
const elementId = this.flags.element;
this.ctx.syrin.playAmbientSound(id, {
this.provider.ctx().syrin.playAmbientSound(id, {
kind: 'element',
volume: power,
elementId,
Expand All @@ -56,7 +55,7 @@ export class AmbientSound {
}
}
} else {
this.ctx.syrin.stopAmbientSound(id, userId);
this.provider.ctx().syrin.stopAmbientSound(id, userId);
}
}
}
Loading

0 comments on commit 21c713e

Please sign in to comment.