2023-03-27 22:02:13 +00:00
|
|
|
package shard
|
2023-03-17 21:27:47 +00:00
|
|
|
|
|
|
|
import (
|
2023-12-01 06:56:34 +00:00
|
|
|
"context"
|
|
|
|
|
2023-06-22 06:53:38 +00:00
|
|
|
"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"
|
2023-11-28 03:23:28 +00:00
|
|
|
"github.com/CPunch/gopenfusion/internal/service"
|
2023-03-17 21:27:47 +00:00
|
|
|
)
|
|
|
|
|
2023-03-27 22:02:13 +00:00
|
|
|
type PacketHandler func(peer *protocol.CNPeer, pkt protocol.Packet) error
|
|
|
|
|
2023-03-17 21:27:47 +00:00
|
|
|
type ShardServer struct {
|
2023-11-28 03:23:28 +00:00
|
|
|
service *service.Service
|
|
|
|
dbHndlr *db.DBHandler
|
|
|
|
redisHndlr *redis.RedisHandler
|
|
|
|
chunks map[entity.ChunkPosition]*entity.Chunk
|
2023-03-17 21:27:47 +00:00
|
|
|
}
|
|
|
|
|
2023-12-01 06:56:34 +00:00
|
|
|
func NewShardServer(ctx context.Context, dbHndlr *db.DBHandler, redisHndlr *redis.RedisHandler, port int) (*ShardServer, error) {
|
|
|
|
srvc := service.NewService(ctx, "SHARD", port)
|
2023-03-17 21:27:47 +00:00
|
|
|
|
|
|
|
server := &ShardServer{
|
2023-11-28 03:23:28 +00:00
|
|
|
service: srvc,
|
|
|
|
dbHndlr: dbHndlr,
|
|
|
|
redisHndlr: redisHndlr,
|
|
|
|
chunks: make(map[entity.ChunkPosition]*entity.Chunk),
|
2023-03-17 21:27:47 +00:00
|
|
|
}
|
|
|
|
|
2023-11-28 03:23:28 +00:00
|
|
|
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
|
2023-03-17 21:27:47 +00:00
|
|
|
|
2023-06-22 06:53:38 +00:00
|
|
|
redisHndlr.RegisterShard(redis.ShardMetadata{
|
|
|
|
IP: config.GetAnnounceIP(),
|
|
|
|
Port: port,
|
|
|
|
})
|
|
|
|
|
2023-03-17 21:27:47 +00:00
|
|
|
return server, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (server *ShardServer) Start() {
|
2023-06-27 05:36:02 +00:00
|
|
|
server.LoadNPCs()
|
2023-11-28 03:23:28 +00:00
|
|
|
server.service.Start()
|
2023-03-17 21:27:47 +00:00
|
|
|
}
|
|
|
|
|
2023-12-01 06:56:34 +00:00
|
|
|
func (server *ShardServer) onDisconnect(peer *protocol.CNPeer) {
|
2023-11-28 03:23:28 +00:00
|
|
|
// remove from chunks
|
2023-12-01 06:56:34 +00:00
|
|
|
plr, ok := peer.UserData().(*entity.Player)
|
|
|
|
if ok && plr != nil {
|
|
|
|
server.removeEntity(plr)
|
2023-06-25 08:33:17 +00:00
|
|
|
}
|
2023-03-17 21:27:47 +00:00
|
|
|
}
|
|
|
|
|
2023-12-01 06:56:34 +00:00
|
|
|
func (server *ShardServer) onConnect(peer *protocol.CNPeer) {
|
|
|
|
|
2023-03-17 21:27:47 +00:00
|
|
|
}
|