Esempio n. 1
0
 public void addplayerv1(playerinfor pi)
 {
     foreach (var v in players)
     {
         LoginServerProxy lsp;
         MatchServer.getsingleton().LoginServers.TryGetValue(pi.loginserverproxyid, out lsp);
         MemoryStream ms = new MemoryStream();
         Serializer.Serialize(ms, v.Value);
         byte[] btms    = ms.ToArray();
         byte[] t       = new byte[btms.Length + 4];
         byte[] ownerid = BitConverter.GetBytes(pi.playerid);
         Array.Copy(ownerid, 0, t, 0, 4);
         Array.Copy(btms, 0, t, 4, btms.Length);
         lsp.sendtologinserver((Byte)CMDMatchServer.OTHERPLAYERINFOR, t);
     }
     foreach (var v in players)
     {
         LoginServerProxy lsp;
         MatchServer.getsingleton().LoginServers.TryGetValue(v.Value.loginserverproxyid, out lsp);
         MemoryStream ms = new MemoryStream();
         Serializer.Serialize(ms, pi);
         byte[] btms    = ms.ToArray();
         byte[] t       = new byte[btms.Length + 4];
         byte[] ownerid = BitConverter.GetBytes(v.Value.playerid);
         Array.Copy(ownerid, 0, t, 0, 4);
         Array.Copy(btms, 0, t, 4, btms.Length);
         lsp.sendtologinserver((Byte)CMDMatchServer.OTHERPLAYERINFOR, t);
     }
     players.TryAdd(pi.playerid, pi);
     Logger.log("roomid : " + id + " number of players :" + players.Count);
     //if (players.Count == halfroomnumber)
     //{
     //}
 }
Esempio n. 2
0
        public void sort(string conditions, playerinfor pi)
        {
            int    index           = conditions.IndexOf("???", StringComparison.OrdinalIgnoreCase);
            string condition       = conditions.Substring(0, index);
            string remaincondition = conditions.Substring(index + 3);
            Filter filter;

            if (layers.TryGetValue(condition, out filter))
            {
            }
            else
            {
                filter = new Filter();
                layers.TryAdd(condition, filter);
            }
///////////////////////////////////////////////////////////
            if (remaincondition == "")
            {
                filter.halfroomnumber = Int32.Parse(condition);
                filter.addplayer(pi);
            }
            else
            {
                filter.sort(remaincondition, pi);
            }
        }
Esempio n. 3
0
        public void addtomatchpoolV1(playerinfor player)
        {
            bool b = matchpool.TryAdd(player.playerid, player);

            if (b)
            {
            }
            else
            {
                Logger.log("the same key already in matchpool");
            }
        }
Esempio n. 4
0
        public void addtomatchpool(playerinfor player)
        {
            bool b = matchpool.TryAdd(player.playerid, player);

            if (b)
            {
                filter.sort(player.SimulateInforStr, player);
            }
            else
            {
                Logger.log("the same key already in matchpool");
            }
        }
Esempio n. 5
0
 public void rejoin(playerinfor pi)
 {
     foreach (var v in players)
     {
         if (v.Value.offline)
         {
             continue;
         }
         LoginServerProxy lsp;
         MatchServer.getsingleton().LoginServers.TryGetValue(pi.loginserverproxyid, out lsp);
         MemoryStream ms = new MemoryStream();
         Serializer.Serialize(ms, v.Value);
         byte[] btms    = ms.ToArray();
         byte[] t       = new byte[btms.Length + 4];
         byte[] ownerid = BitConverter.GetBytes(pi.playerid);
         Array.Copy(ownerid, 0, t, 0, 4);
         Array.Copy(btms, 0, t, 4, btms.Length);
         lsp.sendtologinserver((Byte)CMDMatchServer.OTHERPLAYERINFOR, t);
     }
     foreach (var v in players)
     {
         if (v.Value.offline)
         {
             continue;
         }
         LoginServerProxy lsp;
         MatchServer.getsingleton().LoginServers.TryGetValue(v.Value.loginserverproxyid, out lsp);
         MemoryStream ms = new MemoryStream();
         Serializer.Serialize(ms, pi);
         byte[] btms    = ms.ToArray();
         byte[] t       = new byte[btms.Length + 4];
         byte[] ownerid = BitConverter.GetBytes(v.Value.playerid);
         Array.Copy(ownerid, 0, t, 0, 4);
         Array.Copy(btms, 0, t, 4, btms.Length);
         lsp.sendtologinserver((Byte)CMDMatchServer.OTHERPLAYERINFOR, t);
     }
     players.TryAdd(pi.playerid, pi);
 }
