feat: 添加ItemInfo结构体并重构抽蛋和任务系统

- 在common/data/color.go中添加ItemInfo结构体用于表示发放物品的信息
- 在common/utils/tomap.go中添加RandomSlice泛型函数用于从切片中随机选取元素
- 重构action_egg.go中的EggGamePlay功能,实现抽蛋逻辑和物品发放
- 更新fight_boss.go中使用新的ItemInfo结构体替换旧的model.ItemInfo
- 修改user_talk.go中获取物品数量的逻辑
- 更新user_task.go中任务完成逻辑使用新的ItemInfo结构体
- 在egg.go中更新抽蛋结果结构体使用ItemInfo
- 更新战斗奖励结构体使用ItemInfo
- 在player.go中添加学习力道具处理逻辑
- 重构任务系统使用新的ItemInfo结构体
- 移除旧的model.ItemInfo定义
- 更新宠物奖励配置模型添加成长值等字段
- 实现GetEgg方法用于获取扭蛋奖励
- 修复宠物融合材料服务中的道具验证逻辑
```
This commit is contained in:
2025-12-26 20:38:08 +08:00
parent b8d6772256
commit c9bc4be244
18 changed files with 242 additions and 134 deletions

View File

@@ -48,3 +48,10 @@ type GlowFilter struct {
Knockout bool `json:"knockout,omitempty"`
ColorMatrixFilter [20]float32 `json:"matrix,omitempty"`
}
// ItemInfo
// 用于表示发放物品的信息
type ItemInfo struct {
ItemId uint32 `json:"itemId" description:"发放物品ID"` // 发放物品ID
ItemCnt uint32 `json:"itemCount" description:"发放物品的数量"` // 发放物品的数量,
}

View File

@@ -1,5 +1,7 @@
package utils
import "github.com/gogf/gf/v2/util/grand"
// ToMap converts a slice to a map with the keyFunc determining what the key of a value should be.
// Will override any double values.
func ToMap[T any, K comparable](slice []T, keyFunc func(T) K) map[K]T {
@@ -35,6 +37,7 @@ func Max[T Number](a, b T) T {
}
return b
}
// Max 泛型函数:接收两个同类型的 Number 参数,返回最大值
func Min[T Number](a, b T) T {
if a < b {
@@ -42,3 +45,42 @@ func Min[T Number](a, b T) T {
}
return b
}
// RandomSlice 泛型函数从任意类型的切片中随机选取n个不重复元素
// T: 泛型类型参数any表示兼容任意类型
// slice: 源切片(任意类型)
// n: 要选取的元素数量
// 返回值: 与源切片同类型的新切片包含随机选取的n个元素
func RandomSlice[T any](slice []T, n int) []T {
// 边界条件1n≤0 或 源切片为空 → 返回空切片
if n <= 0 || len(slice) == 0 {
return []T{}
}
// 边界条件2n≥源切片长度 → 返回源切片的拷贝(避免修改原切片)
if n >= len(slice) {
copySlice := make([]T, len(slice))
copy(copySlice, slice)
return copySlice
}
// 步骤1生成源切片的索引切片
indices := make([]int, len(slice))
for i := range indices {
indices[i] = i
}
// 步骤2Fisher-Yates洗牌算法打乱索引保证随机性均匀
for i := len(indices) - 1; i > 0; i-- {
j := grand.Intn(i + 1) // 生成0~i的随机数
indices[i], indices[j] = indices[j], indices[i]
}
// 步骤3取前n个打乱后的索引映射为源切片的元素
result := make([]T, n)
for i := 0; i < n; i++ {
result[i] = slice[indices[i]]
}
return result
}