Exemplo n.º 1
0
        private void HandleCodeResultPacket(FM_Packet packet)
        {
            if (_gui.InvokeRequired)
            {
                HandleCodeResultPacketDel d = HandleCodeResultPacket;
                _gui.Invoke(d, new object[] { packet });
            }
            else
            {
                _gui.SetResultColor(_attempt, packet._message);
                // all 10s are red all 1s are white
                if (packet._message == "40")
                {
                    _gui.Enabled = false;
                    DialogResult result = MessageBox.Show("YOU WIN! \nDo you wish to play again?", "You are the winner!", MessageBoxButtons.YesNo);
                    switch (result)
                    {
                    case DialogResult.Yes:
                        init();
                        break;

                    case DialogResult.No:
                        Application.Exit();
                        break;
                    }
                }
                else if (_attempt == 9)
                {
                    _gui.Enabled = false;
                    MessageBox.Show("You failed to guess your opponents code :( \nWaiting for opponent to finish", "Waiting for opponent");
                    SendPacket(new FM_Packet(_ID, "GameTie", "Failed to guess within 9 attempts."));
                }
            }
        }
Exemplo n.º 2
0
 private void HandleDisconnectPacket(FM_Packet packet)
 {
     //Console.WriteLine("Player with ID " + packet._id + " has disconnected");
     _playerCount--;
     _names[Int16.Parse(packet._id)] = "Anonymous";
     _wins[Int16.Parse(packet._id)]  = 0;
     if (_connectPackages > _playerCount)
     {
         _connectPackages--;
     }
 }
Exemplo n.º 3
0
 private void HandleInitialCodePacket(FM_Packet packet)
 {
     //Console.WriteLine("Received code: " + packet._message + " from player " + packet._id);
     _playerCodes[int.Parse(packet._id)] = packet._message;
     if (_playerCodes.Count(s => s != null) == MAX_PLAYERS)
     {
         foreach (SslStream s in streamArray)
         {
             SendPacket(new FM_Packet("Ready", "GET READY"), s);
         }
         _timeStart = DateTime.Now;
     }
 }
Exemplo n.º 4
0
 private void HandleGameTiePacket(FM_Packet packet)
 {
     _ties++;
     if (_ties == 2)
     {
         FM_Packet gameTie = new FM_Packet("GameTie", "Reset game.");
         foreach (SslStream s in streamArray)
         {
             SendPacket(gameTie, s);
         }
         _ties = 0;
     }
 }
Exemplo n.º 5
0
        public int CheckID(FM_Packet packet)
        {
            int OpponentID;

            if (packet._id == "1")
            {
                OpponentID = 0;
            }
            else
            {
                OpponentID = 1;
            }
            return(OpponentID);
        }
Exemplo n.º 6
0
 private void HandleConnectPacket(FM_Packet packet)
 {
     _connectPackages++;
     Console.WriteLine("amount of connectpackages: " + _connectPackages);
     if (_connectPackages == MAX_PLAYERS)
     {
         FM_Packet p = new FM_Packet("Connect", "All players have connected");
         foreach (SslStream s in streamArray)
         {
             SendPacket(p, s);
         }
         _connectPackages = 0;
     }
 }
Exemplo n.º 7
0
        private void HandleGameTiePacket(FM_Packet packet)
        {
            _gui.Enabled = false;
            DialogResult result = MessageBox.Show("The game has ended in a tie... \nDo you wish to play again?", "Tie",
                                                  MessageBoxButtons.YesNo);

            switch (result)
            {
            case DialogResult.Yes:
                init();
                break;

            case DialogResult.No:
                Application.Exit();
                break;
            }
        }
Exemplo n.º 8
0
        private void HandleNameChangePacket(FM_Packet packet)
        {
            int OpponentID = CheckID(packet);

            _names[Byte.Parse(packet._id)] = packet._message;
            if (packet._message == _mostWinsName)
            {
                _wins[Byte.Parse(packet._id)] = _mostWins;
            }
            SendPacket(packet, streamArray[OpponentID]);
            if (packet._id == "0")
            {
                _playerNames[0] = packet._message;
            }
            else
            {
                _playerNames[1] = packet._message;
            }
        }
