Skip to content

Commit

Permalink
refactor(generate): 完善角色和环境生成逻辑并优化生成方法注释
Browse files Browse the repository at this point in the history
  • Loading branch information
Encaik committed Sep 15, 2024
1 parent 57bec8e commit 41365d9
Show file tree
Hide file tree
Showing 3 changed files with 133 additions and 13 deletions.
2 changes: 2 additions & 0 deletions src/app/models/log.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,7 @@ export const getTypeDesc = (type: LogType): string => {
return '人物';
case LogType.Env:
return '环境';
default:
return '其他';
}
};
101 changes: 101 additions & 0 deletions src/app/utils/generate.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
import { BattleCharacter, Env, EnvType, InitCharacter } from '../models';
import { Generate, getCharacterName, getCharacterTalent } from './generate';

describe('Util Generate', () => {
beforeEach(async () => {
// 在每个测试之前重置随机数种子,以便每次测试结果一致
jasmine.clock().install();
jasmine.clock().mockDate();
});

afterEach(() => {
jasmine.clock().uninstall();
});

describe('#characters()', () => {
it('should generate correct number of characters', () => {
const length = 5;
const characters = Generate.characters(length);
expect(characters.length).toBe(length);
characters.forEach((character: InitCharacter) => {
expect(character.id.startsWith('character-')).toBe(true);
expect(character.baseInfo.name).toBeTruthy();
expect(character.baseInfo.gender).toMatch(/^(|)$/);
expect(character.baseInfo.age).toBeGreaterThanOrEqual(12);
expect(character.baseInfo.talent).toEqual(jasmine.any(Array));
expect(character.skillInfo.hp).toBeGreaterThanOrEqual(100);
expect(character.skillInfo.mp).toBeGreaterThanOrEqual(100);
expect(character.skillInfo.attack).toBeGreaterThanOrEqual(20);
expect(character.skillInfo.defence).toBeGreaterThanOrEqual(20);
expect(character.skillInfo.speed).toBeGreaterThanOrEqual(0);
});
});
});

describe('#enemys()', () => {
it('should generate correct number of enemies', () => {
const length = 5;
const enemies = Generate.enemys(length);
expect(enemies.length).toBe(length);
enemies.forEach((enemy: BattleCharacter) => {
expect(enemy.id.startsWith('enemy-')).toBe(true);
expect(enemy.isEnemy).toBe(true);
expect(enemy.baseInfo.name).toBeTruthy();
expect(enemy.baseInfo.gender).toMatch(/^(|)$/);
expect(enemy.baseInfo.age).toBeGreaterThanOrEqual(12);
expect(enemy.baseInfo.talent).toEqual(jasmine.any(Array));
expect(enemy.statusInfo.hp).toBeGreaterThanOrEqual(100);
expect(enemy.statusInfo.mp).toBeGreaterThanOrEqual(100);
expect(enemy.statusInfo.buffs).toEqual(jasmine.any(Array));
expect(enemy.levelInfo.energy).toBeGreaterThanOrEqual(0);
expect(enemy.levelInfo.level).toBe(0);
expect(enemy.attrInfo.hp).toBeGreaterThanOrEqual(100);
expect(enemy.attrInfo.mp).toBeGreaterThanOrEqual(100);
expect(enemy.attrInfo.attack).toBeGreaterThanOrEqual(20);
expect(enemy.attrInfo.defence).toBeGreaterThanOrEqual(20);
expect(enemy.attrInfo.speed).toBeGreaterThanOrEqual(0);
expect(enemy.attrInfo.critRate).toBeGreaterThanOrEqual(2);
expect(enemy.attrInfo.critDamage).toBeGreaterThanOrEqual(10);
});
});
});

describe('#envs()', () => {
it('should generate correct number of environments', () => {
const length = 5;
const { envs, galaxiesId } = Generate.envs(length);
expect(envs.length).toBe(length);
envs.forEach((env: Env) => {
expect(env.id.startsWith('env-')).toBe(true);
expect(env.name).toEqual(jasmine.any(String));
expect(Object.values(EnvType)).toContain(env.type);
expect(env.galaxiesId).toEqual(galaxiesId);
expect(env.levelMap).toEqual(jasmine.any(Object));
expect(env.weight).toBeGreaterThanOrEqual(0.75);
expect(env.maxEnergy).toBeGreaterThanOrEqual(80000);
});
});
});

describe('#getCharacterName()', () => {
it('should return a valid character name', () => {
const name = getCharacterName();
expect(name).toBeTruthy();
const parts = name.split('');
expect(parts[0]).toEqual(jasmine.any(String)); // Assuming the first part is the surname
expect(parts[1]).toEqual(jasmine.any(String)); // Assuming the second part is the name
if (parts.length === 3) {
expect(parts[2]).toEqual(jasmine.any(String)); // Assuming the third part is another name
}
});
});

describe('#getCharacterTalent()', () => {
it('should return a valid array of talents', () => {
const talents = getCharacterTalent(1);
expect(talents).toEqual(jasmine.any(Array));
expect(talents.length).toBe(1);
expect(talents[0]).toEqual(jasmine.any(Object));
});
});
});
43 changes: 30 additions & 13 deletions src/app/utils/generate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import NAMES from '../assets/data/names.json';
import SURNAMES from '../assets/data/surnames.json';
import TALENTS from '../assets/data/talents.json';
import WORLDS from '../assets/data/worlds.json';
import { BattleCharacter, Character, CharacterTalent, Env, EnvType, InitCharacter } from '../models';
import { BattleCharacter, CharacterTalent, Env, EnvType, InitCharacter } from '../models';

