Exemple #1
0
        public void OnLogout(Player player)
        {
            if (player.tempData.status == PlayerTempData.Statue.Fight)
            {
                Room room = player.tempData.room;

                RoomSystem.instance.DelPlayerForRoom(room, player);

                //广播
                ProtocolBase protocol = ServNet.instance.proto.Decode(null, 0, 0);
                protocol.AddString("Hit");
                protocol.AddString(player.id);
                protocol.AddString(player.id);
                protocol.AddFloat(999);
                RoomSystem.instance.BroadcastInRoom(room, protocol);

                RoomSystem.instance.DealWithWinForRoom(room);
            }
            if (player.tempData.status == PlayerTempData.Statue.Room)
            {
                Room room = player.tempData.room;
                RoomSystem.instance.DelPlayerForRoom(room, player);
                RoomSystem.instance.BroadcastInRoom(room, room.GetRoomInfoBack());
            }
        }
        //玩家拾取盒子
        //参数:盒子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);
        }
        //释放技能
        //参数: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);
        }
        //伤害
        //参数:被击者id,伤害数值damage
        //返回协议:广播 攻击者id,被击者id,伤害数值damage
        public void MsgHit(Player player, ProtocolBase protocol)
        {
            if (player.tempData.status != PlayerTempData.Statue.Fight)
            {
                return;
            }

            //作弊校验
//             long lastShootTime = player.tempData.lastShootTime;
//             if (Sys.GetTimeStamp() - lastShootTime < 1) {
//                 Console.WriteLine("MsgHit 开炮作弊 " + player.id);
//                 return;
//             }
//             player.tempData.lastShootTime = Sys.GetTimeStamp();

            int    start     = 0;
            string protoName = protocol.GetString(start, ref start);
            string enenmyID  = protocol.GetString(start, ref start);
            float  damage    = protocol.GetFloat(start, ref start);

            //扣除生命值
            Room room = player.tempData.room;

            lock (room.playerDict) {
                if (!room.playerDict.ContainsKey(enenmyID))
                {
                    Console.WriteLine("MsgHit not Contains enemy " + enenmyID);
                    return;
                }
                Player enemy = room.playerDict[enenmyID];
                //             if (enemy == null)
                //                 return;
                if (enemy.tempData.hp <= 0)
                {
                    return;
                }
                enemy.tempData.hp -= damage;
                Console.WriteLine("MsgHit " + enenmyID + " hp:" + enemy.tempData.hp);
            }

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

            protocolRet.AddString("Hit");
            protocolRet.AddString(player.id);
            protocolRet.AddString(enenmyID);
            protocolRet.AddFloat(damage);
            RoomSystem.instance.BroadcastInRoom(room, protocolRet);

            //胜负判断
            RoomSystem.instance.DealWithWinForRoom(room);
        }
        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);
        }
 //这个函数不用写在这里...
 //返回值代表,id这个用户是不是下线了
 public static bool KickOff(string id)
 {
     Conn[] conns = World.instance.conns;
     for (int i = 0; i < conns.Length; ++i)
     {
         //if (conns[i] == null)        //没必要
         //    continue;
         if (conns[i].status == Conn.Status.None)
         {
             continue;
         }
         if (conns[i].status == Conn.Status.Connected)
         {
             continue;
         }
         if (conns[i].player.id == id)
         {
             lock (conns[i].player){
                 ProtocolBase protocolNotify = ServNet.instance.proto.Decode(null, 0, 0);
                 protocolNotify.AddString("ForceLogout");
                 conns[i].player.Send(protocolNotify);    //通知被踢下线的客户端
                 if (conns[i].player.Logout())
                 {
                     return(true);
                 }
                 else
                 {
                     Console.WriteLine("保存玩家数据失败,无法踢下线");
                     return(false);
                 }
             }
         }
     }
     return(true);
 }
        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);
        }
Exemple #8
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);
            }
        }
        //收到transform信息
        //参数:posX,posY,posZ,rotX,rotY,rotZ,gunRot,gunRoll
        //返回协议:广播 id,posX,posY,posZ,rotX,rotY,rotZ,gunRot,gunRoll
        public void MsgUpdateUnitInfo(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);

            float gunRot  = protocol.GetFloat(start, ref start);
            float gunRoll = protocol.GetFloat(start, ref start);

            Room room = player.tempData.room;

            //作弊校验
            //略

            //为什么tempData中只保留了一部分数据,为了校验???
            player.tempData.posX = posX;
            player.tempData.posY = posY;
            player.tempData.posZ = posZ;
            player.tempData.lastUpdatePosTime = Sys.GetTimeStamp();

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

            protocolRet.AddString("UpdateUnitInfo");
            protocolRet.AddString(player.id);
            protocolRet.AddFloat(posX);
            protocolRet.AddFloat(posY);
            protocolRet.AddFloat(posZ);
            protocolRet.AddFloat(rotX);
            protocolRet.AddFloat(rotY);
            protocolRet.AddFloat(rotZ);
            protocolRet.AddFloat(gunRot);
            protocolRet.AddFloat(gunRoll);
            RoomSystem.instance.BroadcastInRoom(room, protocolRet);
        }
        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);
        }
Exemple #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);
        }
Exemple #13
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);
        }
        public void Update()
        {
            msgDist.Update();

            if (status == Status.Connected)
            {
                if (Sys.GetTimeStamp() - lastTickTime > heartBeatTime)
                {
                    ProtocolBase protocol = proto.Decode(null, 0, 0);
                    protocol.AddString("HeartBeat");
                    Send(protocol);
                    lastTickTime = Sys.GetTimeStamp();
                }
            }
        }
Exemple #16
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);
        }
Exemple #17
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);
        }
Exemple #18
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);
            }
        }
Exemple #19
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());
        }