Files
bl/common/utils/tomap.go
昔念 c9bc4be244 ```
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方法用于获取扭蛋奖励
- 修复宠物融合材料服务中的道具验证逻辑
```
2025-12-26 20:38:08 +08:00

87 lines
2.4 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 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 {
m := make(map[K]T, len(slice))
for _, v := range slice {
m[keyFunc(v)] = v
}
return m
}
// ToSliceMap converts a slice to a map with the keyFunc determining what the key of a value should be.
// Will append to the slice if the key already exists.
func ToSliceMap[T any, K comparable](slice []T, keyFunc func(T) K) map[K][]T {
m := make(map[K][]T, len(slice))
for _, v := range slice {
key := keyFunc(v)
m[key] = append(m[key], v)
}
return m
}
// 定义泛型约束:只允许数值类型(整数、浮点数等)
type Number interface {
int | int8 | int16 | int32 | int64 |
uint | uint8 | uint16 | uint32 | uint64 |
float32 | float64
}
// Max 泛型函数:接收两个同类型的 Number 参数,返回最大值
func Max[T Number](a, b T) T {
if a > b {
return a
}
return b
}
// Max 泛型函数:接收两个同类型的 Number 参数,返回最大值
func Min[T Number](a, b T) T {
if a < b {
return a
}
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
}