const SURNAMES_LEN = SURNAMES.length;
const NAMES_LEN = NAMES.length;
Expand All @@ -21,9 +21,11 @@ export class Generate {
*
* @param length 角色数量
* @returns 角色数组,包含每个角色的名字、性别、年龄和能力特质
*
* @todo 完善天赋体系及角色升级逻辑
* @body 丰富角色属性和计算逻辑,封装一个用于计算属性的工具类
*/
static characters(length: number): InitCharacter[] {
// @todo: 完善特质体系及相关逻辑
return Array.from({ length }, (_, i) => ({
id: `character-${uuidv4()}`,
baseInfo: {
Expand Down Expand Up @@ -73,9 +75,23 @@ export class Generate {
}));
}

/**
* 生成指定数量的环境对象,并分配到一个星系ID下
*
* 此方法用于创建一个包含多个环境对象的数组,这些环境对象共享同一个星系ID
* 它首先生成一个唯一的星系ID,然后根据指定的长度生成相应数量的环境对象
* 每个环境对象都具有随机生成的属性,如类型、等级映射、权重和最大能量等
*
* @param length 环境对象数组的长度,即要生成的环境对象数量
* @returns 返回一个包含两个属性的对象:
* - envs: 一个环境对象数组
* - galaxiesId: 所有环境对象所属的星系ID
*
* @todo 完善世界等级体系
* @body 等级体系随机选中一套预设,并完善预设等级数量不同时的其他参数
*/
static envs(length: number): { envs: Env[]; galaxiesId: string } {
const galaxiesId = generateId();
// @todo: 等级体系随机选中一套预设,并完善预设等级数量不同时的其他参数
const galaxiesId = `galaxie-${uuidv4()}`;
return {
envs: Array.from({ length }, (_, i) => {
const weight: number = Number((Math.random() * 0.5 + 0.75).toFixed(2));
Expand All @@ -98,10 +114,12 @@ export class Generate {
/**
* 获取一个随机的角色名字
* 该函数通过组合姓氏和名字来生成随机角色名字,有一定概率生成双名
* @todo: 随机生成名字需要根据性别产生名字
* @returns {string} 随机生成的角色名字
*
* @todo 随机生成名字需要根据性别产生名字
* @body 将名从原有的数组分为男名数组和女名数组
*/
function getCharacterName() {
export function getCharacterName() {
let name = '';
// 随机选择一个姓氏
name += SURNAMES[Math.floor(Math.random() * SURNAMES_LEN)];
Expand All @@ -115,17 +133,16 @@ function getCharacterName() {
}

/**
* 获取一个随机的角色特质
* @returns {string} 随机生成的角色特质
* 获取一个随机的角色天赋
* @returns {string} 随机生成的角色天赋
*
* @todo 完善天赋生成逻辑
* @body 设计天赋生成、使用及计算等功能,并完成开发
*/
function getCharacterTalent(length: number): CharacterTalent[] {
export function getCharacterTalent(length: number): CharacterTalent[] {
let talentList: Set<CharacterTalent> = new Set();
for (let index = 0; index < length; index++) {
talentList.add(TALENTS[Math.floor(Math.random() * TALENTS_LEN)]);
}
return Array.from(talentList);
}

export const generateId = () => {
return Math.random().toString(36).substring(2, 15) + Math.random().toString(36).substring(2, 15);
};

1 comment on commit 41365d9

@vercel
Copy link

@vercel vercel bot commented on 41365d9 Sep 15, 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-encaiks-projects.vercel.app
wanjie.vercel.app

Please sign in to comment.