Example #1
0
        /// <summary>
        /// 离开房间
        /// </summary>
        /// <param name="player"></param>
        /// <param name="protocolBase"></param>
        public void MsgLeaveRoom(Player player, ProtocolBase protocolBase)
        {
            ProtocolBytes protocol = new ProtocolBytes();

            protocol.AddString("LeaveRoom");

            //条件检测,如果玩家不在房间,则返回 -1,表示离开房间失败
            if (player.tempData.status != PlayerTempData.Status.Room)
            {
                Console.WriteLine("MsgLeaveRoom status error " + player.id);
                protocol.AddInt(-1);
                player.Send(protocol);
                return;
            }

            //处理,返回 0 表示离开房间成功
            protocol.AddInt(0);
            player.Send(protocol);
            Room room = player.tempData.room;

            RoomMgr.instance.LeaveRoom(player);
            if (room == null)
            {
                room.Broadcast(room.GetRoomInfo());
            }
        }
Example #2
0
        //注册
        //协议参数:str 用户名,str 密码
        //返回协议:-1 表示失败,0表示成功
        public void MsgRegister(Conn conn, ProtocolBase protoBase)
        {
            //获取数值
            int           start     = 0;
            ProtocolBytes protocol  = (ProtocolBytes)protoBase;
            string        protoName = protocol.GetString(start, ref start);
            string        id        = protocol.GetString(start, ref start);
            string        pw        = protocol.GetString(start, ref start);
            string        strFormat = "[HandleConnMsg -> MsgRegister() ] 收到  [" + conn.GetAddress() + "]  的注册协议    ";

            Console.WriteLine(strFormat + "用户名:" + id + " 密码:" + pw);

            //构建返回协议
            protocol = new ProtocolBytes();
            protocol.AddString("Register");

            //注册
            if (DataMgr.instance.Register(id, pw))
            {
                protocol.AddInt(0);
            }
            else
            {
                protocol.AddInt(-1);
            }
            //创建角色
            DataMgr.instance.CreatePlayer(id);

            //返回协议给客户端
            conn.Send(protocol);
        }
Example #3
0
        //开启战斗
        public void StartFight()
        {
            ProtocolBytes protocol = new ProtocolBytes();

            protocol.AddString("Fight");
            status = Status.Fight;
            int teamPos1 = 1;
            int teamPos2 = 1;

            lock (playerList)
            {
                protocol.AddInt(playerList.Count);
                foreach (Player p in playerList.Values)
                {
                    p.tempData.hp = 200;
                    protocol.AddString(p.id);
                    protocol.AddInt(p.tempData.team);
                    if (p.tempData.team == 1)
                    {
                        protocol.AddInt(teamPos1++);
                    }
                    else
                    {
                        protocol.AddInt(teamPos2++);
                    }
                    p.tempData.status = PlayerTempData.Status.Fight;
                }
                Broadcast(protocol);
            }
        }
Example #4
0
        //登录
        //协议参数:str用户名,str密码
        //返回协议:-1表示失败,0表示成功
        public void MsgLogin(Conn conn, ProtocolBase protoBase)
        {
            //获取数值
            int           start     = 0;
            ProtocolBytes protocol  = (ProtocolBytes)protoBase;
            string        protoName = protocol.GetString(start, ref start);
            string        id        = protocol.GetString(start, ref start);
            string        pw        = protocol.GetString(start, ref start);
            string        strFormat = "[HandleConnMsg -> MsgLogin() ] 收到  [" + conn.GetAddress() + "]  的登录协议    ";

            Console.WriteLine(strFormat + "用户名:" + id + " 密码:" + pw);

            //构建返回协议
            ProtocolBytes protocolRet = new ProtocolBytes();

            protocolRet.AddString("Login");


            //验证
            if (!DataMgr.instance.CheckPassword(id, pw))
            {
                Console.WriteLine("[登录检查失败]");
                protocolRet.AddInt(-1);
                conn.Send(protocolRet);
                return;
            }

            //是否已经登录
            ProtocolBytes protocolLogout = new ProtocolBytes();

            protocolLogout.AddString("Logout");
            if (!Player.KickOff(id, protocolLogout))
            {
                protocolRet.AddInt(-1);
                conn.Send(protocolRet);
                return;
            }

            //获取玩家数据
            PlayerData playerData = DataMgr.instance.GetPlayerData(id);

            if (playerData == null)
            {
                protocolRet.AddInt(-1);
                conn.Send(protocolRet);
                return;
            }
            conn.player      = new Player(id, conn);
            conn.player.data = playerData;

            //事件触发
            ServNet.instance.handlePlayerEvent.OnLogin(conn.player);

            //登录成功,回复0
            protocolRet.AddInt(0);
            conn.Send(protocolRet);
            return;
        }
