feat(common/utils): 添加时间范围检查工具函数 添加了 IsCurrentTimeInRange 函数用于判断当前时间是否在指定的 HH:MM 时间区间内,支持当前日期的时间比较功能。 refactor(logic/controller): 重构 Boss 挑战逻辑并集成配置服务 - 集成 service 模块替代原有硬编码逻辑 - 重构 PlayerFightBoss 方法,使用新的配置数据结构 - 移除已废弃的 processMonID 函数和相关注释代码 refactor(logic/space): 优化地图 Boss 信息管理和天气系统 - 更新地图 Boss 数据
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"math/rand/v2"
|
||||
"time"
|
||||
|
||||
"github.com/gogf/gf/v2/util/gconv"
|
||||
"github.com/gogf/gf/v2/util/grand"
|
||||
@@ -122,3 +123,31 @@ func RandomByProbs[T any](natureSet []T, probs []string) (T, error) {
|
||||
// 3. 调用核心函数,复用概率计算逻辑
|
||||
return RandomByWeight(natureSet, probInts)
|
||||
}
|
||||
|
||||
// IsCurrentTimeInRange 判断当前时间是否在 startStr 和 endStr 表示的时间区间内(格式:HH:MM)
|
||||
// 返回值:true=在区间内,false=不在区间内,error=时间解析失败
|
||||
func IsCurrentTimeInRange(startStr, endStr string) (bool, error) {
|
||||
// 1. 解析开始和结束时间字符串为 time.Time 对象(日期用当前日期)
|
||||
now := time.Now()
|
||||
location := now.Location() // 使用当前时区,避免时区偏差
|
||||
|
||||
// 解析开始时间(HH:MM)
|
||||
startTime, err := time.ParseInLocation("15:04", startStr, location)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("解析开始时间 %s 失败:%w", startStr, err)
|
||||
}
|
||||
// 解析结束时间(HH:MM)
|
||||
endTime, err := time.ParseInLocation("15:04", endStr, location)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("解析结束时间 %s 失败:%w", endStr, err)
|
||||
}
|
||||
|
||||
// 2. 把开始/结束时间的日期替换为当前日期(只保留时分)
|
||||
startToday := time.Date(now.Year(), now.Month(), now.Day(),
|
||||
startTime.Hour(), startTime.Minute(), 0, 0, location)
|
||||
endToday := time.Date(now.Year(), now.Month(), now.Day(),
|
||||
endTime.Hour(), endTime.Minute(), 0, 0, location)
|
||||
|
||||
// 3. 比较当前时间是否在 [startToday, endToday] 区间内
|
||||
return now.After(startToday) && now.Before(endToday), nil
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user