Compare commits
2 Commits
f5e3acd4d6
...
a19b354dff
| Author | SHA1 | Date | |
|---|---|---|---|
| a19b354dff | |||
| 48e178e853 |
2
go.mod
2
go.mod
@@ -3,7 +3,7 @@ module git.hlsq.asia/mmorpg/service-scene
|
|||||||
go 1.23.1
|
go 1.23.1
|
||||||
|
|
||||||
require (
|
require (
|
||||||
git.hlsq.asia/mmorpg/service-common v0.0.0-20260108144540-69e82ec0fe77
|
git.hlsq.asia/mmorpg/service-common v0.0.0-20260110153008-0be254c6a298
|
||||||
github.com/judwhite/go-svc v1.2.1
|
github.com/judwhite/go-svc v1.2.1
|
||||||
go.uber.org/zap v1.27.0
|
go.uber.org/zap v1.27.0
|
||||||
google.golang.org/grpc v1.71.1
|
google.golang.org/grpc v1.71.1
|
||||||
|
|||||||
6
go.sum
6
go.sum
@@ -1,9 +1,7 @@
|
|||||||
filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA=
|
filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA=
|
||||||
filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4=
|
filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4=
|
||||||
git.hlsq.asia/mmorpg/service-common v0.0.0-20260107061047-e02e56781e78 h1:4CP8w5jHDOdN7hym8YrtioxNDd7PS2WcZDrO6dtY344=
|
git.hlsq.asia/mmorpg/service-common v0.0.0-20260110153008-0be254c6a298 h1:zKQC0gqQfP9sUVYs7OsoHSrzdk379u6aenko6rMx1go=
|
||||||
git.hlsq.asia/mmorpg/service-common v0.0.0-20260107061047-e02e56781e78/go.mod h1:xv6m1I2jUA6mudKVznygpnzMoshBQarthHD1QnkW4qc=
|
git.hlsq.asia/mmorpg/service-common v0.0.0-20260110153008-0be254c6a298/go.mod h1:xv6m1I2jUA6mudKVznygpnzMoshBQarthHD1QnkW4qc=
|
||||||
git.hlsq.asia/mmorpg/service-common v0.0.0-20260108144540-69e82ec0fe77 h1:39SVfV3+uysM25P+dSnJMvtJJI9l2aZ+/n/ooTdHqxk=
|
|
||||||
git.hlsq.asia/mmorpg/service-common v0.0.0-20260108144540-69e82ec0fe77/go.mod h1:xv6m1I2jUA6mudKVznygpnzMoshBQarthHD1QnkW4qc=
|
|
||||||
github.com/BurntSushi/toml v1.2.0 h1:Rt8g24XnyGTyglgET/PRUNlrUeu9F5L+7FilkXfZgs0=
|
github.com/BurntSushi/toml v1.2.0 h1:Rt8g24XnyGTyglgET/PRUNlrUeu9F5L+7FilkXfZgs0=
|
||||||
github.com/BurntSushi/toml v1.2.0/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
|
github.com/BurntSushi/toml v1.2.0/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
|
||||||
github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs=
|
github.com/bsm/ginkgo/v2 v2.12.0 h1:Ny8MWAHyOepLGlLKYmXG4IEkioBysk6GpaRTLC8zwWs=
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ type gatewayStream struct {
|
|||||||
stream grpc.ClientStream
|
stream grpc.ClientStream
|
||||||
}
|
}
|
||||||
|
|
||||||
func findGatewayBySID(sid int64, fun GatewayFun) (*gatewayStream, error) {
|
func findGatewayBySID(sid string, fun GatewayFun) (*gatewayStream, error) {
|
||||||
key := gatewayKey(sid, fun)
|
key := gatewayKey(sid, fun)
|
||||||
|
|
||||||
if v, ok := gatewayServer.Load(key); ok {
|
if v, ok := gatewayServer.Load(key); ok {
|
||||||
@@ -54,7 +54,7 @@ func findGatewayBySID(sid int64, fun GatewayFun) (*gatewayStream, error) {
|
|||||||
return ss, nil
|
return ss, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func SendMessageToGateway(sid int64, fun GatewayFun, msg proto.Message, re ...bool) error {
|
func SendMessageToGateway(sid string, fun GatewayFun, msg proto.Message, re ...bool) error {
|
||||||
ss, err := findGatewayBySID(sid, fun)
|
ss, err := findGatewayBySID(sid, fun)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
@@ -79,6 +79,6 @@ func SendMessageToGateway(sid int64, fun GatewayFun, msg proto.Message, re ...bo
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func gatewayKey(sid int64, fun GatewayFun) string {
|
func gatewayKey(sid string, fun GatewayFun) string {
|
||||||
return strconv.FormatInt(sid, 10) + "-" + strconv.Itoa(int(fun))
|
return sid + "-" + strconv.Itoa(int(fun))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -20,25 +20,25 @@ import (
|
|||||||
// Instance 场景类
|
// Instance 场景类
|
||||||
type Instance struct {
|
type Instance struct {
|
||||||
wg sync.WaitGroup
|
wg sync.WaitGroup
|
||||||
players map[int64]*npc.PlayerNode // 存储所有玩家节点 [usn]
|
players map[string]*npc.PlayerNode // 存储所有玩家节点 [usn]
|
||||||
ctx context.Context // 停止指令
|
ctx context.Context // 停止指令
|
||||||
cancel context.CancelFunc // 停止函数
|
cancel context.CancelFunc // 停止函数
|
||||||
logger *zap.SugaredLogger // 日志
|
logger *zap.SugaredLogger // 日志
|
||||||
lastLogicTime int64 // 上次逻辑帧执行时间(毫秒时间戳)
|
lastLogicTime int64 // 上次逻辑帧执行时间(毫秒时间戳)
|
||||||
|
|
||||||
SID int64 // 服务ID
|
SID string // 服务ID
|
||||||
InstanceID int32 // 副本ID
|
InstanceID int32 // 副本ID
|
||||||
UniqueNo int64 // 唯一编号
|
UniqueNo string // 唯一编号
|
||||||
EventIn chan proto.Message // 消息入口
|
EventIn chan proto.Message // 消息入口
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewScene 初始化场景
|
// NewScene 初始化场景
|
||||||
func NewScene(sid int64, instanceID int32) *Instance {
|
func NewScene(sid string, instanceID int32) *Instance {
|
||||||
s := &Instance{
|
s := &Instance{
|
||||||
players: make(map[int64]*npc.PlayerNode),
|
players: make(map[string]*npc.PlayerNode),
|
||||||
SID: sid,
|
SID: sid,
|
||||||
InstanceID: instanceID,
|
InstanceID: instanceID,
|
||||||
UniqueNo: utils.SnowflakeInstance().Generate().Int64(),
|
UniqueNo: utils.SnowflakeInstance().Generate().String(),
|
||||||
EventIn: make(chan proto.Message, 1024),
|
EventIn: make(chan proto.Message, 1024),
|
||||||
}
|
}
|
||||||
s.logger = log.GetLogger().Named(fmt.Sprintf("instance %v:%v", s.InstanceID, s.UniqueNo))
|
s.logger = log.GetLogger().Named(fmt.Sprintf("instance %v:%v", s.InstanceID, s.UniqueNo))
|
||||||
@@ -104,7 +104,7 @@ func (i *Instance) onEvent(e proto.Message) {
|
|||||||
// 逻辑帧
|
// 逻辑帧
|
||||||
func (i *Instance) onLogic(delta int64) {
|
func (i *Instance) onLogic(delta int64) {
|
||||||
positionUpdate := make([]*sc_pb.PositionInfo, 0)
|
positionUpdate := make([]*sc_pb.PositionInfo, 0)
|
||||||
sid := int64(0)
|
sid := ""
|
||||||
// 处理玩家指令
|
// 处理玩家指令
|
||||||
for _, node := range i.players {
|
for _, node := range i.players {
|
||||||
if node.LogicAction(delta) {
|
if node.LogicAction(delta) {
|
||||||
@@ -121,7 +121,7 @@ func (i *Instance) onLogic(delta int64) {
|
|||||||
Info: positionUpdate,
|
Info: positionUpdate,
|
||||||
})
|
})
|
||||||
if err := stream_client.SendMessageToGateway(sid, stream_client.FunToClient, &grpc_pb.ToClientReq{
|
if err := stream_client.SendMessageToGateway(sid, stream_client.FunToClient, &grpc_pb.ToClientReq{
|
||||||
USN: -1,
|
USN: "",
|
||||||
MessageID: int32(sc_pb.MessageID_MESSAGE_ID_POSITION),
|
MessageID: int32(sc_pb.MessageID_MESSAGE_ID_POSITION),
|
||||||
Payload: payload,
|
Payload: payload,
|
||||||
}); err != nil {
|
}); err != nil {
|
||||||
|
|||||||
@@ -8,34 +8,34 @@ var Mgr *insManager
|
|||||||
|
|
||||||
type insManager struct {
|
type insManager struct {
|
||||||
sync.RWMutex
|
sync.RWMutex
|
||||||
insMap map[int64]*Instance // [uniqueNo]
|
insMap map[string]*Instance // [uniqueNo]
|
||||||
}
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
Mgr = &insManager{
|
Mgr = &insManager{
|
||||||
insMap: make(map[int64]*Instance),
|
insMap: make(map[string]*Instance),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *insManager) Add(uniqueNo int64, ins *Instance) {
|
func (m *insManager) Add(uniqueNo string, ins *Instance) {
|
||||||
m.Lock()
|
m.Lock()
|
||||||
defer m.Unlock()
|
defer m.Unlock()
|
||||||
m.insMap[uniqueNo] = ins
|
m.insMap[uniqueNo] = ins
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *insManager) Delete(uniqueNo int64) {
|
func (m *insManager) Delete(uniqueNo string) {
|
||||||
m.Lock()
|
m.Lock()
|
||||||
defer m.Unlock()
|
defer m.Unlock()
|
||||||
delete(m.insMap, uniqueNo)
|
delete(m.insMap, uniqueNo)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *insManager) GetAll() map[int64]*Instance {
|
func (m *insManager) GetAll() map[string]*Instance {
|
||||||
m.RLock()
|
m.RLock()
|
||||||
defer m.RUnlock()
|
defer m.RUnlock()
|
||||||
return m.insMap
|
return m.insMap
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m *insManager) GetByUniqueNo(uniqueNo int64) *Instance {
|
func (m *insManager) GetByUniqueNo(uniqueNo string) *Instance {
|
||||||
m.RLock()
|
m.RLock()
|
||||||
defer m.RUnlock()
|
defer m.RUnlock()
|
||||||
return m.insMap[uniqueNo]
|
return m.insMap[uniqueNo]
|
||||||
|
|||||||
@@ -7,8 +7,8 @@ import (
|
|||||||
|
|
||||||
// PlayerNode 定义玩家节点结构体
|
// PlayerNode 定义玩家节点结构体
|
||||||
type PlayerNode struct {
|
type PlayerNode struct {
|
||||||
USN int64 // 用户ID
|
USN string // 用户ID
|
||||||
GatewaySID int64 // 网关服务ID
|
GatewaySID string // 网关服务ID
|
||||||
MoveSpeed float32 // 移动速度
|
MoveSpeed float32 // 移动速度
|
||||||
|
|
||||||
Position [2]float32 // 二维坐标 [x, y]
|
Position [2]float32 // 二维坐标 [x, y]
|
||||||
@@ -17,7 +17,7 @@ type PlayerNode struct {
|
|||||||
Action []*grpc_pb.ActionReq // 其他操作
|
Action []*grpc_pb.ActionReq // 其他操作
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewPlayerNode(gatewaySID int64, usn int64) *PlayerNode {
|
func NewPlayerNode(gatewaySID string, usn string) *PlayerNode {
|
||||||
return &PlayerNode{
|
return &PlayerNode{
|
||||||
USN: usn,
|
USN: usn,
|
||||||
GatewaySID: gatewaySID,
|
GatewaySID: gatewaySID,
|
||||||
|
|||||||
Reference in New Issue
Block a user