//开始战斗
        //参数:无
        //返回协议:-1代表失败,0代表成功
        public void MsgStartFight(Player player, ProtocolBase protocol)
        {
            //是否在房间内
            if (player.tempData.status != PlayerTempData.Statue.Room)
            {
                Console.WriteLine("MsgStartFight 玩家" + player.id + "并不在房间中");
                protocol.AddInt(-1);
                player.Send(protocol);
                return;
            }

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

            Room room = player.tempData.room;

            if (!room.CanStartFight())
            {
                Console.WriteLine("MsgStartFight 房间不满足开站条件");
                protocol.AddInt(-1);
                player.Send(protocol);
                return;
            }

            protocol.AddInt(0);
            player.Send(protocol);
            RoomSystem.instance.BroadcastInRoom(room, protocol);
            RoomSystem.instance.StartFightForRoom(room);
        }
        //玩家拾取盒子
        //参数:盒子id
        //返回协议:0代表成功,-1代表失败
        //          广播 要删除的Box的ID
        public void MsgPlayerAvatarGetBox(Player player, ProtocolBase protocol)
        {
            int    start     = 0;
            string protoName = protocol.GetString(start, ref start);
            string boxID     = protocol.GetString(start, ref start);

            bool flag = false;

            lock (player.tempData.room.boxSet) {
                if (player.tempData.room.boxSet.Contains(boxID))        //J...
                {
                    player.tempData.room.boxSet.Remove(boxID);          //J...
                    flag = true;
                }
            }

            ProtocolBase protocolBack = ServNet.instance.proto.Decode(null, 0, 0);

            protocolBack.AddString("PlayerAvatarGetBox");
            if (flag)
            {
                protocolBack.AddInt(0);

                ProtocolBase protocolBroadcast = ServNet.instance.proto.Decode(null, 0, 0);
                protocolBroadcast.AddString("DelBox");
                protocolBroadcast.AddString(boxID);
                RoomSystem.instance.BroadcastInRoom(player.tempData.room, protocolBroadcast);
            }
            else
            {
                protocolBack.AddInt(-1);
            }
            player.Send(protocolBack);
        }
Exemplo n.º 3
0
        //注册
        //协议参数:用户名,密码
        //返回协议:-1代表失败,0代表成功

        public void MsgRegister(Conn conn, ProtocolBase protocol)
        {
            //解析出所有参数
            int    start     = 0;
            string protoName = protocol.GetString(start, ref start);
            string id        = protocol.GetString(start, ref start);
            string pw        = protocol.GetString(start, ref start);

            Console.WriteLine("[收到注册协议]" + conn.GetAddress() + "发起的 " + "用户名:" + id + " 密码:" + pw);

            bool ret = DataMgr.instance.Register(id, pw);

            //返回协议
            ProtocolBase protocolBack = ServNet.instance.proto.Decode(null, 0, 0);

            protocolBack.AddString("Register");
            if (ret)
            {
                protocolBack.AddInt(0);
            }
            else
            {
                protocolBack.AddInt(-1);
            }
            ServerSystem.instance.Send(conn, protocolBack);

            //创建角色:一个账号对应一个角色的模式
            if (ret)
            {
                DataMgr.instance.CreatePlayer(id);
            }
        }
Exemplo n.º 4
0
        //登录
        //协议参数:用户名,密码
        //返回协议:-1密码错误,-2顶下线失败,-3读取角色数据失败,0成功
        public void MsgLogin(Conn conn, ProtocolBase protocol)
        {
            //解析出所有参数
            int    start     = 0;
            string protoName = protocol.GetString(start, ref start);
            string id        = protocol.GetString(start, ref start);
            string pw        = protocol.GetString(start, ref start);

            Console.WriteLine("[收到登录协议]" + conn.GetAddress() + "发起的 " + "用户名:" + id + " 密码:" + pw);

            //检查密码
            bool         ret          = DataMgr.instance.CheckPassWord(id, pw);
            ProtocolBase protocolBack = ServNet.instance.proto.Decode(null, 0, 0);

            protocolBack.AddString("Login");
            if (!ret)
            {
                protocolBack.AddInt(-1);
                ServerSystem.instance.Send(conn, protocolBack);
                return;
            }

            //检查是否已经登录了
            ret = Player.KickOff(id);
            if (!ret)
            {
                protocolBack.AddInt(-2);
                ServerSystem.instance.Send(conn, protocolBack);
                return;
            }

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

            if (playerData == null)
            {
                protocolBack.AddInt(-3);
                ServerSystem.instance.Send(conn, protocolBack);
                return;
            }

            Player player = new Player(id, conn);

            ServerSystem.instance.ConnLogin(conn, player, playerData);

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

            protocolBack.AddInt(0);
            ServerSystem.instance.Send(conn, protocolBack);
        }
        public ProtocolBase GetRoomListBack()
        {
            ProtocolBase protocol = ServNet.instance.proto.Decode(null, 0, 0);

            protocol.AddString("GetRoomList");
            protocol.AddInt(World.instance.roomList.Count);
            for (int i = 0; i < World.instance.roomList.Count; ++i)
            {
                Room room = World.instance.roomList[i];
                protocol.AddInt(room.playerDict.Count);
                protocol.AddInt((int)room.status);
            }
            return(protocol);
        }
