Ejemplo n.º 1
0
 public void HandleRequest(MainPack pack, Client client, bool isUDP = false)
 {
     if (controlDict.TryGetValue(pack.RequestCode, out BaseController controller))
     {
         string     metname = pack.ActionCode.ToString();
         MethodInfo method  = controller.GetType().GetMethod(metname);
         if (method == null)
         {
             Console.WriteLine("没有找到对应的处理方法");
             return;
         }
         UnityEngine.Debug.Log("处理方法" + metname);
         object[] obj;
         if (isUDP)//UDP
         {
             obj = new object[] { client, pack };
             method.Invoke(controller, obj);
         }
         else//TCP
         {
             obj = new object[] { server, client, pack };
             object ret = method.Invoke(controller, obj);
             if (ret != null)
             {
                 client.Send(ret as MainPack);
                 Console.WriteLine("发送数据:");
             }
         }
     }
     else
     {
         Console.WriteLine("没有找到对应的controller处理");
     }
 }
Ejemplo n.º 2
0
        public void HandleRequest(MainPack pack, LanClient client)
        {
            if (controllerDict.TryGetValue(pack.Requestcode, out BaseController controller))
            {
                string     methodName = pack.Actioncode.ToString();
                MethodInfo method     = controller.GetType().GetMethod(methodName);

                if (method == null)
                {
                    Console.WriteLine("找不到方法 {0}", methodName);
                    return;
                }


                object[] o   = new object[] { client, pack };
                object   ret = method.Invoke(controller, o);

                if (ret == null)
                {
                    return;
                }

                client.Send(ret as MainPack);
            }
            else
            {
                Console.WriteLine("找不到負責處理的 controller");
            }
        }
Ejemplo n.º 3
0
 public MainPack JoinRoom(Client client, MainPack pack)
 {
     foreach (Room r in roomList)
     {
         if (r.GetRoomInFo.RoomName.Equals(pack.Str))
         {
             if (r.GetRoomInFo.RoomStatus == 0)
             {
                 //可以加入房间
                 r.Join(client);
                 pack.RoomPack.Add(r.GetRoomInFo);
                 foreach (PlayerPack p in r.GetPlayerInFo())
                 {
                     pack.PlayerPack.Add(p);
                 }
                 pack.ReturnCode = ReturnCode.Succeed;
                 return(pack);
             }
             else
             {
                 //房间不可加入
                 pack.ReturnCode = ReturnCode.Fail;
                 return(pack);
             }
         }
     }
     //没有此房间
     pack.ReturnCode = ReturnCode.ReturnNone;
     return(pack);
 }
Ejemplo n.º 4
0
 /// <summary>
 /// 加入房间
 /// </summary>
 /// <param name="client">要加入房间的client</param>
 /// <param name="pack">包含要加入房间的房间名的数据包</param>
 /// <returns>包含ReturnCode的数据包</returns>
 public MainPack JoinRoom(Client client, MainPack pack)
 {
     foreach (Room room in rooms)
     {
         //找到房间
         if (room.GetRoomPack.RoomName.Equals(pack.JoinRoomName))
         {
             if (room.GetRoomPack.RoomState == RoomState.Waitting)
             {
                 //可以加入
                 room.Join(client);
                 pack.RoomPack.Add(room.GetRoomPack);
                 foreach (PlayerPack playerPack in room.GetPlayerPacks())
                 {
                     pack.PlayerPack.Add(playerPack);
                 }
                 pack.ReturnCode = ReturnCode.Succeed;
                 return(pack);
             }
             else
             {
                 //不可加入
                 pack.ReturnCode = ReturnCode.Fail;
                 return(pack);
             }
         }
     }
     //没有此房间
     pack.ReturnCode = ReturnCode.NoRoom;
     return(pack);
 }
Ejemplo n.º 5
0
    public void ExitGame(Client client)
    {
        MainPack pack = new MainPack();

        if (client == clientList[0])
        {
            //房主退出
            pack.ActionCode = ActionCode.ExitGame;
            pack.Str        = "r";
            Broadcast(client, pack);
            server.RemoveRoom(this);
            client.GetRoom = null;
        }
        else
        {
            //其他成员退出
            clientList.Remove(client);
            client.GetRoom  = null;
            pack.ActionCode = ActionCode.UpCharacterList;
            foreach (var clientTemp in clientList)
            {
                PlayerPack playerPack = new PlayerPack();
                playerPack.PlayerName = clientTemp.GetUserInFo.UserName;
                playerPack.HP         = clientTemp.GetUserInFo.HP;
                pack.PlayerPack.Add(playerPack);
            }
            pack.Str = client.GetUserInFo.UserName;
            Broadcast(client, pack);
        }
    }
