Skip to content

Commit

Permalink
refactor: 移除 slice 包和 hash 包,新增 listings、mappings 包存放数组、切片、映射等数据结构,原 …
Browse files Browse the repository at this point in the history
…slice、hash 包中的工具函数迁移至 collection 包,与 sher 包合并并移除 sher 包。完善 collection 包测试用例
  • Loading branch information
kercylan98 committed Jan 11, 2024
1 parent 3408c21 commit 66d9034
Show file tree
Hide file tree
Showing 111 changed files with 3,539 additions and 3,487 deletions.
4 changes: 2 additions & 2 deletions game/activity/controller_internal.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
Expand Down
51 changes: 26 additions & 25 deletions game/activity/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -21,36 +22,36 @@ 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) {
if !reflect.TypeOf(activityId).AssignableTo(reflect.TypeOf(handler).In(0)) {
return
}
handler(activityId.(ID))
}, slice.GetValue(priority, 0))
}, collection.FindFirstOrDefaultInSlice(priority, 0))
}

// OnUpcomingEvent 即将开始的活动事件
Expand All @@ -75,15 +76,15 @@ 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) {
if !reflect.TypeOf(activityId).AssignableTo(reflect.TypeOf(handler).In(0)) {
return
}
handler(activityId.(ID))
}, slice.GetValue(priority, 0))
}, collection.FindFirstOrDefaultInSlice(priority, 0))
}

// OnStartedEvent 活动开始事件
Expand Down Expand Up @@ -116,15 +117,15 @@ 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) {
if !reflect.TypeOf(activityId).AssignableTo(reflect.TypeOf(handler).In(0)) {
return
}
handler(activityId.(ID))
}, slice.GetValue(priority, 0))
}, collection.FindFirstOrDefaultInSlice(priority, 0))
}

// OnEndedEvent 活动结束事件
Expand All @@ -149,15 +150,15 @@ 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) {
if !reflect.TypeOf(activityId).AssignableTo(reflect.TypeOf(handler).In(0)) {
return
}
handler(activityId.(ID))
}, slice.GetValue(priority, 0))
}, collection.FindFirstOrDefaultInSlice(priority, 0))
}

// OnExtendedShowStartedEvent 活动结束后延长展示开始事件
Expand All @@ -182,15 +183,15 @@ 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) {
if !reflect.TypeOf(activityId).AssignableTo(reflect.TypeOf(handler).In(0)) {
return
}
handler(activityId.(ID))
}, slice.GetValue(priority, 0))
}, collection.FindFirstOrDefaultInSlice(priority, 0))
}

// OnExtendedShowEndedEvent 活动结束后延长展示结束事件
Expand All @@ -215,15 +216,15 @@ 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) {
if !reflect.TypeOf(activityId).AssignableTo(reflect.TypeOf(handler).In(0)) {
return
}
handler(activityId.(ID))
}, slice.GetValue(priority, 0))
}, collection.FindFirstOrDefaultInSlice(priority, 0))
}

// OnNewDayEvent 新的一天事件
Expand Down
11 changes: 5 additions & 6 deletions game/space/room_controller.go
Original file line number Diff line number Diff line change
@@ -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"
)

Expand Down Expand Up @@ -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 判断是否有座位
Expand Down Expand Up @@ -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 判断是否有实体
Expand Down Expand Up @@ -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 获取实体数量
Expand Down Expand Up @@ -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 {
Expand Down
12 changes: 6 additions & 6 deletions game/space/room_manager.go
Original file line number Diff line number Diff line change
@@ -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"
)

