Skip to content

Commit

Permalink
feat: modular 包新增 dimension 概念,适用于根据特定宿主进行模块化,例如房间与房间之间的各组件相互隔离
Browse files Browse the repository at this point in the history
  • Loading branch information
kercylan98 committed Mar 12, 2024
1 parent d1d5bd4 commit 1402b85
Show file tree
Hide file tree
Showing 8 changed files with 178 additions and 0 deletions.
40 changes: 40 additions & 0 deletions modular/dimension.go
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
}
4 changes: 4 additions & 0 deletions modular/example/internal/dimension/core/events.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package core

type Events struct {
}
9 changes: 9 additions & 0 deletions modular/example/internal/dimension/core/room.go
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
}
20 changes: 20 additions & 0 deletions modular/example/internal/dimension/dimension.go
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,
)
}
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 modular/example/internal/dimension/dimensions/exposes/visitors.go
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)
}
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 索引
}
7 changes: 7 additions & 0 deletions modular/example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"github.com/kercylan98/minotaur/modular"
"github.com/kercylan98/minotaur/modular/example/internal/dimension"
"github.com/kercylan98/minotaur/modular/example/internal/service/services/attack"
"github.com/kercylan98/minotaur/modular/example/internal/service/services/login"
"github.com/kercylan98/minotaur/modular/example/internal/service/services/server"
Expand All @@ -13,5 +14,11 @@ func main() {
new(server.Service),
new(login.Service),
)

err := dimension.New(1) // generate a room
if err != nil {
panic(err)
}

modular.Run()
}

0 comments on commit 1402b85

Please sign in to comment.