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-12-01 23:11:41 +00:00
|
|
|
"github.com/CPunch/gopenfusion/cnet"
|
2023-12-02 01:56:23 +00:00
|
|
|
"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-03-17 21:27:47 +00:00
|
|
|
)
|
|
|
|
|
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
|
|
|
|
2023-03-17 21:27:47 +00:00
|
|
|
type ShardServer struct {
|
2023-12-02 01:22:49 +00:00
|
|
|
service *cnet.Service
|
2023-11-28 03:23:28 +00:00
|
|
|
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) {
|
2023-12-02 01:22:49 +00:00
|
|
|
srvc := cnet.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-02 01:15:00 +00:00
|
|
|
func (server *ShardServer) onDisconnect(peer *cnet.Peer) {
|
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-02 01:15:00 +00:00
|
|
|
func (server *ShardServer) onConnect(peer *cnet.Peer) {
|
2023-12-01 06:56:34 +00:00
|
|
|
|
2023-03-17 21:27:47 +00:00
|
|
|
}
|