Skip to content

Commit

Permalink
move unit into feature
Browse files Browse the repository at this point in the history
  • Loading branch information
Greegko committed May 30, 2023
1 parent 1861dcc commit b783e31
Show file tree
Hide file tree
Showing 47 changed files with 110 additions and 146 deletions.
39 changes: 6 additions & 33 deletions packages/core-test/tests/commands/unit/equip.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { EquipmentSlot, ItemType, StashLocation, UnitCommand } from "@rpg-village/core";
import { EquipmentSlot, ItemType, UnitCommand } from "@rpg-village/core";

import { test } from "../../utils";

Expand All @@ -17,15 +17,12 @@ test("should be able to equip item", {
args: {
unitId: "test-hero-id",
itemId: "test-item-id",
stash: StashLocation.Unit,
},
},
],
expectedState: {
units: {
"test-hero-id": {
equipment: { [EquipmentSlot.Torso]: { id: "test-item-id", itemType: ItemType.Armor } },
},
"test-hero-id": { equipment: { [EquipmentSlot.Torso]: { id: "test-item-id", itemType: ItemType.Armor } } },
},
},
});
Expand All @@ -39,16 +36,7 @@ test("should remove item from the stash", {
},
},
},
commands: [
{
command: UnitCommand.EquipItem,
args: {
unitId: "test-hero-id",
itemId: "test-item-id",
stash: StashLocation.Unit,
},
},
],
commands: [{ command: UnitCommand.EquipItem, args: { unitId: "test-hero-id", itemId: "test-item-id" } }],
expectedState: { units: { "test-hero-id": { stash: { items: [] } } } },
});

Expand All @@ -64,18 +52,12 @@ test("should keep original on no available item id", {
commands: [
{
command: UnitCommand.EquipItem,
args: {
unitId: "test-hero-id",
itemId: "missing-item-id",
stash: StashLocation.Unit,
},
args: { unitId: "test-hero-id", itemId: "missing-item-id" },
},
],
expectedState: {
units: {
"test-hero-id": {
equipment: { [EquipmentSlot.Torso]: { id: "stay" } },
},
"test-hero-id": { equipment: { [EquipmentSlot.Torso]: { id: "stay" } } },
},
},
});
Expand All @@ -89,16 +71,7 @@ test("should unequip the current equipped item on same place target", {
},
},
},
commands: [
{
command: UnitCommand.EquipItem,
args: {
unitId: "test-hero-id",
itemId: "new-item-id",
stash: StashLocation.Unit,
},
},
],
commands: [{ command: UnitCommand.EquipItem, args: { unitId: "test-hero-id", itemId: "new-item-id" } }],
expectedState: {
units: {
"test-hero-id": {
Expand Down
24 changes: 1 addition & 23 deletions packages/core-test/tests/commands/unit/unequip.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ test("should be able to un-equip item", {
commands: [
{
command: UnitCommand.UnequipItem,
args: { unitId: "test-hero-id", itemId: "test-item-id", stash: StashLocation.Unit },
args: { unitId: "test-hero-id", itemId: "test-item-id" },
},
],
expectedState: { units: { "test-hero-id": { equipment: {} } } },
Expand All @@ -41,25 +41,3 @@ test("should add un-equiped item to the unit stash", {
},
},
});

test("should add un-equiped item to the village stash", {
initState: {
units: {
"test-hero-id": {
equipment: { [EquipmentSlot.Torso]: { id: "test-item-id" } },
stash: { items: [] },
},
},
},
commands: [
{
command: UnitCommand.UnequipItem,
args: { unitId: "test-hero-id", itemId: "test-item-id", stash: StashLocation.Village },
},
],
expectedState: {
village: {
stash: { items: [{ id: "test-item-id" }] },
},
},
});
5 changes: 1 addition & 4 deletions packages/core-test/tests/utils/create-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -127,10 +127,7 @@ function createCallback(createdState: GameState) {
y: 0,
});

createMapReference({
mapSize: MapSize.Endless,
mapLocationIds: [village.locationId],
});
return village.locationId;
}

function createActivityReference(activityArgs: Partial<Activity>) {
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/features/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "./soul";
export * from "./unit";
2 changes: 1 addition & 1 deletion packages/core/src/features/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"extends": "../../tsconfig-base.json",
"include": ["./index.ts"],
"references": [{ "path": "./soul" }]
"references": [{ "path": "./soul" }, { "path": "./unit" }]
}
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,25 +1,18 @@
import { ItemID } from "@models";

import { UnitID } from "../interfaces";
import { UnitID } from ".";

export enum UnitCommand {
EquipItem = "unit/equip-item",
UnequipItem = "unit/unequip-item",
}

export enum StashLocation {
Unit,
Village,
}

export interface UnitCommandEquipItemArgs {
unitId: UnitID;
itemId: ItemID;
stash: StashLocation;
}

export interface UnitCommandUnequipItemArgs {
unitId: UnitID;
itemId: ItemID;
stash: StashLocation;
}
File renamed without changes.
5 changes: 5 additions & 0 deletions packages/core/src/features/unit/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"extends": "../../../tsconfig-base.json",
"include": ["**/*.ts"],
"references": [{ "path": "../../core" }, { "path": "../../models" }]
}
29 changes: 29 additions & 0 deletions packages/core/src/features/unit/unit-command-handler.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { injectable } from "inversify";

