mirror of
https://github.com/linux-odyssey/linux-odyssey.git
synced 2026-11-05 19:20:12 +08:00
feat: getTasks
This commit is contained in:
1 parent
254e4f75ed
commit
d2cdd1304a
4 files changed
+74
-1
No files matched your search
@@ -1,7 +1,13 @@
|
|||||||
/* eslint-disable no-await-in-loop */
|
/* eslint-disable no-await-in-loop */
|
||||||
import { Event } from './Event'
|
import { Event } from './Event'
|
||||||
import { GlobalException } from './Exception'
|
import { GlobalException } from './Exception'
|
||||||
import { IQuest, IFileExistenceChecker, ICommand, IResponse } from './schema'
|
import {
|
||||||
|
IQuest,
|
||||||
|
IFileExistenceChecker,
|
||||||
|
ICommand,
|
||||||
|
IResponse,
|
||||||
|
ITask,
|
||||||
|
} from './schema'
|
||||||
import { Stage } from './Stage'
|
import { Stage } from './Stage'
|
||||||
|
|
||||||
export class Quest {
|
export class Quest {
|
||||||
@@ -67,4 +73,21 @@ export class Quest {
|
|||||||
getResponses(completed: string[]): IResponse[] {
|
getResponses(completed: string[]): IResponse[] {
|
||||||
return completed.map((id) => this.events.get(id)!.getResponse())
|
return completed.map((id) => this.events.get(id)!.getResponse())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getTasks(completed: string[]): ITask[] {
|
||||||
|
const completedSet = new Set(completed)
|
||||||
|
const completedStages = this.stages
|
||||||
|
.filter((stage) => completedSet.has(stage.id))
|
||||||
|
.map((stage) => ({
|
||||||
|
id: stage.id,
|
||||||
|
name: stage.name,
|
||||||
|
completed: true,
|
||||||
|
}))
|
||||||
|
const activeStages = this.getActiveStages(completed).map((stage) => ({
|
||||||
|
id: stage.id,
|
||||||
|
name: stage.name,
|
||||||
|
completed: false,
|
||||||
|
}))
|
||||||
|
return [...completedStages, ...activeStages]
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -5,6 +5,7 @@ import {
|
|||||||
ISession,
|
ISession,
|
||||||
IFileExistenceChecker,
|
IFileExistenceChecker,
|
||||||
IResponse,
|
IResponse,
|
||||||
|
ITask,
|
||||||
} from './schema'
|
} from './schema'
|
||||||
|
|
||||||
export class Session implements ISession {
|
export class Session implements ISession {
|
||||||
@@ -43,4 +44,8 @@ export class Session implements ISession {
|
|||||||
getResponses(): IResponse[] {
|
getResponses(): IResponse[] {
|
||||||
return this.quest.getResponses(this.completed)
|
return this.quest.getResponses(this.completed)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getTasks(): ITask[] {
|
||||||
|
return this.quest.getTasks(this.completed)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
@@ -15,3 +15,9 @@ export interface ICommand {
|
|||||||
export interface ISession {
|
export interface ISession {
|
||||||
completedEvents: string[]
|
completedEvents: string[]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface ITask {
|
||||||
|
id: string
|
||||||
|
name: string
|
||||||
|
completed: boolean
|
||||||
|
}
|
||||||
@@ -12,6 +12,7 @@ const quest: IQuest = {
|
|||||||
stages: [
|
stages: [
|
||||||
{
|
{
|
||||||
id: 'stage1',
|
id: 'stage1',
|
||||||
|
name: 'Stage 1',
|
||||||
requirements: [],
|
requirements: [],
|
||||||
condition: {
|
condition: {
|
||||||
command: 'echo start',
|
command: 'echo start',
|
||||||
@@ -20,6 +21,7 @@ const quest: IQuest = {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
id: 'stage2',
|
id: 'stage2',
|
||||||
|
name: 'Stage 2',
|
||||||
requirements: ['stage1'],
|
requirements: ['stage1'],
|
||||||
condition: {
|
condition: {
|
||||||
command: 'ls',
|
command: 'ls',
|
||||||
@@ -28,6 +30,7 @@ const quest: IQuest = {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
id: 'stage3',
|
id: 'stage3',
|
||||||
|
name: 'Stage 3',
|
||||||
requirements: ['stage1'],
|
requirements: ['stage1'],
|
||||||
condition: {
|
condition: {
|
||||||
command: 'mkdir hello',
|
command: 'mkdir hello',
|
||||||
@@ -36,6 +39,7 @@ const quest: IQuest = {
|
|||||||
},
|
},
|
||||||
{
|
{
|
||||||
id: 'stage4',
|
id: 'stage4',
|
||||||
|
name: 'Stage 4',
|
||||||
requirements: ['stage2', 'stage3'],
|
requirements: ['stage2', 'stage3'],
|
||||||
condition: {
|
condition: {
|
||||||
command: 'echo finish',
|
command: 'echo finish',
|
||||||
@@ -129,4 +133,39 @@ describe('Session', () => {
|
|||||||
{ type: 'narrative', content: 'Narrative 2' },
|
{ type: 'narrative', content: 'Narrative 2' },
|
||||||
])
|
])
|
||||||
})
|
})
|
||||||
|
|
||||||
|
it('should return tasks', () => {
|
||||||
|
const session = new Session(
|
||||||
|
{
|
||||||
|
completedEvents: [],
|
||||||
|
},
|
||||||
|
quest,
|
||||||
|
new MockFileChecker()
|
||||||
|
)
|
||||||
|
expect(session.getTasks()).toEqual([
|
||||||
|
{ id: 'stage1', name: 'Stage 1', completed: false },
|
||||||
|
])
|
||||||
|
|
||||||
|
session.complete('stage1')
|
||||||
|
expect(session.getTasks()).toEqual([
|
||||||
|
{ id: 'stage1', name: 'Stage 1', completed: true },
|
||||||
|
{ id: 'stage2', name: 'Stage 2', completed: false },
|
||||||
|
{ id: 'stage3', name: 'Stage 3', completed: false },
|
||||||
|
])
|
||||||
|
|
||||||
|
session.complete('stage2')
|
||||||
|
expect(session.getTasks()).toEqual([
|
||||||
|
{ id: 'stage1', name: 'Stage 1', completed: true },
|
||||||
|
{ id: 'stage2', name: 'Stage 2', completed: true },
|
||||||
|
{ id: 'stage3', name: 'Stage 3', completed: false },
|
||||||
|
])
|
||||||
|
|
||||||
|
session.complete('stage3')
|
||||||
|
expect(session.getTasks()).toEqual([
|
||||||
|
{ id: 'stage1', name: 'Stage 1', completed: true },
|
||||||
|
{ id: 'stage2', name: 'Stage 2', completed: true },
|
||||||
|
{ id: 'stage3', name: 'Stage 3', completed: true },
|
||||||
|
{ id: 'stage4', name: 'Stage 4', completed: false },
|
||||||
|
])
|
||||||
|
})
|
||||||
})
|
})
|
||||||
Reference in new issue
Block a user