Skip to content

Commit

Permalink
update activity handler interface
Browse files Browse the repository at this point in the history
  • Loading branch information
Greegko committed Nov 7, 2022
1 parent c19bff9 commit 1fabd51
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 24 deletions.
4 changes: 2 additions & 2 deletions packages/core/src/core/module/module.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ModuleConfig } from "@core/global-type";
import { IStore } from "@core/store";

import { IActivityHandler } from "@modules/activity";
import { AnyActivity, IActivityHandler } from "@modules/activity";

export interface ProvideClass {
new (...args: any[]): any;
Expand All @@ -17,7 +17,7 @@ export interface StoreClass {
}

export interface ActivityClass {
new (...args: any[]): IActivityHandler<any, any>;
new (...args: any[]): IActivityHandler<AnyActivity>;
}

export interface ModulActivity {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { Activity, PartyActivityStartArgs } from "./activity";
import { Activity } from "./activity";

export interface IActivityHandler<StaringArgs extends PartyActivityStartArgs, ActivityState> {
start(startArgs: StaringArgs): ActivityState;
isRunnable(startArgs: StaringArgs): boolean;
isDone(activity: Activity<ActivityState>): boolean;
execute(activity: Activity<ActivityState>): ActivityState;
resolve(activity: Activity<ActivityState>): void;
export interface IActivityHandler<A extends Activity> {
start(startArgs: A['startArgs']): A['state'];
isRunnable(startArgs: A['startArgs']): boolean;
isDone(activity: Activity): boolean;
execute(activity: Activity): A['state'];
resolve(activity: Activity): void;
}
11 changes: 7 additions & 4 deletions packages/core/src/modules/activity/interfaces/activity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,22 @@ export type ActivityID = string;
export enum ActivityType {
Party = "party",
}
export type Activity<T = unknown, S = unknown> = {

export type Activity<State = unknown, StartingArgs = unknown> = {
id: ActivityID;
state: T;
state: State;
name: string;
startArgs: S;
startArgs: StartingArgs;
type: ActivityType;
};

export type AnyActivity = Activity<any, any>;

export type PartyActivityStartArgs = {
partyId: PartyID;
involvedPartyId?: PartyID;
};

export type PartyActivity<T = unknown> = Activity<T, PartyActivityStartArgs> & {
state: T;
type: ActivityType.Party;
};
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { AnyActivity } from "./activity";
import { IActivityHandler } from "./activity-handler";

export type GetActivityHandlerByName = (name: string) => IActivityHandler<any, any>;
export type GetActivityHandlerByName = (name: string) => IActivityHandler<AnyActivity>;
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 @@ -18,7 +18,7 @@ export type BattleState = { battleId: BattleID };
export type BattleStartArgs = { partyId: PartyID; involvedPartyId: PartyID };

@injectable()
export class BattleActivity implements IActivityHandler<BattleStartArgs, BattleState> {
export class BattleActivity implements IActivityHandler<Activity<BattleState, BattleStartArgs>> {
constructor(
private partyService: PartyService,
private battleService: BattleService,
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/modules/map/activites/explore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export type ExploreStartArgs = {
};

@injectable()
export class MapExploreActivity implements IActivityHandler<ExploreStartArgs, ExploreState> {
export class MapExploreActivity implements IActivityHandler<Activity<ExploreState, ExploreStartArgs>> {
constructor(
private mapService: MapService,
private mapLocationStore: MapLocationStore,
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/modules/map/activites/travel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export type TravelStartArgs = {
};

@injectable()
export class MapTravelActivity implements IActivityHandler<TravelStartArgs, TravelState> {
export class MapTravelActivity implements IActivityHandler<Activity<TravelState, TravelStartArgs>> {
constructor(private partyStore: PartyStore, private mapService: MapService, private eventSystem: EventSystem) {}

start({ partyId, targetLocationId }: TravelStartArgs): TravelState {
Expand Down
8 changes: 4 additions & 4 deletions packages/core/src/modules/village/activities/village-heal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,20 @@ export type VillageHealState = {
partyId: PartyID;
};

export type VillageHealStateArgs = VillageHealState;
export type VillageHealStartArgs = VillageHealState;
export type RecoverableUnit = { id: UnitID; hp: number; maxhp: number };

@injectable()
export class VillageHealActivity implements IActivityHandler<VillageHealStateArgs, VillageHealState> {
export class VillageHealActivity implements IActivityHandler<Activity<VillageHealState, VillageHealStartArgs>> {
constructor(private unitStore: UnitStore, private unitService: UnitService, private partyStore: PartyStore) {}

start({ partyId }: VillageHealStateArgs): VillageHealState {
start({ partyId }: VillageHealStartArgs): VillageHealState {
return {
partyId,
};
}

isRunnable({ partyId }: VillageHealStateArgs): boolean {
isRunnable({ partyId }: VillageHealStartArgs): boolean {
return this.getRecoverableUnits(partyId).length > 0;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ export type TrainingFieldState = {
progress: number;
};

export type TrainingFieldStateArgs = TrainingFieldState;
export type TrainingFieldStartArgs = TrainingFieldState;
export type RecoverableUnit = { id: UnitID; hp: number; maxhp: number };

@injectable()
export class TrainingFieldTrainActivity implements IActivityHandler<TrainingFieldStateArgs, TrainingFieldState> {
export class TrainingFieldTrainActivity implements IActivityHandler<Activity<TrainingFieldState, TrainingFieldStartArgs>> {
constructor(private unitService: UnitService, private partyStore: PartyStore) {}

start({ partyId }: TrainingFieldStateArgs): TrainingFieldState {
start({ partyId }: TrainingFieldStartArgs): TrainingFieldState {
return {
partyId,
progress: 100,
Expand Down

0 comments on commit 1fabd51

Please sign in to comment.