Esempio n. 6
0
        void addplayer(playerinfor pi)
        {
            lock (playersLock)
            {
                Filter filter;
                players.TryAdd(pi.playerid, pi);
                Filtertracker.TryAdd(pi.playerid, this);
                if (players.Count == halfroomnumber)//when number of player in this filter is equal to Room.halfroomnumber then move these players to room *************
                {
                    Logger.log("players.Count == Room.halfroomnumber : " + halfroomnumber);
                    String[] sperater   = { "???" };
                    String[] conditions = pi.SimulateInforStr.Split(sperater, StringSplitOptions.RemoveEmptyEntries);

                    ICollection <Room> waitingrooms = RoomManager.getsingleton().getcurrentwaitingroom();
                    Room room = null;
                    foreach (var v in waitingrooms)
                    {
                        bool b = v.b_wholeteam;//waiting for another whole team
                        Logger.log("v.b_wholeteam    :" + b);
                        String[] roomconditions = v.getconditions();
                        if (roomconditions.Length == 0)
                        {
                            continue;
                        }

                        /*
                         * match the team which meet certain criteria to the same room
                         */
                        bool b1 = roomconditions[0] != conditions[0];// && roomconditions[1] != conditions[1];    //ensure these two team with different national flag
                        Logger.log("meet certain criteria    :" + b1);

                        if (b && b1)//search for a whole team with different national flag
                        {
                            room = v;
                            Logger.log("the certain criteria is meet");
                        }
                    }
                    if (room == null)
                    {
                        room = RoomManager.getsingleton().createroom(halfroomnumber, pi.SimulateInforStr);
                        Logger.log("room == null so create a room");
                    }
                    foreach (var v in players.Values)
                    {
                        room.addplayer(v);
                        Filtertracker.TryRemove(v.playerid, out filter);
                    }
                    players.Clear();
                }
            }
            if (players.Count != 1)
            {
                return;
            }
            Task.Run(async() =>
            {
                try
                {
                    await Task.Delay(System.TimeSpan.FromMilliseconds(Room.expiredtime));
                    lock (playersLock)
                    {
                        Filter filter;
                        Logger.log("filter time expired");
                        if (players.Count == 0)
                        {
                            Logger.log("number of player in this filter is equal to Room.halfroomnumber then move these players to room *************");
                            return;
                        }
                        Func <int, int, Room> localgetroom = (int i1, int i2) => {
                            ICollection <Room> waitingrooms = RoomManager.getsingleton().getcurrentwaitingroom();
                            Room room1 = null;
                            foreach (var v in waitingrooms)
                            {
                                bool b = v.b_anyplayerisok; //this room has been waiting too long
                                if (b)                      //search for a whole team with different national flag
                                {
                                    room1 = v;
                                }
                            }
                            if (room1 == null)
                            {
                                room1 = RoomManager.getsingleton().createroom(halfroomnumber, "");//this room does not reject any national flag
                            }
                            return(room1);
                        };

                        Room room = localgetroom(0, 0);
                        foreach (var v in players.Values)
                        {
                            Filtertracker.TryRemove(v.playerid, out filter);
                            if (!room.isfull)
                            {
                                room.addplayer(v);
                            }
                            else
                            {
                                room = localgetroom(0, 0);
                                room.addplayer(v);
                                Logger.log("room is full to create a new room and add player-----------------------------");
                            }
                        }
                        players.Clear();
                    }
                }
                catch (Exception e)
                {
                    Logger.log(e.ToString());
                }
            });
        }
