Skip to content

Commit

Permalink
feat(任务): 任务组件的更新包括显示任务条件和完成状态
Browse files Browse the repository at this point in the history
  • Loading branch information
Encaik committed Sep 26, 2024
1 parent 16f48ff commit ce57282
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 38 deletions.
8 changes: 7 additions & 1 deletion src/app/layouts/sider/components/task/task.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,17 @@ <h5 nz-typography>{{ currentTask.title }}</h5>
<span class="mr-2">{{ ItemMap[reward.id].name }} * {{ reward.count }}</span>
}
</p>
<p>
任务目标:
@for (condition of currentTask.conditions; track $index) {
<span class="mr-2">{{ condition.title }}({{ condition.current }}/{{ condition.goal }})</span>
}
</p>
<p>完成情况:{{ currentTask.isCompleted ? '已完成' : '未完成' }}</p>
</div>
<div>
@if (currentTask.isCompleted) {
<!-- <button nz-button (click)="onGetRewardClick()">获得奖励</button> -->
<button nz-button (click)="onGetRewardClick()">获得奖励</button>
}
</div>
</div>
Expand Down
51 changes: 20 additions & 31 deletions src/app/layouts/sider/components/task/task.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ import { NzButtonModule } from 'ng-zorro-antd/button';
import { NzEmptyModule } from 'ng-zorro-antd/empty';
import { NzTypographyModule } from 'ng-zorro-antd/typography';

import { getItemLevelClass, ItemMap, LogLevel, LogType, Task } from '../../../../models';
import { Task, ItemMap, getItemLevelClass, LogLevel, LogType } from '../../../../models';
import { BackpackService } from '../../../../services/backpack.service';
import { LogService } from '../../../../services/log.service';
import { StatisticsService } from '../../../../storages/statistics.service';
import { TaskService } from '../../../../services/task.service';

