Skip to content

Commit

Permalink
Delegateメソッドが複数回呼び出される問題を修正
Browse files Browse the repository at this point in the history
  • Loading branch information
mitsuyoshi-yamazaki committed Aug 20, 2024
1 parent 6e2e185 commit 5935a12
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export type EventDrivenTestProcessId = ProcessId<Dependency, string, Api, EventD

export class EventDrivenTestProcess extends ApplicationProcess<Dependency, string, Api, EventDrivenTestProcessState, EventDrivenTestProcess> {
public readonly applicationName = "EventDrivenTest"
public readonly version = new SemanticVersion(1, 0, 2)
public readonly version = new SemanticVersion(1, 0, 3)

public readonly dependencies: ProcessDependencies = {
processes: [
Expand Down
58 changes: 44 additions & 14 deletions src/os_v5/processes/economy/claim_room/claim_room_process.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { SystemCalls } from "os_v5/system_calls/interface"
import { CreepDistributorProcessApi } from "os_v5/processes/game_object_management/creep/creep_distributor_process"
import { AnyTaskDrivenCreep, CreepTaskObserver, CreepTaskStateManagementProcessApi, TaskDrivenCreep } from "os_v5/processes/game_object_management/creep/creep_task_state_management_process"
import { Timestamp } from "shared/utility/timestamp"
import { ClaimRoomDelegate, ClaimRoomProblem } from "./delegate"
import { ClaimRoomDelegate, ClaimRoomProblem, ClaimRoomProblemUnknown } from "./delegate"
import { CreepTaskError, CreepTaskResult } from "os_v5/processes/game_object_management/creep/creep_task_result"
import { processTypeDecodingMap, processTypeEncodingMap, SerializedProcessTypes } from "os_v5/process/process_type_map"
import { CreepProviderApi } from "../../bot/creep_provider_api"
Expand Down Expand Up @@ -62,6 +62,7 @@ export class ClaimRoomProcess extends Process<Dependency, RoomName, void, ClaimR

private readonly codename: string
private readonly estimatedFinishTime: Timestamp
private onTickDependency: Dependency | null = null

private constructor(
public readonly processId: ClaimRoomProcessId,
Expand Down Expand Up @@ -113,6 +114,8 @@ export class ClaimRoomProcess extends Process<Dependency, RoomName, void, ClaimR
}

public run(dependency: Dependency): void {
this.onTickDependency = dependency

const { spawned } = dependency.getSpawnedCreepsFor(this.processId)
const creepsWithTasks = dependency.registerTaskDrivenCreeps<"", Record<string, never>>(spawned, { observer: { processId: this.processId, observer: this } })

Expand All @@ -132,28 +135,29 @@ export class ClaimRoomProcess extends Process<Dependency, RoomName, void, ClaimR
switch (this.claimState.case) {
case "initialized":
this.spawnClaimer(dependency)
this.claimState = {
this.changeClaimState(dependency, {
case: "spawn_requested",
}
})
return

case "spawn_requested":
case "running":
case "running": {
if (Game.time >= this.estimatedFinishTime) {
return
}
dependency.claimRoomDidFailClaiming(this, {
const problem: ClaimRoomProblemUnknown = {
case: "unknown",
reason: `haven't finished in ${Game.time - this.launchTime} ticks, state: ${this.claimState.case}`,
}
this.changeClaimState(dependency, {
case: "failed",
problem,
})
return
}

case "finished":
dependency.claimRoomDidFinishClaiming(this)
return

case "failed":
dependency.claimRoomDidFailClaiming(this, this.claimState.problem)
return

default: {
Expand All @@ -164,6 +168,28 @@ export class ClaimRoomProcess extends Process<Dependency, RoomName, void, ClaimR
}
}

private changeClaimState(dependency: Dependency, newState: ClaimState): void {
this.claimState = newState

switch (newState.case) {
case "initialized":
case "spawn_requested":
case "running":
break
case "finished":
dependency.claimRoomDidFinishClaiming(this)
break
case "failed":
dependency.claimRoomDidFailClaiming(this, newState.problem)
break
default: {
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const _: never = newState
break
}
}
}

private claimerTaskFor(creep: TaskDrivenCreep<"", Record<string, never>>): CreepTask.AnyTask {
if (creep.room.name !== this.roomName) {
return CreepTask.Tasks.MoveToRoom.create(this.roomName, [])
Expand Down Expand Up @@ -201,8 +227,10 @@ export class ClaimRoomProcess extends Process<Dependency, RoomName, void, ClaimR
if (result.taskType !== "ClaimController") {
return
}
this.claimState = {
case: "finished",
if (this.onTickDependency != null) {
this.changeClaimState(this.onTickDependency, {
case: "finished",
})
}
}

Expand All @@ -211,9 +239,11 @@ export class ClaimRoomProcess extends Process<Dependency, RoomName, void, ClaimR
if (problem == null) {
return
}
this.claimState = {
case: "failed",
problem,
if (this.onTickDependency != null) {
this.changeClaimState(this.onTickDependency, {
case: "failed",
problem,
})
}
}

Expand Down
1 change: 1 addition & 0 deletions src/os_v5/processes/economy/claim_room/delegate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export type ClaimRoomProblem = ClaimRoomProblemClaimFailed
| ClaimRoomProblemUnknown


/// 呼び出されるのは afterRun() のタイミングであることがある
export type ClaimRoomDelegate = CreepProviderApi & {
claimRoomDidFinishClaiming(process: ClaimRoomProcess): void
claimRoomDidFailClaiming(process: ClaimRoomProcess, problem: ClaimRoomProblem): void
Expand Down
2 changes: 1 addition & 1 deletion src/os_v5/standard_io/commands/launch_command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ registerProcess("V3ProcessLauncherProcess", (argumentParser) => {
registerProcess("EventDrivenTestProcess", (argumentParser) => {
const name = argumentParser.string("name").parse()
const parentRoomName = argumentParser.roomName("parent_room_name").parse({my: true})
const childProcessType = argumentParser.typedString("child_process", "EventDrivenTestChildProcessTypes", isEventDrivenTestChildProcessTypes).parse()
const childProcessType = argumentParser.typedString("child_process_type", "EventDrivenTestChildProcessTypes", isEventDrivenTestChildProcessTypes).parse()

const childProcessArguments = ((): ChildProcessArguments => {
switch (childProcessType) {
Expand Down

0 comments on commit 5935a12

Please sign in to comment.