Compare commits

4 Commits

11 changed files with 67 additions and 69 deletions

View File

@@ -2,11 +2,11 @@ package app
import (
"fmt"
"git.hlsq.asia/mmorpg/service-common/discover/common"
"git.hlsq.asia/mmorpg/service-common/log"
"git.hlsq.asia/mmorpg/service-common/module"
"git.hlsq.asia/mmorpg/service-scene/config"
"git.hlsq.asia/mmorpg/service-scene/internal/grpc_server"
"git.hlsq.asia/alex/service-common/discover/common"
"git.hlsq.asia/alex/service-common/log"
"git.hlsq.asia/alex/service-common/module"
"git.hlsq.asia/alex/service-scene/config"
"git.hlsq.asia/alex/service-scene/internal/grpcserver"
"github.com/judwhite/go-svc"
"sync"
)
@@ -19,7 +19,7 @@ func (p *Program) Init(_ svc.Environment) error {
p.moduleList = append(p.moduleList, &module.Log{Cfg: config.Get().Log})
p.moduleList = append(p.moduleList, &module.DB{Cfg: config.Get().DB, AppName: config.Get().App.Name})
p.moduleList = append(p.moduleList, &module.Snowflake{})
p.moduleList = append(p.moduleList, &module.Grpc{Server: grpc_server.NewServer(config.Get().Serve.Grpc)})
p.moduleList = append(p.moduleList, &module.Grpc{Server: grpcserver.NewServer(config.Get().Serve.Grpc)})
p.moduleList = append(p.moduleList, &module.Prometheus{Cfg: config.Get().Metric})
p.moduleList = append(p.moduleList, &module.Tracer{Cfg: config.Get().Metric, ServiceName: common.KeyDiscoverServiceNameScene})
p.moduleList = append(p.moduleList, &module.Discover{})

View File

@@ -1,6 +1,6 @@
package config
import "git.hlsq.asia/mmorpg/service-common/config"
import "git.hlsq.asia/alex/service-common/config"
const path = "./config"

2
deploy/Jenkinsfile vendored
View File

@@ -3,7 +3,7 @@ pipeline {
environment {
// 仓库
REPO_URL = 'https://git.hlsq.asia/mmorpg/service-scene.git'
REPO_URL = 'https://git.hlsq.asia/alex/service-scene.git'
REPO_CREDENTIALS_ID = '80805ba2-f4ac-4d84-aee6-d4cce5fc0a96'
// Registry

4
go.mod
View File

@@ -1,9 +1,9 @@
module git.hlsq.asia/mmorpg/service-scene
module git.hlsq.asia/alex/service-scene
go 1.24.0
require (
git.hlsq.asia/mmorpg/service-common v0.0.0-20260207051302-0ca8a0ccbb14
git.hlsq.asia/alex/service-common master
github.com/judwhite/go-svc v1.2.1
go.uber.org/zap v1.27.0
google.golang.org/grpc v1.77.0

4
go.sum
View File

@@ -1,7 +1,7 @@
filippo.io/edwards25519 v1.1.0 h1:FNf4tywRC1HmFuKW5xopWpigGjJKiJSV0Cqo0cJWDaA=
filippo.io/edwards25519 v1.1.0/go.mod h1:BxyFTGdWcka3PhytdK4V28tE5sGfRvvvRV7EaN4VDT4=
git.hlsq.asia/mmorpg/service-common v0.0.0-20260207051302-0ca8a0ccbb14 h1:4r3tNWRzGVY3Xx6UiGjJJnwoMoWlVqbyGrljxl5d/nQ=
git.hlsq.asia/mmorpg/service-common v0.0.0-20260207051302-0ca8a0ccbb14/go.mod h1:mMhZcumphj6gaVTppVYsMTkd+5HupmQgAc53Pd4MH9I=
git.hlsq.asia/alex/service-common v0.0.0-20260211064821-558677bc3a2e h1:MNVS1NXNVn4DeTZPcvvA8YFM8afCzZedLPUYMffNgZs=
git.hlsq.asia/alex/service-common v0.0.0-20260211064821-558677bc3a2e/go.mod h1:mMhZcumphj6gaVTppVYsMTkd+5HupmQgAc53Pd4MH9I=
github.com/BurntSushi/toml v1.2.0 h1:Rt8g24XnyGTyglgET/PRUNlrUeu9F5L+7FilkXfZgs0=
github.com/BurntSushi/toml v1.2.0/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/IBM/sarama v1.46.3 h1:njRsX6jNlnR+ClJ8XmkO+CM4unbrNr/2vB5KK6UA+IE=

View File

@@ -1,45 +1,45 @@
package grpc_server
package grpcserver
import (
"context"
"git.hlsq.asia/mmorpg/service-common/log"
"git.hlsq.asia/mmorpg/service-common/proto/rs/grpc_pb"
"git.hlsq.asia/mmorpg/service-common/proto/ss/ss_pb"
"git.hlsq.asia/mmorpg/service-scene/internal/instance"
"git.hlsq.asia/alex/service-common/log"
"git.hlsq.asia/alex/service-common/proto/rs/rspb"
"git.hlsq.asia/alex/service-common/proto/ss/sspb"
"git.hlsq.asia/alex/service-scene/internal/instance"
"google.golang.org/protobuf/proto"
"sync"
)
func (s *Server) Enter(ctx context.Context, req *grpc_pb.EnterReq) (*grpc_pb.EnterResp, error) {
func (s *Server) Enter(ctx context.Context, req *rspb.EnterReq) (*rspb.EnterResp, error) {
ins, loaded := instance.Mgr.LoadOrStoreByInstanceID(req.InstanceID, instance.NewScene(s.SID, req.InstanceID))
if !loaded {
ins.Start(s.Cfg.TTL)
}
ins.EventIn <- req
payload, _ := proto.Marshal(&ss_pb.S2C_EnterInstance{
Info: &ss_pb.PositionInfo{
payload, _ := proto.Marshal(&sspb.S2C_EnterInstance{
Info: &sspb.PositionInfo{
USN: req.USN,
X: 1,
Y: 1,
},
})
return &grpc_pb.EnterResp{
return &rspb.EnterResp{
SceneSID: s.SID,
UniqueNo: ins.UniqueNo,
MessageID: int32(ss_pb.MessageID_MESSAGE_ID_ENTER_INSTANCE),
MessageID: int32(sspb.MessageID_MESSAGE_ID_ENTER_INSTANCE),
Payload: payload,
}, nil
}
func (s *Server) Leave(ctx context.Context, req *grpc_pb.LeaveReq) (*grpc_pb.LeaveResp, error) {
func (s *Server) Leave(ctx context.Context, req *rspb.LeaveReq) (*rspb.LeaveResp, error) {
if i := instance.Mgr.GetByUniqueNo(req.UniqueNo); i != nil {
i.EventIn <- req
}
return &grpc_pb.LeaveResp{}, nil
return &rspb.LeaveResp{}, nil
}
func (s *Server) Action(server grpc_pb.Scene_ActionServer) error {
func (s *Server) Action(server rspb.Scene_ActionServer) error {
wg := &sync.WaitGroup{}
wg.Add(1)
go func() {
@@ -64,5 +64,5 @@ func (s *Server) Action(server grpc_pb.Scene_ActionServer) error {
}
}()
wg.Wait()
return server.SendAndClose(&grpc_pb.ActionResp{})
return server.SendAndClose(&rspb.ActionResp{})
}

View File

@@ -1,15 +1,15 @@
package grpc_server
package grpcserver
import (
"git.hlsq.asia/mmorpg/service-common/config"
"git.hlsq.asia/mmorpg/service-common/discover/common"
"git.hlsq.asia/mmorpg/service-common/net/grpc/service"
"git.hlsq.asia/mmorpg/service-common/proto/rs/grpc_pb"
"git.hlsq.asia/alex/service-common/config"
"git.hlsq.asia/alex/service-common/discover/common"
"git.hlsq.asia/alex/service-common/net/grpc/service"
"git.hlsq.asia/alex/service-common/proto/rs/rspb"
"google.golang.org/grpc"
)
type Server struct {
grpc_pb.UnimplementedSceneServer
rspb.UnimplementedSceneServer
service.Base
}
@@ -32,7 +32,7 @@ func (s *Server) OnCustomGrpcServerOption() []grpc.ServerOption {
}
func (s *Server) OnInit(serve *grpc.Server) {
grpc_pb.RegisterSceneServer(serve, s)
rspb.RegisterSceneServer(serve, s)
}
func (s *Server) OnClose() {

View File

@@ -3,13 +3,13 @@ package instance
import (
"context"
"fmt"
"git.hlsq.asia/mmorpg/service-common/discover"
"git.hlsq.asia/mmorpg/service-common/log"
"git.hlsq.asia/mmorpg/service-common/net/grpc/grpc_client"
"git.hlsq.asia/mmorpg/service-common/proto/rs/grpc_pb"
"git.hlsq.asia/mmorpg/service-common/proto/ss/ss_pb"
"git.hlsq.asia/mmorpg/service-common/utils"
"git.hlsq.asia/mmorpg/service-scene/internal/npc"
"git.hlsq.asia/alex/service-common/discover"
"git.hlsq.asia/alex/service-common/log"
"git.hlsq.asia/alex/service-common/net/grpc/grpcclient"
"git.hlsq.asia/alex/service-common/proto/rs/rspb"
"git.hlsq.asia/alex/service-common/proto/ss/sspb"
"git.hlsq.asia/alex/service-common/utils"
"git.hlsq.asia/alex/service-scene/internal/npc"
"go.uber.org/zap"
"google.golang.org/protobuf/proto"
"runtime/debug"
@@ -90,25 +90,25 @@ func (i *Instance) Start(ttl int64) {
// 网络帧,将玩家的操作储存起来,到逻辑帧再处理玩家操作
func (i *Instance) onEvent(e proto.Message) {
switch v := e.(type) {
case *grpc_pb.EnterReq:
case *rspb.EnterReq:
i.players[v.USN] = npc.NewPlayerNode(v.GatewaySID, v.USN)
case *grpc_pb.ActionReq:
case *rspb.ActionReq:
if node, ok := i.players[v.USN]; ok {
node.AddAction(v)
}
case *grpc_pb.LeaveReq:
case *rspb.LeaveReq:
delete(i.players, v.USN)
}
}
// 逻辑帧
func (i *Instance) onLogic(delta int64) {
positionUpdate := make([]*ss_pb.PositionInfo, 0)
positionUpdate := make([]*sspb.PositionInfo, 0)
sid := int64(0)
// 处理玩家指令
for _, node := range i.players {
if node.LogicAction(delta) {
positionUpdate = append(positionUpdate, &ss_pb.PositionInfo{
positionUpdate = append(positionUpdate, &sspb.PositionInfo{
USN: node.USN,
X: int32(node.Position[0] * 100),
Y: int32(node.Position[1] * 100),
@@ -117,12 +117,12 @@ func (i *Instance) onLogic(delta int64) {
}
}
if len(positionUpdate) > 0 {
payload, _ := proto.Marshal(&ss_pb.S2C_Position{
payload, _ := proto.Marshal(&sspb.S2C_Position{
Info: positionUpdate,
})
if err := grpc_client.SendMessageToGateway(sid, grpc_client.FunToClient, &grpc_pb.ToClientReq{
if err := grpcclient.SendMessageToGateway(sid, grpcclient.FunToClient, &rspb.ToClientReq{
USN: -1,
MessageID: int32(ss_pb.MessageID_MESSAGE_ID_POSITION),
MessageID: int32(sspb.MessageID_MESSAGE_ID_POSITION),
Payload: payload,
}); err != nil {
i.logger.Errorf("send action err: %v", err)

View File

@@ -1,8 +1,6 @@
package npc
import (
"git.hlsq.asia/mmorpg/service-common/proto/rs/grpc_pb"
)
import "git.hlsq.asia/alex/service-common/proto/rs/rspb"
// NPCNode 定义NPC节点
type NPCNode struct {
@@ -11,7 +9,7 @@ type NPCNode struct {
Position [2]float64 // 二维坐标 [x, y]
MoveCross int8 // 移动十字,1 上 2 下 4 左 8 右
Action []*grpc_pb.ActionReq // 其他操作
Action []*rspb.ActionReq // 其他操作
}
func NewNPCNode(gatewaySID int64, usn int64) *NPCNode {
@@ -20,6 +18,6 @@ func NewNPCNode(gatewaySID int64, usn int64) *NPCNode {
GatewaySID: gatewaySID,
Position: [2]float64{0, 0},
MoveCross: 0,
Action: make([]*grpc_pb.ActionReq, 0),
Action: make([]*rspb.ActionReq, 0),
}
}

View File

@@ -1,8 +1,8 @@
package npc
import (
"git.hlsq.asia/mmorpg/service-common/proto/rs/grpc_pb"
"git.hlsq.asia/mmorpg/service-common/proto/ss/ss_pb"
"git.hlsq.asia/alex/service-common/proto/rs/rspb"
"git.hlsq.asia/alex/service-common/proto/ss/sspb"
)
// PlayerNode 定义玩家节点结构体
@@ -14,7 +14,7 @@ type PlayerNode struct {
Position [2]float32 // 二维坐标 [x, y]
MoveDirection [2]int8 // 移动方向 [x, y]
MoveCount int8 // 移动指令使用计数
Action []*grpc_pb.ActionReq // 其他操作
Action []*rspb.ActionReq // 其他操作
}
func NewPlayerNode(gatewaySID int64, usn int64) *PlayerNode {
@@ -23,12 +23,12 @@ func NewPlayerNode(gatewaySID int64, usn int64) *PlayerNode {
GatewaySID: gatewaySID,
MoveSpeed: 5 * 0.00001,
Position: [2]float32{0, 0},
Action: make([]*grpc_pb.ActionReq, 0),
Action: make([]*rspb.ActionReq, 0),
}
}
// AddAction 将指令存储到玩家身上
func (p *PlayerNode) AddAction(e *grpc_pb.ActionReq) {
func (p *PlayerNode) AddAction(e *rspb.ActionReq) {
p.Action = append(p.Action, e)
}
@@ -36,14 +36,14 @@ func (p *PlayerNode) AddAction(e *grpc_pb.ActionReq) {
// return 是否更新玩家状态
func (p *PlayerNode) LogicAction(delta int64) bool {
for _, a := range p.Action {
switch ss_pb.ActionID(a.Action) {
case ss_pb.ActionID_ACTION_ID_MOVE:
switch sspb.ActionID(a.Action) {
case sspb.ActionID_ACTION_ID_MOVE:
p.MoveDirection = [2]int8{int8(a.DirX), int8(a.DirY)}
p.MoveCount = 0
case ss_pb.ActionID_ACTION_ID_ATTACK:
case sspb.ActionID_ACTION_ID_ATTACK:
}
}
p.Action = make([]*grpc_pb.ActionReq, 0)
p.Action = make([]*rspb.ActionReq, 0)
return p.move(delta)
}

View File

@@ -2,8 +2,8 @@ package main
import (
"fmt"
"git.hlsq.asia/mmorpg/service-scene/app"
"git.hlsq.asia/mmorpg/service-scene/config"
"git.hlsq.asia/alex/service-scene/app"
"git.hlsq.asia/alex/service-scene/config"
"github.com/judwhite/go-svc"
"syscall"
)