Beispiel #1
0
        public void ClObjProcess()
        {
            RegPacket pack = new RegPacket();

            pack.Command = PacketsToServer.RegPacket;
            pack.Name    = name;
            string mes = JsonConvert.SerializeObject(pack);

            Send(mes);
            NetworkStream stream = null;

            try
            {
                stream = client.GetStream();
                byte[] data = new byte[1024]; // буфер для получаемых данных
                while (true)
                {
                    // получаем сообщение
                    StringBuilder builder = new StringBuilder();
                    int           bytes   = 0;

                    do
                    {
                        bytes = stream.Read(data, 0, data.Length);
                        builder.Append(Encoding.Unicode.GetString(data, 0, bytes));
                    }while (stream.DataAvailable);
                    string message = builder.ToString();
                    que.Enqueue(message);
                    ReciveMesFromServ(message);
                }
            }
            catch (Exception ex)
            {
            }
            finally
            {
                if (stream != null)
                {
                    stream.Close();
                }
                if (client != null)
                {
                    client.Close();
                }
            }
        }
Beispiel #2
0
        public void ReciveMessages(string message)
        {
            switch (JsonConvert.DeserializeObject <RegPacket>(message).Command)
            {
            case PacketsToServer.RegPacket:
                RegPacket       reg    = JsonConvert.DeserializeObject <RegPacket>(message);
                bool            flag   = ServerMain.Check(reg.Name);
                ResultRegPacket result = new ResultRegPacket();
                if (flag)
                {
                    result.Command         = PacketsToServer.ResultRegPacket;
                    result.StatusOfRegistr = Status.success;
                    clientinf = new Player(reg.Name);
                    ServerMain.gameclients.Add(clientinf);
                    ServerMain.FirstList.Add(reg.Name, client);
                    Console.WriteLine("Имя клиента " + reg.Name);
                    ListOfAllClients list = new ListOfAllClients();
                    list.Command        = PacketsToServer.ListOfAllClients;
                    list.ListAllClients = makelist();
                    strpacket           = JsonConvert.SerializeObject(list);
                    SendToAllPlayersInTheGame(strpacket);
                }
                else
                {
                    result.Command         = PacketsToServer.ResultRegPacket;
                    result.StatusOfRegistr = Status.fail;
                    Console.WriteLine("Клиент с таким именем уже существует");
                }
                strpacket = JsonConvert.SerializeObject(result);
                Send(strpacket);
                break;

            case PacketsToServer.AskGamePacket:

                AskGamePacket     stwp   = JsonConvert.DeserializeObject <AskGamePacket>(message);
                StartWindowPacket stgapc = new StartWindowPacket();    //отправляю список клиента при входе в игру
                stgapc.Command        = PacketsToServer.StartWindowPacket;
                stgapc.ListAllClients = makelist();
                strpacket             = JsonConvert.SerializeObject(stgapc);
                Send(strpacket);

                break;

            case PacketsToServer.WaitGamePacket:
                WaitGamePacket waitfg = JsonConvert.DeserializeObject <WaitGamePacket>(message);
                this.clientinf.Status = StatusGamer.lookforgame;
                List <Player>        listWaitingplayers = MakeReadyForGameListOfPlayers();
                ListOfWaitingClients waitcl             = new ListOfWaitingClients();
                waitcl.Command            = PacketsToServer.ListOfWaitingClients;
                waitcl.ListWaitingClients = makelist();
                strpacket = JsonConvert.SerializeObject(waitcl);
                Send(strpacket);
                break;

            case PacketsToServer.ChooseEnemyPacket:
                ChooseEnemyPacket ch = JsonConvert.DeserializeObject <ChooseEnemyPacket>(message);
                this.clientinf.Status = StatusGamer.lookforgame;

                foreach (Player client in ServerMain.gameclients)
                {
                    if (client.Name == ch.EnemyLogin && client.Status == StatusGamer.lookforgame &&
                        CheckMe(clientinf) == StatusGamer.lookforgame)
                    {
                        ResultChooseEnemyPacketSuccess resultchen = new ResultChooseEnemyPacketSuccess();
                        resultchen.Command            = PacketsToServer.ResultChooseEnemyPacketSuccess;
                        resultchen.StatusOfChoseEnemy = Status.success;
                        resultchen.enemylogin         = ch.EnemyLogin;
                        var obj = JsonConvert.DeserializeObject <List <Card> >(File.ReadAllText("1.json"));
                        resultchen.listAllCards = obj;
                        strpacket = JsonConvert.SerializeObject(resultchen);
                        Send(strpacket);

                        return;
                    }
                    else
                    {
                        ResultChooseEnemyPacketFailed pack2 = new ResultChooseEnemyPacketFailed();
                        pack2.Command            = PacketsToServer.ResultChooseEnemyPacketFailed;
                        pack2.StatusOfChoseEnemy = Status.fail;
                        strpacket = JsonConvert.SerializeObject(pack2);
                        Send(strpacket);
                        return;
                    }
                }
                break;

            case PacketsToServer.ChoosenCardListPacket:
                ChoosenCardListPacket kol         = JsonConvert.DeserializeObject <ChoosenCardListPacket>(message);
                ResultChooseCardList  packetcheck = new ResultChooseCardList();
                packetcheck.Command = PacketsToServer.ResultChooseCardList;
                if (checkkoloda(kol.Koloda))
                {
                    packetcheck.ResultOfChooseCard = Status.success;
                }
                else
                {
                    packetcheck.ResultOfChooseCard = Status.fail;
                }
                strpacket = JsonConvert.SerializeObject(packetcheck);
                Send(strpacket);
                break;

            case PacketsToServer.StartGamePacket:
                StartGamePacket stgapacket = JsonConvert.DeserializeObject <StartGamePacket>(message);
                stgapacket.Me.Status    = StatusGamer.playing;
                stgapacket.Enemy.Status = StatusGamer.playing;
                room         = new Rooms();
                room.Player1 = clientinf;
                room.Player2 = stgapacket.Enemy;
                StartGame(this.clientinf, stgapacket.Enemy);
                break;

            case PacketsToServer.StepPacket:
                StepPacket stpac = JsonConvert.DeserializeObject <StepPacket>(message);
                GameStep(stpac.EnemyCard, stpac.MyCard, stpac.Enemy);

                break;

            case PacketsToServer.PacketArenaCardNow:
                PacketArenaCardNow parcadnow = JsonConvert.DeserializeObject <PacketArenaCardNow>(message);
                if (parcadnow.MyCard.Price < this.clientinf.Mana)
                {
                    controller.ToArena(this.clientinf, parcadnow.MyCard);
                    SendDataToUsers send = DataToSendPrepare(room.Player2);
                    strpacket = JsonConvert.SerializeObject(send);
                    SendToAllPlayersInTheRoom(strpacket, room.Player2);
                }
                else
                {
                    Error error = new Error();
                    error.Command     = PacketsToServer.Error;
                    error.ErrorToUser = "******";
                }
                break;
            }
        }
