Files
service-robot/internal/ws/handler.go

99 lines
2.0 KiB
Go

package ws
import (
"git.hlsq.asia/mmorpg/service-common/log"
"git.hlsq.asia/mmorpg/service-common/proto/ss/ss_pb"
"google.golang.org/protobuf/proto"
"math"
"math/rand"
"sync"
"time"
)
type LoginSuccess struct {
}
func (_ *LoginSuccess) Handle(data []byte, client *Client) {
msg := &ss_pb.S2C_LoginSuccess{}
if err := proto.Unmarshal(data, msg); err != nil {
log.Errorf("handle msg error")
client.Stop()
return
}
_ = client.WriteMessage(ss_pb.MessageID_MESSAGE_ID_ENTER_INSTANCE, &ss_pb.C2S_EnterInstance{
InstanceID: msg.InstanceID,
})
}
type QueueUp struct {
}
func (_ *QueueUp) Handle(data []byte, client *Client) {
msg := &ss_pb.S2C_QueueUp{}
if err := proto.Unmarshal(data, msg); err != nil {
log.Errorf("handle msg error")
client.Stop()
return
}
}
type EnterInstance struct {
}
func (_ *EnterInstance) Handle(data []byte, client *Client) {
msg := &ss_pb.S2C_EnterInstance{}
if err := proto.Unmarshal(data, msg); err != nil {
log.Errorf("handle msg error")
client.Stop()
return
}
randDir := func() (float64, float64) {
randX := float64(rand.Intn(201) - 100)
randY := float64(rand.Intn(201) - 100)
var normalizedX, normalizedY float64
if length := math.Sqrt(randX*randX + randY*randY); length > 0 {
normalizedX = randX / length
normalizedY = randY / length
}
return normalizedX, normalizedY
}
go func() {
for {
x, y := randDir()
_ = client.WriteMessage(ss_pb.MessageID_MESSAGE_ID_ACTION, &ss_pb.C2S_Action{
Sequence: 0,
Timestamp: 0,
Action: ss_pb.ActionID_ACTION_ID_MOVE,
DirX: int32(x * 100),
DirY: int32(y * 100),
SkillID: 0,
})
time.Sleep(500 * time.Millisecond)
}
}()
}
var s2cPositionPool = sync.Pool{
New: func() interface{} {
return &ss_pb.Message{}
},
}
type Position struct {
}
func (_ *Position) Handle(data []byte, client *Client) {
//msg := s2cPositionPool.Get().(*ss_pb.Message)
//if err := proto.Unmarshal(data, msg); err != nil {
// log.Errorf("handle msg error")
// client.Stop()
// return
//}
//s2cPositionPool.Put(msg)
}