Esempio n. 1
0
        private void BtnCreateNewGame_Click(object sender, EventArgs e)
        {
            try
            {
                string name = txtName.Text;

                var factory = new ConnectionFactory()
                {
                    HostName = "localhost"
                };
                using (var connection = factory.CreateConnection())
                    using (var channel = connection.CreateModel())
                    {
                        channel.ExchangeDeclare(exchange: "createGameResponse", type: "direct");



                        var queueName = channel.QueueDeclare().QueueName;
                        channel.QueueBind(queue: queueName,
                                          exchange: "createGameResponse",
                                          routingKey: queueName);

                        Console.WriteLine(" [*] Waiting for logs.");

                        var  consumer = new EventingBasicConsumer(channel);
                        bool primio   = false;
                        var  messageR = "";
                        consumer.Received += (model, ea) =>
                        {
                            var body1    = ea.Body;
                            var message1 = Encoding.UTF8.GetString(body1);
                            messageR = message1;
                            primio   = true;
                        };

                        channel.BasicConsume(queue: queueName,
                                             autoAck: true,
                                             consumer: consumer);
                        var message = "gamename:" + name + ": maxplayercount:" + dUpDown.Text + ": player:" + this.context.player.username + ": routingKey:" + queueName;
                        var body    = Encoding.UTF8.GetBytes(message);
                        channel.BasicPublish(exchange: "createGameCall",
                                             routingKey: "createGame",
                                             basicProperties: null,
                                             body: body);
                        while (!primio)
                        {
                            ;
                        }
                        if (messageR.Length == 36)
                        {
                            MessageBox.Show(messageR.ToString());
                        }
                        else
                        {
                            var s1 = JsonConvert.DeserializeObject <Game>(messageR);
                            this.context.games.Add(s1);
                            this.Hide();
                            GameForm l = new GameForm(s1, this.context);
                            l.ShowDialog();
                            this.Close();
                        }
                    };
            }
            catch (Exception ec)
            {
                MessageBox.Show(ec.Message);
            }
        }
Esempio n. 2
0
        private void BtnJoin_Click(object sender, EventArgs e)
        {
            try
            {
                string username = lblUsername.Text;
                string name     = listView1.SelectedItems[0].Text;
                var    factory  = new ConnectionFactory()
                {
                    HostName = "localhost"
                };
                using (var connection = factory.CreateConnection())
                    using (var channel = connection.CreateModel())
                    {
                        channel.ExchangeDeclare(exchange: "joinGameResponse", type: "direct");

                        var queueName = channel.QueueDeclare().QueueName;

                        channel.QueueBind(queue: queueName,
                                          exchange: "joinGameResponse",
                                          routingKey: queueName);

                        Console.WriteLine(" [*] Waiting for logs.");

                        var  consumer = new EventingBasicConsumer(channel);
                        bool primio   = false;
                        var  messageR = "";
                        consumer.Received += (model, ea) =>
                        {
                            var body1    = ea.Body;
                            var message1 = Encoding.UTF8.GetString(body1);
                            messageR = message1;
                            primio   = true;
                        };

                        channel.BasicConsume(queue: queueName,
                                             autoAck: true,
                                             consumer: consumer);
                        var message = "username:"******": gamename:" + name + ": routingKey:" + queueName;
                        var body    = Encoding.UTF8.GetBytes(message);
                        channel.BasicPublish(exchange: "joinGameCall",
                                             routingKey: "joinGame",
                                             basicProperties: null,
                                             body: body);
                        while (!primio)
                        {
                            ;
                        }
                        if (messageR.Length < 30)
                        {
                            MessageBox.Show(messageR.ToString());
                        }
                        else
                        {
                            var s1 = JsonConvert.DeserializeObject <Game>(messageR);

                            if (s1.currentPlayerCount == s1.maxPlayerCount)
                            {
                                this.listView1.Items.Clear();
                                foreach (Game game in context.games)
                                {
                                    if (!game.name.Equals(s1.name))
                                    {
                                        ListViewItem item = new ListViewItem(game.name);
                                        item.SubItems.Add(game.currentPlayerCount.ToString());
                                        item.SubItems.Add(game.maxPlayerCount.ToString());
                                        this.listView1.Items.Add(item);
                                    }
                                }
                                this.listView1.Refresh();
                            }

                            this.Hide();
                            GameForm l = new GameForm(s1, context);
                            l.ShowDialog();
                            this.Close();
                        }
                    }
            }
            catch (Exception ec)
            {
                ec.Source = "No game selected!";
                MessageBox.Show(ec.Source.ToString());
            }
        }