public void OnConnectButtonClick(object sender, EventArgs e)
        {
            this.InvokeEx(() => selectContenderPage.Enabled = false);

            string        selectedPlayer = (selectContenderPage.listBoxContenders.SelectedItem as RegistrationData).Login;
            StartGameData startGameData  = new StartGameData(
                signInPage.textBoxLogin.Text
                , selectedPlayer
                , ( GameColor )random.Next(2)
                , ( ChessDirection )random.Next(2)
                );

            socket.Send(Packet.StartGamePacket(startGameData));

            WaitResult(2000);
            this.InvokeEx(() => selectContenderPage.Enabled = true);
        }
        private void StartGame(StartGameData startGameData)
        {
            Page = gamePage;

            Player player1 = new Player(startGameData.LoginQuery, startGameData.ColorQuery, startGameData.DirectionQuery);
            Player player2 = new Player(
                startGameData.LoginReply
                , startGameData.ColorQuery == GameColor.White ? GameColor.Black : GameColor.White
                , startGameData.DirectionQuery == ChessDirection.Up ? ChessDirection.Down : ChessDirection.Up
                );

            gamePage.Game.Start(player1, player2);
            Task.Factory.StartNew(() =>
            {
                while (!IsEndGame && !IsDisposed && socket.Connected)
                {
                    WaitResult();
                }
            });
        }
        private void OnReceive(object sender, SocketAsyncEventArgs e)
        {
            connectLocker.Set();

            try
            {
                if (e.Buffer != null)
                {
                    Packet packet = Packet.FromBytes(e.Buffer);

                    switch (packet.PacketType)
                    {
                    case Packet.Type.SignIn:
                    {
                        SignInData       signInData   = ( SignInData )packet.Data;
                        RegistrationData registration = database.Find(signInData.Login);
                        if (registration == null || registration.Password != signInData.Password)
                        {
                            e.AcceptSocket.Send(Packet.SignInResultPacket(SignInResult.InvalidPasswordOrLogin));
                        }
                        else
                        {
                            e.AcceptSocket.Send(Packet.SignInResultPacket(SignInResult.OK));
                            this.InvokeEx(() => listBoxFreeClients.Items.Add(registration));
                            this.InvokeEx(() => connections.Add(registration.Login, e.AcceptSocket));
                        }
                    }
                    break;

                    case Packet.Type.Registration:
                    {
                        RegistrationData registrationData = ( RegistrationData )packet.Data;
                        if (database.ContainsLogin(registrationData.Login))
                        {
                            e.AcceptSocket.Send(Packet.RegistrationResultPacket(RegistrationResult.LoginAllreadyExist));
                        }
                        else
                        {
                            e.AcceptSocket.Send(Packet.RegistrationResultPacket(RegistrationResult.OK));
                            database.Add(registrationData);
                        }
                    }
                    break;

                    case Packet.Type.GetConnectedPlayers:
                    {
                        string login = ( string )packet.Data;
                        List <RegistrationData> players = new List <RegistrationData>();
                        foreach (RegistrationData reg in listBoxFreeClients.Items)
                        {
                            if (reg.Login != login)
                            {
                                players.Add(reg);
                            }
                        }
                        e.AcceptSocket.Send(Packet.GiveConnectedPlayersPacket(players));
                    }
                    break;

                    case Packet.Type.StartGame:
                    {
                        StartGameData startGameData = ( StartGameData )packet.Data;
                        Socket        query         = connections[startGameData.LoginReply];
                        query.Send(packet);
                    }
                    break;

                    case Packet.Type.StartGameResult:
                    {
                        StartGameData startGameData = ( StartGameData )packet.Data;

                        if (startGameData.Result == StartGameResult.OK)
                        {
                            this.InvokeEx(() =>
                                {
                                    listBoxFreeClients.Items.Remove(database.Find(startGameData.LoginQuery));
                                    listBoxFreeClients.Items.Remove(database.Find(startGameData.LoginReply));
                                    listBoxInTheGame.Items.Add(startGameData);
                                });
                        }

                        Socket reply = connections[startGameData.LoginQuery];
                        reply.Send(packet);
                    }
                    break;

                    case Packet.Type.MoveChess:
                    {
                        ChessMoveData chessMoveData = ( ChessMoveData )packet.Data;
                        Socket        send          = connections[chessMoveData.LoginСontender];
                        send.Send(packet);
                    }
                    break;

                    case Packet.Type.AbortGame:
                    case Packet.Type.EndGame:
                    {
                        this.InvokeEx(() =>
                            {
                                StartGameData startGameDataRemove = null;

                                string login = FindLoginBySocket(e.AcceptSocket);
                                foreach (StartGameData startGameData in listBoxInTheGame.Items)
                                {
                                    if (startGameData.LoginQuery == login || startGameData.LoginReply == login)
                                    {
                                        if (packet.PacketType == Packet.Type.AbortGame)
                                        {
                                            string sendLogin = (login == startGameData.LoginQuery)
                                                    ?   startGameData.LoginReply
                                                    :   startGameData.LoginQuery;
                                            connections[sendLogin].Send(packet);
                                        }
                                        startGameDataRemove = startGameData;
                                        break;
                                    }
                                }

                                if (startGameDataRemove != null)
                                {
                                    listBoxFreeClients.Items.Add(database.Find(startGameDataRemove.LoginQuery));
                                    listBoxFreeClients.Items.Add(database.Find(startGameDataRemove.LoginReply));
                                    listBoxInTheGame.Items.Remove(startGameDataRemove);
                                }
                            });
                    }
                    break;
                    }
                }
            }
            catch
            {
                e.AcceptSocket.Close();
                this.InvokeEx(() =>
                {
                    string removeItem             = string.Empty;
                    RegistrationData registration = database.Find(FindLoginBySocket(e.AcceptSocket));
                    if (registration != null && listBoxFreeClients.Items.Contains(registration))
                    {
                        listBoxFreeClients.Items.Remove(registration);
                        removeItem = registration.Login;
                    }

                    if (removeItem.Length > 0)
                    {
                        connections.Remove(removeItem);
                    }
                });
            }
        }
        private void OnRecieve(object sender, SocketAsyncEventArgs e)
        {
            waitLocker.Set();

            if (e.AcceptSocket == null)
            {
                return;
            }

            Packet packet;

            try
            {
                packet = Packet.FromBytes(e.Buffer);
            }
            catch
            {
                return;
            }

            switch (packet.PacketType)
            {
            case Packet.Type.GiveConnectedPlayers:
            {
                List <RegistrationData> connectedPlayers = (List <RegistrationData>)packet.Data;
                this.InvokeEx(() =>
                    {
                        selectContenderPage.listBoxContenders.Items.Clear();
                        selectContenderPage.listBoxContenders.Items.AddRange(connectedPlayers.ToArray());
                    });
            }
            break;

            case Packet.Type.StartGame:
            {
                StartGameData startGameData = ( StartGameData )packet.Data;

                DialogResult result = MessageBox.Show(startGameData.LoginQuery + " offers play", "Start game offer", MessageBoxButtons.YesNo);
                if (result == DialogResult.Yes)
                {
                    socket.Send(Packet.StartGameReplyPacket(startGameData.Reply(StartGameResult.OK)));
                    this.InvokeEx(() => StartGame(startGameData));
                }
                else if (result == DialogResult.No)
                {
                    socket.Send(Packet.StartGameReplyPacket(startGameData.Reply(StartGameResult.Cancel)));
                }
            }
            break;

            case Packet.Type.StartGameResult:
            {
                StartGameData startGameData = ( StartGameData )packet.Data;
                if (startGameData.Result == StartGameResult.OK)
                {
                    this.InvokeEx(() => StartGame(startGameData));
                }
            }
            break;

            case Packet.Type.MoveChess:
            {
                ChessMoveData chessMoveData = ( ChessMoveData )packet.Data;
                this.InvokeEx(() =>
                    {
                        Chess chess = gamePage.Game.Desk[chessMoveData.From].Chess;
                        if (gamePage.Game.Move(chess, chessMoveData.To))
                        {
                            gamePage.Game.SwapPlayers();
                            gamePage.GameControl.Repaint();
                            if (IsEndGame)
                            {
                                Page = selectContenderPage;
                                UpdateContendersList();
                                MessageBox.Show("Game over");
                            }
                            if (IsCheck)
                            {
                                MessageBox.Show("Check");
                            }
                        }
                    });
            }
            break;

            case Packet.Type.AbortGame:
                this.InvokeEx(() =>
                {
                    MessageBox.Show("Abort game");
                    gamePage.Game.GameState = Game.State.Finish;
                    Page = selectContenderPage;
                    UpdateContendersList();
                });
                break;
            }
        }
示例#5
0
 public static Packet StartGameReplyPacket(StartGameData replyData)
 {
     return(new Packet(Type.StartGameResult, replyData));
 }
示例#6
0
 public static Packet StartGamePacket(StartGameData data)
 {
     return(new Packet(Type.StartGame, data));
 }