refactor: 移除 slice 包和 hash 包,新增 listings、mappings 包存放数组、切片、映射等数据结构,原 slice、hash 包中的工具函数迁移至 collection 包,与 sher 包合并并移除 sher 包。完善 collection 包测试用例
This commit is contained in:
@@ -2,8 +2,8 @@ package activity
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/kercylan98/minotaur/utils/collection"
|
||||
"github.com/kercylan98/minotaur/utils/generic"
|
||||
"github.com/kercylan98/minotaur/utils/hash"
|
||||
"github.com/kercylan98/minotaur/utils/times"
|
||||
"reflect"
|
||||
"time"
|
||||
@@ -94,7 +94,7 @@ func regController[Type, ID generic.Basic, Data any, EntityID generic.Basic, Ent
|
||||
// 实体数据加载器
|
||||
activityEntityDataLoader = append(activityEntityDataLoader, func(handler func(activityType any, activityId any, entityId any, data any)) {
|
||||
controller.mutex.RLock()
|
||||
entities := hash.Copy(controller.entityData[activityId])
|
||||
entities := collection.CloneMap(controller.entityData[activityId])
|
||||
controller.mutex.RUnlock()
|
||||
for entityId, data := range entities {
|
||||
handler(controller.t, activityId, entityId, data)
|
||||
|
||||
@@ -2,9 +2,10 @@ package activity
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"github.com/kercylan98/minotaur/utils/collection"
|
||||
listings2 "github.com/kercylan98/minotaur/utils/collection/listings"
|
||||
"github.com/kercylan98/minotaur/utils/generic"
|
||||
"github.com/kercylan98/minotaur/utils/log"
|
||||
"github.com/kercylan98/minotaur/utils/slice"
|
||||
"github.com/kercylan98/minotaur/utils/timer"
|
||||
"github.com/kercylan98/minotaur/utils/times"
|
||||
"reflect"
|
||||
@@ -21,28 +22,28 @@ type (
|
||||
)
|
||||
|
||||
var (
|
||||
upcomingEventHandlers map[any]*slice.Priority[func(activityId any)] // 即将开始的活动事件处理器
|
||||
startedEventHandlers map[any]*slice.Priority[func(activityId any)] // 活动开始事件处理器
|
||||
endedEventHandlers map[any]*slice.Priority[func(activityId any)] // 活动结束事件处理器
|
||||
extShowStartedEventHandlers map[any]*slice.Priority[func(activityId any)] // 活动结束后延长展示开始事件处理器
|
||||
extShowEndedEventHandlers map[any]*slice.Priority[func(activityId any)] // 活动结束后延长展示结束事件处理器
|
||||
newDayEventHandlers map[any]*slice.Priority[func(activityId any)] // 新的一天事件处理器
|
||||
upcomingEventHandlers map[any]*listings2.PrioritySlice[func(activityId any)] // 即将开始的活动事件处理器
|
||||
startedEventHandlers map[any]*listings2.PrioritySlice[func(activityId any)] // 活动开始事件处理器
|
||||
endedEventHandlers map[any]*listings2.PrioritySlice[func(activityId any)] // 活动结束事件处理器
|
||||
extShowStartedEventHandlers map[any]*listings2.PrioritySlice[func(activityId any)] // 活动结束后延长展示开始事件处理器
|
||||
extShowEndedEventHandlers map[any]*listings2.PrioritySlice[func(activityId any)] // 活动结束后延长展示结束事件处理器
|
||||
newDayEventHandlers map[any]*listings2.PrioritySlice[func(activityId any)] // 新的一天事件处理器
|
||||
)
|
||||
|
||||
func init() {
|
||||
upcomingEventHandlers = make(map[any]*slice.Priority[func(activityId any)])
|
||||
startedEventHandlers = make(map[any]*slice.Priority[func(activityId any)])
|
||||
endedEventHandlers = make(map[any]*slice.Priority[func(activityId any)])
|
||||
extShowStartedEventHandlers = make(map[any]*slice.Priority[func(activityId any)])
|
||||
extShowEndedEventHandlers = make(map[any]*slice.Priority[func(activityId any)])
|
||||
newDayEventHandlers = make(map[any]*slice.Priority[func(activityId any)])
|
||||
upcomingEventHandlers = make(map[any]*listings2.PrioritySlice[func(activityId any)])
|
||||
startedEventHandlers = make(map[any]*listings2.PrioritySlice[func(activityId any)])
|
||||
endedEventHandlers = make(map[any]*listings2.PrioritySlice[func(activityId any)])
|
||||
extShowStartedEventHandlers = make(map[any]*listings2.PrioritySlice[func(activityId any)])
|
||||
extShowEndedEventHandlers = make(map[any]*listings2.PrioritySlice[func(activityId any)])
|
||||
newDayEventHandlers = make(map[any]*listings2.PrioritySlice[func(activityId any)])
|
||||
}
|
||||
|
||||
// RegUpcomingEvent 注册即将开始的活动事件处理器
|
||||
func RegUpcomingEvent[Type, ID generic.Basic](activityType Type, handler UpcomingEventHandler[ID], priority ...int) {
|
||||
handlers, exist := upcomingEventHandlers[activityType]
|
||||
if !exist {
|
||||
handlers = slice.NewPriority[func(activityId any)]()
|
||||
handlers = listings2.NewPrioritySlice[func(activityId any)]()
|
||||
upcomingEventHandlers[activityType] = handlers
|
||||
}
|
||||
handlers.Append(func(activityId any) {
|
||||
@@ -50,7 +51,7 @@ func RegUpcomingEvent[Type, ID generic.Basic](activityType Type, handler Upcomin
|
||||
return
|
||||
}
|
||||
handler(activityId.(ID))
|
||||
}, slice.GetValue(priority, 0))
|
||||
}, collection.FindFirstOrDefaultInSlice(priority, 0))
|
||||
}
|
||||
|
||||
// OnUpcomingEvent 即将开始的活动事件
|
||||
@@ -75,7 +76,7 @@ func OnUpcomingEvent[Type, ID generic.Basic](activity *Activity[Type, ID]) {
|
||||
func RegStartedEvent[Type, ID generic.Basic](activityType Type, handler StartedEventHandler[ID], priority ...int) {
|
||||
handlers, exist := startedEventHandlers[activityType]
|
||||
if !exist {
|
||||
handlers = slice.NewPriority[func(activityId any)]()
|
||||
handlers = listings2.NewPrioritySlice[func(activityId any)]()
|
||||
startedEventHandlers[activityType] = handlers
|
||||
}
|
||||
handlers.Append(func(activityId any) {
|
||||
@@ -83,7 +84,7 @@ func RegStartedEvent[Type, ID generic.Basic](activityType Type, handler StartedE
|
||||
return
|
||||
}
|
||||
handler(activityId.(ID))
|
||||
}, slice.GetValue(priority, 0))
|
||||
}, collection.FindFirstOrDefaultInSlice(priority, 0))
|
||||
}
|
||||
|
||||
// OnStartedEvent 活动开始事件
|
||||
@@ -116,7 +117,7 @@ func OnStartedEvent[Type, ID generic.Basic](activity *Activity[Type, ID]) {
|
||||
func RegEndedEvent[Type, ID generic.Basic](activityType Type, handler EndedEventHandler[ID], priority ...int) {
|
||||
handlers, exist := endedEventHandlers[activityType]
|
||||
if !exist {
|
||||
handlers = slice.NewPriority[func(activityId any)]()
|
||||
handlers = listings2.NewPrioritySlice[func(activityId any)]()
|
||||
endedEventHandlers[activityType] = handlers
|
||||
}
|
||||
handlers.Append(func(activityId any) {
|
||||
@@ -124,7 +125,7 @@ func RegEndedEvent[Type, ID generic.Basic](activityType Type, handler EndedEvent
|
||||
return
|
||||
}
|
||||
handler(activityId.(ID))
|
||||
}, slice.GetValue(priority, 0))
|
||||
}, collection.FindFirstOrDefaultInSlice(priority, 0))
|
||||
}
|
||||
|
||||
// OnEndedEvent 活动结束事件
|
||||
@@ -149,7 +150,7 @@ func OnEndedEvent[Type, ID generic.Basic](activity *Activity[Type, ID]) {
|
||||
func RegExtendedShowStartedEvent[Type, ID generic.Basic](activityType Type, handler ExtendedShowStartedEventHandler[ID], priority ...int) {
|
||||
handlers, exist := extShowStartedEventHandlers[activityType]
|
||||
if !exist {
|
||||
handlers = slice.NewPriority[func(activityId any)]()
|
||||
handlers = listings2.NewPrioritySlice[func(activityId any)]()
|
||||
extShowStartedEventHandlers[activityType] = handlers
|
||||
}
|
||||
handlers.Append(func(activityId any) {
|
||||
@@ -157,7 +158,7 @@ func RegExtendedShowStartedEvent[Type, ID generic.Basic](activityType Type, hand
|
||||
return
|
||||
}
|
||||
handler(activityId.(ID))
|
||||
}, slice.GetValue(priority, 0))
|
||||
}, collection.FindFirstOrDefaultInSlice(priority, 0))
|
||||
}
|
||||
|
||||
// OnExtendedShowStartedEvent 活动结束后延长展示开始事件
|
||||
@@ -182,7 +183,7 @@ func OnExtendedShowStartedEvent[Type, ID generic.Basic](activity *Activity[Type,
|
||||
func RegExtendedShowEndedEvent[Type, ID generic.Basic](activityType Type, handler ExtendedShowEndedEventHandler[ID], priority ...int) {
|
||||
handlers, exist := extShowEndedEventHandlers[activityType]
|
||||
if !exist {
|
||||
handlers = slice.NewPriority[func(activityId any)]()
|
||||
handlers = listings2.NewPrioritySlice[func(activityId any)]()
|
||||
extShowEndedEventHandlers[activityType] = handlers
|
||||
}
|
||||
handlers.Append(func(activityId any) {
|
||||
@@ -190,7 +191,7 @@ func RegExtendedShowEndedEvent[Type, ID generic.Basic](activityType Type, handle
|
||||
return
|
||||
}
|
||||
handler(activityId.(ID))
|
||||
}, slice.GetValue(priority, 0))
|
||||
}, collection.FindFirstOrDefaultInSlice(priority, 0))
|
||||
}
|
||||
|
||||
// OnExtendedShowEndedEvent 活动结束后延长展示结束事件
|
||||
@@ -215,7 +216,7 @@ func OnExtendedShowEndedEvent[Type, ID generic.Basic](activity *Activity[Type, I
|
||||
func RegNewDayEvent[Type, ID generic.Basic](activityType Type, handler NewDayEventHandler[ID], priority ...int) {
|
||||
handlers, exist := newDayEventHandlers[activityType]
|
||||
if !exist {
|
||||
handlers = slice.NewPriority[func(activityId any)]()
|
||||
handlers = listings2.NewPrioritySlice[func(activityId any)]()
|
||||
newDayEventHandlers[activityType] = handlers
|
||||
}
|
||||
handlers.Append(func(activityId any) {
|
||||
@@ -223,7 +224,7 @@ func RegNewDayEvent[Type, ID generic.Basic](activityType Type, handler NewDayEve
|
||||
return
|
||||
}
|
||||
handler(activityId.(ID))
|
||||
}, slice.GetValue(priority, 0))
|
||||
}, collection.FindFirstOrDefaultInSlice(priority, 0))
|
||||
}
|
||||
|
||||
// OnNewDayEvent 新的一天事件
|
||||
|
||||
@@ -1,9 +1,8 @@
|
||||
package space
|
||||
|
||||
import (
|
||||
"github.com/kercylan98/minotaur/utils/collection"
|
||||
"github.com/kercylan98/minotaur/utils/generic"
|
||||
"github.com/kercylan98/minotaur/utils/hash"
|
||||
"github.com/kercylan98/minotaur/utils/slice"
|
||||
"sync"
|
||||
)
|
||||
|
||||
@@ -201,7 +200,7 @@ func (rc *RoomController[EntityID, RoomID, Entity, Room]) GetNotEmptySeat() []in
|
||||
// GetEmptySeat 获取空座位
|
||||
// - 空座位需要在有对象离开座位后才可能出现
|
||||
func (rc *RoomController[EntityID, RoomID, Entity, Room]) GetEmptySeat() []int {
|
||||
return slice.Copy(rc.vacancy)
|
||||
return collection.CloneSlice(rc.vacancy)
|
||||
}
|
||||
|
||||
// HasSeat 判断是否有座位
|
||||
@@ -291,7 +290,7 @@ func (rc *RoomController[EntityID, RoomID, Entity, Room]) GetRoom() Room {
|
||||
func (rc *RoomController[EntityID, RoomID, Entity, Room]) GetEntities() map[EntityID]Entity {
|
||||
rc.entitiesRWMutex.RLock()
|
||||
defer rc.entitiesRWMutex.RUnlock()
|
||||
return hash.Copy(rc.entities)
|
||||
return collection.CloneMap(rc.entities)
|
||||
}
|
||||
|
||||
// HasEntity 判断是否有实体
|
||||
@@ -321,7 +320,7 @@ func (rc *RoomController[EntityID, RoomID, Entity, Room]) GetEntityExist(id Enti
|
||||
func (rc *RoomController[EntityID, RoomID, Entity, Room]) GetEntityIDs() []EntityID {
|
||||
rc.entitiesRWMutex.RLock()
|
||||
defer rc.entitiesRWMutex.RUnlock()
|
||||
return hash.KeyToSlice(rc.entities)
|
||||
return collection.ConvertMapKeysToSlice(rc.entities)
|
||||
}
|
||||
|
||||
// GetEntityCount 获取实体数量
|
||||
@@ -454,7 +453,7 @@ func (rc *RoomController[EntityID, RoomID, Entity, Room]) GetRoomID() RoomID {
|
||||
// Broadcast 广播,该函数会将所有房间中满足 conditions 的对象传入 handler 中进行处理
|
||||
func (rc *RoomController[EntityID, RoomID, Entity, Room]) Broadcast(handler func(Entity), conditions ...func(Entity) bool) {
|
||||
rc.entitiesRWMutex.RLock()
|
||||
entities := hash.Copy(rc.entities)
|
||||
entities := collection.CloneMap(rc.entities)
|
||||
rc.entitiesRWMutex.RUnlock()
|
||||
for _, entity := range entities {
|
||||
for _, condition := range conditions {
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
package space
|
||||
|
||||
import (
|
||||
"github.com/kercylan98/minotaur/utils/collection"
|
||||
"github.com/kercylan98/minotaur/utils/generic"
|
||||
"github.com/kercylan98/minotaur/utils/hash"
|
||||
"sync"
|
||||
)
|
||||
|
||||
@@ -54,7 +54,7 @@ func (rm *RoomManager[EntityID, RoomID, Entity, Room]) GetRoom(id RoomID) *RoomC
|
||||
func (rm *RoomManager[EntityID, RoomID, Entity, Room]) GetRooms() map[RoomID]*RoomController[EntityID, RoomID, Entity, Room] {
|
||||
rm.roomsRWMutex.RLock()
|
||||
defer rm.roomsRWMutex.RUnlock()
|
||||
return hash.Copy(rm.rooms)
|
||||
return collection.CloneMap(rm.rooms)
|
||||
}
|
||||
|
||||
// GetRoomCount 获取房间管理器接管的房间数量
|
||||
@@ -68,13 +68,13 @@ func (rm *RoomManager[EntityID, RoomID, Entity, Room]) GetRoomCount() int {
|
||||
func (rm *RoomManager[EntityID, RoomID, Entity, Room]) GetRoomIDs() []RoomID {
|
||||
rm.roomsRWMutex.RLock()
|
||||
defer rm.roomsRWMutex.RUnlock()
|
||||
return hash.KeyToSlice(rm.rooms)
|
||||
return collection.ConvertMapKeysToSlice(rm.rooms)
|
||||
}
|
||||
|
||||
// HasEntity 判断特定对象是否在任一房间中,当对象不在任一房间中时将返回 false
|
||||
func (rm *RoomManager[EntityID, RoomID, Entity, Room]) HasEntity(entityId EntityID) bool {
|
||||
rm.roomsRWMutex.RLock()
|
||||
rooms := hash.Copy(rm.rooms)
|
||||
rooms := collection.CloneMap(rm.rooms)
|
||||
rm.roomsRWMutex.RUnlock()
|
||||
for _, room := range rooms {
|
||||
if room.HasEntity(entityId) {
|
||||
@@ -88,7 +88,7 @@ func (rm *RoomManager[EntityID, RoomID, Entity, Room]) HasEntity(entityId Entity
|
||||
// - 由于一个对象可能在多个房间中,因此返回值为 map 类型
|
||||
func (rm *RoomManager[EntityID, RoomID, Entity, Room]) GetEntityRooms(entityId EntityID) map[RoomID]*RoomController[EntityID, RoomID, Entity, Room] {
|
||||
rm.roomsRWMutex.RLock()
|
||||
rooms := hash.Copy(rm.rooms)
|
||||
rooms := collection.CloneMap(rm.rooms)
|
||||
rm.roomsRWMutex.RUnlock()
|
||||
var result = make(map[RoomID]*RoomController[EntityID, RoomID, Entity, Room])
|
||||
for id, room := range rooms {
|
||||
@@ -102,7 +102,7 @@ func (rm *RoomManager[EntityID, RoomID, Entity, Room]) GetEntityRooms(entityId E
|
||||
// Broadcast 向所有房间对象广播消息,该方法将会遍历所有房间控制器并调用 RoomController.Broadcast 方法
|
||||
func (rm *RoomManager[EntityID, RoomID, Entity, Room]) Broadcast(handler func(Entity), conditions ...func(Entity) bool) {
|
||||
rm.roomsRWMutex.RLock()
|
||||
rooms := hash.Copy(rm.rooms)
|
||||
rooms := collection.CloneMap(rm.rooms)
|
||||
rm.roomsRWMutex.RUnlock()
|
||||
for _, room := range rooms {
|
||||
room.Broadcast(handler, conditions...)
|
||||
|
||||
@@ -37,7 +37,7 @@ func TestCond(t *testing.T) {
|
||||
|
||||
player := &Player{
|
||||
tasks: map[string][]*Task{
|
||||
task.Type: []*Task{task},
|
||||
task.Type: {task},
|
||||
},
|
||||
}
|
||||
OnRefreshTaskCounterEvent(task.Type, player, 1)
|
||||
|
||||
Reference in New Issue
Block a user