Interaction logic for MainGame.xaml
Inheritance: System.Windows.Controls.Page
Exemplo n.º 1
0
        public void StartGame()
        {
            Client client;

            client = new Client();
            string addr = LobbyModel.GameServerConnectionString;
            string[] args = addr.Split(':');
            Trace.Assert(args.Length == 2);
            client.IpString = args[0];
            client.PortNumber = int.Parse(args[1]);

            busyIndicator.BusyContent = Resources["Busy.JoinGame"];
            busyIndicator.IsBusy = true;

            //client.Start(isReplay, FileStream = file.open(...))
            BackgroundWorker worker = new BackgroundWorker();

            worker.DoWork += (o, ea) =>
            {
                try
                {
                    ea.Result = false;
                    client.Start(null, LobbyModel.LoginToken);
                    client.RecordStream = FileRotator.CreateFile("./Replays", "SGSREPLAY", ".sgs", 10);
                    ea.Result = true;
                }
                catch (Exception e)
                {
                    Trace.TraceError("Connection failure : " + e.StackTrace);
                    Trace.Assert(false);
                }
            };

            worker.RunWorkerCompleted += (o, ea) =>
            {
                busyIndicator.IsBusy = false;
                if ((bool)ea.Result)
                {
                    chatBox.Document.Blocks.Clear();
                    LobbyViewModel.Instance.OnChat -= chatEventHandler;
                    MainGame game = new MainGame();
                    game.NetworkClient = client;
                    this.NavigationService.Navigate(game);
                    return;
                }
                else
                {
                    MessageBox.Show("Failed to create connection for " + LobbyModel.GameServerConnectionString);
                    Trace.Assert(false);
                }
            };

            worker.RunWorkerAsync();
        }
Exemplo n.º 2
0
        private void _StartGame()
        {
            Application.Current.Dispatcher.Invoke((ThreadStart)delegate()
            {
                Client client;

                client = new Client();
                string addr = LobbyModel.GameServerConnectionString;
                string[] args = addr.Split(':');
                Trace.Assert(args.Length == 2);
                client.IpString = args[0];
                client.PortNumber = int.Parse(args[1]);

                busyIndicator.BusyContent = Resources["Busy.JoinGame"];
                busyIndicator.IsBusy = true;

                //client.Start(isReplay, FileStream = file.open(...))
                BackgroundWorker worker = new BackgroundWorker();

                worker.DoWork += (o, ea) =>
                {
                    try
                    {
                        ea.Result = false;
                        client.Start();
                        client.RecordStream = FileRotator.CreateFile("./Replays", "SGSREPLAY", ".sgs", 10);
                        ea.Result = true;
                    }
                    catch (Exception)
                    {
                    }
                };

                worker.RunWorkerCompleted += (o, ea) =>
                {
                    busyIndicator.IsBusy = false;
                    if ((bool)ea.Result)
                    {
                        MainGame game = new MainGame();
                        game.NetworkClient = client;
                        this.NavigationService.Navigate(game);
                        return;
                    }
                    else
                    {
                        MessageBox.Show("Failed to create connection for " + LobbyModel.GameServerConnectionString);
                    }
                };

                worker.RunWorkerAsync();
            });
        }
