Skip to content

Commit

Permalink
Migrated to inversify
Browse files Browse the repository at this point in the history
  • Loading branch information
tlaanemaa committed Oct 5, 2022
1 parent c82b5fb commit be19de9
Show file tree
Hide file tree
Showing 64 changed files with 360 additions and 279 deletions.
5 changes: 5 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"cSpell.words": [
"inversify"
]
}
63 changes: 22 additions & 41 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,8 @@
"packages/*"
],
"dependencies": {
"inversify": "^6.0.1",
"reflect-metadata": "^0.1.13",
"runtypes": "^6.6.0",
"tsyringe": "^4.7.0",
"uuid": "^9.0.0"
},
"devDependencies": {
Expand Down
4 changes: 2 additions & 2 deletions packages/api/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import "reflect-metadata";
import { container } from "tsyringe";
import { container } from "@moose-rocket/container";
import { SocketServer } from "./server/SocketServer";

container.resolve(SocketServer);
container.get(SocketServer);
7 changes: 4 additions & 3 deletions packages/api/src/room/Room.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,22 @@
import "reflect-metadata";
import { MessagePort } from "node:worker_threads";
import { container } from "tsyringe";
import "@moose-rocket/game";
import { FastMap, Game } from "@moose-rocket/core";
import { DEPENDENCIES, FastMap, Game } from "@moose-rocket/core";
import {
AnyMessage,
ConnectionApprovedMessage,
Parser,
} from "@moose-rocket/messaging";
import "./components";
import { container } from "@moose-rocket/container";

/**
* Each Room will be executed on a new worker thread
*/
export class Room {
private readonly players = new FastMap<MessagePort>();
private readonly game = container.resolve<Game>("Game");

private readonly game = container.get<Game>(DEPENDENCIES.Game);

constructor(private readonly id: string) {
this.game.init();
Expand Down
7 changes: 5 additions & 2 deletions packages/api/src/room/components/ServerAudioLoader.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { singleton } from "tsyringe";
import { injectable } from "inversify";
import { container } from "@moose-rocket/container";
import { Audio, AudioLoader } from "@moose-rocket/core";

@singleton()
@injectable()
export class ServerAudioLoader implements AudioLoader {
load(src: string): Audio {
return {
play() {},
};
}
}

container.bind(ServerAudioLoader).toSelf().inSingletonScope();
7 changes: 5 additions & 2 deletions packages/api/src/room/components/ServerBackgroundRenderer.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { singleton } from "tsyringe";
import { injectable } from "inversify";
import { container } from "@moose-rocket/container";
import { BackgroundRenderer } from "@moose-rocket/core";

@singleton()
@injectable()
export class ServerBackgroundRenderer implements BackgroundRenderer {
draw(): void {}
}

container.bind(ServerBackgroundRenderer).toSelf().inSingletonScope();
7 changes: 5 additions & 2 deletions packages/api/src/room/components/ServerEntityRenderer.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { singleton } from "tsyringe";
import { injectable } from "inversify";
import { container } from "@moose-rocket/container";
import { Entity, EntityRenderer } from "@moose-rocket/core";

@singleton()
@injectable()
export class ServerEntityRenderer implements EntityRenderer {
windowWidth: number = 1000;
windowHeight: number = 1000;

draw(entities: Entity[]): void {}
}

container.bind(ServerEntityRenderer).toSelf().inSingletonScope();
7 changes: 5 additions & 2 deletions packages/api/src/room/components/ServerTextureLoader.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { singleton } from "tsyringe";
import { injectable } from "inversify";
import { container } from "@moose-rocket/container";
import { Texture, TextureLoader } from "@moose-rocket/core";

@singleton()
@injectable()
export class ServerTextureLoader implements TextureLoader {
load(
src: string,
Expand All @@ -11,3 +12,5 @@ export class ServerTextureLoader implements TextureLoader {
return {} as Texture;
}
}

container.bind(ServerTextureLoader).toSelf().inSingletonScope();
9 changes: 6 additions & 3 deletions packages/api/src/room/components/ServerTimer.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { singleton } from "tsyringe";
import { injectable } from "inversify";
import { container } from "@moose-rocket/container";
import { Timer } from "@moose-rocket/core";
import { Logger } from "@moose-rocket/logger";

type Task = () => void;

@singleton()
@injectable()
export class ServerTimer implements Timer {
private interval: NodeJS.Timer | null = null;
private tasks: Task[] = [];
Expand All @@ -28,10 +29,12 @@ export class ServerTimer implements Timer {

private handleFrame() {
try {
console.log("SERVER TICK")
console.log("SERVER TICK");
this.tasks.forEach((task) => task());
} catch (e) {
this.logger.error(e);
}
}
}

container.bind(ServerTimer).toSelf().inSingletonScope();
7 changes: 5 additions & 2 deletions packages/api/src/room/components/UserInput.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { singleton } from "tsyringe";
import { injectable } from "inversify";
import { container } from "@moose-rocket/container";
import { INTENT, UserInput, EventSource } from "@moose-rocket/core";

@singleton()
@injectable()
export class ServerUserInput
extends EventSource<INTENT, number>
implements UserInput {}

container.bind(ServerUserInput).toSelf().inSingletonScope();
52 changes: 6 additions & 46 deletions packages/api/src/room/components/index.ts
Original file line number Diff line number Diff line change
@@ -1,52 +1,12 @@
import { container, Lifecycle } from "tsyringe";
import {
AudioLoader,
BackgroundRenderer,
EntityRenderer,
TextureLoader,
Timer,
UserInput,
} from "@moose-rocket/core";
import { ServerAudioLoader } from "./ServerAudioLoader";
import { ServerBackgroundRenderer } from "./ServerBackgroundRenderer";
import { ServerEntityRenderer } from "./ServerEntityRenderer";
import { ServerTextureLoader } from "./ServerTextureLoader";
import { ServerTimer } from "./ServerTimer";
import { ServerUserInput } from "./UserInput";
import "./ServerAudioLoader";
import "./ServerBackgroundRenderer";
import "./ServerEntityRenderer";
import "./ServerTextureLoader";
import "./ServerTimer";
import "./UserInput";

/**
* FIXME: Dirty hack to temporarily allow running stuff that
* relies on window in the server
*/
(global as any).window = { innerWidth: 1000, innerHeight: 1000 };

container.register<Timer>(
"Timer",
{ useClass: ServerTimer },
{ lifecycle: Lifecycle.Singleton }
);
container.register<UserInput>(
"UserInput",
{ useClass: ServerUserInput },
{ lifecycle: Lifecycle.Singleton }
);
container.register<BackgroundRenderer>(
"BackgroundRenderer",
{ useClass: ServerBackgroundRenderer },
{ lifecycle: Lifecycle.Singleton }
);
container.register<EntityRenderer>(
"EntityRenderer",
{ useClass: ServerEntityRenderer },
{ lifecycle: Lifecycle.Singleton }
);
container.register<TextureLoader>(
"TextureLoader",
{ useClass: ServerTextureLoader },
{ lifecycle: Lifecycle.Singleton }
);
container.register<AudioLoader>(
"AudioLoader",
{ useClass: ServerAudioLoader },
{ lifecycle: Lifecycle.Singleton }
);
7 changes: 5 additions & 2 deletions packages/api/src/server/Authentication.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { singleton } from "tsyringe";
import { container } from "@moose-rocket/container";
import { injectable } from "inversify";

// FIXME: Move this somewhere else
const knownUsers: { [key: string]: string } = {
banana: "45c993a5-f1bd-4bb9-b6a7-e543b3f80560",
};

@singleton()
@injectable()
export class Authentication {
/**
* Returns the user ID if the user is recognized, or null if they're not
Expand All @@ -28,3 +29,5 @@ export class Authentication {
return null;
}
}

container.bind(Authentication).toSelf().inSingletonScope();
7 changes: 5 additions & 2 deletions packages/api/src/server/RoomManager.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { WebSocket } from "ws";
import { singleton } from "tsyringe";
import { injectable } from "inversify";
import { container } from "@moose-rocket/container";
import { FastMap } from "@moose-rocket/core";
import { Logger } from "@moose-rocket/logger";
import { RoomThread } from "../room";

@singleton()
@injectable()
export class RoomManager {
private readonly rooms = new FastMap<RoomThread>();

Expand Down Expand Up @@ -33,3 +34,5 @@ export class RoomManager {
return room;
}
}

container.bind(RoomManager).toSelf().inSingletonScope();
Loading

0 comments on commit be19de9

Please sign in to comment.