gopenfusion/shard/shardserver.go

66 lines
2.0 KiB
Go
Raw Normal View History

2023-03-27 22:02:13 +00:00
package shard
import (
"github.com/CPunch/gopenfusion/config"
2023-08-23 23:16:24 +00:00
"github.com/CPunch/gopenfusion/internal/db"
"github.com/CPunch/gopenfusion/internal/entity"
"github.com/CPunch/gopenfusion/internal/protocol"
"github.com/CPunch/gopenfusion/internal/redis"
"github.com/CPunch/gopenfusion/internal/service"
)
2023-03-27 22:02:13 +00:00
type PacketHandler func(peer *protocol.CNPeer, pkt protocol.Packet) error
type ShardServer struct {
service *service.Service
dbHndlr *db.DBHandler
redisHndlr *redis.RedisHandler
chunks map[entity.ChunkPosition]*entity.Chunk
}
func NewShardServer(dbHndlr *db.DBHandler, redisHndlr *redis.RedisHandler, port int) (*ShardServer, error) {
srvc := service.NewService("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()
}
func (server *ShardServer) onDisconnect(peer *protocol.CNPeer, _plr interface{}) {
// remove from chunks
if _plr != nil {
server.removeEntity(_plr.(*entity.Player))
2023-06-25 08:33:17 +00:00
}
}
func (server *ShardServer) onConnect(peer *protocol.CNPeer) interface{} {
return nil
}