hrbattle 1.1.1 → 1.1.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hrbattle",
3
- "version": "1.1.1",
3
+ "version": "1.1.3",
4
4
  "description": "一个基于回合制战斗引擎,支持技能脚本、buff系统和效果解析。",
5
5
  "type": "module",
6
6
  "main": "dist/index.js",
@@ -1,139 +1,139 @@
1
- import type { BattleInitUnit, BuffConfig, SkillDefinition, SkillTemplate, UnitStats } from "../types.js";
2
-
3
- import designedBuffs from "./json/designed-buffs.json";
4
- import designedHeroSkillBooks from "./json/designed-hero-skill-books.json";
5
- import designedMonsterSkillBooks from "./json/designed-monster-skill-books.json";
6
- import designedRoster from "./json/designed-roster.json";
7
- import designedSkillTemplates from "./json/designed-skill-templates.json";
8
-
9
- type JsonHeroSkillBook = {
10
- heroId: string;
11
- heroName: string;
12
- element: BattleInitUnit["element"];
13
- role: string;
14
- skills: SkillDefinition[];
15
- };
16
-
17
- type JsonRoster = {
18
- heroStatProfiles: Record<string, UnitStats>;
19
- defaultLineup: {
20
- team1: string[];
21
- team2: string[];
22
- };
23
- };
24
-
25
- export interface BattleJsonConfigBundle {
26
- buffs: BuffConfig[];
27
- skillTemplates: SkillTemplate[];
28
- heroSkillBooks: JsonHeroSkillBook[];
29
- monsterSkillBooks: JsonHeroSkillBook[];
30
- roster: JsonRoster;
31
- }
32
-
33
- export function loadBattleJsonConfigBundle(): BattleJsonConfigBundle {
34
- return {
35
- buffs: designedBuffs as BuffConfig[],
36
- skillTemplates: designedSkillTemplates as SkillTemplate[],
37
- heroSkillBooks: designedHeroSkillBooks as JsonHeroSkillBook[],
38
- monsterSkillBooks: designedMonsterSkillBooks as JsonHeroSkillBook[],
39
- roster: designedRoster as JsonRoster
40
- };
41
- }
42
-
43
- export function validateBattleJsonConfigBundle(bundle: BattleJsonConfigBundle): void {
44
- const buffIds = new Set(bundle.buffs.map((item) => item.id));
45
- const templateIds = new Set(bundle.skillTemplates.map((item) => item.id));
46
-
47
- for (const hero of bundle.heroSkillBooks) {
48
- if (!bundle.roster.heroStatProfiles[hero.heroId]) {
49
- throw new Error(`missing-stat-profile:${hero.heroId}`);
50
- }
51
- for (const skill of hero.skills) {
52
- if (skill.configTemplateId && !templateIds.has(skill.configTemplateId)) {
53
- throw new Error(`missing-skill-template:${hero.heroId}:${skill.id}:${skill.configTemplateId}`);
54
- }
55
- }
56
- }
57
-
58
- for (const monster of bundle.monsterSkillBooks) {
59
- for (const skill of monster.skills) {
60
- if (skill.configTemplateId && !templateIds.has(skill.configTemplateId)) {
61
- throw new Error(`missing-skill-template:${monster.heroId}:${skill.id}:${skill.configTemplateId}`);
62
- }
63
- }
64
- }
65
-
66
- for (const template of bundle.skillTemplates) {
67
- for (const step of template.steps) {
68
- if (step.buffId && !buffIds.has(step.buffId)) {
69
- throw new Error(`missing-buff-config:${template.id}:${step.buffId}`);
70
- }
71
- }
72
- }
73
- }
74
-
75
- function cloneStats(stats: UnitStats): UnitStats {
76
- return { ...stats };
77
- }
78
-
79
- function buildUnitInstanceId(teamId: 1 | 2, position: 1 | 2 | 3 | 4, heroId: string): string {
80
- return `t${teamId}_p${position}_${heroId}`;
81
- }
82
-
83
- /** 英雄入参:可以是纯 heroId 字符串,也可以带技能等级覆盖 */
84
- export type HeroInput = string | {
85
- heroId: string;
86
- /** 技能等级覆盖,key 为 skillId,value 为等级数值 */
87
- skillLevels?: Record<string, number>;
88
- };
89
-
90
- function normalizeHeroInput(input: HeroInput): { heroId: string; skillLevels?: Record<string, number> } {
91
- if (typeof input === "string") return { heroId: input };
92
- return input;
93
- }
94
-
95
- export function buildBattleUnitsFromJsonBundle(
96
- bundle: BattleJsonConfigBundle,
97
- team1?: HeroInput[],
98
- team2?: HeroInput[]
99
- ): BattleInitUnit[] {
100
- const team1Inputs = team1 ?? bundle.roster.defaultLineup.team1;
101
- const team2Inputs = team2 ?? bundle.roster.defaultLineup.team2;
102
-
103
- const heroBookMap = new Map([
104
- ...bundle.heroSkillBooks.map((item) => [item.heroId, item] as const),
105
- ...bundle.monsterSkillBooks.map((item) => [item.heroId, item] as const),
106
- ]);
107
-
108
- const buildTeam = (teamId: 1 | 2, inputs: HeroInput[]): BattleInitUnit[] => {
109
- return inputs.map((input, index) => {
110
- const { heroId, skillLevels } = normalizeHeroInput(input);
111
- const heroBook = heroBookMap.get(heroId);
112
- if (!heroBook) {
113
- throw new Error(`missing-hero-skill-book:${heroId}`);
114
- }
115
- const position = (index + 1) as 1 | 2 | 3 | 4;
116
- const stats = bundle.roster.heroStatProfiles[heroId];
117
- if (!stats) {
118
- throw new Error(`missing-hero-stat-profile:${heroId}`);
119
- }
120
- return {
121
- id: buildUnitInstanceId(teamId, position, heroId),
122
- name: heroBook.heroName,
123
- teamId,
124
- position,
125
- element: heroBook.element,
126
- stats: cloneStats(stats),
127
- skills: heroBook.skills.map((skill) => {
128
- const cloned = { ...skill };
129
- if (skillLevels && skillLevels[skill.id] !== undefined) {
130
- cloned.level = skillLevels[skill.id];
131
- }
132
- return cloned;
133
- })
134
- };
135
- });
136
- };
137
-
138
- return [...buildTeam(1, team1Inputs), ...buildTeam(2, team2Inputs)];
139
- }
1
+ import type { BattleInitUnit, BuffConfig, SkillDefinition, SkillTemplate, UnitStats } from "../types.js";
2
+
3
+ import designedBuffs from "./json/designed-buffs.json";
4
+ import designedHeroSkillBooks from "./json/designed-hero-skill-books.json";
5
+ import designedMonsterSkillBooks from "./json/designed-monster-skill-books.json";
6
+ import designedRoster from "./json/designed-roster.json";
7
+ import designedSkillTemplates from "./json/designed-skill-templates.json";
8
+
9
+ type JsonHeroSkillBook = {
10
+ heroId: string;
11
+ heroName: string;
12
+ element: BattleInitUnit["element"];
13
+ role: string;
14
+ skills: SkillDefinition[];
15
+ };
16
+
17
+ type JsonRoster = {
18
+ heroStatProfiles: Record<string, UnitStats>;
19
+ defaultLineup: {
20
+ team1: string[];
21
+ team2: string[];
22
+ };
23
+ };
24
+
25
+ export interface BattleJsonConfigBundle {
26
+ buffs: BuffConfig[];
27
+ skillTemplates: SkillTemplate[];
28
+ heroSkillBooks: JsonHeroSkillBook[];
29
+ monsterSkillBooks: JsonHeroSkillBook[];
30
+ roster: JsonRoster;
31
+ }
32
+
33
+ export function loadBattleJsonConfigBundle(): BattleJsonConfigBundle {
34
+ return {
35
+ buffs: designedBuffs as BuffConfig[],
36
+ skillTemplates: designedSkillTemplates as SkillTemplate[],
37
+ heroSkillBooks: designedHeroSkillBooks as JsonHeroSkillBook[],
38
+ monsterSkillBooks: designedMonsterSkillBooks as JsonHeroSkillBook[],
39
+ roster: designedRoster as JsonRoster
40
+ };
41
+ }
42
+
43
+ export function validateBattleJsonConfigBundle(bundle: BattleJsonConfigBundle): void {
44
+ const buffIds = new Set(bundle.buffs.map((item) => item.id));
45
+ const templateIds = new Set(bundle.skillTemplates.map((item) => item.id));
46
+
47
+ for (const hero of bundle.heroSkillBooks) {
48
+ if (!bundle.roster.heroStatProfiles[hero.heroId]) {
49
+ throw new Error(`missing-stat-profile:${hero.heroId}`);
50
+ }
51
+ for (const skill of hero.skills) {
52
+ if (skill.configTemplateId && !templateIds.has(skill.configTemplateId)) {
53
+ throw new Error(`missing-skill-template:${hero.heroId}:${skill.id}:${skill.configTemplateId}`);
54
+ }
55
+ }
56
+ }
57
+
58
+ for (const monster of bundle.monsterSkillBooks) {
59
+ for (const skill of monster.skills) {
60
+ if (skill.configTemplateId && !templateIds.has(skill.configTemplateId)) {
61
+ throw new Error(`missing-skill-template:${monster.heroId}:${skill.id}:${skill.configTemplateId}`);
62
+ }
63
+ }
64
+ }
65
+
66
+ for (const template of bundle.skillTemplates) {
67
+ for (const step of template.steps) {
68
+ if (step.buffId && !buffIds.has(step.buffId)) {
69
+ throw new Error(`missing-buff-config:${template.id}:${step.buffId}`);
70
+ }
71
+ }
72
+ }
73
+ }
74
+
75
+ function cloneStats(stats: UnitStats): UnitStats {
76
+ return { ...stats };
77
+ }
78
+
79
+ function buildUnitInstanceId(teamId: 1 | 2, position: 1 | 2 | 3 | 4, heroId: string): string {
80
+ return `t${teamId}_p${position}_${heroId}`;
81
+ }
82
+
83
+ /** 英雄入参:可以是纯 heroId 字符串,也可以带技能等级覆盖 */
84
+ export type HeroInput = string | {
85
+ heroId: string;
86
+ /** 技能等级覆盖,key 为 skillId,value 为等级数值 */
87
+ skillLevels?: Record<string, number>;
88
+ };
89
+
90
+ function normalizeHeroInput(input: HeroInput): { heroId: string; skillLevels?: Record<string, number> } {
91
+ if (typeof input === "string") return { heroId: input };
92
+ return input;
93
+ }
94
+
95
+ export function buildBattleUnitsFromJsonBundle(
96
+ bundle: BattleJsonConfigBundle,
97
+ team1?: HeroInput[],
98
+ team2?: HeroInput[]
99
+ ): BattleInitUnit[] {
100
+ const team1Inputs = team1 ?? bundle.roster.defaultLineup.team1;
101
+ const team2Inputs = team2 ?? bundle.roster.defaultLineup.team2;
102
+
103
+ const heroBookMap = new Map([
104
+ ...bundle.heroSkillBooks.map((item) => [item.heroId, item] as const),
105
+ ...bundle.monsterSkillBooks.map((item) => [item.heroId, item] as const),
106
+ ]);
107
+
108
+ const buildTeam = (teamId: 1 | 2, inputs: HeroInput[]): BattleInitUnit[] => {
109
+ return inputs.map((input, index) => {
110
+ const { heroId, skillLevels } = normalizeHeroInput(input);
111
+ const heroBook = heroBookMap.get(heroId);
112
+ if (!heroBook) {
113
+ throw new Error(`missing-hero-skill-book:${heroId}`);
114
+ }
115
+ const position = (index + 1) as 1 | 2 | 3 | 4;
116
+ const stats = bundle.roster.heroStatProfiles[heroId];
117
+ if (!stats) {
118
+ throw new Error(`missing-hero-stat-profile:${heroId}`);
119
+ }
120
+ return {
121
+ id: buildUnitInstanceId(teamId, position, heroId),
122
+ name: heroBook.heroName,
123
+ teamId,
124
+ position,
125
+ element: heroBook.element,
126
+ stats: cloneStats(stats),
127
+ skills: heroBook.skills.map((skill) => {
128
+ const cloned = { ...skill };
129
+ if (skillLevels && skillLevels[skill.id] !== undefined) {
130
+ cloned.level = skillLevels[skill.id];
131
+ }
132
+ return cloned;
133
+ })
134
+ };
135
+ });
136
+ };
137
+
138
+ return [...buildTeam(1, team1Inputs), ...buildTeam(2, team2Inputs)];
139
+ }