Exemplo n.º 6
0
        public ProtocolBase GetRoomInfoBack()
        {
            ProtocolBase protocol = ServNet.instance.proto.Decode(null, 0, 0);

            protocol.AddString("GetRoomInfo");
            protocol.AddInt(playerDict.Count);
            foreach (Player p in playerDict.Values)
            {
                protocol.AddString(p.id);
                protocol.AddInt(p.tempData.team);
                protocol.AddInt(p.data.maxScore);
                protocol.AddInt(p.tempData.isOwner ? 1 : 0);
            }
            return(protocol);
        }
        //释放技能
        //参数:posX,posY,posZ,rotX,rotY,rotZ,skillType
        //返回协议:广播 释放者id,posX,posY,posZ,rotX,rotY,rotZ,skillType
        public void MsgReleaseSkill(Player player, ProtocolBase protocol)
        {
            if (player.tempData.status != PlayerTempData.Statue.Fight)
            {
                return;
            }
            int    start     = 0;
            string protoName = protocol.GetString(start, ref start);
            float  posX      = protocol.GetFloat(start, ref start);
            float  posY      = protocol.GetFloat(start, ref start);
            float  posZ      = protocol.GetFloat(start, ref start);
            float  rotX      = protocol.GetFloat(start, ref start);
            float  rotY      = protocol.GetFloat(start, ref start);
            float  rotZ      = protocol.GetFloat(start, ref start);
            int    skillType = protocol.GetInt(start, ref start);

            //加上id后广播
            Room         room        = player.tempData.room;
            ProtocolBase protocolRet = ServNet.instance.proto.Decode(null, 0, 0);

            protocolRet.AddString("ReleaseSkill");
            protocolRet.AddString(player.id);
            protocolRet.AddFloat(posX);
            protocolRet.AddFloat(posY);
            protocolRet.AddFloat(posZ);
            protocolRet.AddFloat(rotX);
            protocolRet.AddFloat(rotY);
            protocolRet.AddFloat(rotZ);
            protocolRet.AddInt(skillType);
            RoomSystem.instance.BroadcastInRoom(room, protocolRet);
        }
        public void DealWithWinForRoom(Room room)
        {
            int isWin = room.IsWin();

            if (isWin == 0)
            {
                return;
            }

            room.status = Room.Status.Prepare;        //原本放在临界区内的
            lock (room.playerDict) {
                foreach (Player player in room.playerDict.Values)
                {
                    player.tempData.status = PlayerTempData.Statue.Room;        //不对,玩家可能还在战场中...
                    if (player.tempData.team == isWin)
                    {
                        player.data.maxScore++;     //只是模拟下更新数据,没有实际意义
                    }
                }
            }

            //广播
            ProtocolBase protocol = ServNet.instance.proto.Decode(null, 0, 0);

            protocol.AddString("Result");
            protocol.AddInt(isWin);
            BroadcastInRoom(room, protocol);
        }
Exemplo n.º 9
0
        //登出
        //协议参数:无
        //返回协议:0代表成功,-1代表失败
        public void MsgLogout(Conn conn, ProtocolBase protocol)
        {
            ProtocolBase protocolBack = ServNet.instance.proto.Decode(null, 0, 0);

            protocolBack.AddString("Logout");
            if (conn.player.Logout())
            {
                protocolBack.AddInt(0);
            }
            else
            {
                protocolBack.AddInt(-1);
            }

            ServerSystem.instance.Send(conn, protocolBack);
        }
        public void StartFightForRoom(Room room)
        {
            room.status = Room.Status.Fight;

            //生成协议
            ProtocolBase protocol = ServNet.instance.proto.Decode(null, 0, 0);

            protocol.AddString("Fight");
            lock (room.playerDict) {
                protocol.AddInt(room.playerDict.Count);
                int teamPos1 = 1;
                int teamPos2 = 1;
                foreach (Player p in room.playerDict.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.Statue.Fight;
                }
            }

            //清除所有Box
            room.boxSet.Clear();

            //启动Box生成器
            room.buffBoxTimer.AutoReset = true;
            room.buffBoxTimer.Interval  = room.buffBoxInterval;
            room.buffBoxTimer.Elapsed  += new System.Timers.ElapsedEventHandler((s, e) => BuffBoxTick(room));
            room.buffBoxTimer.Start();
            room.skillBoxTimer.AutoReset = true;
            room.skillBoxTimer.Interval  = room.skillBoxInterval;
            room.skillBoxTimer.Elapsed  += new System.Timers.ElapsedEventHandler((s, e) => SkillBoxTick(room));
            room.skillBoxTimer.Start();

            //广播
            BroadcastInRoom(room, protocol);
        }
