-
Notifications
You must be signed in to change notification settings - Fork 29
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: modular 包新增 dimension 概念,适用于根据特定宿主进行模块化,例如房间与房间之间的各组件相互隔离
- Loading branch information
1 parent
d1d5bd4
commit 1402b85
Showing
8 changed files
with
178 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package modular | ||
|
||
// Dimension 维度接口 | ||
// - 维度与服务的区别在于,维度是对非全局性的服务进行抽象,例如:依赖特定游戏房间的局内玩家管理服务 | ||
type Dimension[Owner any] interface { | ||
// OnInit 服务初始化阶段,该阶段不应该依赖其他任何服务 | ||
OnInit(owner Owner) error | ||
|
||
// OnPreload 预加载阶段,在进入该阶段时,所有服务已经初始化完成,可在该阶段注入其他服务的依赖 | ||
OnPreload() error | ||
|
||
// OnMount 挂载阶段,该阶段所有服务本身及依赖的服务都已经初始化完成,可在该阶段进行服务功能的定义 | ||
OnMount() error | ||
} | ||
|
||
// RunDimensions 运行维度 | ||
func RunDimensions[Owner any](owner Owner, dimensions ...Dimension[Owner]) error { | ||
// OnInit | ||
for _, dimension := range dimensions { | ||
if err := dimension.OnInit(owner); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
// OnPreload | ||
for _, dimension := range dimensions { | ||
if err := dimension.OnPreload(); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
// OnMount | ||
for _, dimension := range dimensions { | ||
if err := dimension.OnMount(); err != nil { | ||
return err | ||
} | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package core | ||
|
||
type Events struct { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package core | ||
|
||
import "github.com/kercylan98/minotaur/modular/example/internal/dimension/dimensions/exposes" | ||
|
||
type Room struct { | ||
RoomId int64 | ||
*Events | ||
exposes.VisitorsExpose | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package dimension | ||
|
||
import ( | ||
"github.com/kercylan98/minotaur/modular" | ||
"github.com/kercylan98/minotaur/modular/example/internal/dimension/core" | ||
"github.com/kercylan98/minotaur/modular/example/internal/dimension/dimensions/dimensions/visitors" | ||
) | ||
|
||
func New(roomId int64) error { | ||
visitorsDimension := new(visitors.Dimension) | ||
|
||
return modular.RunDimensions(&core.Room{ | ||
RoomId: roomId, | ||
Events: &core.Events{}, | ||
|
||
VisitorsExpose: visitorsDimension, | ||
}, | ||
visitorsDimension, | ||
) | ||
} |
64 changes: 64 additions & 0 deletions
64
modular/example/internal/dimension/dimensions/dimensions/visitors/visitors.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package visitors | ||
|
||
import ( | ||
"fmt" | ||
"github.com/kercylan98/minotaur/modular/example/internal/dimension/core" | ||
"github.com/kercylan98/minotaur/modular/example/internal/dimension/dimensions/exposes" | ||
"github.com/kercylan98/minotaur/modular/example/internal/dimension/dimensions/models" | ||
"github.com/kercylan98/minotaur/utils/collection" | ||
) | ||
|
||
type Dimension struct { | ||
*core.Room // 房间 Id | ||
visitors map[string]*models.VisitorsMember // 所有访客 | ||
visitorIds []string // 所有访客 OpenId | ||
} | ||
|
||
func (d *Dimension) OnInit(owner *core.Room) error { | ||
exposes.Visitors = d | ||
d.Room = owner | ||
d.visitors = make(map[string]*models.VisitorsMember) | ||
fmt.Println("visitors dimension initialized") | ||
return nil | ||
} | ||
|
||
func (d *Dimension) OnPreload() error { | ||
fmt.Println("visitors dimension preloaded") | ||
return nil | ||
} | ||
|
||
func (d *Dimension) OnMount() error { | ||
fmt.Println("visitors dimension mounted") | ||
return nil | ||
} | ||
|
||
func (d *Dimension) Count() int { | ||
return len(d.visitors) | ||
} | ||
|
||
func (d *Dimension) OpenIds() []string { | ||
return d.visitorIds | ||
} | ||
|
||
func (d *Dimension) Has(openId string) bool { | ||
return collection.KeyInMap(d.visitors, openId) | ||
} | ||
|
||
func (d *Dimension) Del(openId string) { | ||
member := d.Get(openId) | ||
if member == nil { | ||
return | ||
} | ||
delete(d.visitors, openId) | ||
collection.DropSliceByIndices(&d.visitorIds, member.OpenIdIdx) | ||
} | ||
|
||
func (d *Dimension) Get(openId string) *models.VisitorsMember { | ||
return d.visitors[openId] | ||
} | ||
|
||
func (d *Dimension) Add(member *models.VisitorsMember) { | ||
member.OpenIdIdx = len(d.visitorIds) | ||
d.visitorIds = append(d.visitorIds, member.OpenId) | ||
d.visitors[member.OpenId] = member | ||
} |
27 changes: 27 additions & 0 deletions
27
modular/example/internal/dimension/dimensions/exposes/visitors.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package exposes | ||
|
||
import ( | ||
"github.com/kercylan98/minotaur/modular/example/internal/dimension/dimensions/models" | ||
) | ||
|
||
var Visitors VisitorsExpose | ||
|
||
type VisitorsExpose interface { | ||
// Count 访客数量 | ||
Count() int | ||
|
||
// OpenIds 访客 OpenId 列表 | ||
OpenIds() []string | ||
|
||
// Has 是否存在指定 OpenId 的访客 | ||
Has(openId string) bool | ||
|
||
// Del 删除指定 OpenId 的访客 | ||
Del(openId string) | ||
|
||
// Get 获取指定 OpenId 的访客 | ||
Get(openId string) *models.VisitorsMember | ||
|
||
// Add 添加访客 | ||
Add(member *models.VisitorsMember) | ||
} |
7 changes: 7 additions & 0 deletions
7
modular/example/internal/dimension/dimensions/models/visitors.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package models | ||
|
||
// VisitorsMember 访客成员 | ||
type VisitorsMember struct { | ||
OpenId string // 访客成员 OpenId | ||
OpenIdIdx int // 访客成员 OpenId 索引 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters