Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
kercylan98 committed Jul 25, 2023
2 parents 10bedb2 + d71d843 commit c5b0fbe
Show file tree
Hide file tree
Showing 74 changed files with 1,630 additions and 2,336 deletions.
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ mindmap
root((Minotaur))
/component 通用组件接口定义
/components 通用组件内置实现
/config 针对配置导表的配置加载
/configuration 配置管理功能
/game 游戏通用功能接口定义
/builtin 游戏通用功能内置实现
/notify 通知功能接口定义
/planner 策划相关工具目录
/configexport 配置导表功能实现
/pce 配置导表功能实现
/report 数据埋点及上报功能
/server 网络服务器支持
/cross 内置跨服功能实现
Expand Down Expand Up @@ -113,7 +113,6 @@ func main() {
```
其他的一些支持事件的结构体(非所有):
- `game.Room` 游戏房间实现
- `synchronization.Map` 并发安全的`Map`实现
- ...
### 可选项
大部分的 `New` 函数均可使用可选项进行创建,具体函数前缀通常为 `With`
Expand Down
24 changes: 12 additions & 12 deletions component/components/lockstep.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package components
import (
"encoding/json"
"github.com/kercylan98/minotaur/component"
"github.com/kercylan98/minotaur/utils/synchronization"
"github.com/kercylan98/minotaur/utils/concurrent"
"github.com/kercylan98/minotaur/utils/timer"
"sync"
"sync/atomic"
Expand All @@ -13,8 +13,8 @@ import (
// NewLockstep 创建一个锁步(帧)同步默认实现的组件(Lockstep)进行返回
func NewLockstep[ClientID comparable, Command any](options ...LockstepOption[ClientID, Command]) *Lockstep[ClientID, Command] {
lockstep := &Lockstep[ClientID, Command]{
clients: synchronization.NewMap[ClientID, component.LockstepClient[ClientID]](),
frames: synchronization.NewMap[int, []Command](),
clients: concurrent.NewBalanceMap[ClientID, component.LockstepClient[ClientID]](),
frames: concurrent.NewBalanceMap[int, []Command](),
ticker: timer.GetTicker(10),
frameRate: 15,
serialization: func(frame int, commands []Command) []byte {
Expand All @@ -25,7 +25,7 @@ func NewLockstep[ClientID comparable, Command any](options ...LockstepOption[Cli
data, _ := json.Marshal(frameStruct)
return data
},
clientCurrentFrame: synchronization.NewMap[ClientID, int](),
clientCurrentFrame: concurrent.NewBalanceMap[ClientID, int](),
}
for _, option := range options {
option(lockstep)
Expand All @@ -40,12 +40,12 @@ func NewLockstep[ClientID comparable, Command any](options ...LockstepOption[Cli
// - 从特定帧开始追帧
// - 兼容各种基于TCP/UDP/Unix的网络类型,可通过客户端实现其他网络类型同步
type Lockstep[ClientID comparable, Command any] struct {
clients *synchronization.Map[ClientID, component.LockstepClient[ClientID]] // 接受广播的客户端
frames *synchronization.Map[int, []Command] // 所有帧指令
ticker *timer.Ticker // 定时器
frameMutex sync.Mutex // 帧锁
currentFrame int // 当前帧
clientCurrentFrame *synchronization.Map[ClientID, int] // 客户端当前帧数
clients *concurrent.BalanceMap[ClientID, component.LockstepClient[ClientID]] // 接受广播的客户端
frames *concurrent.BalanceMap[int, []Command] // 所有帧指令
ticker *timer.Ticker // 定时器
frameMutex sync.Mutex // 帧锁
currentFrame int // 当前帧
clientCurrentFrame *concurrent.BalanceMap[ClientID, int] // 客户端当前帧数
running atomic.Bool

frameRate int // 帧率(每秒N帧)
Expand Down Expand Up @@ -123,8 +123,8 @@ func (slf *Lockstep[ClientID, Command]) StopBroadcast() {

// AddCommand 添加命令到当前帧
func (slf *Lockstep[ClientID, Command]) AddCommand(command Command) {
slf.frames.AtomGetSet(slf.currentFrame, func(value []Command, exist bool) (newValue []Command, isSet bool) {
return append(value, command), true
slf.frames.Atom(func(m map[int][]Command) {
m[slf.currentFrame] = append(m[slf.currentFrame], command)
})
}

Expand Down
6 changes: 3 additions & 3 deletions game/builtin/attrs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,18 @@ package builtin

import (
"github.com/kercylan98/minotaur/game"
"github.com/kercylan98/minotaur/utils/concurrent"
"github.com/kercylan98/minotaur/utils/huge"
"github.com/kercylan98/minotaur/utils/synchronization"
)

func NewAttrs() *Attrs {
return &Attrs{
attrs: synchronization.NewMap[int, any](),
attrs: concurrent.NewBalanceMap[int, any](),
}
}

type Attrs struct {
attrs *synchronization.Map[int, any]
attrs *concurrent.BalanceMap[int, any]

attrChangeEventHandles []game.AttrChangeEventHandle
attrIdChangeEventHandles map[int][]game.AttrChangeEventHandle
Expand Down
20 changes: 10 additions & 10 deletions game/builtin/ranking_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@ package builtin
import (
"encoding/json"
"github.com/kercylan98/minotaur/game"
"github.com/kercylan98/minotaur/utils/concurrent"
"github.com/kercylan98/minotaur/utils/generic"
"github.com/kercylan98/minotaur/utils/synchronization"
)

// NewRankingList 创建一个排名从0开始的排行榜
func NewRankingList[CompetitorID comparable, Score generic.Ordered](options ...RankingListOption[CompetitorID, Score]) *RankingList[CompetitorID, Score] {
rankingList := &RankingList[CompetitorID, Score]{
rankCount: 100,
competitors: synchronization.NewMap[CompetitorID, Score](),
competitors: concurrent.NewBalanceMap[CompetitorID, Score](),
}
for _, option := range options {
option(rankingList)
Expand All @@ -22,7 +22,7 @@ func NewRankingList[CompetitorID comparable, Score generic.Ordered](options ...R
type RankingList[CompetitorID comparable, Score generic.Ordered] struct {
asc bool
rankCount int
competitors *synchronization.Map[CompetitorID, Score]
competitors *concurrent.BalanceMap[CompetitorID, Score]
scores []*scoreItem[CompetitorID, Score] // CompetitorID, Score

rankChangeEventHandles []game.RankChangeEventHandle[CompetitorID, Score]
Expand Down Expand Up @@ -244,11 +244,11 @@ func (slf *RankingList[CompetitorID, Score]) competitor(competitorId CompetitorI

func (slf *RankingList[CompetitorID, Score]) UnmarshalJSON(bytes []byte) error {
var t struct {
Competitors *synchronization.Map[CompetitorID, Score] `json:"competitors,omitempty"`
Scores []*scoreItem[CompetitorID, Score] `json:"scores,omitempty"`
Asc bool `json:"asc,omitempty"`
Competitors *concurrent.BalanceMap[CompetitorID, Score] `json:"competitors,omitempty"`
Scores []*scoreItem[CompetitorID, Score] `json:"scores,omitempty"`
Asc bool `json:"asc,omitempty"`
}
t.Competitors = synchronization.NewMap[CompetitorID, Score]()
t.Competitors = concurrent.NewBalanceMap[CompetitorID, Score]()
if err := json.Unmarshal(bytes, &t); err != nil {
return err
}
Expand All @@ -260,9 +260,9 @@ func (slf *RankingList[CompetitorID, Score]) UnmarshalJSON(bytes []byte) error {

func (slf *RankingList[CompetitorID, Score]) MarshalJSON() ([]byte, error) {
var t struct {
Competitors *synchronization.Map[CompetitorID, Score] `json:"competitors,omitempty"`
Scores []*scoreItem[CompetitorID, Score] `json:"scores,omitempty"`
Asc bool `json:"asc,omitempty"`
Competitors *concurrent.BalanceMap[CompetitorID, Score] `json:"competitors,omitempty"`
Scores []*scoreItem[CompetitorID, Score] `json:"scores,omitempty"`
Asc bool `json:"asc,omitempty"`
}
t.Competitors = slf.competitors
t.Scores = slf.scores
Expand Down
11 changes: 5 additions & 6 deletions game/builtin/room.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,15 @@ package builtin

import (
"github.com/kercylan98/minotaur/game"
"github.com/kercylan98/minotaur/utils/asynchronous"
"github.com/kercylan98/minotaur/utils/hash"
"github.com/kercylan98/minotaur/utils/concurrent"
"github.com/kercylan98/minotaur/utils/log"
)

// NewRoom 创建一个默认的内置游戏房间 Room
func NewRoom[PlayerID comparable, Player game.Player[PlayerID]](guid int64, options ...RoomOption[PlayerID, Player]) *Room[PlayerID, Player] {
room := &Room[PlayerID, Player]{
guid: guid,
players: asynchronous.NewMap[PlayerID, Player](),
players: concurrent.NewBalanceMap[PlayerID, Player](),
}
for _, option := range options {
option(room)
Expand All @@ -27,7 +26,7 @@ type Room[PlayerID comparable, Player game.Player[PlayerID]] struct {
owner PlayerID
noMaster bool
playerLimit int
players hash.Map[PlayerID, Player]
players *concurrent.BalanceMap[PlayerID, Player]
kickCheckHandle func(room *Room[PlayerID, Player], id, target PlayerID) error

playerJoinRoomEventHandles []game.PlayerJoinRoomEventHandle[PlayerID, Player]
Expand All @@ -51,8 +50,8 @@ func (slf *Room[PlayerID, Player]) GetPlayer(id PlayerID) Player {
}

// GetPlayers 获取所有玩家
func (slf *Room[PlayerID, Player]) GetPlayers() hash.MapReadonly[PlayerID, Player] {
return slf.players.(hash.MapReadonly[PlayerID, Player])
func (slf *Room[PlayerID, Player]) GetPlayers() map[PlayerID]Player {
return slf.players.Map()
}

// GetPlayerCount 获取玩家数量
Expand Down
6 changes: 3 additions & 3 deletions game/builtin/room_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@ package builtin

import (
"github.com/kercylan98/minotaur/game"
"github.com/kercylan98/minotaur/utils/synchronization"
"github.com/kercylan98/minotaur/utils/concurrent"
"sync/atomic"
)

func NewRoomManager[PlayerID comparable, Room game.Room[PlayerID, game.Player[PlayerID]]]() *RoomManager[PlayerID, Room] {
return &RoomManager[PlayerID, Room]{
rooms: synchronization.NewMap[int64, Room](),
rooms: concurrent.NewBalanceMap[int64, Room](),
}
}

// RoomManager 房间管理器
type RoomManager[PlayerID comparable, Room game.Room[PlayerID, game.Player[PlayerID]]] struct {
guid atomic.Int64
rooms *synchronization.Map[int64, Room]
rooms *concurrent.BalanceMap[int64, Room]
}

// GenGuid 生成一个新的房间guid
Expand Down
4 changes: 2 additions & 2 deletions game/builtin/room_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package builtin

import (
"github.com/kercylan98/minotaur/game"
"github.com/kercylan98/minotaur/utils/synchronization"
"github.com/kercylan98/minotaur/utils/concurrent"
)

// RoomOption 房间构建可选项
Expand All @@ -11,7 +11,7 @@ type RoomOption[PlayerID comparable, Player game.Player[PlayerID]] func(room *Ro
// WithRoomSync 通过线程安全的方式创建房间
func WithRoomSync[PlayerID comparable, Player game.Player[PlayerID]]() RoomOption[PlayerID, Player] {
return func(room *Room[PlayerID, Player]) {
room.players = synchronization.NewMap[PlayerID, Player]()
room.players = concurrent.NewBalanceMap[PlayerID, Player]()
}
}

Expand Down
41 changes: 38 additions & 3 deletions game/builtin/room_seat.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package builtin

import (
"github.com/kercylan98/minotaur/game"
"github.com/kercylan98/minotaur/utils/asynchronous"
"github.com/kercylan98/minotaur/utils/concurrent"
"github.com/kercylan98/minotaur/utils/hash"
"github.com/kercylan98/minotaur/utils/slice"
"sync"
Expand All @@ -12,7 +12,7 @@ import (
func NewRoomSeat[PlayerID comparable, Player game.Player[PlayerID]](room game.Room[PlayerID, Player], options ...RoomSeatOption[PlayerID, Player]) *RoomSeat[PlayerID, Player] {
roomSeat := &RoomSeat[PlayerID, Player]{
Room: room,
seatPS: asynchronous.NewMap[PlayerID, int](),
seatPS: concurrent.NewBalanceMap[PlayerID, int](),
}
for _, option := range options {
option(roomSeat)
Expand All @@ -26,7 +26,7 @@ type RoomSeat[PlayerID comparable, Player game.Player[PlayerID]] struct {
game.Room[PlayerID, Player]
mutex sync.RWMutex
vacancy []int
seatPS hash.Map[PlayerID, int]
seatPS *concurrent.BalanceMap[PlayerID, int]
seatSP []*PlayerID
duplicateLock bool
fillIn bool
Expand Down Expand Up @@ -240,6 +240,41 @@ func (slf *RoomSeat[PlayerID, Player]) GetNextSeatVacancy(seat int) int {
return seat
}

// GetPrevSeat 获取特定座位号上一个未缺席的座位号
func (slf *RoomSeat[PlayerID, Player]) GetPrevSeat(seat int) int {
l := len(slf.seatSP)
if l == 0 || seat >= l || seat < 0 {
return -1
}
var target = seat
for {
target--
if target < 0 {
target = l - 1
}
if target == seat {
return seat
}
if slf.seatSP[target] != nil {
return target
}
}
}

// GetPrevSeatVacancy 获取特定座位号上一个座位号
// - 缺席将不会被忽略
func (slf *RoomSeat[PlayerID, Player]) GetPrevSeatVacancy(seat int) int {
l := len(slf.seatSP)
if l == 0 || seat >= l || seat < 0 {
return -1
}
seat--
if seat < 0 {
seat = l - 1
}
return seat
}

func (slf *RoomSeat[PlayerID, Player]) onJoinRoom(room game.Room[PlayerID, Player], player Player) {
slf.AddSeat(player.GetID())
}
Expand Down
Loading

0 comments on commit c5b0fbe

Please sign in to comment.