Esempio n. 7
0
        public Player(int id, KChannel channel, LoginServer loginserver)
        {
            this.id                    = id;
            this.channel               = channel;
            this.loginserver           = loginserver;
            this.channel.ondisconnect += () => {
                Player v;
                this.loginserver.Players.TryRemove(id, out v);
                loginserver.sendtomatchserver((byte)CMDMatchServer.PLAYEREXITQUEST, BitConverter.GetBytes(id));
                Logger.log(id + " :ondisconnect");
            };
            this.channel.onUserLevelReceivedCompleted += (ref byte[] buffer) =>
            {
#if RTT
                var str = System.Text.Encoding.UTF8.GetString(buffer);
                //Console.WriteLine(str);
                ASCIIEncoding asen    = new ASCIIEncoding();
                byte[]        buffer1 = asen.GetBytes(str + "server");
                send(ref buffer1);
#else
                switch ((CMDPlayer)buffer[0])
                {
                case CMDPlayer.SINGUP:
                    Logger.log("Sing up ");
                    //write data base
                    break;

                case CMDPlayer.LOGIN:
                    Logger.log("log in ");
                    var str = Encoding.getstring(buffer, 1, buffer.Length - 1);
                    Logger.log(str);    //username password ,these infor maybe used to query database
                    ////////////////////////////////////////////////////////////////////
                    //read data base
                    //simulate playerinfor
                    int simulateddata;
                    for (int i = 0; i < 2; i++)
                    {
                        simulateddata = RandomHelper.RandomNumber(1, 3);
                        playerinfor  += simulateddata.ToString() + "???";
                    }
                    ////////////////////////////////////////////////////////////////////////////////////
                    //ack
                    byte[] infor = Encoding.getbyte(playerinfor);
                    byte[] t     = new byte[playerinfor.Length + 4];
                    t.WriteTo(0, id);
                    Array.Copy(infor, 0, t, 4, infor.Length);
                    send((byte)CMDPlayer.LOGIN, t);
                    break;

                case CMDPlayer.MATCHREQUEST:
                    int halfroomnumber = BitConverter.ToInt32(buffer, 1);
                    Logger.log("match ");
                    Logger.log("halfroomnumber :" + halfroomnumber);
                    Logger.log(playerinfor);
                    var playerinfor_ = new playerinfor {
                        playerid         = id,
                        SimulateInforStr = playerinfor + halfroomnumber + "???",
                        SimulateInforInt = 1234
                    };
                    MemoryStream ms = new MemoryStream();
                    Serializer.Serialize(ms, playerinfor_);
                    loginserver.sendtomatchserver((byte)CMDMatchServer.MATCHREQUEST, ms.ToArray());
                    break;

                case CMDPlayer.CREATEROOM:
                    Logger.log("createroom ");
                    Logger.log(playerinfor);
                    playerinfor_ = new playerinfor
                    {
                        playerid         = id,
                        homeowner        = true,
                        SimulateInforStr = playerinfor,
                        halfroomnumber   = BitConverter.ToInt32(buffer, 1),  //here the halfroomnumber seem to be useless
                    };
                    ms = new MemoryStream();
                    Serializer.Serialize(ms, playerinfor_);
                    loginserver.sendtomatchserver((byte)CMDMatchServer.CREATEROOM, ms.ToArray());
                    break;

                case CMDPlayer.JOINROOM:
                    Logger.log("joinroom ");
                    Logger.log(playerinfor);
                    playerinfor_ = new playerinfor
                    {
                        playerid         = id,
                        homeowner        = false,
                        SimulateInforStr = playerinfor,
                        roomnumber       = BitConverter.ToInt32(buffer, 1),
                    };
                    ms = new MemoryStream();
                    Serializer.Serialize(ms, playerinfor_);
                    loginserver.sendtomatchserver((byte)CMDMatchServer.JOINROOM, ms.ToArray());
                    break;

                case CMDPlayer.STARTGAME:
                    Logger.log("startgame ");
                    Logger.log(playerinfor);
                    playerinfor_ = new playerinfor
                    {
                        playerid   = id,
                        homeowner  = true,
                        roomnumber = BitConverter.ToInt32(buffer, 1),
                    };
                    ms = new MemoryStream();
                    Serializer.Serialize(ms, playerinfor_);
                    loginserver.sendtomatchserver((byte)CMDMatchServer.STARTGAME, ms.ToArray());
                    break;

                case CMDPlayer.EXITREQUEST:
                    Logger.log("CMDPlayer.EXITREQUEST");
                    loginserver.sendtomatchserver((byte)CMDMatchServer.PLAYEREXITQUEST, BitConverter.GetBytes(id));

                    break;

                case CMDPlayer.RECONNECTLOGIN:    //
                    Logger.log("CMDPlayer.RECONNECTLOGIN");
                    str = Encoding.getstring(buffer, 1, buffer.Length - 1);
                    Logger.log(str);    //username password ,these infor maybe used to query database
                    ////////////////////////////////////////////////////////////////////
                    //read data base
                    //simulate playerinfor
                    for (int i = 0; i < 2; i++)
                    {
                        simulateddata = RandomHelper.RandomNumber(1, 3);
                        playerinfor  += simulateddata.ToString() + "???";
                    }
                    send((byte)CMDPlayer.RECONNECTLOGIN, BitConverter.GetBytes(id));
                    break;

                /*
                 * server side : here may just for ios device when front_back switch this player will be die so replace it
                 * client side : when front_back switch first check the connection to DS ,if it is connecting then do nothing ,if it is disconnect then reconnect to ds directly
                 */
                case CMDPlayer.RECONNECT:    //
                    Logger.log("CMDPlayer.RECONNECT");
                    int id_    = BitConverter.ToInt32(buffer, 1);
                    int roomid = BitConverter.ToInt32(buffer, 5);
                    halfroomnumber = BitConverter.ToInt32(buffer, 9);
                    str            = Encoding.getstring(buffer, 13, buffer.Length - 13);
                    Logger.log(str);    //username password ,these infor maybe used to query database
                    ////////////////////////////////////////////////////////////////////
                    //read data base
                    //simulate playerinfor
                    for (int i = 0; i < 2; i++)
                    {
                        simulateddata = RandomHelper.RandomNumber(1, 3);
                        playerinfor  += simulateddata.ToString() + "???";
                    }
                    playerinfor_ = new playerinfor
                    {
                        playerid         = id,
                        oldplayerid      = id_,
                        roomid           = roomid,
                        SimulateInforStr = playerinfor + halfroomnumber + "???",
                        SimulateInforInt = 1234
                    };
                    ms = new MemoryStream();
                    Serializer.Serialize(ms, playerinfor_);
                    loginserver.sendtomatchserver((byte)CMDMatchServer.RECONNECT, ms.ToArray());
                    break;

                case CMDPlayer.RECONNECTV1:    //
                    Logger.log("CMDPlayer.RECONNECTv1");
                    id_    = BitConverter.ToInt32(buffer, 1);
                    roomid = BitConverter.ToInt32(buffer, 5);
                    int owner = BitConverter.ToInt32(buffer, 9);
                    str = Encoding.getstring(buffer, 13, buffer.Length - 13);
                    Logger.log(str);    //username password ,these infor maybe used to query database
                    ////////////////////////////////////////////////////////////////////
                    //read data base
                    //simulate playerinfor
                    for (int i = 0; i < 2; i++)
                    {
                        simulateddata = RandomHelper.RandomNumber(1, 3);
                        playerinfor  += simulateddata.ToString() + "???";
                    }
                    playerinfor_ = new playerinfor
                    {
                        playerid         = id,
                        oldplayerid      = id_,
                        roomid           = roomid,
                        homeowner        = owner == 1? true : false,
                        SimulateInforStr = playerinfor,
                        SimulateInforInt = 1234
                    };
                    ms = new MemoryStream();
                    Serializer.Serialize(ms, playerinfor_);
                    loginserver.sendtomatchserver((byte)CMDMatchServer.RECONNECTV1, ms.ToArray());
                    break;

                case CMDPlayer.CHECKCONNECTION:    //
                    Logger.log("CMDPlayer.CHECKCONNECTION");
                    send((byte)CMDPlayer.CHECKCONNECTION, BitConverter.GetBytes(0));
                    break;

                default:
                    break;
                }
#endif
            };
        }