Beispiel #3
0
        public void ReciveMessages(string message)
        {
            // try
            // {
            switch (JsonConvert.DeserializeObject <RegPacket>(message).Command)
            {
            case PacketsToServer.RegPacket:
                RegPacket reg  = JsonConvert.DeserializeObject <RegPacket>(message);
                bool      flag = ServerMain.Check(reg.Name);

                if (flag)
                {
                    ResultRegPacket result = new ResultRegPacket();
                    result.Command         = PacketsToServer.ResultRegPacket;
                    result.StatusOfRegistr = Status.success;
                    clientinf = new Player(reg.Name);
                    ServerMain.gameclients.Add(clientinf);
                    Thread.Sleep(50);
                    lock (ServerMain.FirstList)
                    {
                        ServerMain.FirstList.Add(reg.Name, this.client);
                    }
                    result.ListAllClients = makelist();
                    strpacket             = JsonConvert.SerializeObject(result) + "$";
                    Console.WriteLine("Имя клиента " + reg.Name);
                }
                else
                {
                    ResultRegPacketFailed result = new ResultRegPacketFailed();
                    result.Command         = PacketsToServer.ResultRegPacket;
                    result.StatusOfRegistr = Status.fail;
                    strpacket = JsonConvert.SerializeObject(result) + "$";
                    Console.WriteLine("Клиент с таким именем уже существует");
                }

                Send(strpacket);
                ListOfAllClients list = new ListOfAllClients();
                list.Command        = PacketsToServer.ListOfAllClients;
                list.ListAllClients = makelist();
                strpacket           = JsonConvert.SerializeObject(list) + "$";
                SendToAllPlayersInTheGameWithOutME(strpacket);
                break;

            case PacketsToServer.AskGamePacket:

                AskGamePacket     stwp   = JsonConvert.DeserializeObject <AskGamePacket>(message);
                StartWindowPacket stgapc = new StartWindowPacket();    //отправляю список клиента при входе в игру
                stgapc.Command        = PacketsToServer.StartWindowPacket;
                stgapc.ListAllClients = makelist();
                strpacket             = JsonConvert.SerializeObject(stgapc) + "$";
                Send(strpacket);

                break;

            case PacketsToServer.WaitGamePacket:
                WaitGamePacket waitfg = JsonConvert.DeserializeObject <WaitGamePacket>(message);
                foreach (Player b in ServerMain.gameclients)
                {
                    if (b.Name == waitfg.login)
                    {
                        b.Status = StatusGamer.lookforgame;
                    }
                }
                sendingList();
                break;

            case PacketsToServer.StopLookingforGamePacket:
                StopLookingForGame stop = JsonConvert.DeserializeObject <StopLookingForGame>(message);
                foreach (Player b in ServerMain.gameclients)
                {
                    if (b.Name == stop.Name)
                    {
                        b.Status = StatusGamer.sleeping;
                    }
                }
                sendingList();
                break;

            case PacketsToServer.EXIT:
                Exit ex = JsonConvert.DeserializeObject <Exit>(message);
                Console.WriteLine("Клиент покинул игру");
                foreach (Player b in ServerMain.gameclients)
                {
                    if (b.Name == ex.login)
                    {
                        ServerMain.gameclients.Remove(b);
                        //  ServerMain.FirstList.Remove(b.Name);
                    }
                }
                ListOfAllClients list1 = new ListOfAllClients();
                list1.Command        = PacketsToServer.ListOfAllClients;
                list1.ListAllClients = makelist();
                strpacket            = JsonConvert.SerializeObject(list1) + "$";
                SendToAllPlayersInTheGameWithOutME(strpacket);
                foreach (KeyValuePair <string, TcpClient> b in ServerMain.FirstList)
                {
                    if (b.Key == ex.login)
                    {
                        b.Value.Close();
                        ServerMain.FirstList.Remove(b.Key);
                        return;
                    }
                }

                break;

            case PacketsToServer.ChooseEnemyPacket:
                ChooseEnemyPacket ch = JsonConvert.DeserializeObject <ChooseEnemyPacket>(message);
                this.clientinf.Status = StatusGamer.lookforgame;

                foreach (Player client in ServerMain.gameclients)
                {
                    if (client.Name == ch.EnemyLogin && client.Status == StatusGamer.lookforgame &&
                        CheckMe(clientinf) == StatusGamer.lookforgame)
                    {
                        AskGamePacket packet = new AskGamePacket();
                        packet.Command = PacketsToServer.AskGamePacket;
                        packet.login   = ch.MyLogin;
                        strpacket      = JsonConvert.SerializeObject(packet) + "$";

                        SendToEnemy(strpacket, ch.EnemyLogin);

                        return;
                    }
                    else if (client.Name != clientinf.Name)
                    {
                        ResultChooseEnemyPacketFailed pack2 = new ResultChooseEnemyPacketFailed();
                        pack2.Command            = PacketsToServer.ResultChooseEnemyPacketFailed;
                        pack2.StatusOfChoseEnemy = Status.fail;
                        strpacket = JsonConvert.SerializeObject(pack2) + "$";
                        Send(strpacket);
                    }
                }
                break;

            case PacketsToServer.AnsGamePacket:
                AnsGamePacket answer = JsonConvert.DeserializeObject <AnsGamePacket>(message);
                if (answer.state == true)
                {
                    ResultChooseEnemyPacketSuccess resultchen = new ResultChooseEnemyPacketSuccess();
                    resultchen.Command            = PacketsToServer.ResultChooseEnemyPacketSuccess;
                    resultchen.StatusOfChoseEnemy = Status.success;
                    resultchen.enemylogin         = answer.Enemylogin; //логин врага
                    resultchen.MyLogin            = answer.Mylogin;    //логин врага
                    var obj = JsonConvert.DeserializeObject <List <CardHeroes> >(File.ReadAllText("1.json"));
                    resultchen.listAllCards = obj;
                    strpacket = JsonConvert.SerializeObject(resultchen) + "$";
                    Send(strpacket);
                    resultchen.enemylogin = answer.Mylogin;
                    resultchen.MyLogin    = answer.Enemylogin;
                    strpacket             = JsonConvert.SerializeObject(resultchen) + "$";
                    SendToEnemy(strpacket, answer.Enemylogin);
                    Rooms room = new Rooms();
                    room.Player1 = clientinf;
                    room.Player2 = WhoIsHe(answer.Enemylogin);
                    ServerMain.rooms.Add(room);
                }
                else
                {
                    ResultChooseEnemyPacketFailed pack2 = new ResultChooseEnemyPacketFailed();
                    pack2.Command            = PacketsToServer.ResultChooseEnemyPacketFailed;
                    pack2.StatusOfChoseEnemy = Status.fail;
                    strpacket = JsonConvert.SerializeObject(pack2) + "$";
                    Send(strpacket);
                }

                break;

            case PacketsToServer.ChoosenCardListPacket:
                lock (_locker)
                {
                    ChoosenCardListPacket kol         = JsonConvert.DeserializeObject <ChoosenCardListPacket>(message);
                    ResultChooseCardList  packetcheck = new ResultChooseCardList();
                    ISErrorOfEnemy        err         = new ISErrorOfEnemy();
                    err.Command         = PacketsToServer.ISErrorOfEnemy;
                    packetcheck.Command = PacketsToServer.ResultChooseCardList;
                    if (checkkoloda(kol.Koloda))
                    {
                        err.ISErr = false;
                        packetcheck.ResultOfChooseCard = Status.success;
                        clientinf.Deck = kol.Koloda;
                    }
                    else
                    {
                        err.ISErr = true;
                        packetcheck.ResultOfChooseCard = Status.fail;
                    }
                    strpacket         = JsonConvert.SerializeObject(packetcheck) + "$";
                    clientinf.NumRoom = InWhichRoom(clientinf);
                    Send(strpacket);
                    strpacket = JsonConvert.SerializeObject(err) + "$";
                    ////int i=InWhichRoom()
                    ////if(ServerMain.rooms[].Player1.Name!=kol.Me)
                    ////SendToEnemy(strpacket, rooms.Player1.Name);
                    if (ServerMain.rooms[clientinf.NumRoom].Player2.Name != clientinf.Name)
                    {
                        SendToEnemy(strpacket, ServerMain.rooms[clientinf.NumRoom].Player2.Name);
                    }
                    else
                    {
                        SendToEnemy(strpacket, ServerMain.rooms[clientinf.NumRoom].Player1.Name);
                    }
                }
                break;

            case PacketsToServer.StartGamePacket:
                lock (_locker)
                {
                    StartGamePacket stgapacket = JsonConvert.DeserializeObject <StartGamePacket>(message);
                    foreach (Player pl in ServerMain.gameclients)
                    {
                        if (stgapacket.Me == pl.Name || stgapacket.Enemy == pl.Name)
                        {
                            pl.Status = StatusGamer.playing;
                        }
                    }
                    sendingList();
                    foreach (Player pl in ServerMain.gameclients)
                    {
                        if (stgapacket.Enemy == pl.Name)
                        {
                            StartGame(pl);
                        }
                    }
                }
                break;

            case PacketsToServer.StepPacket:
                StepPacket stpac = JsonConvert.DeserializeObject <StepPacket>(message);
                GameStep(stpac.EnemyCard, stpac.MyCard, WhoIsHe(stpac.Enemy));

                break;

            case PacketsToServer.EndStep:
                EndStep         step = JsonConvert.DeserializeObject <EndStep>(message);
                SendDataToUsers a;
                if (ServerMain.rooms[clientinf.NumRoom].Player1 == clientinf)
                {
                    controller.IsmHoda(clientinf, ServerMain.rooms[clientinf.NumRoom].Player2);
                    a          = DataToSendPrepare(ServerMain.rooms[clientinf.NumRoom].Player2);
                    a.AmIFirst = clientinf.IsHod;
                    strpacket  = JsonConvert.SerializeObject(a) + "$";
                    Send(strpacket);
                    a         = DataToSendPrepareEnemy(ServerMain.rooms[clientinf.NumRoom].Player2);
                    strpacket = JsonConvert.SerializeObject(a) + "$";
                    SendToEnemy(strpacket, ServerMain.rooms[clientinf.NumRoom].Player2.Name);
                }
                else
                {
                    controller.IsmHoda(clientinf, ServerMain.rooms[clientinf.NumRoom].Player1);
                    a          = DataToSendPrepare(ServerMain.rooms[clientinf.NumRoom].Player1);
                    a.AmIFirst = clientinf.IsHod;
                    strpacket  = JsonConvert.SerializeObject(a) + "$";
                    Send(strpacket);
                    a         = DataToSendPrepareEnemy(ServerMain.rooms[clientinf.NumRoom].Player1);
                    strpacket = JsonConvert.SerializeObject(a) + "$";
                    SendToEnemy(strpacket, ServerMain.rooms[clientinf.NumRoom].Player1.Name);
                }
                break;

            case PacketsToServer.PacketArenaCardNow:
                PacketArenaCardNow parcadnow = JsonConvert.DeserializeObject <PacketArenaCardNow>(message);
                if (parcadnow.MyCard.Price <= this.clientinf.Mana)
                {
                    controller.ToArena(this.clientinf, parcadnow.MyCard);
                    CardOnABoard card = new CardOnABoard();
                    card.Command           = PacketsToServer.CardOnABoard;
                    card.login             = clientinf.Name;
                    parcadnow.MyCard.Index = parcadnow.IndeXMyCard;
                    card.card = parcadnow.MyCard;
                    strpacket = JsonConvert.SerializeObject(card) + "$";
                    if (clientinf.Name != ServerMain.rooms[clientinf.NumRoom].Player2.Name)
                    {
                        SendToAllPlayersInTheRoom(strpacket, ServerMain.rooms[clientinf.NumRoom].Player2.Name);
                    }
                    else
                    {
                        SendToAllPlayersInTheRoom(strpacket, ServerMain.rooms[clientinf.NumRoom].Player1.Name);
                    }
                }
                else
                {
                    Error error = new Error();
                    error.Command     = PacketsToServer.Error;
                    error.ErrorToUser = MessagesToClientErrors.NotEnouthMana;
                }
                break;

            case PacketsToServer.EnemyLeftGamePacket:
                EnemyLeftGamePacket left    = JsonConvert.DeserializeObject <EnemyLeftGamePacket>(message);
                EnemyLeftGamePacket newleft = new EnemyLeftGamePacket();
                newleft.Command = PacketsToServer.EnemyLeftGamePacket;
                newleft.login   = left.login;
                strpacket       = JsonConvert.SerializeObject(newleft) + "$";
                if (ServerMain.rooms[clientinf.NumRoom].Player1 == clientinf)
                {
                    SendToEnemy(strpacket, ServerMain.rooms[clientinf.NumRoom].Player2.Name);
                    foreach (Player player in ServerMain.gameclients)
                    {
                        if (player == clientinf || player == ServerMain.rooms[clientinf.NumRoom].Player2)
                        {
                            player.Status = StatusGamer.sleeping; sendingList();
                        }
                    }
                }
                else
                {
                    SendToEnemy(strpacket, ServerMain.rooms[clientinf.NumRoom].Player1.Name);
                    foreach (Player player in ServerMain.gameclients)
                    {
                        if (player == clientinf || player == ServerMain.rooms[clientinf.NumRoom].Player2)
                        {
                            player.Status = StatusGamer.sleeping; sendingList();
                        }
                    }
                }

                ServerMain.gameclients.Remove(WhoIsHe(left.login));
                ServerMain.FirstList.Remove(left.login);
                ServerMain.rooms.Remove(ServerMain.rooms[clientinf.NumRoom]);

                break;
            }
        }
