Example #1
0
        public static void OnAddBlockReq(Player player, MemoryStream stream)
        {
            Ultilities.Print("OnAddBlockReq");
            CSAddBlockReq req = NetworkManager.Deserialize <CSAddBlockReq>(stream);

            Vector2Int chunk      = Ultilities.GetChunk(req.block.position);
            bool       addSuccess = TerrainData.AddBlockInChunk(chunk, req.block);

            //回包
            CSAddBlockRes res = new CSAddBlockRes();

            if (addSuccess)
            {
                res.RetCode = 0;
                res.block   = req.block;
            }
            else
            {
                res.RetCode = 2;
            }
            NetworkManager.Enqueue(player.socket, ENUM_CMD.CS_ADD_BLOCK_RES, res);
            if (addSuccess)
            {
                //同步给有该chunk视野的其他玩家
                foreach (Player p in TerrainData.GetChunkViewPlayers(chunk))
                {
                    if (p.id != player.id)
                    {
                        AddBlockNotify(p, req.block);
                    }
                }
            }
        }
Example #2
0
        public static void OnDeleteBlockReq(Player player, MemoryStream stream)
        {
            Ultilities.Print("OnDeleteBlockReq");
            CSDeleteBlockReq req = NetworkManager.Deserialize <CSDeleteBlockReq>(stream);

            Vector2Int chunk   = Ultilities.GetChunk(req.position);
            bool       deleted = TerrainData.RemoveBlockInChunk(chunk, req.position);

            CSDeleteBlockRes res = new CSDeleteBlockRes();

            if (deleted)
            {
                res.RetCode  = 0;
                res.position = req.position;
            }
            else
            {
                res.RetCode = 3;
            }
            NetworkManager.Enqueue(player.socket, ENUM_CMD.CS_DELETE_BLOCK_RES, res);
            if (deleted)
            {
                //同步给有该chunk视野的其他玩家
                foreach (Player p in TerrainData.GetChunkViewPlayers(chunk))
                {
                    if (p.id != player.id)
                    {
                        DeleteBlockNotify(p, req.position);
                    }
                }
            }
        }
Example #3
0
        public static void OnRegisterReq(Player player, MemoryStream stream)
        {
            CSRegisterReq req = NetworkManager.Deserialize <CSRegisterReq>(stream);

            Ultilities.Print($"CSRegisterReq,account={req.Account},req.Name={req.Name},req.Password={req.Password}");
            bool hasRegistered = Redis.GetAccountData(req.Account, out AccountData accountData);

            //检测是否已注册
            if (hasRegistered)
            {
                CSRegisterRes res = new CSRegisterRes {
                    RetCode = 8
                };
                NetworkManager.Enqueue(player.socket, ENUM_CMD.CS_REGISTER_RES, res);
            }
            else
            {
                accountData = new AccountData
                {
                    playerID = Redis.GetPlayerIndexAdd(),
                    account  = req.Account,
                    password = req.Password,
                    name     = req.Name
                };
                Ultilities.Print($"SetAccountData,playerID={accountData.playerID},account={accountData.account},password={accountData.password},name={accountData.name}");
                Redis.SetAccountData(accountData.account, accountData);
                CSRegisterRes res = new CSRegisterRes {
                    RetCode = 0
                };
                NetworkManager.Enqueue(player.socket, ENUM_CMD.CS_REGISTER_RES, res);
            }
        }
Example #4
0
        public static void OnChunksEnterLeaveViewReq(Player player, MemoryStream stream)
        {
            CSChunksEnterLeaveViewReq req = NetworkManager.Deserialize <CSChunksEnterLeaveViewReq>(stream);

            Ultilities.Print("CSChunksEnterLeaveViewReq," + req.EnterViewChunks.Count + "," + req.LeaveViewChunks.Count);

            bool leaveView = true;
            bool enterView = ProcessChunksEnterView(player, req.EnterViewChunks, out List <CSChunk> enterViewChunks);
            List <Vector2Int> leaveViewChunks = null;

            if (enterView)
            {
                if (req.LeaveViewChunks.Count > 0)
                {
                    leaveView = ProcessChunksLeaveView(player, req.LeaveViewChunks, out leaveViewChunks);
                }
            }

            if (enterView && leaveView)
            {
                CSChunksEnterLeaveViewRes res = new CSChunksEnterLeaveViewRes
                {
                    RetCode = 0
                };
                res.EnterViewChunks.AddRange(enterViewChunks);
                if (leaveViewChunks != null)
                {
                    res.LeaveViewChunks.AddRange(Vector2Int.Vector2IntList_To_CSVector2IntList(leaveViewChunks));
                }
                Ultilities.Print("CSChunksEnterLeaveViewRes," + res.EnterViewChunks.Count + "," + res.LeaveViewChunks.Count);
                NetworkManager.Enqueue(player.socket, ENUM_CMD.CS_CHUNKS_ENTER_LEAVE_VIEW_RES, res);
            }
            else
            {
                CSChunksEnterLeaveViewRes res = new CSChunksEnterLeaveViewRes
                {
                    RetCode = 4
                };
                Ultilities.Print("CSChunksEnterLeaveViewRes," + res.EnterViewChunks.Count + "," + res.LeaveViewChunks.Count);
                NetworkManager.Enqueue(player.socket, ENUM_CMD.CS_CHUNKS_ENTER_LEAVE_VIEW_RES, res);
            }
        }