import { commandHandler } from "@core";

import { UnitCommand, UnitCommandEquipItemArgs, UnitCommandUnequipItemArgs } from "./interfaces";
import { UnitService } from "./unit-service";

@injectable()
export class UnitCommandHandler {
constructor(private unitService: UnitService) {}

@commandHandler(UnitCommand.EquipItem)
equipItem({ unitId, itemId }: UnitCommandEquipItemArgs) {
const item = this.unitService.takeItemFromStash(unitId, itemId);

if (!item) return;

this.unitService.equipItem(unitId, item);
}

@commandHandler(UnitCommand.UnequipItem)
unequipEquipment({ unitId, itemId }: UnitCommandUnequipItemArgs) {
const item = this.unitService.unequipEquipment(unitId, itemId);

if (!item) return;

this.unitService.stashItems(unitId, [item]);
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,20 @@
import { injectable } from "inversify";
import { add, always, assoc, dissoc, evolve, find, inc, map, propEq, toPairs, when } from "rambda";

import { Equipment, EquipmentItem, EquipmentSlot, Item, ItemID, addItems, getItem, removeItem } from "@models";
import {
Equipment,
EquipmentItem,
EquipmentSlot,
Item,
ItemID,
addItems,
getItem,
isEquipmentItem,
removeItem,
} from "@models";

import { UnitID, UnitStash } from "./interfaces";
import { getEquipmentSlot } from "./lib";
import { nextLevelXp } from "./lib/next-level-xp";
import { UnitStore } from "./unit-store";

Expand Down Expand Up @@ -79,4 +90,31 @@ export class UnitService {

return item;
}

equipItem(unitId: UnitID, item: Item): void {
if (!isEquipmentItem(item)) return;

const slot = getEquipmentSlot(item);

if (!slot) return;

const oldItem = this.getEquipmentBySlot(unitId, slot);

if (oldItem) {
this.unequipEquipment(unitId, oldItem.id);
this.stashItems(unitId, [oldItem]);
}

this.setEquipment(unitId, slot, item);
}

unequipEquipment(unitId: UnitID, itemId: ItemID): EquipmentItem | undefined {
const equipment = this.getEquipmentByItemId(unitId, itemId);

if (!equipment) return;

this.setEquipment(unitId, equipment[0], undefined);

return equipment[1];
}
}
2 changes: 1 addition & 1 deletion packages/core/src/game/create-game-instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import { GameInstance } from "./interfaces";
export type CreateGameInstance = (config?: GameConfig) => GameInstance;

const coreModules = [
modules.unitModule,
modules.activityModule,
modules.gameModule,
modules.mapModule,
Expand All @@ -33,6 +32,7 @@ const coreModules = [
modules.shopModule,
modules.optionsModule,
features.soulModule,
features.unitModule,
];

export const createGameInstance: CreateGameInstance = (config?: GameConfig) => {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/modules/battle/battle-activity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { complement, prop } from "rambda";

import { ModuleConfig, ModuleConfigToken } from "@core";

import { isAlive } from "@features/unit";
import { IActivityHandler } from "@modules/activity";
import { PartyActivity, PartyID, PartyService } from "@modules/party";
import { isAlive } from "@modules/unit";
import { VillageConfig, VillageStashService } from "@modules/village";

import { BattleService } from "./battle-service";
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/modules/battle/battle-service.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { injectable } from "inversify";
import { forEach } from "rambda";

import { UnitStore } from "@features/unit";
import { PartyID, PartyService } from "@modules/party";
import { UnitStore } from "@modules/unit";

import { Battle } from "./battle";
import { BattleStore } from "./battle-store";
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/modules/battle/battle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { all, clone, complement, forEach } from "rambda";

import { sample } from "@lib/sample";

import { Unit, calculateUnitStatsWithEffects, isAlive } from "@modules/unit";
import { Unit, calculateUnitStatsWithEffects, isAlive } from "@features/unit";

import { BattleParty, BattleState } from "./interfaces";

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Unit } from "@modules/unit";
import { Unit } from "@features/unit";

export interface BattleState {
attackerParty: BattleParty;
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/modules/battle/lib/calculate-loot.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Unit } from "@features/unit";
import { Loot } from "@models";
import { Unit } from "@modules/unit";

export function calculateLoot(units: Unit[]): Loot {
return {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Unit, calculateUnitStatsWithEffects } from "@modules/unit";
import { Unit, calculateUnitStatsWithEffects } from "@features/unit";

export function calculateUnitStrength(unit: Unit): number {
const battleStats = calculateUnitStatsWithEffects(unit);
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/modules/battle/lib/calculate-xp-gain.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { sum } from "rambda";

import { Unit } from "@modules/unit";
import { Unit } from "@features/unit";

export function calculateXpGain(units: Unit[]): number {
return sum(units.map(unit => unit.level * 25));
Expand Down
1 change: 0 additions & 1 deletion packages/core/src/modules/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import "@core-types";
export * from "./game";
export * from "./activity";
export * from "./battle";
export * from "./unit";
export * from "./party";
export * from "./village";
export * from "./map";
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/modules/map/lib/generate-enemy.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Unit, UnitType, calculateEffectsValue } from "@features/unit";
import { EffectStatic, MiscEffectType } from "@models";
import { PartyStash } from "@modules/party";
import { Unit, UnitType, calculateEffectsValue } from "@modules/unit";

export interface EnemyPartyGeneration {
units: Omit<Unit, "id">[];
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/modules/map/map-event-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { append, evolve } from "rambda";

import { eventHandler } from "@core";

import { UnitStore } from "@features/unit";
import { EffectStatic } from "@models";
import { PartyOwner, PartyService } from "@modules/party";
import { UnitStore } from "@modules/unit";

import { MapEvent, MapEventNewLocationArgs, MapID, MapLocationID, MapLocationType } from "./interfaces";
import { generateEnemyParty } from "./lib";
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/modules/party/interfaces/party.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { UnitID } from "@features/unit";
import { ItemStash, ResourceStash } from "@models";
import { ActivityID } from "@modules/activity";
import { UnitID } from "@modules/unit";

import { PartyOwner } from "./party-owner";

Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/modules/party/party-service.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { injectable } from "inversify";
import { any, without } from "rambda";

import { Unit, UnitID, UnitService, UnitStore, isAlive } from "@features/unit";
import { Loot, addResource } from "@models";
import { ActivityStore } from "@modules/activity";
import { Unit, UnitID, UnitService, UnitStore, isAlive } from "@modules/unit";

import { Party, PartyID, PartyStash } from "./interfaces";
import { PartyStore } from "./party-store";
Expand Down
Loading

0 comments on commit b783e31

Please sign in to comment.