Beispiel #4
0
        public void ClObjProcess()
        {
            NetworkStream stream = null;

            try
            {
                stream = client.GetStream();
                byte[]    data = new byte[1024]; // буфер для получаемых данных
                RegPacket pack = new RegPacket();
                pack.Command = PacketsToServer.RegPacket;
                pack.Name    = name;
                string mes = JsonConvert.SerializeObject(pack) + "$";
                Thread.Sleep(5);
                Send(mes);
                while (true)
                {
                    // получаем сообщение
                    StringBuilder builder = new StringBuilder();
                    int           bytes   = 0;

                    do
                    {
                        bytes = stream.Read(data, 0, data.Length);
                        builder.Append(Encoding.Unicode.GetString(data, 0, bytes));
                    }while (stream.DataAvailable);
                    string         message   = builder.ToString();
                    Queue <string> qupackets = new Queue <string>();
                    if (message != "")
                    {
                        string[] cases = message.Split('$');
                        for (int i = 0; i < cases.Length; i++)
                        {
                            if (cases[i] != "")
                            {
                                qupackets.Enqueue(cases[i]);
                            }
                        }
                        while (qupackets.Count != 0)
                        {
                            ReciveMesFromServ(qupackets.Dequeue());
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show("Сервер недоступен", "Ошибка!");
                while (Application.OpenForms.Count != 0)
                {
                    Form ifrm1 = Application.OpenForms[0];
                    if (ifrm1.InvokeRequired)
                    {
                        ifrm1.Invoke((MethodInvoker)(() =>
                                                     ifrm1.Close()));
                    }
                    else
                    {
                        ifrm1.Close();
                    }
                }
            }
            finally
            {
                if (stream != null)
                {
                    stream.Close();
                }
                if (client != null)
                {
                    client.Close();
                }
            }
        }