@Component({
Expand All @@ -15,40 +14,30 @@ import { TaskService } from '../../../../services/task.service';
imports: [NzTypographyModule, NzEmptyModule, NzButtonModule],
templateUrl: './task.component.html'
})
export class TaskComponent {
export class TaskComponent implements OnInit {
private taskSrv = inject(TaskService);
private statisticsService = inject(StatisticsService);
private backpackSrv = inject(BackpackService);
private logSrv = inject(LogService);
currentTask: Task | undefined;
ItemMap = ItemMap;

// ngOnInit(): void {
// this.taskSrv.task$.subscribe(task => (this.currentTask = task));
// this.statisticsService.statistics$.subscribe(event => {
// if (!this.currentTask) return;
// this.currentTask.isCompleted = this.currentTask.conditions.every(
// condition =>
// condition.type === event.type &&
// condition.field === event.field &&
// condition.count <= this.statisticsService.getValue(condition.type, condition.field)
// );
// });
// }
ngOnInit(): void {
this.taskSrv.task$.subscribe(task => (this.currentTask = task));
}

// onGetRewardClick() {
// if (!this.currentTask) return;
// let msg: string = '';
// this.currentTask.rewards.forEach(reward => {
// const item = ItemMap[reward.id];
// this.backpackSrv.addItem(item, reward.count);
// msg += `<span class="${getItemLevelClass(item.level)}">${item.name}</span> * ${reward.count} `;
// });
// this.logSrv.log({
// msg: `完成任务,获得奖励:${msg}`,
// type: LogType.Item,
// level: LogLevel.Info
// });
// this.currentTask && this.taskSrv.complatedTask(this.currentTask);
// }
onGetRewardClick() {
if (!this.currentTask) return;
let msg: string = '';
this.currentTask.rewards.forEach(reward => {
const item = ItemMap[reward.id];
this.backpackSrv.addItem(item, reward.count);
msg += `<span class="${getItemLevelClass(item.level)}">${item.name}</span> * ${reward.count} `;
});
this.logSrv.log({
msg: `完成任务,获得奖励:${msg}`,
type: LogType.Item,
level: LogLevel.Info
});
this.currentTask && this.taskSrv.complatedTask(this.currentTask);
}
}
24 changes: 22 additions & 2 deletions src/app/models/task.model.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
import { CharacterEventOperate, EventType, Event } from './event.model';

export interface Task {
id: number;
nextId: number;
title: string;
description: string;
rewards: TaskReward[];
isCompleted: boolean;
conditions: any[];
conditions: TaskCondition[];
}

export interface TaskCondition {
title: string;
event: Event;
current: number;
goal: number;
}

export interface TaskReward {
Expand All @@ -26,7 +35,18 @@ export const TASKS: Record<string, Task> = {
}
],
isCompleted: false,
conditions: []
conditions: [
{
title: '完成修炼',
event: {
type: EventType.Character,
operate: CharacterEventOperate.Cultivation,
data: null
},
current: 0,
goal: 1
}
]
},
2: {
id: 2,
Expand Down
6 changes: 2 additions & 4 deletions src/app/pages/home/home.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@ import { NzTypographyModule } from 'ng-zorro-antd/typography';

import { BattleModalComponent } from '../../components/battle-modal/battle-modal.component';
import { LogType, LogLevel, BattleCharacter } from '../../models';
import { CharacterEventOperate, EventType } from '../../models/event.model';
import { ItemMap } from '../../models/item.model';
import { BackpackService } from '../../services/backpack.service';
import { CharacterService } from '../../services/character.service';
import { EnvService } from '../../services/env.service';
import { EventService } from '../../services/event.service';
import { LogService } from '../../services/log.service';
import { RuntimeService } from '../../services/runtime.service';
import { Generate } from '../../utils/generate';
import { BackpackComponent } from './components/backpack/backpack.component';
import { EventService } from '../../services/event.service';
import { CharacterEventOperate, EventType } from '../../models/event.model';

@Component({
selector: 'app-home',
Expand Down Expand Up @@ -51,8 +51,6 @@ export class HomeComponent implements OnInit {
data: null
})
.then(isUpgrade => {
console.log(isUpgrade);

this.rtSrv.nextTimeTick();
if (isUpgrade) {
this.isUpgrade = true;
Expand Down
4 changes: 4 additions & 0 deletions src/app/services/event.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import { inject, Injectable } from '@angular/core';
import { Subject } from 'rxjs';

import { Event, EventType } from '../models/event.model';
import { CharacterService } from './character.service';
import { TaskService } from './task.service';

@Injectable({
providedIn: 'root'
Expand All @@ -10,10 +12,12 @@ export class EventService {
event: Subject<Event> = new Subject();
event$ = this.event.asObservable();

private taskSrv = inject(TaskService);
private characterSrv = inject(CharacterService);

sendEvent(event: Event): Promise<any> {
this.event.next(event);
this.taskSrv.update(event);
return new Promise(resolve => {
switch (event.type) {
case EventType.Character:
Expand Down
20 changes: 20 additions & 0 deletions src/app/services/task.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Injectable } from '@angular/core';
import { Subject } from 'rxjs';

import { Task, TASKS } from '../models';
import { Event } from '../models/event.model';

@Injectable({
providedIn: 'root'
Expand All @@ -11,6 +12,25 @@ export class TaskService {
taskSub: Subject<Task> = new Subject();
task$ = this.taskSub.asObservable();

update(event: Event) {
let isCompleted = true;
this.currentTask?.conditions?.forEach(condition => {
if (this.isEqual(condition.event, event)) {
condition.current++;
}
if (condition.current < condition.goal) {
isCompleted = false;
}
});
if (this.currentTask) {
this.currentTask.isCompleted = isCompleted;
}
}

isEqual(obj1: any, obj2: any): boolean {
return JSON.stringify(obj1) === JSON.stringify(obj2);
}

complatedTask(task: Task): void {
const nextTask = TASKS[task.nextId] || undefined;
if (!nextTask) return;
Expand Down

1 comment on commit ce57282

@vercel
Copy link

@vercel vercel bot commented on ce57282 Sep 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

wanjie – ./

wanjie-encaiks-projects.vercel.app
wanjie.vercel.app
wanjie-git-main-encaiks-projects.vercel.app

Please sign in to comment.