Exemplo n.º 9
0
        private void HandleGameLostPacket(FM_Packet packet)
        {
            if (_gui.InvokeRequired)
            {
                HandleGameLostPacketDel d = HandleGameLostPacket;
                _gui.Invoke(d, new object[] { packet });
            }
            else
            {
                _gui.Enabled = false;
                DialogResult result = MessageBox.Show(packet._message + "\nDo you wish to play again?", "You lose!", MessageBoxButtons.YesNo);
                switch (result)
                {
                case DialogResult.Yes:
                    init();
                    break;

                case DialogResult.No:
                    Application.Exit();
                    break;
                }
            }
        }
Exemplo n.º 10
0
        private void HandleHighscoresPacket(FM_Packet packet)
        {
            //Console.WriteLine("fastest times are: " + _fastestTimes.ToString());
            string highscores = "Current fastest times are: \n";

            for (int i = 0; i < _fastestTimes.Length; i++)
            {
                if (i == 9)
                {
                    highscores += (i + 1) + ".  '" + _fastestTimesNames[i] + "'    with    " +
                                  FormatTime(_fastestTimes[i]) + "\n";
                }
                else
                {
                    highscores += (i + 1) + ".    '" + _fastestTimesNames[i] + "'    with    " +
                                  FormatTime(_fastestTimes[i]) + "\n";
                }
            }
            highscores += "\nCurrent most wins recordholder is:    '" + _mostWinsName + "'    with    " + _mostWins +
                          " wins";

            //Console.WriteLine(highscores);
            SendPacket(new FM_Packet(packet._id, "Highscores", highscores));
        }