Example #5
0
        public static void OnSendMessageReq(Player player, MemoryStream stream)
        {
            CSSendMessageReq req = NetworkManager.Deserialize <CSSendMessageReq>(stream);
            //Console.WriteLine($"OnSendMessageReq,content={req.Content}");

            int retCode = player.inRoom ? 0 : -1;

            if (player.inRoom)
            {
                Ultilities.Print(req.Content, player.name);
            }

            CSSendMessageRes res = new CSSendMessageRes
            {
                RetCode = retCode
            };

            NetworkManager.Enqueue(player.socket, ENUM_CMD.CS_SEND_MESSAGE_RES, res);

            MessageNotify(player.name, req.Content);
        }
Example #6
0
        public static void OnHeroMoveReq(Player player, MemoryStream stream)
        {
            CSHeroMoveReq req = NetworkManager.Deserialize <CSHeroMoveReq>(stream);

            //Ultilities.Print($"CSHeroMoveReq,id={player.id},position=({req.Position.x},{req.Position.y},{req.Position.z}),rotation=({req.Rotation.x},{req.Rotation.y},{req.Rotation.z})");
            player.position = new Vector3(req.Position.x, req.Position.y, req.Position.z);
            player.rotation = new Vector3(req.Rotation.x, req.Rotation.y, req.Rotation.z);

            //写数据库
            Redis.SetPlayerData(player.id, new CSPlayer
            {
                PlayerID = player.id,
                Name     = player.name,
                Position = new CSVector3 {
                    x = player.position.x, y = player.position.y, z = player.position.z
                },
                Rotation = new CSVector3 {
                    x = player.rotation.x, y = player.rotation.y, z = player.rotation.z
                },
            });

            //玩家的移动同步给看得到这个chunk的其他玩家
            List <Player> playersInChunk = TerrainData.GetChunkViewPlayers(player.curChunk);

            foreach (Player p in playersInChunk)
            {
                if (p.id != player.id)
                {
                    CSPlayerMoveNotify notify = new CSPlayerMoveNotify
                    {
                        PlayerID = player.id,
                        Position = req.Position,
                        Rotation = req.Rotation
                    };
                    NetworkManager.Enqueue(p.socket, ENUM_CMD.CS_PLAYER_MOVE_NOTIFY, notify);
                }
            }
        }
Example #7
0
        public static void OnLoginReq(Player player, MemoryStream stream)
        {
            CSLoginReq req = NetworkManager.Deserialize <CSLoginReq>(stream);

            Ultilities.Print($"CSLoginReq,account={req.Account}");

            //检测是否已注册
            bool bAccountExist = Redis.GetAccountData(req.Account, out AccountData accountData);

            //Ultilities.Print($"bAccountExist={bAccountExist}");
            if (!bAccountExist)
            {
                CSLoginRes res = new CSLoginRes {
                    RetCode = 6
                };
                NetworkManager.Enqueue(player.socket, ENUM_CMD.CS_LOGIN_RES, res);
            }
            else
            {
                //检测是否已登录,防止顶号
                bool hasLoggedIn = players.ContainsKey(accountData.playerID);
                //Ultilities.Print($"hasLoggedIn={hasLoggedIn}");
                if (hasLoggedIn)
                {
                    CSLoginRes res = new CSLoginRes {
                        RetCode = 5
                    };
                    NetworkManager.Enqueue(player.socket, ENUM_CMD.CS_LOGIN_RES, res);
                }
                else
                {
                    //检查密码是否正确
                    bool bCorrect = req.Password.Equals(accountData.password);
                    //Ultilities.Print($"req.Password={req.Password},accountData.password={accountData.password},bCorrect={bCorrect}");
                    if (!bCorrect)
                    {
                        CSLoginRes res = new CSLoginRes {
                            RetCode = 7
                        };
                        NetworkManager.Enqueue(player.socket, ENUM_CMD.CS_LOGIN_RES, res);
                    }
                    else
                    {
                        bool hasData = Redis.GetPlayerData(accountData.playerID, out CSPlayer playerData);
                        //Ultilities.Print($"hasData={hasData}");
                        if (!hasData)
                        {
                            //如果是第一次登陆,则写数据库
                            playerData = new CSPlayer
                            {
                                PlayerID = accountData.playerID,
                                Name     = accountData.name,
                                Position = new CSVector3 {
                                    x = 0, y = 30, z = 0
                                },
                                Rotation = new CSVector3 {
                                    x = 0, y = 0, z = 0
                                }
                            };
                            Redis.SetPlayerData(playerData.PlayerID, playerData);
                        }

                        AddPlayer(player, playerData);
                        //Ultilities.Print($"playerData,playerID={accountData.playerID},account={accountData.account},password={accountData.password},name={accountData.name}");
                        //Ultilities.Print($"player,id={player.id},name={player.name}");

                        CSLoginRes res = new CSLoginRes
                        {
                            RetCode    = 0,
                            PlayerData = playerData
                        };
                        NetworkManager.Enqueue(player.socket, ENUM_CMD.CS_LOGIN_RES, res);

                        MessageNotify("system", "player " + player.name + " has logged in, current number of player : " + players.Count);
                    }
                }
            }
        }