Ejemplo n.º 6
0
    private void Time()
    {
        MainPack pack = new MainPack();

        //pack.ActionCode = ActionCode.Chat;
        pack.Str = "房主已启动游戏...";
        Broadcast(null, pack);
        Thread.Sleep(1000);
        for (int i = 5; i > 0; i--)
        {
            pack.Str = i.ToString();
            Broadcast(null, pack);
            Thread.Sleep(1000);
        }

        pack.ActionCode = ActionCode.Starting;


        foreach (var VARIABLE in clientList)
        {
            PlayerPack player = new PlayerPack();
            VARIABLE.GetUserInFo.HP = 100;
            player.PlayerName       = VARIABLE.GetUserInFo.UserName;
            player.HP = VARIABLE.GetUserInFo.HP;
            pack.PlayerPack.Add(player);
        }
        Broadcast(null, pack);
    }
Ejemplo n.º 7
0
    public void ExitRoom(Server server, Client client)
    {
        MainPack pack = new MainPack();

        if (roomInfo.RoomStatus == RoomStatus.Playing)//游戏已经开始
        {
            ExitGame(client);
        }
        else//游戏未开始
        {
            if (client == clientList[0])
            {
                //房主离开
                client.GetRoom  = null;
                pack.ActionCode = ActionCode.ExitRoom;
                Broadcast(client, pack);
                server.RemoveRoom(this);
                return;
            }
            clientList.Remove(client);
            roomInfo.RoomStatus = RoomStatus.RoomNone;
            client.GetRoom      = null;
            pack.ActionCode     = ActionCode.UpPlayerList;
            foreach (PlayerPack player in GetPlayerInFo())
            {
                pack.PlayerPack.Add(player);
            }
            Broadcast(client, pack);
        }
    }