Esempio n. 8
0
        public void addplayer(playerinfor pi)
        {
            foreach (var v in players)
            {
                if (v.Value.offline)
                {
                    continue;
                }
                LoginServerProxy lsp;
                MatchServer.getsingleton().LoginServers.TryGetValue(pi.loginserverproxyid, out lsp);
                MemoryStream ms = new MemoryStream();
                Serializer.Serialize(ms, v.Value);
                byte[] btms    = ms.ToArray();
                byte[] t       = new byte[btms.Length + 4];
                byte[] ownerid = BitConverter.GetBytes(pi.playerid);
                Array.Copy(ownerid, 0, t, 0, 4);
                Array.Copy(btms, 0, t, 4, btms.Length);
                lsp.sendtologinserver((Byte)CMDMatchServer.OTHERPLAYERINFOR, t);
            }
            foreach (var v in players)
            {
                if (v.Value.offline)
                {
                    continue;
                }
                LoginServerProxy lsp;
                MatchServer.getsingleton().LoginServers.TryGetValue(v.Value.loginserverproxyid, out lsp);
                MemoryStream ms = new MemoryStream();
                Serializer.Serialize(ms, pi);
                byte[] btms    = ms.ToArray();
                byte[] t       = new byte[btms.Length + 4];
                byte[] ownerid = BitConverter.GetBytes(v.Value.playerid);
                Array.Copy(ownerid, 0, t, 0, 4);
                Array.Copy(btms, 0, t, 4, btms.Length);
                lsp.sendtologinserver((Byte)CMDMatchServer.OTHERPLAYERINFOR, t);
            }
////////////////////
            if (players.Count >= halfroomnumber)
            {
                pi.side = 1;
            }
            else
            {
                pi.side = 0;
            }
            players.TryAdd(pi.playerid, pi);
            if (players.Count == halfroomnumber)
            {
                b_wholeteam = true;
            }
            else
            {
                b_wholeteam = false;
            }
            if (players.Count == halfroomnumber * 2)
            {
                isfull = true;
                lock (RoomsLock)
                {
                    if (!b_created)
                    {
                        b_created = true;
                        Logger.log("room is full so create ds request=================================================== : " + id);
                        RoomManager.getsingleton().waitingtofighting(id);
                        MatchServer.getsingleton().sendtoloadbalanceserver((byte)CMDLoadBalanceServer.CREATEDS, BitConverter.GetBytes(id));
                    }
                }
            }
            Task.Run(async() =>
            {
                try
                {
                    await Task.Delay(System.TimeSpan.FromMilliseconds(Room.expiredtime));
                    Logger.log("room time expired 000");
                    b_anyplayerisok = true;
                    /////////////////////////////////////////////////////////////////////////
                    ///
                    await Task.Delay(System.TimeSpan.FromMilliseconds(Room.expiredtime));
                    lock (RoomsLock)
                    {
                        if (!b_created)
                        {
                            b_created = true;
                            Logger.log("room time expired 111===============================================================: " + id);
                            RoomManager.getsingleton().waitingtofighting(id);
                            MatchServer.getsingleton().sendtoloadbalanceserver((byte)CMDLoadBalanceServer.CREATEDS, BitConverter.GetBytes(id));
                        }
                    }
                }
                catch (Exception e)
                {
                    Logger.log(e.ToString());
                }
            });
        }
