You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

52 lines
1.6 KiB
Go

package model
import "time"
// Achievement defines a static achievement template.
type Achievement struct {
ID string `json:"id"`
Title string `json:"title"`
Description string `json:"description"`
ConditionType string `json:"conditionType"` // level, kills, gold, elite_kills, loot_legendary, kills_no_death
ConditionValue int `json:"conditionValue"`
RewardType string `json:"rewardType"` // gold, potion, title
RewardAmount int `json:"rewardAmount"`
}
// HeroAchievement records that a hero has unlocked an achievement.
type HeroAchievement struct {
HeroID int64 `json:"heroId"`
AchievementID string `json:"achievementId"`
UnlockedAt time.Time `json:"unlockedAt"`
Achievement *Achievement `json:"achievement,omitempty"`
}
// AchievementView is the response struct combining an achievement definition
// with unlock status for a specific hero.
type AchievementView struct {
Achievement
Unlocked bool `json:"unlocked"`
UnlockedAt *time.Time `json:"unlockedAt,omitempty"`
}
// CheckAchievementCondition returns true if the hero's stats satisfy the
// achievement's unlock condition.
func CheckAchievementCondition(a *Achievement, hero *Hero) bool {
switch a.ConditionType {
case "level":
return hero.Level >= a.ConditionValue
case "kills":
return hero.TotalKills >= a.ConditionValue
case "gold":
return hero.Gold >= int64(a.ConditionValue)
case "elite_kills":
return hero.EliteKills >= a.ConditionValue
case "loot_legendary":
return hero.LegendaryDrops >= a.ConditionValue
case "kills_no_death":
return hero.KillsSinceDeath >= a.ConditionValue
default:
return false
}
}