Ejemplo n.º 8
0
        public bool Logon(MainPack pack)
        {
            string username = pack.Loginpack.Username;
            string password = pack.Loginpack.Password;

            string       sqlstr = string.Format("select * from lan.userdata where username='******'", username);
            MySqlCommand cmd    = new MySqlCommand(sqlstr, mysqlCon);

            try
            {
                if (cmd.ExecuteReader().HasRows)
                {
                    Console.WriteLine("已經註冊");
                    return(false);
                }
                else
                {
                    sqlstr = string.Format("insert into `lan`.`userdata` (`username`, `password`) values ('{0}', '{1}') ", username, password);
                    cmd    = new MySqlCommand(sqlstr, mysqlCon);
                    cmd.ExecuteNonQuery();

                    return(true);
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return(false);
            }
        }
Ejemplo n.º 9
0
        public MainPack Login(MainPack pack)
        {
            string password = pack.Loginpack.Password;
            string email    = pack.Loginpack.Email;
            //检查邮箱是否存在
            string          sqlCheck = string.Format("select * from userdata where email='{0}';", email);
            MySqlCommand    comd     = new MySqlCommand(sqlCheck, mysqlCon);
            MySqlDataReader read     = comd.ExecuteReader();

            if (read.Read() == false)
            {
                Console.WriteLine("邮箱不存在");
                read.Close();
                pack.Returncode = ReturnCode.Fail;
                return(pack);
            }
            if (read["password"].ToString() != password)
            {
                Console.WriteLine("密码不一致");
                read.Close();
                pack.Returncode = ReturnCode.Fail;
                return(pack);
            }
            pack.Loginpack.Uid      = read["uid"].ToString();
            pack.Loginpack.Password = read["password"].ToString();
            pack.Loginpack.Username = read["username"].ToString();
            pack.Loginpack.Diamonds = read["diamonds"].ToString();
            pack.Returncode         = ReturnCode.Succeed;
            read.Close();
            return(pack);
        }
Ejemplo n.º 10
0
        public void ReadBuffer(int len, Action <MainPack> HandleRequest)//len为接受的数据长度
        {
            //解析消息
            //消息buffer=1*包头(int 4个字节)+n*包体
            startindex += len;
            if (startindex <= 4)//包头int四个字节,小于等于4肯定是不完整的
            {
                return;
            }
            //count计算的包头包含的包体的长度
            int count = BitConverter.ToInt32(buffer, 0);//返回转换的字节数组指定位置处四个字节(32位)有符号整数

            while (true)
            {
                if (startindex >= (count + 4))                                                        //当前长度大于包头+包体
                {
                    MainPack pack = (MainPack)MainPack.Descriptor.Parser.ParseFrom(buffer, 4, count); //处理包体
                    HandleRequest(pack);
                    Array.Copy(buffer, count + 4, buffer, 0, startindex - count - 4);                 //把后面的消息覆盖到前面
                    startindex -= (count + 4);                                                        //剩余长度计算
                }
                else
                {
                    break;
                }
            }
        }
Ejemplo n.º 11
0
        private void StartTimerThread()
        {
            MainPack pack = new MainPack();

            pack.ActionCode = ActionCode.Chat;
            pack.ChatStr    = "房主已启动游戏";
            Broadcast(null, pack);
            Thread.Sleep(1000);
            for (int i = 5; i > 0; i--)
            {
                pack.ChatStr = i.ToString();
                Broadcast(null, pack);
                Thread.Sleep(1000);
            }
            pack.ActionCode = ActionCode.ServerStartGame;

            foreach (Client client in clientList)
            {
                PlayerPack playerPack = new PlayerPack();
                playerPack.Hp         = client.GetPlayerPack().Hp;
                playerPack.PlayerName = client.GetPlayerPack().PlayerName;
                pack.PlayerPack.Add(playerPack);
            }

            Broadcast(null, pack);
        }
Ejemplo n.º 12
0
        public bool Logon(MainPack pack)
        {
            string username = pack.LoginPack.Username;
            string password = pack.LoginPack.Password;

            string insertSql = $"insert into user (username,password) values ('{username}','{password}')";


            try
            {
                if (sqlConnection.State != System.Data.ConnectionState.Open)
                {
                    sqlConnection.Open();
                }
                MySqlCommand commd = new MySqlCommand(insertSql, sqlConnection);
                int          len   = commd.ExecuteNonQuery();
                if (len < 1)
                {
                    throw new Exception("该账号已注册,请直接登录");
                }
                return(true);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return(false);
            }
            finally
            {
                sqlConnection.Close();
                Console.WriteLine("数据库连接已关闭!");
            }
        }
Ejemplo n.º 13
0
        public bool Login(MainPack pack)
        {
            string username = pack.LoginPack.Username;
            string password = pack.LoginPack.Password;

            string querySql = $"select * from user where username = '******' and password = '******';";


            try
            {
                if (sqlConnection.State != System.Data.ConnectionState.Open)
                {
                    sqlConnection.Open();
                }
                MySqlCommand commd = new MySqlCommand(querySql, sqlConnection);

                MySqlDataReader reader = commd.ExecuteReader();

                if (reader.Read())
                {
                    return(true);
                }
                throw new Exception("账号或密码错误!");
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
                return(false);
            }
            finally
            {
                sqlConnection.Close();
                Console.WriteLine("数据库连接已关闭!");
            }
        }
 public void HandleRequest(MainPack pack, Client client)
 {
     Console.WriteLine(pack.Requestcode);
     //找出对应的处理方法
     if (controlDic.TryGetValue(pack.Requestcode, out BaseController controller))
     {
         string     methodName = pack.Actioncode.ToString();                 //得到名字
         MethodInfo method     = controller.GetType().GetMethod(methodName); //方法
         if (method == null)
         {
             Console.WriteLine("没有找到对应的处理方法!");
             return;
         }
         object[] obj = new object[] { server, client, pack };
         Object   ret = method.Invoke(controller, obj);//发射调用方法
         Console.WriteLine("返回" + ret);
         if (ret != null)
         {
             client.Send(ret as MainPack);
         }
     }
     else
     {
         Console.WriteLine("没有找到对应controller的处理方法!");
     }
 }
Ejemplo n.º 15
0
        public void HandleRequest(MainPack pack, Client client, bool isUdp = false)
        {
            if (controllerDictionary.TryGetValue(pack.RequestCode, out BaseController controller))
            {
                string     methodName = pack.ActionCode.ToString();
                MethodInfo method     = controller.GetType().GetMethod(methodName);
                if (method == null)
                {
                    Console.WriteLine("没有找到指定的事件处理" + pack.ActionCode.ToString());
                    return;
                }
                object[] obj;

                if (isUdp)
                {
                    obj = new object[] { client, pack };
                    method.Invoke(controller, obj);
                }
                else
                {
                    obj = new object[] { server, client, pack };
                    object ret = method.Invoke(controller, obj);
                    if (ret != null)
                    {
                        client.Send(ret as MainPack);
                    }
                }
            }
            else
            {
                Console.WriteLine("没有找到对应的Controller处理");
            }
        }
Ejemplo n.º 16
0
 private void Update()
 {
     if (pack != null)
     {
         logonPanel.OnResponse(pack);
         pack = null;
     }
 }
Ejemplo n.º 17
0
 public void SendTo(MainPack pack)
 {
     if (IEP == null)
     {
         return;
     }
     us.SendTo(pack, IEP);
 }
Ejemplo n.º 18
0
 private void Update()
 {
     if (pack != null)
     {
         roomPanel.UpdatePlayerList(pack);
         pack = null;
     }
 }
Ejemplo n.º 19
0
 private void Update()
 {
     if (pack != null)
     {
         roomListPanel.JoinRoomOnResponse(pack);
         pack = null;
     }
 }
Ejemplo n.º 20
0
 private void Update()
 {
     if (pack != null)
     {
         GameFace.instance.GameExit(pack);
         pack = null;
     }
 }
Ejemplo n.º 21
0
        public void SendRequest(string str)
        {
            MainPack pack = new MainPack();

            pack.RequestCode = requestCode;
            pack.ActionCode  = actionCode;
            pack.ChatStr     = str;
            base.SendRequest(pack);
        }