Expand Down Expand Up @@ -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 获取房间管理器接管的房间数量
Expand All @@ -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) {
Expand All @@ -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 {
Expand All @@ -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...)
Expand Down
2 changes: 1 addition & 1 deletion game/task/task_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions planner/pce/exporter/cmd/exportgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"github.com/kercylan98/minotaur/planner/pce"
"github.com/kercylan98/minotaur/planner/pce/cs"
"github.com/kercylan98/minotaur/planner/pce/tmpls"
"github.com/kercylan98/minotaur/utils/collection"
"github.com/kercylan98/minotaur/utils/file"
"github.com/kercylan98/minotaur/utils/hash"
"github.com/kercylan98/minotaur/utils/str"
"github.com/spf13/cobra"
"github.com/tealeg/xlsx"
Expand Down Expand Up @@ -64,7 +64,7 @@ func init() {
var exporter = pce.NewExporter()
loader := pce.NewLoader(pce.GetFields())

excludes := hash.ToMapBool(str.SplitTrimSpace(exclude, ","))
excludes := collection.ConvertSliceToBoolMap(str.SplitTrimSpace(exclude, ","))
for _, xlsxFile := range xlsxFiles {
xf, err := xlsx.OpenFile(xlsxFile)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions planner/pce/exporter/cmd/exportjson.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"github.com/kercylan98/minotaur/planner/pce"
"github.com/kercylan98/minotaur/planner/pce/cs"
"github.com/kercylan98/minotaur/planner/pce/tmpls"
"github.com/kercylan98/minotaur/utils/collection"
"github.com/kercylan98/minotaur/utils/file"
"github.com/kercylan98/minotaur/utils/hash"
"github.com/kercylan98/minotaur/utils/str"
"github.com/spf13/cobra"
"github.com/tealeg/xlsx"
Expand Down Expand Up @@ -61,7 +61,7 @@ func init() {
var exporter = pce.NewExporter()
loader := pce.NewLoader(pce.GetFields())

excludes := hash.ToMapBool(str.SplitTrimSpace(exclude, ","))
excludes := collection.ConvertSliceToBoolMap(str.SplitTrimSpace(exclude, ","))
for _, xlsxFile := range xlsxFiles {
xf, err := xlsx.OpenFile(xlsxFile)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions planner/pce/exporter/cmd/exportjson_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"github.com/kercylan98/minotaur/planner/pce"
"github.com/kercylan98/minotaur/planner/pce/cs"
"github.com/kercylan98/minotaur/planner/pce/tmpls"
"github.com/kercylan98/minotaur/utils/collection"
"github.com/kercylan98/minotaur/utils/file"
"github.com/kercylan98/minotaur/utils/hash"
"github.com/kercylan98/minotaur/utils/str"
"github.com/tealeg/xlsx"
"os"
Expand Down Expand Up @@ -61,7 +61,7 @@ func TestExecute(t *testing.T) {
var exporter = pce.NewExporter()
loader := pce.NewLoader(pce.GetFields())

excludes := hash.ToMapBool(str.SplitTrimSpace(exclude, ","))
excludes := collection.ConvertSliceToBoolMap(str.SplitTrimSpace(exclude, ","))
for _, xlsxFile := range xlsxFiles {
xf, err := xlsx.OpenFile(xlsxFile)
if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions planner/pce/tmpl_field.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package pce

import (
"fmt"
"github.com/kercylan98/minotaur/utils/hash"
"github.com/kercylan98/minotaur/utils/collection"
"github.com/kercylan98/minotaur/utils/str"
"strings"
)
Expand Down Expand Up @@ -96,7 +96,7 @@ func (slf *TmplField) handleSlice(fieldName, fieldType string, fields map[string
}
slf.slice = true
t := strings.TrimPrefix(fieldType, "[]")
if hash.Exist(fields, t) {
if collection.FindInMapKey(fields, t) {
slf.Struct = nil
slf.Type = t
} else {
Expand Down
4 changes: 2 additions & 2 deletions planner/pce/tmpl_struct.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package pce

import (
"github.com/kercylan98/minotaur/utils/hash"
"github.com/kercylan98/minotaur/utils/collection"
)

// TmplStruct 模板结构
Expand All @@ -19,7 +19,7 @@ func (slf *TmplStruct) addField(parent, name, desc, fieldType string, fields map
Desc: desc,
Type: fieldType,
}
if !hash.Exist(fields, fieldType) {
if !collection.FindInMapKey(fields, fieldType) {
field.setStruct(parent, name, desc, fieldType, fields)
} else {
field.Type = GetFieldGolangType(fields[fieldType])
Expand Down
2 changes: 1 addition & 1 deletion server/bot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,5 @@ func TestNewBot(t *testing.T) {
bot.SendPacket([]byte("hello"))
})

srv.Run(":9600")
_ = srv.Run(":9600")
}
4 changes: 2 additions & 2 deletions server/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import (
"fmt"
"github.com/gorilla/websocket"
"github.com/kercylan98/minotaur/server/writeloop"
"github.com/kercylan98/minotaur/utils/collection"
"github.com/kercylan98/minotaur/utils/concurrent"
"github.com/kercylan98/minotaur/utils/hash"
"github.com/kercylan98/minotaur/utils/log"
"github.com/kercylan98/minotaur/utils/random"
"github.com/kercylan98/minotaur/utils/timer"
Expand Down Expand Up @@ -203,7 +203,7 @@ func (slf *Conn) GetData(key any) any {

// ViewData 查看只读的连接数据
func (slf *Conn) ViewData() map[any]any {
return hash.Copy(slf.data)
return collection.CloneMap(slf.data)
}

// SetMessageData 设置消息数据,该数据将在消息处理完成后释放
Expand Down
Loading

0 comments on commit 66d9034

Please sign in to comment.