Skip to content

Commit

Permalink
types rework
Browse files Browse the repository at this point in the history
  • Loading branch information
hmbanan666 committed Apr 16, 2024
1 parent 1f72939 commit 0fac751
Show file tree
Hide file tree
Showing 43 changed files with 367 additions and 431 deletions.
8 changes: 2 additions & 6 deletions .dependency-cruiser.js → .dependency-cruiser.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -327,11 +327,7 @@ module.exports = {
*/
// extensions: [".js", ".jsx", ".ts", ".tsx", ".d.ts"],
/* What to consider a 'main' field in package.json */

// if you migrate to ESM (or are in an ESM environment already) you will want to
// have "module" in the list of mainFields, like so:
// mainFields: ["module", "main", "types", "typings"],
mainFields: ["main", "types", "typings"],
mainFields: ["module", "main", "types", "typings"],
/*
A list of alias fields in package.jsons
See [this specification](https://github.com/defunctzombie/package-browser-field-spec) and
Expand Down Expand Up @@ -390,4 +386,4 @@ module.exports = {
}
}
};
// generated: [email protected].0 on 2024-04-08T09:50:19.700Z
// generated: [email protected].1 on 2024-04-16T10:36:05.632Z
7 changes: 4 additions & 3 deletions apps/api/src/game/common/event.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,23 @@ import { createId } from "@paralleldrive/cuid2";
import {
type EventStatus,
type EventType,
type IGameEvent,
getDatePlusSeconds,
} from "../../../../../packages/api-sdk/src";

interface EventOptions {
interface IEventOptions {
type: EventType;
secondsToEnd: number;
}

export class Event {
export class Event implements IGameEvent {
public id: string;
public type: EventType;
public status: EventStatus;
public endsAt: Date;
public deletesAt: Date;

constructor({ type, secondsToEnd }: EventOptions) {
constructor({ type, secondsToEnd }: IEventOptions) {
this.id = createId();
this.type = type;
this.endsAt = getDatePlusSeconds(secondsToEnd);
Expand Down
17 changes: 11 additions & 6 deletions apps/api/src/game/common/group.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,28 @@
import { createId } from "@paralleldrive/cuid2";
import type {
GameGroup,
GameSceneType,
Player,
IGameGroup,
IGameObjectPlayer,
} from "../../../../../packages/api-sdk/src";

export class Group implements GameGroup {
interface IGroupOptions {
creator: IGameObjectPlayer;
target: GameSceneType;
}

export class Group implements IGameGroup {
id: string;
players: Player[] = [];
players: IGameObjectPlayer[] = [];
target: GameSceneType;

constructor(creator: Player, target: GameSceneType) {
constructor({ creator, target }: IGroupOptions) {
this.id = createId();

this.join(creator);
this.target = target;
}

join(player: Player): boolean {
join(player: IGameObjectPlayer): boolean {
const check = this.findPlayer(player.id);
if (check) {
return false;
Expand Down
11 changes: 8 additions & 3 deletions apps/api/src/game/common/inventory.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,22 @@
import { createId } from "@paralleldrive/cuid2";
import type {
Inventory as IInventory,
IGameInventory,
InventoryItem,
ItemType,
} from "../../../../../packages/api-sdk/src";
import { db } from "../../db/db.client";

export class Inventory implements IInventory {
interface IInventoryOptions {
objectId: string;
id: string;
}

export class Inventory implements IGameInventory {
public id: string;
public objectId: string;
public items: InventoryItem[] = [];

constructor(objectId: string, id: string) {
constructor({ id, objectId }: IInventoryOptions) {
this.id = id;
this.objectId = objectId;
}
Expand Down
19 changes: 13 additions & 6 deletions apps/api/src/game/common/raid.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
import { getDatePlusMinutes } from "../../../../../packages/api-sdk/src";
import type { Flag } from "../objects";
import { Raider } from "../objects/raider";
import {
type IGameObjectFlag,
type IGameRaid,
getDatePlusMinutes,
} from "../../../../../packages/api-sdk/src";
import { Raider } from "../objects";

export class Raid {
interface IRaidOptions {
raidersCount: number;
}

export class Raid implements IGameRaid {
public raiders: Raider[] = [];
public raidEndsAt!: Date;
public raidDeletesAt!: Date;

constructor(raidersCount: number) {
constructor({ raidersCount }: IRaidOptions) {
this.init(raidersCount);
}

Expand All @@ -25,7 +32,7 @@ export class Raid {
return raiders;
}

moveAllRaidersBackToCamp(flag: Flag) {
moveAllRaidersBackToCamp(flag: IGameObjectFlag) {
for (const raider of this.raiders) {
raider.target = flag;
raider.state = "MOVING";
Expand Down
19 changes: 10 additions & 9 deletions apps/api/src/game/common/skill.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
import { createId } from "@paralleldrive/cuid2";
import type {
Skill as ISkill,
SkillType,
} from "../../../../../packages/api-sdk/src";
import type { IGameSkill } from "../../../../../packages/api-sdk/src";
import { db } from "../../db/db.client";

export class Skill implements ISkill {
interface ISkillOptions {
id: string;
}

export class Skill implements IGameSkill {
public id: string;
public objectId: string | null = null;
public type: SkillType | null = null;
public type!: IGameSkill["type"];

public lvl = 0;
public xp = 0;
public xpNextLvl = 0;

constructor(id: string) {
constructor({ id }: ISkillOptions) {
this.id = id;
}

Expand Down Expand Up @@ -58,7 +59,7 @@ export class Skill implements ISkill {
}

this.objectId = skill.objectId;
this.type = skill.type as SkillType;
this.type = skill.type as IGameSkill["type"];
this.lvl = skill.lvl;
this.xp = skill.xp;
this.xpNextLvl = skill.xpNextLvl;
Expand All @@ -70,7 +71,7 @@ export class Skill implements ISkill {
});
}

public static createInDB(objectId: string, type: SkillType) {
public static createInDB(objectId: string, type: IGameSkill["type"]) {
return db.skill.create({
data: {
id: createId(),
Expand Down
18 changes: 14 additions & 4 deletions apps/api/src/game/objects/flag.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,28 @@
import { createId } from "@paralleldrive/cuid2";
import { getRandomInRange } from "../../../../../packages/api-sdk/src";
import {
type IGameObjectFlag,
getRandomInRange,
} from "../../../../../packages/api-sdk/src";
import { MAX_X, MAX_Y, MIN_X, MIN_Y } from "../../config";
import { GameObject } from "./gameObject";

export class Flag extends GameObject {
interface IFlagOptions {
x?: number;
y?: number;
id?: string;
isOnScreen?: boolean;
}

export class Flag extends GameObject implements IGameObjectFlag {
public readonly entity = "FLAG";
public isOnScreen = true;

constructor(x?: number, y?: number, id?: string, isOnScreen?: boolean) {
constructor({ x, y, id, isOnScreen }: IFlagOptions) {
const finalId = id ?? createId();
const finalX = x ?? getRandomInRange(MIN_X, MAX_X);
const finalY = y ?? getRandomInRange(MIN_Y, MAX_Y);

super(finalId, finalX, finalY);
super({ id: finalId, x: finalX, y: finalY });

this.isOnScreen = isOnScreen ?? true;
}
Expand Down
29 changes: 18 additions & 11 deletions apps/api/src/game/objects/gameObject.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,32 @@
import type {
GameObjectEntity,
GameObjectTree,
GameObject as IGameObject,
import {
type IGameObject,
type IGameObjectDirection,
type IGameObjectEntity,
type IGameObjectState,
getRandomInRange,
} from "../../../../../packages/api-sdk/src";
import { getRandomInRange } from "../../../../../packages/api-sdk/src";
import { MAX_X, MAX_Y, MIN_X, MIN_Y } from "../../config";
import { sendMessage } from "../../websocket/websocket.server";

interface IGameObjectOptions {
id: string;
x: number;
y: number;
}

export class GameObject implements IGameObject {
public id: string;
public x: number;
public y: number;
public entity: GameObjectEntity;
public health = 100;

public direction: IGameObject["direction"] = "RIGHT";
public state: IGameObject["state"] = "IDLE";
public entity: IGameObjectEntity;
public direction: IGameObjectDirection = "RIGHT";
public state: IGameObjectState = "IDLE";

public target: GameObject | GameObjectTree | undefined;
public target: IGameObject | undefined;

constructor(id: string, x: number, y: number) {
constructor({ id, x, y }: IGameObjectOptions) {
this.id = id;
this.x = x;
this.y = y;
Expand Down Expand Up @@ -101,7 +108,7 @@ export class GameObject implements IGameObject {
return Math.abs(this.target.y - this.y);
}

public setTarget(target: GameObject | GameObjectTree) {
public setTarget(target: IGameObject) {
this.target = target;
this.state = "MOVING";
}
Expand Down
1 change: 1 addition & 0 deletions apps/api/src/game/objects/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export { Wolf } from "./wolf";
export { Tree } from "./tree";
export { Stone } from "./stone";
export { Flag } from "./flag";
export { Raider } from "./raider";
19 changes: 11 additions & 8 deletions apps/api/src/game/objects/player.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { createId } from "@paralleldrive/cuid2";
import type {
GameObjectPlayer,
ItemType,
import {
type IGameObjectPlayer,
type ItemType,
getRandomInRange,
} from "../../../../../packages/api-sdk/src";
import { getRandomInRange } from "../../../../../packages/api-sdk/src";
import { MAX_X, MAX_Y, MIN_X, MIN_Y } from "../../config";
import { db } from "../../db/db.client";
import { Inventory, Skill } from "../common";
import { GameObject } from "./gameObject";
import { Stone } from "./stone";
import { Tree } from "./tree";

export class Player extends GameObject implements GameObjectPlayer {
export class Player extends GameObject implements IGameObjectPlayer {
public readonly entity = "PLAYER";
public coins = 0;
public reputation = 0;
Expand All @@ -30,7 +30,7 @@ export class Player extends GameObject implements GameObjectPlayer {
const x = getRandomInRange(MIN_X, MAX_X);
const y = getRandomInRange(MIN_Y, MAX_Y);

super(objectId, x, y);
super({ id: objectId, x, y });

console.log(`Creating player ${objectId}!`);
}
Expand Down Expand Up @@ -299,7 +299,10 @@ export class Player extends GameObject implements GameObjectPlayer {
return;
}

const inventory = new Inventory(this.id, this.inventoryId);
const inventory = new Inventory({
objectId: this.id,
id: this.inventoryId,
});
await inventory.init();
this.inventory = inventory;
}
Expand All @@ -308,7 +311,7 @@ export class Player extends GameObject implements GameObjectPlayer {
this.skills = [];
const skills = await Skill.findAllInDB(this.id);
for (const skill of skills) {
const instance = new Skill(skill.id);
const instance = new Skill({ id: skill.id });
await instance.init();
this.skills.push(instance);
}
Expand Down
11 changes: 6 additions & 5 deletions apps/api/src/game/objects/rabbit.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,20 @@
import { createId } from "@paralleldrive/cuid2";
import { getRandomInRange } from "../../../../../packages/api-sdk/src";
import {
type IGameObjectRabbit,
getRandomInRange,
} from "../../../../../packages/api-sdk/src";
import { MAX_X, MAX_Y, MIN_X, MIN_Y } from "../../config";
import { GameObject } from "./gameObject";

export class Rabbit extends GameObject {
export class Rabbit extends GameObject implements IGameObjectRabbit {
public readonly entity = "RABBIT";

constructor() {
const id = createId();
const x = getRandomInRange(MIN_X, MAX_X);
const y = getRandomInRange(MIN_Y, MAX_Y);

super(id, x, y);

console.log("Creating new rabbit!");
super({ id, x, y });
}

live() {
Expand Down
8 changes: 3 additions & 5 deletions apps/api/src/game/objects/raider.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { createId } from "@paralleldrive/cuid2";
import {
type GameObjectRaider,
type IGameObjectRaider,
getRandomInRange,
} from "../../../../../packages/api-sdk/src";
import {
Expand All @@ -11,7 +11,7 @@ import {
} from "../../config";
import { GameObject } from "./gameObject";

export class Raider extends GameObject implements GameObjectRaider {
export class Raider extends GameObject implements IGameObjectRaider {
public readonly entity = "RAIDER";
public userName = "рейдер";
public colorIndex = 0;
Expand All @@ -22,9 +22,7 @@ export class Raider extends GameObject implements GameObjectRaider {
const finalX = getRandomInRange(RAIDER_CAMP_MIN_X, RAIDER_CAMP_MAX_X);
const finalY = getRandomInRange(RAIDER_CAMP_MIN_Y, RAIDER_CAMP_MAX_Y);

super(objectId, finalX, finalY);

console.log(`Creating raider ${objectId}!`);
super({ id: objectId, x: finalX, y: finalY });
}

live() {
Expand Down
Loading

0 comments on commit 0fac751

Please sign in to comment.