Skip to content

Commit

Permalink
fix: 修复 activity 类型转换错误问题,增加案例目录 activity/internal/example
Browse files Browse the repository at this point in the history
  • Loading branch information
kercylan98 committed Nov 24, 2023
1 parent 605a308 commit 3a33947
Show file tree
Hide file tree
Showing 6 changed files with 61 additions and 7 deletions.
2 changes: 1 addition & 1 deletion game/activity/activity.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ func (slf *Activity[Type, ID]) refresh() {
slf.mutex.Lock()
defer slf.mutex.Unlock()
curr := time.Now()
if slf.state = slf.options.Tl.GetStateByTime(curr); slf.state == stateUpcoming {
if slf.state = slf.options.Tl.GetStateByTime(curr); slf.state == stateUpcoming || (slf.state == stateStarted && !slf.options.Tl.HasState(stateUpcoming)) {
ticker.StopTimer(slf.retentionKey)
slf.initializeData()
}
Expand Down
12 changes: 6 additions & 6 deletions game/activity/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,22 @@ import (
type none byte

// DefineNoneDataActivity 声明无数据的活动类型
func DefineNoneDataActivity[Type, ID generic.Basic](activityType Type) NoneDataActivityController[Type, ID, none, none, none] {
return regController(&Controller[Type, ID, none, none, none]{
func DefineNoneDataActivity[Type, ID generic.Basic](activityType Type) NoneDataActivityController[Type, ID, *none, none, *none] {
return regController(&Controller[Type, ID, *none, none, *none]{
t: activityType,
})
}

// DefineGlobalDataActivity 声明拥有全局数据的活动类型
func DefineGlobalDataActivity[Type, ID generic.Basic, Data any](activityType Type) GlobalDataActivityController[Type, ID, Data, none, none] {
return regController(&Controller[Type, ID, Data, none, none]{
func DefineGlobalDataActivity[Type, ID generic.Basic, Data any](activityType Type) GlobalDataActivityController[Type, ID, Data, none, *none] {
return regController(&Controller[Type, ID, Data, none, *none]{
t: activityType,
})
}

// DefineEntityDataActivity 声明拥有实体数据的活动类型
func DefineEntityDataActivity[Type, ID, EntityID generic.Basic, EntityData any](activityType Type) EntityDataActivityController[Type, ID, none, EntityID, EntityData] {
return regController(&Controller[Type, ID, none, EntityID, EntityData]{
func DefineEntityDataActivity[Type, ID, EntityID generic.Basic, EntityData any](activityType Type) EntityDataActivityController[Type, ID, *none, EntityID, EntityData] {
return regController(&Controller[Type, ID, *none, EntityID, EntityData]{
t: activityType,
})
}
Expand Down
24 changes: 24 additions & 0 deletions game/activity/internal/example/activities/activity.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package activities

import (
"github.com/kercylan98/minotaur/game/activity"
"github.com/kercylan98/minotaur/game/activity/internal/example/types"
"github.com/kercylan98/minotaur/utils/super"
"time"
)

var (
DemoActivity = activity.DefineEntityDataActivity[int, int, string, *types.DemoActivityData](1).InitializeEntityData(func(activityId int, entityId string, data *activity.EntityDataMeta[*types.DemoActivityData]) {
// 模拟数据库加载
_ = super.UnmarshalJSON([]byte(`{"last_new_day": "2021-01-01 00:00:00", "data": {"login_num": 3}}`), data)
})
)

func init() {
// 模拟配置加载活动
if err := activity.LoadOrRefreshActivity(1, 1, activity.NewOptions().
WithStartTime(time.Now().Add(time.Second*3)),
); err != nil {
panic(err)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package demoactivity

import (
"github.com/kercylan98/minotaur/game/activity"
"github.com/kercylan98/minotaur/game/activity/internal/example/activities"
"github.com/kercylan98/minotaur/utils/log"
)

func init() {
activity.RegStartedEvent(1, onActivityStart)
}

func onActivityStart(id int) {
log.Info("activity start", log.Int("id", id), log.Any("entity", activities.DemoActivity.GetEntityData(id, "demo_entity")))
}
10 changes: 10 additions & 0 deletions game/activity/internal/example/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package main

import (
_ "github.com/kercylan98/minotaur/game/activity/internal/example/activities/demoactivity"
"time"
)

func main() {
time.Sleep(time.Second * 5)
}
5 changes: 5 additions & 0 deletions game/activity/internal/example/types/player.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package types

type DemoActivityData struct {
LoginNum int `json:"login_num"` // 登录次数
}

0 comments on commit 3a33947

Please sign in to comment.