gopenfusion/shard/shardserver.go

69 lines
2.0 KiB
Go
Raw Normal View History

2023-03-27 22:02:13 +00:00
package shard
import (
"context"
2023-12-01 23:11:41 +00:00
"github.com/CPunch/gopenfusion/cnet"
"github.com/CPunch/gopenfusion/cnet/protocol"
2023-12-03 04:09:11 +00:00
"github.com/CPunch/gopenfusion/internal/config"
2023-08-23 23:16:24 +00:00
"github.com/CPunch/gopenfusion/internal/db"
"github.com/CPunch/gopenfusion/internal/redis"
2023-12-01 22:56:55 +00:00
"github.com/CPunch/gopenfusion/shard/entity"
)
2023-12-02 01:15:00 +00:00
type PacketHandler func(peer *cnet.Peer, pkt protocol.Packet) error
2023-03-27 22:02:13 +00:00
type ShardServer struct {
service *cnet.Service
dbHndlr *db.DBHandler
redisHndlr *redis.RedisHandler
chunks map[entity.ChunkPosition]*entity.Chunk
}
func NewShardServer(ctx context.Context, dbHndlr *db.DBHandler, redisHndlr *redis.RedisHandler, port int) (*ShardServer, error) {
srvc := cnet.NewService(ctx, "SHARD", port)
server := &ShardServer{
service: srvc,
dbHndlr: dbHndlr,
redisHndlr: redisHndlr,
chunks: make(map[entity.ChunkPosition]*entity.Chunk),
}
srvc.AddPacketHandler(protocol.P_CL2FE_REQ_PC_ENTER, server.RequestEnter)
srvc.AddPacketHandler(protocol.P_CL2FE_REQ_PC_LOADING_COMPLETE, server.LoadingComplete)
srvc.AddPacketHandler(protocol.P_CL2FE_REQ_PC_MOVE, server.playerMove)
srvc.AddPacketHandler(protocol.P_CL2FE_REQ_PC_STOP, server.playerStop)
srvc.AddPacketHandler(protocol.P_CL2FE_REQ_PC_JUMP, server.playerJump)
srvc.AddPacketHandler(protocol.P_CL2FE_REQ_SEND_FREECHAT_MESSAGE, server.freeChat)
srvc.AddPacketHandler(protocol.P_CL2FE_REQ_SEND_MENUCHAT_MESSAGE, server.menuChat)
srvc.AddPacketHandler(protocol.P_CL2FE_REQ_PC_AVATAR_EMOTES_CHAT, server.emoteChat)
srvc.OnConnect = server.onConnect
srvc.OnDisconnect = server.onDisconnect
redisHndlr.RegisterShard(redis.ShardMetadata{
IP: config.GetAnnounceIP(),
Port: port,
})
return server, nil
}
func (server *ShardServer) Start() {
server.LoadNPCs()
server.service.Start()
}
2023-12-02 01:15:00 +00:00
func (server *ShardServer) onDisconnect(peer *cnet.Peer) {
// remove from chunks
plr, ok := peer.UserData().(*entity.Player)
if ok && plr != nil {
server.removeEntity(plr)
2023-06-25 08:33:17 +00:00
}
}
2023-12-02 01:15:00 +00:00
func (server *ShardServer) onConnect(peer *cnet.Peer) {
}