2023-03-27 22:02:13 +00:00
|
|
|
package shard
|
2023-03-17 21:27:47 +00:00
|
|
|
|
2023-03-22 22:07:16 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
2023-06-22 06:53:38 +00:00
|
|
|
"log"
|
2023-03-17 21:27:47 +00:00
|
|
|
|
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-08-23 23:16:24 +00:00
|
|
|
"github.com/CPunch/gopenfusion/internal/redis"
|
2023-12-01 22:56:55 +00:00
|
|
|
"github.com/CPunch/gopenfusion/shard/entity"
|
2023-12-01 21:29:19 +00:00
|
|
|
"github.com/CPunch/gopenfusion/util"
|
2023-03-22 22:07:16 +00:00
|
|
|
)
|
|
|
|
|
2023-12-02 01:15:00 +00:00
|
|
|
func (server *ShardServer) attachPlayer(peer *cnet.Peer, meta redis.LoginMetadata) (*entity.Player, error) {
|
2023-11-28 03:23:28 +00:00
|
|
|
dbPlr, err := server.dbHndlr.GetPlayer(int(meta.PlayerID))
|
2023-06-25 06:51:21 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-11-28 03:23:28 +00:00
|
|
|
plr := entity.NewPlayer(peer, dbPlr)
|
2023-06-25 06:51:21 +00:00
|
|
|
|
2023-11-28 03:23:28 +00:00
|
|
|
// once we create the player, it's memory address is owned by the
|
|
|
|
// server.Start() goroutine. the only functions allowed to access
|
|
|
|
// it are the packet handlers as no other goroutines will be
|
|
|
|
// concurrently accessing it.
|
2023-12-01 06:56:34 +00:00
|
|
|
peer.SetUserData(plr)
|
2023-06-25 08:33:17 +00:00
|
|
|
return plr, nil
|
2023-06-25 06:51:21 +00:00
|
|
|
}
|
|
|
|
|
2023-12-02 01:15:00 +00:00
|
|
|
func (server *ShardServer) RequestEnter(peer *cnet.Peer, pkt protocol.Packet) error {
|
2023-03-18 21:40:20 +00:00
|
|
|
var enter protocol.SP_CL2FE_REQ_PC_ENTER
|
|
|
|
pkt.Decode(&enter)
|
|
|
|
|
2023-11-28 03:23:28 +00:00
|
|
|
// resending a shard enter packet?
|
2023-12-01 06:56:34 +00:00
|
|
|
_plr, ok := peer.UserData().(*entity.Player)
|
|
|
|
if ok && _plr != nil {
|
2023-11-28 03:23:28 +00:00
|
|
|
return fmt.Errorf("resent enter packet")
|
|
|
|
}
|
|
|
|
|
2023-06-22 06:53:38 +00:00
|
|
|
loginData, err := server.redisHndlr.GetLogin(enter.IEnterSerialKey)
|
2023-03-22 05:30:58 +00:00
|
|
|
if err != nil {
|
|
|
|
// the error codes for P_FE2CL_REP_PC_ENTER_FAIL aren't referenced in the client :(
|
|
|
|
peer.Send(protocol.P_FE2CL_REP_PC_ENTER_FAIL, protocol.SP_FE2CL_REP_PC_ENTER_FAIL{})
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-06-25 06:51:21 +00:00
|
|
|
plr, err := server.attachPlayer(peer, loginData)
|
|
|
|
if err != nil {
|
2023-03-27 02:08:13 +00:00
|
|
|
return err
|
2023-03-22 22:07:16 +00:00
|
|
|
}
|
|
|
|
|
2023-06-25 06:51:21 +00:00
|
|
|
resp := &protocol.SP_FE2CL_REP_PC_ENTER_SUCC{
|
|
|
|
IID: int32(plr.PlayerID),
|
|
|
|
PCLoadData2CL: plr.ToPCLoadData2CL(),
|
2023-12-01 21:29:19 +00:00
|
|
|
UiSvrTime: util.GetTime(),
|
2023-06-25 06:51:21 +00:00
|
|
|
}
|
2023-03-22 22:07:16 +00:00
|
|
|
|
2023-06-25 03:36:04 +00:00
|
|
|
// setup peer
|
2023-06-25 06:51:21 +00:00
|
|
|
peer.E_key = protocol.CreateNewKey(resp.UiSvrTime, uint64(resp.IID+1), uint64(resp.PCLoadData2CL.IFusionMatter+1))
|
|
|
|
peer.FE_key = loginData.FEKey
|
2023-12-01 23:11:41 +00:00
|
|
|
peer.SetActiveKey(cnet.USE_FE)
|
2023-06-25 03:36:04 +00:00
|
|
|
|
2023-06-22 23:12:31 +00:00
|
|
|
log.Printf("Player %d (AccountID %d) entered\n", resp.IID, loginData.AccountID)
|
2023-06-27 05:36:02 +00:00
|
|
|
if err := peer.Send(protocol.P_FE2CL_REP_PC_ENTER_SUCC, resp); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2023-03-22 22:07:16 +00:00
|
|
|
}
|
|
|
|
|
2023-12-02 01:15:00 +00:00
|
|
|
func (server *ShardServer) LoadingComplete(peer *cnet.Peer, pkt protocol.Packet) error {
|
2023-03-22 22:07:16 +00:00
|
|
|
var loadComplete protocol.SP_CL2FE_REQ_PC_LOADING_COMPLETE
|
|
|
|
pkt.Decode(&loadComplete)
|
|
|
|
|
2023-11-28 03:23:28 +00:00
|
|
|
// was the peer attached to a player?
|
2023-12-01 06:56:34 +00:00
|
|
|
plr, ok := peer.UserData().(*entity.Player)
|
|
|
|
if !ok || plr == nil {
|
2023-11-28 03:23:28 +00:00
|
|
|
return fmt.Errorf("loadingComplete: plr is nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
err := peer.Send(protocol.P_FE2CL_REP_PC_LOADING_COMPLETE_SUCC, protocol.SP_FE2CL_REP_PC_LOADING_COMPLETE_SUCC{IPC_ID: int32(plr.PlayerID)})
|
2023-06-25 03:36:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2023-03-22 22:07:16 +00:00
|
|
|
}
|
|
|
|
|
2023-11-28 03:23:28 +00:00
|
|
|
// we send the chunk updates (PC_NEW, NPC_NEW, etc.) after the enter packet
|
|
|
|
chunkPos := entity.MakeChunkPosition(plr.X, plr.Y)
|
|
|
|
viewableChunks := server.getViewableChunks(chunkPos)
|
2023-12-06 01:17:58 +00:00
|
|
|
|
2023-11-28 03:23:28 +00:00
|
|
|
plr.SetChunkPos(chunkPos)
|
2023-12-06 01:17:58 +00:00
|
|
|
server.getChunk(chunkPos).AddEntity(plr)
|
2023-11-28 03:23:28 +00:00
|
|
|
server.addEntityToChunks(plr, viewableChunks)
|
|
|
|
return nil
|
2023-03-17 21:27:47 +00:00
|
|
|
}
|