add: achievement, game

This commit is contained in:
Vick Scarlet
2026-08-16 20:06:16 +08:00
committed by 神戸小鳥
parent 32f703be0f
commit bcfd24bbba
10 changed files with 355 additions and 73 deletions
+33
View File
@@ -0,0 +1,33 @@
import type {
Achievement,
AchievementOpportunity,
} from '@remake/data/achievement'
import achievements from '@remake/data/achievement'
import type { GameState, ProfileState } from '@/state'
import { createFlatState } from './state'
import { check } from '@remake/condition'
import { produce } from 'immer'
import type { TriggerResult } from '@/game'
const OpportunityMap = Map.groupBy(
achievements.keys(),
a => achievements.get(a)!.opportunity,
)
export function trigger(
opportunity: AchievementOpportunity,
state: GameState,
profile: ProfileState,
): TriggerResult<Achievement['id']> {
const flatState = createFlatState(state, profile)
const triggers = OpportunityMap.get(opportunity)!.filter(a => {
if (state.achievements.has(a)) return false
if (profile.achievements.has(a)) return false
const { condition } = achievements.get(a)!
return check(flatState, condition)
})
const newState = produce(state, draft => {
draft.achievements = new Set([...draft.achievements, ...triggers])
})
return { state: newState, triggers }
}