Exemplo n.º 11
0
        //获取分数
        //无参数
        //返回协议:int分数
        public void MsgGetMaxScore(Player player, ProtocolBase protocol)
        {
            ProtocolBase protocolRet = ServNet.instance.proto.Decode(null, 0, 0);

            protocolRet.AddString("GetMaxScore");
            protocolRet.AddInt(player.data.maxScore);
            player.Send(protocolRet);
            Console.WriteLine("MsgGetMaxScore " + player.id + " " + player.data.maxScore.ToString());
        }
        public static void SkillBoxTick(Room room)
        {
            Random ran      = new Random();
            int    buffType = ran.Next(0, room.skillBoxTypeNum);
            int    posIdx   = ran.Next(0, room.posIdxNum);

            string boxID = RoomSystem.instance.GetGUID();

            lock (room.boxSet) {
                room.boxSet.Add(boxID);
            }

            //广播
            ProtocolBase protocol = ServNet.instance.proto.Decode(null, 0, 0);

            protocol.AddString("AddSkillBox");
            protocol.AddString(boxID);
            protocol.AddInt(buffType);
            protocol.AddInt(posIdx);
            RoomSystem.instance.BroadcastInRoom(room, protocol);
        }
Exemplo n.º 13
0
        //客户端请求加入指定房间
        //参数:房间index
        //用index来确定应该进入的房间是不准确的,因为如果中途房间被关闭了,就会进入错误的房间...
        //返回协议:0代表成功,-1代表失败,-2代表已开战,-3代表房间满
        public void MsgEnterRoom(Player player, ProtocolBase protocol)
        {
            int    start     = 0;
            string protoName = protocol.GetString(start, ref start);
            int    index     = protocol.GetInt(start, ref start);

            ProtocolBase backProtocol = ServNet.instance.proto.Decode(null, 0, 0);

            backProtocol.AddString("EnterRoom");

            if (index < 0 || index >= World.instance.roomList.Count)
            {
                Console.WriteLine("指定房间不存在," + player.id + " 无法进入房间");
                backProtocol.AddInt(-1);
                player.Send(backProtocol);
                return;
            }

            Room room = World.instance.roomList[index];

            if (room.status != Room.Status.Prepare)
            {
                Console.WriteLine("指定房间不是准备状态," + player.id + " 无法进入房间");
                backProtocol.AddInt(-2);
                player.Send(backProtocol);
                return;
            }
            if (RoomSystem.instance.AddPlayerToRoom(room, player))
            {
                RoomSystem.instance.BroadcastInRoom(room, room.GetRoomInfoBack());
                backProtocol.AddInt(0);
                player.Send(backProtocol);
            }
            else
            {
                Console.WriteLine("房间已满," + player.id + " 无法进入房间");
                backProtocol.AddInt(-3);
                player.Send(backProtocol);
            }
        }
Exemplo n.º 14
0
        //客户端请求创建房间
        //参数:无
        //返回协议:0代表成功
        public void MsgCreateRoom(Player player, ProtocolBase protocol)
        {
            ProtocolBase backProtocol = ServNet.instance.proto.Decode(null, 0, 0);;

            backProtocol.AddString("CreateRoom");

            /*
             * if (player.tempData.status != PlayerTempData.Statue.Lobby) {
             *  Console.WriteLine("玩家不在大厅,无法创建房间 " + player.id);
             *  backProtocol.AddInt(-1);
             *  player.Send(backProtocol);
             *  return;
             * }*/

            RoomSystem.instance.CreateRoom(player);
            backProtocol.AddInt(0);
            player.Send(backProtocol);
            Console.WriteLine("创建房间成功 " + player.id);
        }
Exemplo n.º 15
0
        //客户端请求离开房间
        //参数:无
        //返回协议:0代表成功
        public void MsgLeaveRoom(Player player, ProtocolBase protocol)
        {
            ProtocolBase backProtocol = ServNet.instance.proto.Decode(null, 0, 0);

            backProtocol.AddString("LeaveRoom");

            /*
             * if (player.tempData.status != PlayerTempData.Statue.Room) {
             *  Console.WriteLine("玩家不在房间中,无需离开");
             *  backProtocol.AddInt(-1);
             *  player.Send(protocol);
             *  return;
             * }*/
            Room room = player.tempData.room;

            RoomSystem.instance.DelPlayerForRoom(room, player);
            backProtocol.AddInt(0);
            player.Send(backProtocol);

            //广播
            //if (room != null)
            RoomSystem.instance.BroadcastInRoom(room, room.GetRoomInfoBack());
        }