Files
bl/modules/blazing/model/boss.go
2025-08-28 14:38:13 +00:00

198 lines
5.2 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package model
import (
"blazing/cool"
"encoding/json"
)
// ------------------------------
// 1. BOSS主表结构核心ID与关联
// ------------------------------
const TableNameBoss = "boss"
// Boss 主表仅存储核心索引和JSON数据
type Boss struct {
*cool.Model
PlanetID int32 `gorm:"not null;index:idx_boss_planet;comment:'所属星球/地图ID'" json:"planet_id"`
BossID int32 `gorm:"not null;comment:'BOSS序号同星球内唯一'" json:"boss_id"`
Data string `gorm:"type:text;not null;comment:'BOSS详细数据(JSON格式)'" json:"data"`
Galaxy string `gorm:"type:varchar(50);comment:'所属星系(如帕诺星系)'" json:"galaxy"`
}
// 联合唯一索引:确保星球+BOSS序号唯一
func (b *Boss) Indexes() map[string]string {
return map[string]string{
"idx_planet_boss_unique": "planet_id,boss_id",
}
}
// TableName 定义表名
func (*Boss) TableName() string {
return TableNameBoss
}
// GroupName 定义分组名
func (*Boss) GroupName() string {
return "default"
}
// GroupName 定义分组名
func (s *Boss) GetData() string {
return s.Data
}
// NewBoss 创建新的BOSS主表实例
func NewBoss() *Boss {
return &Boss{
Model: cool.NewModel(),
}
}
// ------------------------------
// 2. BOSS详细数据结构JSON存储
// ------------------------------
// BossData 封装BOSS所有详细属性用于JSON序列化
type BossData struct {
// 基础信息
EncyclopediaID int32 `json:"encyclopedia_id"` // BOSS图鉴ID
Desc string `json:"desc"` // BOSS名称描述
Level int32 `json:"level"` // 等级
Capturable bool `json:"capturable"` // 是否可捕捉
// 战斗属性
MaxHP int32 `json:"max_hp"` // 最大血量(-1使用默认公式)
Attack int32 `json:"attack"` // 攻击属性
Defense int32 `json:"defense"` // 防御属性
SpecialAttack int32 `json:"special_attack"` // 特攻属性
SpecialDefense int32 `json:"special_defense"` // 特防属性
Speed int32 `json:"speed"` // 速度属性
Definitely int32 `json:"definitely"` // 必定命中率(默认5)
Dodge int32 `json:"dodge"` // 闪避率(默认5)
Stat string `json:"stat"` // 开局属性变化(攻击,特攻,防御,特防,速度,命中)
// 技能列表
Skills []BossSkill `json:"skills"`
// 免疫效果
ImmunityEffectIDs []int32 `json:"immunity_effect_ids"` // 免疫效果ID列表
ImmunityBuffNames []string `json:"immunity_buff_names"` // 免疫Buff名称列表
// 特殊Buff
SpecialBuffs []SpecialBuff `json:"special_buffs"`
}
// BossSkill 技能详情结构
type BossSkill struct {
ID int32 `json:"id"` // 技能ID
Desc string `json:"desc"` // 技能描述
InfinityPP bool `json:"infinity_pp"` // 是否PP无限
}
// SpecialBuff 特殊Buff结构
type SpecialBuff struct {
Value string `json:"value"` // Buff标识(如boss://map314-boss0)
Params string `json:"params"` // 附加参数(空格分隔)
}
// ------------------------------
// 3. JSON序列化/反序列化工具方法
// ------------------------------
// ToJSON 将BossData转为JSON字符串
func (d *BossData) ToJSON() (string, error) {
data, err := json.Marshal(d)
if err != nil {
return "", err
}
return string(data), nil
}
// ParseBossData 从JSON字符串解析BossData
func ParseBossData(jsonStr string) (*BossData, error) {
var data BossData
if err := json.Unmarshal([]byte(jsonStr), &data); err != nil {
return nil, err
}
return &data, nil
}
// ------------------------------
// 4. 表初始化
// ------------------------------
func init() {
// 创建BOSS主表
cool.CreateTable(&Boss{})
}
// ------------------------------
// 5. 使用示例
// ------------------------------
/*
// 存储BOSS数据示例
func ExampleSaveBoss() {
// 1. 构建详细数据
skills := []BossSkill{
{ID: 10001, Desc: "撞击", InfinityPP: true},
{ID: 20020, Desc: "毒粉", InfinityPP: true},
}
immunityEffects := []int32{7, 36}
immunityBuffs := []string{"condition://brun", "condition://poison"}
specialBuffs := []SpecialBuff{
{Value: "boss://map59-boss0", Params: ""},
}
bossData := &BossData{
EncyclopediaID: 347,
Desc: "远古鱼龙",
Level: 100,
Capturable: false,
MaxHP: 3000,
Attack: 280,
SpecialAttack: 417,
Defense: 350,
SpecialDefense: 317,
Speed: 264,
Definitely: 45,
Stat: "0 0 0 0 0 0",
Skills: skills,
ImmunityEffectIDs: immunityEffects,
ImmunityBuffNames: immunityBuffs,
SpecialBuffs: specialBuffs,
}
// 2. 序列化为JSON
dataJSON, _ := bossData.ToJSON()
// 3. 存储到主表
boss := NewBoss()
boss.PlanetID = 59
boss.BossID = 0
boss.Galaxy = "卡兰星系"
boss.Data = dataJSON
// 实际项目中使用GORM保存
// db.Create(boss)
}
// 查询BOSS数据示例
func ExampleGetBoss() {
// 1. 从数据库查询主表
var boss Boss
// db.Where("planet_id = ? AND boss_id = ?", 59, 0).First(&boss)
// 2. 解析JSON数据
bossData, _ := ParseBossData(boss.Data)
// 3. 使用数据
println("BOSS名称:", bossData.Desc)
println("等级:", bossData.Level)
println("技能数量:", len(bossData.Skills))
}
*/