Example #5
0
        //获取玩家成绩
        public void MsgGetAchieve(Player player, ProtocolBase protocol)
        {
            ProtocolBytes protocolRet = new ProtocolBytes();

            protocolRet.AddString("GetAchieve");
            protocolRet.AddInt(player.data.win);
            protocolRet.AddInt(player.data.fail);
            player.Send(protocolRet);
            Console.WriteLine("MsgGetScore " + player.id + player.data.win);
        }
Example #6
0
        /// <summary>
        /// 加入房间
        /// </summary>
        /// <param name="player"></param>
        /// <param name="protocolBase"></param>
        public void MsgEnterRoom(Player player, ProtocolBase protocolBase)
        {
            //获取数值
            int           start     = 0;
            ProtocolBytes protocol  = (ProtocolBytes)protocolBase;
            string        protoName = protocol.GetString(start, ref start);
            int           index     = protocol.GetInt(start, ref start);

            Console.WriteLine("[收到 MsgEnterRoom] " + player.id + "  加入房间号: " + index);

            //构建进入房间的返回协议
            protocol = new ProtocolBytes();
            protocol.AddString("EnterRoom");

            //判断房间是否存在
            if (index < 0 || index >= RoomMgr.instance.roomList.Count)
            {
                Console.WriteLine("MsgEnterRoom index error " + player.id);
                protocol.AddInt(-1);
                player.Send(protocol);
                return;
            }


            //判断房间的状态,只有处于“准备中”的房间才允许玩家加入
            Room room = RoomMgr.instance.roomList[index];

            if (room.status != Room.Status.Prepare)
            {
                Console.WriteLine("MsgEnterRoom status error " + player.id);
                protocol.AddInt(-1);
                player.Send(protocol);
                return;
            }

            //把玩家添加到房间,加入成功返回 0,加入失败返回 -1
            if (room.AddPlayer(player))
            {
                room.Broadcast(room.GetRoomInfo());
                protocol.AddInt(0);
                player.Send(protocol);
            }
            else
            {
                Console.WriteLine("MsgEnterRoom maxPlayer error " + player.id);
                protocol.AddInt(-1);
                player.Send(protocol);
            }
        }
Example #7
0
        //更新输赢状态
        public void UpdateWin()
        {
            int isWin = IsWin();

            if (isWin == 0)
            {
                return;
            }
            //改变状态 数值处理
            lock (playerList)
            {
                status = Status.Prepare;
                foreach (Player player in playerList.Values)
                {
                    player.tempData.status = PlayerTempData.Status.Room;
                    if (player.tempData.team == isWin)
                    {
                        player.data.win++;
                    }
                    else
                    {
                        player.data.fail++;
                    }
                }
            }

            Console.WriteLine("开始广播战斗结果");
            //广播
            ProtocolBytes protocol = new ProtocolBytes();

            protocol.AddString("Result");
            protocol.AddInt(isWin);
            Broadcast(protocol);
        }
Example #8
0
        //发送列表
        public void SendPlayerList(Player player)
        {
            int           count    = playerList.Count;
            ProtocolBytes protocol = new ProtocolBytes();

            protocol.AddString("GetList");
            protocol.AddInt(count);
            for (int i = 0; i < count; i++)
            {
                ScenePlayer p = playerList[i];
                protocol.AddString(p.id);
                protocol.AddFloat(p.x);
                protocol.AddFloat(p.y);
                protocol.AddFloat(p.z);
                protocol.AddInt(p.score);
            }
            player.Send(protocol);
        }
Example #9
0
        /// <summary>
        /// 构建返回房间列表协议GetRoomList,
        /// 第一个参数为房间数量count,紧接着附带count个房间数据,依次为房间里的玩家数量count,房间状态status
        /// </summary>
        /// <returns></returns>
        public ProtocolBytes GetRoomList()
        {
            ProtocolBytes protocol = new ProtocolBytes();

            protocol.AddString("GetRoomList");
            int count = roomList.Count;

            //往协议添加房间数量
            protocol.AddInt(count);

            //循环添加每个房间的信息
            for (int i = 0; i < count; i++)
            {
                Room room = roomList[i];
                protocol.AddInt(room.playerList.Count);
                protocol.AddInt((int)room.status);
            }
            return(protocol);
        }