Esempio n. 9
0
        public LoginServer() : base("Loginserver")
        {
            LuaTable server     = GetValueFromLua <LuaTable>("server");
            string   nettype    = (string)server["nettype"];
            LuaTable serveraddr = (LuaTable)server[nettype];
            int      port       = (int)(Int64)serveraddr["port"];
            KService service    = Session.createorget(port);

            service.onAcceptAKchannel += (ref KChannel channel) => {
                int id = 0;
                do
                {
                    id = RandomHelper.RandomNumber(int.MinValue, int.MaxValue);
                } while (Players.ContainsKey(id));
                Players.TryAdd(id, new Player(id, channel, this));
                Logger.log("onaccept");
            };
////////////////////////////////////////////////////////////////////////////////////////
            LuaTable remoteserver = GetValueFromLua <LuaTable>("remoteserver");

            nettype    = (string)remoteserver["nettype"];
            serveraddr = (LuaTable)remoteserver[nettype];
            string serverip = (string)serveraddr["serverip"];

            port = (int)(Int64)serveraddr["port"];
            IPAddress ipAd = IPAddress.Parse(serverip);//local ip address  "172.16.5.188"

            channel_matchserver = Session.getnew().GetChannel(new IPEndPoint(ipAd, port));
            channel_matchserver.onUserLevelReceivedCompleted += (ref byte[] buffer) => {
                switch ((CMDMatchServer)buffer[0])
                {
                case CMDMatchServer.MATCHREQUEST:
                    int    playerid = BitConverter.ToInt32(buffer, 1);
                    Player player;
                    bool   b = Players.TryGetValue(playerid, out player);
                    if (b)
                    {
                        int    side       = BitConverter.ToInt32(buffer, 5);
                        int    dsport     = BitConverter.ToInt32(buffer, 9);
                        int    roomid     = BitConverter.ToInt32(buffer, 13);
                        string dswan      = Encoding.getstring(buffer, 17, buffer.Length - 17);
                        byte[] tempbuffer = new byte[buffer.Length - 5];
                        Array.Copy(buffer, 5, tempbuffer, 0, tempbuffer.Length);
                        player.send((byte)CMDPlayer.MATCHREQUEST, tempbuffer);
                        Logger.log("--player.playerinfor-- : " + player.playerinfor + "--side-- : " + side + "--dsport--" + dsport + "--dswan-- " + dswan);
                    }
                    else
                    {
                    }

                    break;

                case CMDMatchServer.CREATEROOM:
                    playerinfor  pi = new playerinfor();
                    MemoryStream ms = new MemoryStream();
                    ms.Write(buffer, 1, buffer.Length - 1);
                    ms.Position = 0;
                    pi          = Serializer.Deserialize <playerinfor>(ms);
                    b           = Players.TryGetValue(pi.playerid, out player);
                    if (b)
                    {
                        player.send((byte)CMDPlayer.CREATEROOM, BitConverter.GetBytes(pi.roomnumber));
                        Logger.log(pi.roomnumber + " :pi.roomnumber :createroom");
                    }
                    else
                    {
                    }
                    break;

                case CMDMatchServer.JOINROOM:
                    pi = new playerinfor();
                    ms = new MemoryStream();
                    ms.Write(buffer, 1, buffer.Length - 1);
                    ms.Position = 0;
                    pi          = Serializer.Deserialize <playerinfor>(ms);
                    b           = Players.TryGetValue(pi.playerid, out player);
                    if (b)
                    {
                        player.send((byte)CMDPlayer.JOINROOM, BitConverter.GetBytes(pi.roomnumber));
                        Logger.log(pi.roomnumber + " :pi.roomnumber :joinroom");
                    }
                    else
                    {
                    }
                    Logger.log(" :joinroom");

                    break;

                case CMDMatchServer.JOINROOMFAILED:
                    pi = new playerinfor();
                    ms = new MemoryStream();
                    ms.Write(buffer, 1, buffer.Length - 1);
                    ms.Position = 0;
                    pi          = Serializer.Deserialize <playerinfor>(ms);
                    b           = Players.TryGetValue(pi.playerid, out player);
                    if (b)
                    {
                        player.send((byte)CMDPlayer.JOINROOMFAILED, BitConverter.GetBytes(pi.roomnumber));
                        Logger.log(pi.roomnumber + " JOINROOMFAILED");
                    }
                    else
                    {
                    }
                    Logger.log(" :joinroom");

                    break;

                case CMDMatchServer.STARTGAME:
                    playerid = BitConverter.ToInt32(buffer, 1);
                    b        = Players.TryGetValue(playerid, out player);
                    if (b)
                    {
                        int    side       = BitConverter.ToInt32(buffer, 5);
                        int    dsport     = BitConverter.ToInt32(buffer, 9);
                        int    roomid     = BitConverter.ToInt32(buffer, 13);
                        string dswan      = Encoding.getstring(buffer, 17, buffer.Length - 17);
                        byte[] tempbuffer = new byte[buffer.Length - 5];
                        Array.Copy(buffer, 5, tempbuffer, 0, tempbuffer.Length);
                        player.send((byte)CMDPlayer.STARTGAME, tempbuffer);
                        Logger.log("--player.playerinfor-- : " + player.playerinfor + "--side-- : " + side + "--dsport--" + dsport + "--dswan-- " + dswan);
                    }
                    else
                    {
                    }
                    Logger.log(" :joinroom");

                    break;

                case CMDMatchServer.PLAYEREXITQUEST:

                    Logger.log(" :playerexitquest");

                    break;

                case CMDMatchServer.OTHERPLAYERINFOR:
                    playerid = BitConverter.ToInt32(buffer, 1);
                    b        = Players.TryGetValue(playerid, out player);
                    if (b)
                    {
                        pi = new playerinfor();
                        ms = new MemoryStream();
                        ms.Write(buffer, 5, buffer.Length - 5);
                        ms.Position = 0;
                        pi          = Serializer.Deserialize <playerinfor>(ms);
                        byte[] otherinfor = Encoding.getbyte(pi.SimulateInforStr);
                        byte[] t          = new byte[otherinfor.Length + 12];
                        t.WriteTo(0, pi.playerid);
                        t.WriteTo(4, pi.side);
                        t.WriteTo(8, pi.oldplayerid);
                        Array.Copy(otherinfor, 0, t, 12, otherinfor.Length);
                        player.send((byte)CMDPlayer.OTHERPLAYERINFOR, t);
                        Logger.log("--otherplayer.playerinfor-- : " + pi.SimulateInforStr + "--side-- : " + pi.side + "--dsport--");
                    }
                    else
                    {
                    }
                    Logger.log(" :playerexitquest");

                    break;

                case CMDMatchServer.RECONNECT:
                    playerid = BitConverter.ToInt32(buffer, 1);
                    int result = BitConverter.ToInt32(buffer, 5);
                    b = Players.TryGetValue(playerid, out player);
                    byte[] byterosend = new byte[8];
                    byterosend.WriteTo(0, playerid);
                    byterosend.WriteTo(4, result);
                    if (b)
                    {
                        player.send((byte)CMDPlayer.RECONNECT, byterosend);
                        Logger.log("--playerid-- : " + playerid + "--reconnect-- : " + result);
                    }
                    else
                    {
                        Logger.log("player id not exist ,this should not happen");
                    }
                    break;

                case CMDMatchServer.RECONNECTV1:
                    playerid   = BitConverter.ToInt32(buffer, 1);
                    result     = BitConverter.ToInt32(buffer, 5);
                    b          = Players.TryGetValue(playerid, out player);
                    byterosend = new byte[8];
                    byterosend.WriteTo(0, playerid);
                    byterosend.WriteTo(4, result);
                    if (b)
                    {
                        player.send((byte)CMDPlayer.RECONNECT, byterosend);
                        Logger.log("--playerid-- : " + playerid + "--reconnect-- : " + result);
                    }
                    else
                    {
                        Logger.log("player id not exist ,this should not happen");
                    }
                    break;

                default:
                    break;
                }
            };
        }
        public LoginServerProxy(int id, KChannel channel, MatchServer matchserver)
        {
            this.id = id;
            this.channel_loginserver = channel;
            this.matchserver         = matchserver;
            this.channel_loginserver.ondisconnect += () => {
                LoginServerProxy lsp;
                bool             b = this.matchserver.LoginServers.TryRemove(id, out lsp);
                if (b)
                {
                    Logger.log("channel_loginserver.ondisconnect successfully");
                }
                else
                {
                    Logger.log("this should not happen for channel_loginserver.ondisconnect");
                }
            };
            channel_loginserver.onUserLevelReceivedCompleted += (ref byte[] buffer) => {
                //var str = Encoding.getstring(buffer, 1, buffer.Length - 1);
                switch ((CMDMatchServer)buffer[0])
                {
                case CMDMatchServer.MATCHREQUEST:
                    playerinfor  pi = new playerinfor();
                    MemoryStream ms = new MemoryStream();
                    ms.Write(buffer, 1, buffer.Length - 1);
                    ms.Position           = 0;
                    pi                    = Serializer.Deserialize <playerinfor>(ms);
                    pi.loginserverproxyid = this.id;
                    Logger.log(pi.SimulateInforStr);
                    matchserver.addtomatchpool(pi);
                    Logger.log(pi.playerid + " :matchrequest");

                    break;

                case CMDMatchServer.CREATEROOM:
                    pi = new playerinfor();
                    ms = new MemoryStream();
                    ms.Write(buffer, 1, buffer.Length - 1);
                    ms.Position           = 0;
                    pi                    = Serializer.Deserialize <playerinfor>(ms);
                    pi.loginserverproxyid = this.id;
                    Logger.log(pi.SimulateInforStr);
                    matchserver.addtomatchpoolV1(pi);
                    Room room = RoomManager.getsingleton().createroom(pi.halfroomnumber);
                    room.addplayerv1(pi);
                    /////////////////////////////////////////
                    pi.roomnumber = room.id;
                    ms            = new MemoryStream();
                    Serializer.Serialize(ms, pi);
                    sendtologinserver((byte)CMDMatchServer.CREATEROOM, ms.ToArray());
                    Logger.log(pi.roomnumber + " :pi.roomnumber CREATEROOM ,pi.halfroomnumber : " + pi.halfroomnumber);
                    break;

                case CMDMatchServer.JOINROOM:
                    pi = new playerinfor();
                    ms = new MemoryStream();
                    ms.Write(buffer, 1, buffer.Length - 1);
                    ms.Position           = 0;
                    pi                    = Serializer.Deserialize <playerinfor>(ms);
                    pi.loginserverproxyid = this.id;
                    Logger.log(pi.SimulateInforStr);
                    matchserver.addtomatchpoolV1(pi);
                    if (RoomManager.getsingleton().CreatingRooms.TryGetValue(pi.roomnumber, out room))
                    {
                        room.addplayerv1(pi);
                        /////////////////////////////////////////
                        pi.roomnumber = room.id;
                        ms            = new MemoryStream();
                        Serializer.Serialize(ms, pi);
                        sendtologinserver((byte)CMDMatchServer.JOINROOM, ms.ToArray());
                        Logger.log(pi.roomnumber + " :pi.roomnumber JOINROOM");
                    }
                    else
                    {
                        ms = new MemoryStream();
                        Serializer.Serialize(ms, pi);
                        sendtologinserver((byte)CMDMatchServer.JOINROOMFAILED, ms.ToArray());
                        Logger.log("join room but the room with specific number is not found ,this roomnumber is : " + pi.roomnumber);
                    }


                    break;

                case CMDMatchServer.STARTGAME:
                    pi = new playerinfor();
                    ms = new MemoryStream();
                    ms.Write(buffer, 1, buffer.Length - 1);
                    ms.Position           = 0;
                    pi                    = Serializer.Deserialize <playerinfor>(ms);
                    pi.loginserverproxyid = this.id;
                    Logger.log(id + " :startgame");
                    if (RoomManager.getsingleton().CreatingRooms.TryGetValue(pi.roomnumber, out room))
                    {
                        Logger.log(pi.roomnumber + " :pi.roomnumber STARTGAME");
                        room.startgame();
                    }
                    else
                    {
                        Logger.log("join room but the room with specific number is not found");
                    }


                    break;

                case CMDMatchServer.PLAYEREXITQUEST:
                    int playerid = BitConverter.ToInt32(buffer, 1);
                    matchserver.removefrommatchpool(playerid);
                    Logger.log("loginserverproxyid: " + id + " :playerexitquest playerid " + playerid);

                    break;

                case CMDMatchServer.RECONNECT:

                    pi = new playerinfor();
                    ms = new MemoryStream();
                    ms.Write(buffer, 1, buffer.Length - 1);
                    ms.Position           = 0;
                    pi                    = Serializer.Deserialize <playerinfor>(ms);
                    pi.loginserverproxyid = this.id;
                    Logger.log("loginserverproxyid: " + id + " playerid RECONNECT:  " + pi.playerid);
                    byte[] feedbackbuffer = new byte[8];
                    feedbackbuffer.WriteTo(0, pi.playerid);
                    Filter filter;
                    if (Filter.Filtertracker.TryGetValue(pi.oldplayerid, out filter))
                    {
                        playerinfor p1;
                        filter.players.TryRemove(pi.oldplayerid, out p1);
                        if (p1 == null)
                        {
                        }
                        else
                        {
                            matchserver.removefrommatchpool(pi.oldplayerid);
                            matchserver.onlyaddtomatchpool(pi);
                            filter.players.TryAdd(pi.playerid, pi);
                            feedbackbuffer.WriteTo(4, 1);
                            sendtologinserver((byte)CMDMatchServer.RECONNECT, feedbackbuffer);
                            Logger.log("=========Filter.Filtertracker:   playerid :" + pi.playerid + "oldplayerid : " + pi.oldplayerid);
                        }

                        break;
                    }
                    if (RoomManager.getsingleton().waitingRooms.TryGetValue(pi.roomid, out room))
                    {
                        playerinfor p1 = room.removeplayer(pi.oldplayerid);    //waitingrooms and fightingrooms may contain the same roomid but playid is Globally unique
                        //if (p1 == null)
                        //{ }
                        //else
                        {
                            matchserver.removefrommatchpool(pi.oldplayerid);
                            matchserver.onlyaddtomatchpool(pi);
                            room.rejoin(pi);
                            feedbackbuffer.WriteTo(4, 1);
                            sendtologinserver((byte)CMDMatchServer.RECONNECT, feedbackbuffer);
                            Logger.log("=========waitingRooms:   playerid :" + pi.playerid + "oldplayerid : " + pi.oldplayerid);
                        }
                        break;
                    }
                    if (RoomManager.getsingleton().fightingRooms.TryGetValue(pi.roomid, out room))
                    {
                        playerinfor p1 = room.removeplayer(pi.oldplayerid);
                        //if (p1 == null)
                        //{ }
                        //else
                        {
                            matchserver.removefrommatchpool(pi.oldplayerid);
                            matchserver.onlyaddtomatchpool(pi);
                            room.rejoin(pi);
                            feedbackbuffer.WriteTo(4, 1);
                            sendtologinserver((byte)CMDMatchServer.RECONNECT, feedbackbuffer);
                            Logger.log("=========fightingRooms:   playerid :" + pi.playerid + "oldplayerid : " + pi.oldplayerid);
                        }

                        break;
                    }

                    feedbackbuffer.WriteTo(4, 0);
                    sendtologinserver((byte)CMDMatchServer.RECONNECT, feedbackbuffer);
                    Logger.log("=========failed:   playerid :" + pi.playerid + "oldplayerid : " + pi.oldplayerid);
                    break;

                case CMDMatchServer.RECONNECTV1:

                    pi = new playerinfor();
                    ms = new MemoryStream();
                    ms.Write(buffer, 1, buffer.Length - 1);
                    ms.Position           = 0;
                    pi                    = Serializer.Deserialize <playerinfor>(ms);
                    pi.loginserverproxyid = this.id;
                    Logger.log("loginserverproxyid: " + id + " playerid RECONNECT:  " + pi.playerid);
                    feedbackbuffer = new byte[8];
                    feedbackbuffer.WriteTo(0, pi.playerid);
                    if (RoomManager.getsingleton().CreatingRooms.TryGetValue(pi.roomid, out room))
                    {
                        playerinfor p1 = room.getplayer(pi.oldplayerid);
                        if (p1 == null)
                        {
                        }
                        else
                        {
                            matchserver.removefrommatchpool(pi.oldplayerid);
                        }
                        matchserver.onlyaddtomatchpool(pi);
                        room.rejoin(pi);
                        feedbackbuffer.WriteTo(4, 1);
                        sendtologinserver((byte)CMDMatchServer.RECONNECTV1, feedbackbuffer);
                        break;
                    }

                    if (RoomManager.getsingleton().CreatedRooms.TryGetValue(pi.roomid, out room))
                    {
                        playerinfor p1 = room.getplayer(pi.oldplayerid);
                        if (p1 == null)
                        {
                        }
                        else
                        {
                            matchserver.removefrommatchpool(pi.oldplayerid);
                        }
                        matchserver.onlyaddtomatchpool(pi);
                        room.rejoin(pi);
                        feedbackbuffer.WriteTo(4, 1);
                        sendtologinserver((byte)CMDMatchServer.RECONNECTV1, feedbackbuffer);
                        break;
                    }

                    feedbackbuffer.WriteTo(4, 0);
                    sendtologinserver((byte)CMDMatchServer.RECONNECT, feedbackbuffer);
                    break;

                default:
                    break;
                }
            };
        }