Skip to content

Commit

Permalink
feat(任务系统): 任务组件更新和统计服务集成
Browse files Browse the repository at this point in the history
  • Loading branch information
Encaik committed Sep 25, 2024
1 parent 6419ddf commit 0f5a7c2
Show file tree
Hide file tree
Showing 9 changed files with 137 additions and 18 deletions.
24 changes: 16 additions & 8 deletions src/app/layouts/sider/components/task/task.component.html
Original file line number Diff line number Diff line change
@@ -1,15 +1,23 @@
<h5 nz-typography class="underline decoration-sky-500">任务面板</h5>
<div class="flex-1 p-4 border-gray-300 border rounded overflow-auto">
@if (currentTask) {
<div class="gap-2">
<h5 nz-typography>{{ currentTask.title }}</h5>
<p>任务描述:{{ currentTask.description }}</p>
<p
>任务奖励:
@for (reward of currentTask.rewards; track $index) {
<span class="mr-2">{{ ItemMap[reward.id].name }} * {{ reward.count }}</span>
<div class="h-full flex flex-col justify-between items-center">
<div class="gap-2 w-full">
<h5 nz-typography>{{ currentTask.title }}</h5>
<p>任务描述:{{ currentTask.description }}</p>
<p>
任务奖励:
@for (reward of currentTask.rewards; track $index) {
<span class="mr-2">{{ ItemMap[reward.id].name }} * {{ reward.count }}</span>
}
</p>
<p>完成情况:{{ currentTask.isCompleted ? '已完成' : '未完成' }}</p>
</div>
<div>
@if (currentTask.isCompleted) {
<button nz-button (click)="onGetRewardClick()">获得奖励</button>
}
</p>
</div>
</div>
} @else {
<div class="w-full h-full flex justify-center items-center">
Expand Down
37 changes: 34 additions & 3 deletions src/app/layouts/sider/components/task/task.component.ts
Original file line number Diff line number Diff line change
@@ -1,23 +1,54 @@
import { Component, inject, OnInit } from '@angular/core';
import { NzButtonModule } from 'ng-zorro-antd/button';
import { NzEmptyModule } from 'ng-zorro-antd/empty';
import { NzTypographyModule } from 'ng-zorro-antd/typography';

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

@Component({
selector: 'app-task',
standalone: true,
imports: [NzTypographyModule, NzEmptyModule],
imports: [NzTypographyModule, NzEmptyModule, NzButtonModule],
templateUrl: './task.component.html'
})
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)
);
});
}

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);
}
}
1 change: 1 addition & 0 deletions src/app/models/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ export * from './item.model'; // 物品
export * from './log.model'; // 日志
export * from './reward.model'; // 奖励池
export * from './runtime.model'; // 运行时
export * from './statistics.model'; // 统计
export * from './task.model'; // 任务
13 changes: 13 additions & 0 deletions src/app/models/statistics.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
export interface CharacterStatistics {
cultivationCount: number;
}

export interface StatisticsEvent {
type: StatisticsEventType;
field: string;
count: number;
}

export enum StatisticsEventType {
Character
}
17 changes: 14 additions & 3 deletions src/app/models/task.model.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import { inject } from '@angular/core';

import { StatisticsService } from '../services/statistics.service';
import { StatisticsEvent, StatisticsEventType } from './statistics.model';

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

export interface TaskReward {
Expand All @@ -26,7 +31,13 @@ export const TASKS: Record<string, Task> = {
}
],
isCompleted: false,
watcher: () => {}
conditions: [
{
type: StatisticsEventType.Character,
field: 'cultivationCount',
count: 1
}
]
},
2: {
id: 2,
Expand All @@ -35,6 +46,6 @@ export const TASKS: Record<string, Task> = {
description: 'This is the second task',
rewards: [],
isCompleted: false,
watcher: () => {}
conditions: []
}
};
9 changes: 8 additions & 1 deletion src/app/services/character.service.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import { inject, Injectable } from '@angular/core';

import { BaseInfo, Character, LogLevel, LogType, SkillInfo, StatusInfo, LevelInfo, AttrInfo } from '../models';
import { BaseInfo, Character, LogLevel, LogType, SkillInfo, StatusInfo, LevelInfo, AttrInfo, StatisticsEventType } from '../models';
import { EnvService } from './env.service';
import { LogService } from './log.service';
import { StatisticsService } from './statistics.service';

@Injectable({
providedIn: 'root'
})
export class CharacterService {
private envSrv = inject(EnvService);
private logSrv = inject(LogService);
private statisticsSrv = inject(StatisticsService);

id: string = '';
baseInfo: BaseInfo = {
Expand Down Expand Up @@ -46,6 +48,11 @@ export class CharacterService {
};

cultivation(): Promise<boolean> {
this.statisticsSrv.update({
type: StatisticsEventType.Character,
field: 'cultivationCount',
count: 1
});
const exp = this.getAddExp();
const levelPrecent = Math.round(this.envSrv.maxExp / Object.keys(this.envSrv.levelMap).length);
const newExp = exp + this.levelInfo.exp;
Expand Down
16 changes: 16 additions & 0 deletions src/app/services/statistics.service.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { TestBed } from '@angular/core/testing';

import { StatisticsService } from './statistics.service';

describe('StatisticsService', () => {
let service: StatisticsService;

beforeEach(() => {
TestBed.configureTestingModule({});
service = TestBed.inject(StatisticsService);
});

it('should be created', () => {
expect(service).toBeTruthy();
});
});
31 changes: 31 additions & 0 deletions src/app/services/statistics.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { Injectable, OnInit } from '@angular/core';
import { Subject } from 'rxjs';

import { CharacterStatistics, StatisticsEvent, StatisticsEventType } from '../models';

@Injectable({
providedIn: 'root'
})
export class StatisticsService {
characterStatistics: CharacterStatistics = {
cultivationCount: 0
};
statistics: Subject<StatisticsEvent> = new Subject();
statistics$ = this.statistics.asObservable();

update(event: StatisticsEvent) {
switch (event.type) {
case StatisticsEventType.Character:
(this.characterStatistics as any)[event.field] += event.count;
break;
}
this.statistics.next(event);
}

getValue(type: StatisticsEventType, field: string) {
switch (type) {
case StatisticsEventType.Character:
return (this.characterStatistics as any)[field];
}
}
}
7 changes: 4 additions & 3 deletions src/app/services/task.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { Injectable } from '@angular/core';
import { Subject } from 'rxjs';
import { inject, Injectable } from '@angular/core';
import { Subject, Subscription } from 'rxjs';

import { Task, TASKS } from '../models';
import { StatisticsEventType, Task, TASKS } from '../models';
import { StatisticsService } from './statistics.service';

@Injectable({
providedIn: 'root'
Expand Down

1 comment on commit 0f5a7c2

@vercel
Copy link

@vercel vercel bot commented on 0f5a7c2 Sep 25, 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-git-main-encaiks-projects.vercel.app
wanjie.vercel.app
wanjie-encaiks-projects.vercel.app

Please sign in to comment.