Exemplo n.º 11
0
        public FM_Client_Controller()
        {
            Application.ApplicationExit += OnApplicationExit;
            try
            {
                _serverConnection = new TcpClient();
                _serverConnection.Connect(_serverIP, _serverPort);
                _sslServerConnection = new SslStream(_serverConnection.GetStream(), false,
                                                     ValidateServerCertificate, null);
                try
                {
                    _sslServerConnection.AuthenticateAsClient(_serverIP, null, SslProtocols.Tls, false);
                    _reader = new StreamReader(_sslServerConnection, Encoding.ASCII);
                }
                catch (Exception e1)
                {
                    Console.WriteLine("Exception: {0}", e1.Message);
                    if (e1.InnerException != null)
                    {
                        Console.WriteLine("Inner exception: {0}", e1.InnerException.Message);
                    }
                    Console.WriteLine("Authentication failed - closing the connection.");
                    _sslServerConnection.Close();
                    _serverConnection.Close();

                    return;
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Couldnt connect to server: " + e);
            }
            new Thread(() =>
            {
                while (true)
                {
                    String dataString = "";
                    FM_Packet packet  = null;

                    if (_serverConnection.Connected)
                    {
                        dataString = (String)formatter.Deserialize(_sslServerConnection);
                        packet     = new JavaScriptSerializer().Deserialize <FM_Packet>(dataString);
                        switch (packet._type)
                        {
                        //sender = incoming client
                        //packet = data van de client
                        case "ID":
                            HandleIDPacket(packet);
                            break;

                        case "OpponentSubmit":
                            HandleOpponentSubmitPacket(packet);
                            break;

                        case "CodeResult":
                            HandleCodeResultPacket(packet);
                            break;

                        case "NameChange":
                            HandleNameChangePacket(packet);
                            break;

                        case "Connect":
                            HandleConnectPacket();
                            break;

                        case "Disconnect":
                            HandleDisconnectPacket(packet);
                            break;

                        case "Ready":
                            HandleReadyPacket();
                            break;

                        case "GameLost":
                            HandleGameLostPacket(packet);
                            break;

                        case "GameTie":
                            HandleGameTiePacket(packet);
                            break;

                        case "Highscores":
                            HandleHighscoresPacket(packet);
                            break;

                        default:     //nothing
                            break;
                        }
                    }
                }
            }).Start();
        }
Exemplo n.º 12
0
        private void HandleCodeSubmitPacket(FM_Packet packet)
        {
            int result     = 0;
            int OpponentID = CheckID(packet);
            //Console.WriteLine("code to check: " + packet._message + " - code to check against: " + _playerCodes[OpponentID]);
            string tempOpponentCode = _playerCodes[OpponentID];
            //Console.WriteLine("tempcode is: " + tempOpponentCode + "and playercode is: " + _playerCodes[OpponentID]);
            string        checkCode   = packet._message;
            StringBuilder sbOppCode   = new StringBuilder(tempOpponentCode);
            StringBuilder sbCheckCode = new StringBuilder(checkCode);

            for (int i = 0; i < tempOpponentCode.Length; i++)
            {
                if (tempOpponentCode.Substring(i, 1).Equals(checkCode.Substring(i, 1)))
                {
                    //Console.WriteLine("+10!");
                    sbOppCode[i]     = 'x';
                    tempOpponentCode = sbOppCode.ToString();
                    sbCheckCode[i]   = 'y';
                    checkCode        = sbCheckCode.ToString();
                    result          += 10;
                }
                //Console.WriteLine("Result has changed to: " + result);
                //Console.WriteLine("tempcodes are: " + tempOpponentCode + " and " + checkCode);
            }
            for (int i = 0; i < tempOpponentCode.Length; i++)
            {
                if (tempOpponentCode.Contains(checkCode.Substring(i, 1)))
                {
                    //Console.WriteLine("+1!");
                    int index = tempOpponentCode.IndexOf(checkCode.Substring(i, 1));
                    sbOppCode[index] = 'x';
                    tempOpponentCode = sbOppCode.ToString();
                    sbCheckCode[i]   = 'y';
                    checkCode        = sbCheckCode.ToString();
                    result          += 1;
                }
                //Console.WriteLine("Result has changed to: " + result);
                //Console.WriteLine("tempcodes are: " + tempOpponentCode + " and " + checkCode);
            }
            if (result < 10)
            {
                SendPacket(new FM_Packet(packet._id, "CodeResult", "0" + result));
                SendPacket(new FM_Packet(OpponentID + "", "OpponentSubmit", packet._message));
            }
            else
            {
                SendPacket(new FM_Packet(packet._id, "CodeResult", result + ""));
                SendPacket(new FM_Packet(OpponentID + "", "OpponentSubmit", packet._message));
            }

            if (result == 40)
            {
                _timeEnd = DateTime.Now;
                bool highscoresChanged = false;
                //Console.WriteLine("OMG Player " + packet._id + " HAS WON THE GAME!");
                SendPacket(new FM_Packet(OpponentID + "", "GameLost", "Your opponent guessed your code!\nSadly, you have lost."));
                _playerCodes = new string[MAX_PLAYERS];

                long totalTicksGameTime = ((_timeEnd.Ticks - _timeStart.Ticks) / TimeSpan.TicksPerMillisecond) - 3000;
                byte winID = Byte.Parse(packet._id);
                _wins[Byte.Parse(packet._id)]++;
                if (_wins[winID] > _mostWins)
                {
                    //Console.WriteLine("New record wins!");
                    _mostWins         = _wins[winID];
                    _mostWinsName     = _names[winID];
                    highscoresChanged = true;
                }
                for (int i = 0; i < _fastestTimes.Length; i++)
                {
                    //Console.WriteLine("i: " + i);
                    //Console.WriteLine("Comparing: " + totalTicksGameTime + " with " + _fastestTimes[i]);
                    if (totalTicksGameTime < _fastestTimes[i])
                    {
                        highscoresChanged = true;
                        //Console.WriteLine("New record time!");
                        for (int j = 9; j > i; j--)
                        {
                            //Console.WriteLine("replacing " + _fastestTimes[j] + " with " + _fastestTimes[j - 1]);
                            _fastestTimes[j] = _fastestTimes[j - 1];
                            //Console.WriteLine("replacing " + _fastestTimesNames[j] + " with " + _fastestTimesNames[j - 1]);
                            _fastestTimesNames[j] = _fastestTimesNames[j - 1];
                        }
                        //Console.WriteLine("replacing " + _fastestTimes[i] + " with " + totalTicksGameTime);
                        _fastestTimes[i]      = totalTicksGameTime;
                        _fastestTimesNames[i] = _names[Byte.Parse(packet._id)];
                        SaveTimes(_fastestTimes, _fastestTimesNames);
                        break;
                    }
                }
                if (highscoresChanged)
                {
                    SetHighscores();
                }
            }
        }
Exemplo n.º 13
0
 public void SendPacket(FM_Packet packet)
 {
     formatter.Serialize(streamArray[int.Parse(packet._id)], new JavaScriptSerializer().Serialize(packet));
 }
Exemplo n.º 14
0
 private void HandleIDPacket(FM_Packet packet)
 {
     _ID = packet._message;
     Console.WriteLine("ID REGISTERED: " + _ID);
 }
Exemplo n.º 15
0
 private void HandleOpponentSubmitPacket(FM_Packet packet)
 {
     _opponentCode = packet._message;
     _gui.SetOpponentTry(_opTry, packet._message);
     _opTry++;
 }
Exemplo n.º 16
0
 private void HandleNameChangePacket(FM_Packet packet)
 {
     _opponentName = packet._message;
     _gui.ChangeOpponentName(packet._message);
     //Set Label opponent gui.
 }
Exemplo n.º 17
0
 private void HandleDisconnectPacket(FM_Packet packet)
 {
     throw new NotImplementedException();
 }
Exemplo n.º 18
0
 public void SendPacket(FM_Packet packet, SslStream stream)
 {
     formatter.Serialize(stream, new JavaScriptSerializer().Serialize(packet));
 }
Exemplo n.º 19
0
 public void SendPacket(FM_Packet packet)
 {
     formatter.Serialize(_sslServerConnection, new JavaScriptSerializer().Serialize(packet));
 }
Exemplo n.º 20
0
        public FM_Server()
        {
            GetHighscores();
            TcpListener _server = null;

            try
            {
                X509Certificate2 _certificate = new X509Certificate2("C:\\certs\\testpfx.pfx", "FIETSA3");
                IPAddress        _localIP     = IPAddress.Parse("127.0.0.1");
                byte             port         = FM_Settings.SERVERPORT;
                _server = new TcpListener(/*_localIP,*/ port);
                _server.Start();
                Console.WriteLine("Waiting for connection...");
                while (true)
                {
                    TcpClient _clientConnection = _server.AcceptTcpClient();

                    new Thread(() =>
                    {
                        if (_playerCount < MAX_PLAYERS)
                        {
                            Console.WriteLine("Connection found!");
                            BinaryFormatter formatter = new BinaryFormatter();
                            SslStream sslStream       = new SslStream(_clientConnection.GetStream(), false);
                            try
                            {
                                Console.WriteLine("Authenticating connection...");
                                sslStream.AuthenticateAsServer(_certificate, false, SslProtocols.Tls, false);
                            }
                            catch (Exception e)
                            {
                                Console.WriteLine("Exception: {0}", e.Message);
                                if (e.InnerException != null)
                                {
                                    Console.WriteLine("Inner exception: {0}", e.InnerException.Message);
                                }
                                Console.WriteLine("Authentication failed - closing the connection.");
                                sslStream.Close();
                                _clientConnection.Close();
                                return;
                            }
                            streamArray[_playerCount] = sslStream;
                            SendPacket(new FM_Packet("ID", "" + _playerCount), sslStream);
                            _playerCount++;
                            Console.WriteLine("Authentication succesfull.");
                            while (true)
                            {
                                String dataString = "";
                                FM_Packet packet  = null;
                                if (_clientConnection.Connected)
                                {
                                    dataString = (String)formatter.Deserialize(sslStream);
                                    packet     = new JavaScriptSerializer().Deserialize <FM_Packet>(dataString);
                                    switch (packet._type)
                                    {
                                    //sender = incoming client
                                    //packet = data van de client
                                    case "Connect":
                                        HandleConnectPacket(packet);
                                        break;

                                    case "InitialCode":
                                        HandleInitialCodePacket(packet);
                                        break;

                                    case "CodeSubmit":
                                        HandleCodeSubmitPacket(packet);
                                        break;

                                    case "NameChange":
                                        HandleNameChangePacket(packet);
                                        break;

                                    case "Disconnect":
                                        HandleDisconnectPacket(packet);
                                        break;

                                    case "Highscores":
                                        HandleHighscoresPacket(packet);
                                        break;

                                    case "GameTie":
                                        HandleGameTiePacket(packet);
                                        break;

                                    default:     //nothing
                                        break;
                                    }
                                }
                            } // end While
                        }
                    }).Start();
                }
            }
            catch (SocketException e)
            {
                Console.WriteLine("SocketException: " + e);
            }
            finally
            {
                _server.Stop();
            }
        }
Exemplo n.º 21
0
 private void HandleHighscoresPacket(FM_Packet packet)
 {
     MessageBox.Show(packet._message);
 }