@game-forge/gameplay-character 0.0.0-stage → 0.1.0
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/LICENSE +21 -0
- package/dist/sdk/character-action-effects.d.ts +4 -0
- package/dist/sdk/character-action-effects.js +24 -0
- package/dist/sdk/character-action-types.d.ts +11 -0
- package/dist/sdk/character-action-types.js +1 -0
- package/dist/sdk/character-registry.d.ts +2 -0
- package/dist/sdk/character-registry.js +85 -0
- package/dist/sdk/character-resource-effects.d.ts +6 -0
- package/dist/sdk/character-resource-effects.js +47 -0
- package/dist/sdk/character-resource-types.d.ts +12 -0
- package/dist/sdk/character-resource-types.js +1 -0
- package/dist/sdk/character-types.d.ts +52 -0
- package/dist/sdk/character-types.js +1 -0
- package/dist/sdk/index.d.ts +6 -0
- package/dist/sdk/index.js +3 -0
- package/package.json +24 -4
- package/README.md +0 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 zopenge
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import type { CreateGameplayCharacterActionRequestOptions, GameplayCharacterActionEffectKind, GameplayCharacterActionEffectPayload, GameplayCharacterActionRequest } from './character-action-types.js';
|
|
2
|
+
export declare const isGameplayCharacterActionEffectKind: (kind: string) => kind is GameplayCharacterActionEffectKind;
|
|
3
|
+
export declare const readGameplayCharacterActionEffectPayload: (payload: unknown) => GameplayCharacterActionEffectPayload;
|
|
4
|
+
export declare const createGameplayCharacterActionRequest: ({ metadata, payload }: CreateGameplayCharacterActionRequestOptions) => GameplayCharacterActionRequest;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
const characterActionEffectKinds = [
|
|
2
|
+
'character.play-action'
|
|
3
|
+
];
|
|
4
|
+
const isRecord = (value) => (typeof value === 'object' && value !== null && !Array.isArray(value));
|
|
5
|
+
const readOptionalString = (payload, fieldName) => {
|
|
6
|
+
const value = payload[fieldName];
|
|
7
|
+
const trimmedValue = typeof value === 'string' ? value.trim() : undefined;
|
|
8
|
+
return trimmedValue ? trimmedValue : undefined;
|
|
9
|
+
};
|
|
10
|
+
export const isGameplayCharacterActionEffectKind = (kind) => characterActionEffectKinds.some((effectKind) => effectKind === kind);
|
|
11
|
+
export const readGameplayCharacterActionEffectPayload = (payload) => {
|
|
12
|
+
if (!isRecord(payload)) {
|
|
13
|
+
throw new Error('Gameplay character action effect requires payload.actionName.');
|
|
14
|
+
}
|
|
15
|
+
const actionName = readOptionalString(payload, 'actionName');
|
|
16
|
+
if (!actionName) {
|
|
17
|
+
throw new Error('Gameplay character action effect requires payload.actionName.');
|
|
18
|
+
}
|
|
19
|
+
return { actionName };
|
|
20
|
+
};
|
|
21
|
+
export const createGameplayCharacterActionRequest = ({ metadata, payload }) => ({
|
|
22
|
+
...metadata,
|
|
23
|
+
...readGameplayCharacterActionEffectPayload(payload)
|
|
24
|
+
});
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { GameplayEffectRequestMetadata } from '@game-forge/gameplay-core';
|
|
2
|
+
export type GameplayCharacterActionEffectKind = 'character.play-action';
|
|
3
|
+
export interface GameplayCharacterActionEffectPayload {
|
|
4
|
+
readonly actionName: string;
|
|
5
|
+
}
|
|
6
|
+
export interface GameplayCharacterActionRequest extends GameplayCharacterActionEffectPayload, GameplayEffectRequestMetadata {
|
|
7
|
+
}
|
|
8
|
+
export interface CreateGameplayCharacterActionRequestOptions {
|
|
9
|
+
readonly metadata: GameplayEffectRequestMetadata;
|
|
10
|
+
readonly payload: unknown;
|
|
11
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
const compareText = (left, right) => left.localeCompare(right);
|
|
2
|
+
const assertNonBlank = (value, message) => {
|
|
3
|
+
if (!value.trim()) {
|
|
4
|
+
throw new Error(message);
|
|
5
|
+
}
|
|
6
|
+
};
|
|
7
|
+
const assertFiniteNumber = (value, message) => {
|
|
8
|
+
if (!Number.isFinite(value)) {
|
|
9
|
+
throw new Error(message);
|
|
10
|
+
}
|
|
11
|
+
};
|
|
12
|
+
const assertUniqueId = (id, knownIds, message) => {
|
|
13
|
+
assertNonBlank(id, message);
|
|
14
|
+
if (knownIds.has(id)) {
|
|
15
|
+
throw new Error(message);
|
|
16
|
+
}
|
|
17
|
+
knownIds.add(id);
|
|
18
|
+
};
|
|
19
|
+
const validateProfile = (profile, abilityIds) => {
|
|
20
|
+
const attributeIds = new Set();
|
|
21
|
+
const resourceIds = new Set();
|
|
22
|
+
const abilityLoadoutIds = new Set();
|
|
23
|
+
for (const attribute of profile.attributes ?? []) {
|
|
24
|
+
assertUniqueId(attribute.id, attributeIds, `Character ${profile.id} has duplicate attribute id: ${attribute.id}.`);
|
|
25
|
+
assertFiniteNumber(attribute.value, `Character ${profile.id} attribute ${attribute.id} value must be finite.`);
|
|
26
|
+
}
|
|
27
|
+
for (const resource of profile.resources ?? []) {
|
|
28
|
+
assertUniqueId(resource.id, resourceIds, `Character ${profile.id} has duplicate resource id: ${resource.id}.`);
|
|
29
|
+
assertFiniteNumber(resource.value, `Character ${profile.id} resource ${resource.id} value must be finite.`);
|
|
30
|
+
if (resource.max !== undefined) {
|
|
31
|
+
assertFiniteNumber(resource.max, `Character ${profile.id} resource ${resource.id} max must be finite.`);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
for (const abilityId of profile.abilityLoadout ?? []) {
|
|
35
|
+
assertUniqueId(abilityId, abilityLoadoutIds, `Character ${profile.id} has duplicate ability id: ${abilityId}.`);
|
|
36
|
+
if (abilityIds && !abilityIds.has(abilityId)) {
|
|
37
|
+
throw new Error(`Character ${profile.id} references unknown ability: ${abilityId}.`);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
const toAttributeRecord = (attributes) => Object.fromEntries((attributes ?? []).map((attribute) => [
|
|
42
|
+
attribute.id,
|
|
43
|
+
attribute.value
|
|
44
|
+
]));
|
|
45
|
+
const toResourceRecord = (resources) => Object.fromEntries((resources ?? []).map((resource) => [
|
|
46
|
+
resource.id,
|
|
47
|
+
{
|
|
48
|
+
...(resource.max === undefined ? {} : { max: resource.max }),
|
|
49
|
+
value: resource.value
|
|
50
|
+
}
|
|
51
|
+
]));
|
|
52
|
+
const readAbilityLoadout = (profile, options) => [...(options.abilityLoadout ?? profile.abilityLoadout ?? [])].sort(compareText);
|
|
53
|
+
const createStateFromProfile = (profile, options) => ({
|
|
54
|
+
abilityLoadout: readAbilityLoadout(profile, options),
|
|
55
|
+
attributes: toAttributeRecord(profile.attributes),
|
|
56
|
+
characterId: profile.id,
|
|
57
|
+
entityId: options.entityId,
|
|
58
|
+
ownerId: options.ownerId,
|
|
59
|
+
resources: toResourceRecord(profile.resources),
|
|
60
|
+
statusTags: [...(profile.statusTags ?? [])].sort(compareText),
|
|
61
|
+
tags: [...(profile.tags ?? [])].sort(compareText),
|
|
62
|
+
...(options.teamId ?? profile.teamId ? { teamId: options.teamId ?? profile.teamId } : {})
|
|
63
|
+
});
|
|
64
|
+
export const createCharacterRegistry = ({ abilityIds, profiles }) => {
|
|
65
|
+
const knownAbilityIds = abilityIds ? new Set(abilityIds) : undefined;
|
|
66
|
+
const profileIds = new Set();
|
|
67
|
+
const profilesById = new Map();
|
|
68
|
+
for (const profile of profiles) {
|
|
69
|
+
assertUniqueId(profile.id, profileIds, `Character registry has duplicate character id: ${profile.id}.`);
|
|
70
|
+
validateProfile(profile, knownAbilityIds);
|
|
71
|
+
profilesById.set(profile.id, profile);
|
|
72
|
+
}
|
|
73
|
+
return {
|
|
74
|
+
createState: (options) => {
|
|
75
|
+
const profile = profilesById.get(options.characterId);
|
|
76
|
+
if (!profile) {
|
|
77
|
+
throw new Error(`Unknown character profile: ${options.characterId}.`);
|
|
78
|
+
}
|
|
79
|
+
return createStateFromProfile(profile, options);
|
|
80
|
+
},
|
|
81
|
+
getProfile: (characterId) => profilesById.get(characterId),
|
|
82
|
+
hasAbility: (characterId, abilityId) => (profilesById.get(characterId)?.abilityLoadout?.some((candidate) => candidate === abilityId) ?? false),
|
|
83
|
+
listProfiles: () => [...profilesById.values()].sort((left, right) => compareText(left.id, right.id))
|
|
84
|
+
};
|
|
85
|
+
};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { CharacterState } from './character-types.js';
|
|
2
|
+
import type { CharacterModifyResourceRequest, CreateGameplayCharacterModifyResourceRequestOptions, GameplayCharacterModifyResourceEffectPayload, GameplayCharacterResourceEffectKind } from './character-resource-types.js';
|
|
3
|
+
export declare const isGameplayCharacterResourceEffectKind: (kind: string) => kind is GameplayCharacterResourceEffectKind;
|
|
4
|
+
export declare const readGameplayCharacterModifyResourceEffectPayload: (payload: unknown) => GameplayCharacterModifyResourceEffectPayload;
|
|
5
|
+
export declare const createGameplayCharacterModifyResourceRequest: ({ metadata, payload }: CreateGameplayCharacterModifyResourceRequestOptions) => CharacterModifyResourceRequest;
|
|
6
|
+
export declare const applyCharacterResourceChange: (state: CharacterState, request: CharacterModifyResourceRequest) => CharacterState;
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
const characterResourceEffectKinds = [
|
|
2
|
+
'character.modify-resource'
|
|
3
|
+
];
|
|
4
|
+
const isRecord = (value) => (typeof value === 'object' && value !== null && !Array.isArray(value));
|
|
5
|
+
const readOptionalString = (payload, fieldName) => {
|
|
6
|
+
const value = payload[fieldName];
|
|
7
|
+
const trimmedValue = typeof value === 'string' ? value.trim() : undefined;
|
|
8
|
+
return trimmedValue ? trimmedValue : undefined;
|
|
9
|
+
};
|
|
10
|
+
const clampResourceValue = (value, resource) => Math.min(resource.max ?? value, Math.max(0, value));
|
|
11
|
+
export const isGameplayCharacterResourceEffectKind = (kind) => characterResourceEffectKinds.some((effectKind) => effectKind === kind);
|
|
12
|
+
export const readGameplayCharacterModifyResourceEffectPayload = (payload) => {
|
|
13
|
+
if (!isRecord(payload)) {
|
|
14
|
+
throw new Error('Gameplay character resource effect requires payload.resourceId.');
|
|
15
|
+
}
|
|
16
|
+
const resourceId = readOptionalString(payload, 'resourceId');
|
|
17
|
+
if (!resourceId) {
|
|
18
|
+
throw new Error('Gameplay character resource effect requires payload.resourceId.');
|
|
19
|
+
}
|
|
20
|
+
if (typeof payload.delta !== 'number' || !Number.isFinite(payload.delta)) {
|
|
21
|
+
throw new Error('Gameplay character resource effect requires finite payload.delta.');
|
|
22
|
+
}
|
|
23
|
+
return {
|
|
24
|
+
delta: payload.delta,
|
|
25
|
+
resourceId
|
|
26
|
+
};
|
|
27
|
+
};
|
|
28
|
+
export const createGameplayCharacterModifyResourceRequest = ({ metadata, payload }) => ({
|
|
29
|
+
...metadata,
|
|
30
|
+
...readGameplayCharacterModifyResourceEffectPayload(payload)
|
|
31
|
+
});
|
|
32
|
+
export const applyCharacterResourceChange = (state, request) => {
|
|
33
|
+
const resource = state.resources[request.resourceId];
|
|
34
|
+
if (!resource) {
|
|
35
|
+
throw new Error(`Character ${state.characterId} does not have resource: ${request.resourceId}.`);
|
|
36
|
+
}
|
|
37
|
+
return {
|
|
38
|
+
...state,
|
|
39
|
+
resources: {
|
|
40
|
+
...state.resources,
|
|
41
|
+
[request.resourceId]: {
|
|
42
|
+
...resource,
|
|
43
|
+
value: clampResourceValue(resource.value + request.delta, resource)
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { GameplayEffectRequestMetadata } from '@game-forge/gameplay-core';
|
|
2
|
+
export type GameplayCharacterResourceEffectKind = 'character.modify-resource';
|
|
3
|
+
export interface GameplayCharacterModifyResourceEffectPayload {
|
|
4
|
+
readonly delta: number;
|
|
5
|
+
readonly resourceId: string;
|
|
6
|
+
}
|
|
7
|
+
export interface CharacterModifyResourceRequest extends GameplayCharacterModifyResourceEffectPayload, GameplayEffectRequestMetadata {
|
|
8
|
+
}
|
|
9
|
+
export interface CreateGameplayCharacterModifyResourceRequestOptions {
|
|
10
|
+
readonly metadata: GameplayEffectRequestMetadata;
|
|
11
|
+
readonly payload: unknown;
|
|
12
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import type { GameplayEntityId, GameplayTeamId } from '@game-forge/gameplay-core';
|
|
2
|
+
export interface CharacterAttribute {
|
|
3
|
+
readonly id: string;
|
|
4
|
+
readonly value: number;
|
|
5
|
+
}
|
|
6
|
+
export interface CharacterResource {
|
|
7
|
+
readonly id: string;
|
|
8
|
+
readonly max?: number;
|
|
9
|
+
readonly value: number;
|
|
10
|
+
}
|
|
11
|
+
export interface CharacterProfile {
|
|
12
|
+
readonly abilityLoadout?: readonly string[];
|
|
13
|
+
readonly attributes?: readonly CharacterAttribute[];
|
|
14
|
+
readonly id: string;
|
|
15
|
+
readonly name?: string;
|
|
16
|
+
readonly resources?: readonly CharacterResource[];
|
|
17
|
+
readonly statusTags?: readonly string[];
|
|
18
|
+
readonly tags?: readonly string[];
|
|
19
|
+
readonly teamId?: GameplayTeamId;
|
|
20
|
+
}
|
|
21
|
+
export interface CharacterStateResource {
|
|
22
|
+
readonly max?: number;
|
|
23
|
+
readonly value: number;
|
|
24
|
+
}
|
|
25
|
+
export interface CharacterState {
|
|
26
|
+
readonly abilityLoadout: readonly string[];
|
|
27
|
+
readonly attributes: Readonly<Record<string, number>>;
|
|
28
|
+
readonly characterId: string;
|
|
29
|
+
readonly entityId: GameplayEntityId;
|
|
30
|
+
readonly ownerId: string;
|
|
31
|
+
readonly resources: Readonly<Record<string, CharacterStateResource>>;
|
|
32
|
+
readonly statusTags: readonly string[];
|
|
33
|
+
readonly tags: readonly string[];
|
|
34
|
+
readonly teamId?: GameplayTeamId;
|
|
35
|
+
}
|
|
36
|
+
export interface CreateCharacterStateOptions {
|
|
37
|
+
readonly abilityLoadout?: readonly string[];
|
|
38
|
+
readonly characterId: string;
|
|
39
|
+
readonly entityId: GameplayEntityId;
|
|
40
|
+
readonly ownerId: string;
|
|
41
|
+
readonly teamId?: GameplayTeamId;
|
|
42
|
+
}
|
|
43
|
+
export interface CharacterRegistryOptions {
|
|
44
|
+
readonly abilityIds?: readonly string[];
|
|
45
|
+
readonly profiles: readonly CharacterProfile[];
|
|
46
|
+
}
|
|
47
|
+
export interface CharacterRegistry {
|
|
48
|
+
createState(options: CreateCharacterStateOptions): CharacterState;
|
|
49
|
+
getProfile(characterId: string): CharacterProfile | undefined;
|
|
50
|
+
hasAbility(characterId: string, abilityId: string): boolean;
|
|
51
|
+
listProfiles(): readonly CharacterProfile[];
|
|
52
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
export * from './character-registry.js';
|
|
2
|
+
export * from './character-action-effects.js';
|
|
3
|
+
export * from './character-resource-effects.js';
|
|
4
|
+
export type * from './character-action-types.js';
|
|
5
|
+
export type * from './character-resource-types.js';
|
|
6
|
+
export type * from './character-types.js';
|
package/package.json
CHANGED
|
@@ -1,6 +1,26 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@game-forge/gameplay-character",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"type": "module",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "https://github.com/zopenge/game-forge.git"
|
|
8
|
+
},
|
|
9
|
+
"publishConfig": {
|
|
10
|
+
"registry": "https://registry.npmjs.org",
|
|
11
|
+
"access": "public",
|
|
12
|
+
"provenance": false
|
|
13
|
+
},
|
|
14
|
+
"license": "MIT",
|
|
15
|
+
"main": "./dist/sdk/index.js",
|
|
16
|
+
"types": "./dist/sdk/index.d.ts",
|
|
17
|
+
"exports": {
|
|
18
|
+
".": {
|
|
19
|
+
"types": "./dist/sdk/index.d.ts",
|
|
20
|
+
"import": "./dist/sdk/index.js"
|
|
21
|
+
}
|
|
22
|
+
},
|
|
23
|
+
"dependencies": {
|
|
24
|
+
"@game-forge/gameplay-core": "0.1.0"
|
|
25
|
+
}
|
|
26
|
+
}
|
package/README.md
DELETED