예제 #1
0
        public Chat(Client cliente, Mapa _mapa)
        {
            InitializeComponent();
            reloj.Interval = new TimeSpan(0, 0, 1);
            reloj.Tick += reloj_Tick;
            reloj.Start();
            InputText.Focus();
            this.mapa = _mapa;
            CargarMapa();
            this.Cliente = cliente;

            this.Cliente.Boom += Cliente_Boom;

            this.Cliente.MoverGrilla += Cliente_MoverGrilla;

            this.Cliente.Accion += Cliente_Accion;

            this.Cliente.MensajeRecibido += (texto) =>
            {
                Dispatcher.BeginInvoke(new Action(() =>
                {
                    char[] splitchar = { '|' };
                    var mensaje = texto.Split(splitchar);
                    if (comandos.Contains(mensaje[2]))
                    {
                        comandoshistoricos.Enqueue(mensaje[2]);
                        ComandosHistoricosFront.Children.Add(new TextBlock { Text = mensaje[2] });
                        ScrollViewerComandos.ScrollToEnd();
                        while (comandoshistoricos.Count > 100)
                            comandoshistoricos.Dequeue();
                    }
                    if (emotes.Contains(mensaje[2]))
                    {
                        StackPanelMensajes.Children.Add(stringToImage(mensaje[2]));
                    }
                    else
                    {
                        StackPanelMensajes.Children.Add(new TextBlock
                        {
                            Text = mensaje[1] + " -> " + mensaje[2],
                            Foreground = new SolidColorBrush((Color)ColorConverter.ConvertFromString(mensaje[0])),
                            FontWeight = (comandos.Contains(mensaje[2]) ? FontWeights.Bold : FontWeights.Normal)
                        });
                    }
                    SrollViewer.ScrollToEnd();
                    actualizarSistemaDeGobierno();
                }));

            };

            InputText.KeyUp += (s, e) =>
            {
                if (e.Key == Key.Enter)
                {
                    EnviarMensaje();
                }
            };
        }
        private void Conectar()
        {
            Client cliente = null;
            Button button = ButtonConnect;
            String IP = TextBoxIP.Text;
            String UserName = TextBoxUserName.Text;
            String Port = puerto.Text;

            TextBoxIP.IsEnabled = button.IsEnabled = false;

            // Hacemos que la tarea de intentar conectarse se haga en un proceso por separado.
            // Esto para no bloquear el thread principal.
            // Una manera más correcta de hacer esto es utilizando Task:
            // http://msdn.microsoft.com/es-es/library/system.threading.tasks.task(v=vs.110).aspx
            //
            // Pero no nos compliquemos más, usemos solo un Thread:
            new Thread(() =>
            {
                // Tareas en backbround.
                cliente = new Client(UserName);
                bool exito = cliente.Conectar(IP, Port);

                // Una vez terminada las tareas volvemos al thread principal.
                Dispatcher.BeginInvoke(new Action(() =>
                {
                    if (exito)
                    {
                        Chat chat = new Chat(cliente, new Mapa("mapa"));
                        chat.Show();
                        this.Close();
                    }
                    else
                    {
                        MessageBox.Show("No se pudo conectar");
                        TextBoxIP.IsEnabled = button.IsEnabled = true;
                    }
                }));
            }).Start();
        }