Exemplo n.º 3
0
        public void StartGame()
        {
            Client client;

            client = new Client();
            string addr = LobbyModel.GameServerConnectionString;

            string[] args = addr.Split(':');
            Trace.Assert(args.Length == 2);
            client.IpString   = args[0];
            client.PortNumber = int.Parse(args[1]);

            busyIndicator.BusyContent = Resources["Busy.JoinGame"];
            busyIndicator.IsBusy      = true;

            LobbyViewModel.Instance.OnChat -= LobbyModel_OnChat;
            chatBox.Document.Blocks.Clear();

            //client.Start(isReplay, FileStream = file.open(...))
            BackgroundWorker worker = new BackgroundWorker();

            worker.DoWork += (o, ea) =>
            {
                try
                {
                    ea.Result = false;
                    var stream = FileRotator.CreateFile("./Replays", "SGSREPLAY", ".sgs", 10);

                    stream.Write(BitConverter.GetBytes((int)0), 0, 4);
                    client.RecordStream = stream;
                    client.Start(stream, LobbyModel.LoginToken);

                    MainGame game = null;

                    Application.Current.Dispatcher.Invoke((ThreadStart) delegate()
                    {
                        try
                        {
                            game = new MainGame();
                            game.OnNavigateBack += (oo, s) =>
                            {
                                s.Navigate(this);
                            };
                            game.NetworkClient = client;
                            if (NavigationService != null)
                            {
                                MainGame.BackwardNavigationService = this.NavigationService;
                            }
                            else
                            {
                                ViewModelBase.IsDetached = true;
                            }
                        }
                        catch (Exception)
                        {
                            game = null;
                        }
                    });

                    if (game != null)
                    {
                        game.Start();
                        ea.Result = true;
                    }
                }
                catch (Exception e)
                {
                    Trace.TraceError("Connection failure : " + e.StackTrace);
                    Trace.Assert(false);
                }
            };

            worker.RunWorkerCompleted += (o, ea) =>
            {
                if ((bool)ea.Result)
                {
                    return;
                }
                else
                {
                    busyIndicator.IsBusy            = false;
                    LobbyViewModel.Instance.OnChat += LobbyModel_OnChat;
                    MessageBox.Show("Failed to create connection for " + LobbyModel.GameServerConnectionString);
                    Trace.Assert(false);
                }
            };

            worker.RunWorkerAsync();
        }
Exemplo n.º 4
0
        public void StartGame()
        {
            Client client;

            client = new Client();
            string addr = LobbyModel.GameServerConnectionString;
            string[] args = addr.Split(':');
            Trace.Assert(args.Length == 2);
            client.IpString = args[0];
            client.PortNumber = int.Parse(args[1]);

            busyIndicator.BusyContent = Resources["Busy.JoinGame"];
            busyIndicator.IsBusy = true;

            //client.Start(isReplay, FileStream = file.open(...))
            BackgroundWorker worker = new BackgroundWorker();

            worker.DoWork += (o, ea) =>
            {
                try
                {
                    ea.Result = false;
                    client.Start(null, LobbyModel.LoginToken);
                    var stream = FileRotator.CreateFile("./Replays", "SGSREPLAY", ".sgs", 10);
                    byte[] zeroBytes = { 0, 0, 0, 0 };
                    stream.Write(zeroBytes, 0, 4);
                    client.RecordStream = stream;

                    MainGame game = null;

                    Application.Current.Dispatcher.Invoke((ThreadStart)delegate()
                    {
                        try
                        {
                            game = new MainGame();
                            game.OnNavigateBack += (oo, s) =>
                            {
                                s.Navigate(this);
                                this.Reload();
                            };
                            game.NetworkClient = client;
                            if (NavigationService != null)
                            {
                                MainGame.BackwardNavigationService = this.NavigationService;
                            }
                            else
                            {
                                ViewModelBase.IsDetached = true;
                            }
                        }
                        catch (Exception)
                        {
                            game = null;
                        }
                    });

                    if (game != null)
                    {
                        game.Start();
                        ea.Result = true;
                    }
                }
                catch (Exception e)
                {
                    Trace.TraceError("Connection failure : " + e.StackTrace);
                    Trace.Assert(false);
                }
            };

            worker.RunWorkerCompleted += (o, ea) =>
            {
                if ((bool)ea.Result)
                {
                    chatBox.Document.Blocks.Clear();
                    LobbyViewModel.Instance.OnChat -= chatEventHandler;
                    return;
                }
                else
                {
                    busyIndicator.IsBusy = false;
                    MessageBox.Show("Failed to create connection for " + LobbyModel.GameServerConnectionString);
                    Trace.Assert(false);
                }
            };

            worker.RunWorkerAsync();
        }