Example #10
0
        //开始战斗
        public void MsgStartFight(Player player, ProtocolBase protocolBase)
        {
            ProtocolBytes protocol = new ProtocolBytes();

            protocol.AddString("StartFight");

            //条件判断
            //玩家是否在房间里
            if (player.tempData.status != PlayerTempData.Status.Room)
            {
                Console.WriteLine("MsgStartFight status error " + player.id);
                protocol.AddInt(-1);
                player.Send(protocol);
                return;
            }

            //玩家是否为房主
            if (!player.tempData.isOwner)
            {
                Console.WriteLine("MsgStartFigth owner error " + player.id);
                protocol.AddInt(-1);
                player.Send(protocol);
                return;
            }

            //房间能否开启战斗
            Room room = player.tempData.room;

            if (!room.IsCanStart())
            {
                Console.WriteLine("MsgStartFight Can Start Error" + player.id);
                protocol.AddInt(-1);
                player.Send(protocol);
                return;
            }

            //开始战斗
            protocol.AddInt(0);
            player.Send(protocol);
            room.StartFight();
        }
Example #11
0
        /// <summary>
        /// 创建房间
        /// </summary>
        /// <param name="player"></param>
        /// <param name="protocolBase"></param>
        public void MsgCreateRoom(Player player, ProtocolBase protocolBase)
        {
            ProtocolBytes protocol = new ProtocolBytes();

            protocol.AddString("CreateRoom");

            //条件检测,如果玩家的状态是在房间中或是战斗中,则不能创建房间,返回创建房间失败
            if (player.tempData.status != PlayerTempData.Status.None)
            {
                Console.WriteLine("MsgCreateRoom Fail" + player.id);
                protocol.AddInt(-1);
                player.Send(protocol);
                return;
            }

            //创建房间并返回成功信息
            RoomMgr.instance.CreateRoom(player);
            protocol.AddInt(0);
            player.Send(protocol);
            Console.WriteLine("MsgCreateRoom OK" + player.id);
        }
Example #12
0
        //房间信息
        public ProtocolBytes GetRoomInfo()
        {
            ProtocolBytes protocol = new ProtocolBytes();

            protocol.AddString("GetRoomInfo");

            //添加房间玩家数量
            protocol.AddInt(playerList.Count);

            //循环添加每个玩家的信息
            foreach (Player p in playerList.Values)
            {
                protocol.AddString(p.id);
                protocol.AddInt(p.tempData.team);
                protocol.AddInt(p.data.win);
                protocol.AddInt(p.data.fail);
                int isOwner = p.tempData.isOwner ? 1 : 0;
                protocol.AddInt(isOwner);
            }
            return(protocol);
        }
Example #13
0
        //下线
        //协议参数:无
        //返回协议:0 正常下线
        public void MsgLogout(Conn conn, ProtocolBase protocolBase)
        {
            ProtocolBytes protocol = new ProtocolBytes();

            protocol.AddString("Logout");
            protocol.AddInt(0);
            if (conn.player == null)
            {
                conn.Send(protocol);
                conn.Close();
            }
            else
            {
                conn.Send(protocol);
                conn.player.Logout();
            }
        }
Example #14
0
        //更新信息
        public void MsgUpdateInfo(Player player, ProtocolBase protocolBase)
        {
            //获取数值
            int           start     = 0;
            ProtocolBytes protocol  = (ProtocolBytes)protocolBase;
            string        protoName = protocol.GetString(start, ref start);
            float         x         = protocol.GetFloat(start, ref start);
            float         y         = protocol.GetFloat(start, ref start);
            float         z         = protocol.GetFloat(start, ref start);
            int           score     = player.data.score;

            Scene.instance.UpdateInfo(player.id, x, y, z, score);

            //广播
            ProtocolBytes protocolRet = new ProtocolBytes();

            protocolRet.AddString("UpdateInfo");
            protocolRet.AddString(player.id);
            protocolRet.AddFloat(x);
            protocolRet.AddFloat(y);
            protocolRet.AddFloat(z);
            protocolRet.AddInt(score);
            ServNet.instance.Broadcast(protocolRet);
        }