Beispiel #1
0
        public void InitializeConnection()
        {
            if (connection == null)
            {
                connection = new HubConnectionBuilder().WithUrl($"http://{ textBox4.Text }:5000/gamehub").Build();   //Set up the hub connection
            }

            // RECEIVING MESSAGES
            //Receive another player's login message
            connection.On <string>("ReceiveLoginMessage", (username) =>
            {
                richTextBox1.AppendText("\n" + username + " has logged in", Color.Green);
            });

            //Receive another player's sent message
            connection.On <string, string>("ReceiveMessage", (username, message) =>
            {
                richTextBox1.AppendText("\n" + username + ": " + message, Color.Black);
            });

            //Game has started info of players sent
            connection.On <string, string, int, string, int>("SendData", (jsonPlayers, jsonMap, playerHealth, scoreboard, roundEnded) =>
            {
                game.players = JsonConvert.DeserializeObject <List <Player> >(jsonPlayers, settings);
                game.map     = JsonConvert.DeserializeObject <Map>(jsonMap, settings);

                switch (roundEnded)
                {
                case 0:
                    PrintScoreboardRound(new ScoreboardTemplateProxy(JsonConvert.DeserializeObject <ScoreboardRound>(scoreboard, settings)));
                    break;

                case 1:
                    PrintScoreboardMatch(new ScoreboardTemplateProxy(JsonConvert.DeserializeObject <ScoreboardMatch>(scoreboard, settings)));
                    break;

                default:
                    throw new NotImplementedException();
                }

                if (game.gameStarted == false)
                {
                    game.gameStarted = true;

                    //Disable things
                    button3.Enabled             = false;
                    button3.Visible             = false;
                    pictureBox1.BackgroundImage = null;

                    //Enable things
                    button2.Enabled  = true;
                    textBox2.Enabled = true;

                    textBox3.Focus();

                    label5.Visible = true;

                    healthBox.Visible   = true;
                    healthLabel.Visible = true;

                    speedBox.Visible   = true;
                    speedLabel.Visible = true;

                    bombCountBox.Visible   = true;
                    bombCountLabel.Visible = true;

                    mineCountBox.Visible   = true;
                    mineCountLabel.Visible = true;

                    superBombCountBox.Visible   = true;
                    superBombCountLabel.Visible = true;

                    superMineCountBox.Visible   = true;
                    superMineCountLabel.Visible = true;

                    bombRadiusBox.Visible   = true;
                    bombRadiusLabel.Visible = true;

                    superBombRadiusBox.Visible   = true;
                    superBombRadiusLabel.Visible = true;

                    timeBox.Visible   = true;
                    timeLabel.Visible = true;

                    game.drawBackground();
                }

                Player player = null;

                //Get player
                foreach (var p in game.players)
                {
                    if (p.id == connection.ConnectionId)
                    {
                        player = p;
                    }
                }

                //Change player stats on the right side
                healthLabel.Text         = player.health.ToString();
                speedLabel.Text          = player.speed.ToString();
                bombCountLabel.Text      = player.bombCount.ToString();
                mineCountLabel.Text      = player.mineCount.ToString();
                superBombCountLabel.Text = player.superBombCount.ToString();
                superMineCountLabel.Text = player.superMineCount.ToString();
                bombRadiusLabel.Text     = player.explosionPower.ToString();

                double timeLeft = (player.undoTimer - game.map.serverTime) / 1000;
                int timeLeftInt = (int)timeLeft;
                if (timeLeftInt < 0.0)
                {
                    timeLabel.Text = "0";
                }
                else
                {
                    timeLabel.Text = timeLeftInt.ToString();
                }

                int expPow = player.explosionPower;
                if (expPow > 4)
                {
                    expPow = 4;
                }
                else if (expPow < 2)
                {
                    expPow = 2;
                }
                superBombRadiusLabel.Text = expPow.ToString();

                game.drawMap();
                pictureBox1.Image = game.GetField().GetImage();
            });

            //Update player images after changing their appearance
            connection.On <string>("UpdatePlayerImages", (jsonPlayers) =>
            {
                game.players = JsonConvert.DeserializeObject <List <Player> >(jsonPlayers, settings);
                game.FormPlayerImages();
            });

            connection.On <string>("SuccessfulLogin", (message) =>
            {
                richTextBox1.AppendText("\nConnected to the server", Color.Green);
                button1.Enabled  = false;
                button1.Visible  = false;
                textBox1.Enabled = false;
                textBox1.Visible = false;
                button3.Enabled  = true;
                button3.Visible  = true;
                textBox4.Enabled = false;
                textBox4.Visible = false;
            });
        }