private async Task ConnectToRemoteServer(ServerInfo serverInfo)
        {
            var delayedConnectingDialog = new DelayedWaitingPrompt();
            var connectingDialogTask    = ShowConnectingDialogDelayed(serverInfo, delayedConnectingDialog);
            Task <StartGameMessage> gameStartSignal;

            try
            {
                client          = InitializeClient();
                gameStartSignal = client.EventRouter.GetAwaitable <StartGameMessage>();
                await client.Connect(serverInfo).AsAwaitable();

                delayedConnectingDialog.PromptIsStillNeeded = false;
                await connectingDialogTask;
                await client.SendRequest(new UserInfoMessage()
                {
                    DisplayName = Environment.UserName
                }).AsAwaitable();
            }
            catch (Exception ex)
            {
                QueueAction(() => Dialog.ShowMessage("Failed to connect".ToRed()).Then(Cleanup));
                return;
            }
            finally
            {
                await delayedConnectingDialog.Cleanup();
            }

            var delayedWaitingForOtherPlayersDialogSignal = new DelayedWaitingPrompt();
            var waitingForOtherPlayersDialogTask          = ShowWaitingMessage("Waiting for other players...".ToConsoleString(), delayedWaitingForOtherPlayersDialogSignal);

            // If we got here then we are connected. Now we wait for the game to start
            var gameStartInfo = await gameStartSignal;

            delayedWaitingForOtherPlayersDialogSignal.PromptIsStillNeeded = false;
            await waitingForOtherPlayersDialogTask;
            await delayedWaitingForOtherPlayersDialogSignal.Cleanup();

            await RevealPlayers(gameStartInfo);

            QueueAction(() => ScenePanel.IsVisible = true);
            Sound.Play("Reload");
            var bgMusicHandle = await Sound.Play("BackgroundMusic").AsAwaitable();

            gameLifetime.OnDisposed(bgMusicHandle.Dispose);
            // wait for the game to end
            var gameOverMessage = await client.EventRouter.GetAwaitable <GameOverMessage>();

            ShowWinnerAndCleanup(gameOverMessage);
        }
Exemplo n.º 2
0
        private async Task ShowWaitingMessage(ConsoleString message, DelayedWaitingPrompt signal)
        {
            await Task.Delay(signal.Delay);

            if (signal.PromptIsStillNeeded == false)
            {
                return;
            }

            await QueueAction(() =>
            {
                signal.Dialog = new Dialog(new Label()
                {
                    Text = message
                });
                signal.Dialog.MaxHeight = 4;
                signal.Dialog.Show();
            }).AsAwaitable();
        }
Exemplo n.º 3
0
        private async Task ShowConnectingDialogDelayed(ServerInfo info, DelayedWaitingPrompt signal)
        {
            await Task.Delay(signal.Delay);

            if (signal.PromptIsStillNeeded == false)
            {
                return;
            }

            await QueueAction(() =>
            {
                signal.Dialog = new Dialog(new Label()
                {
                    Text = $"Connecting to {info.Server}:{info.Port}".ToCyan()
                });
                signal.Dialog.MaxHeight = 4;
                signal.Dialog.Show();
            }).AsAwaitable();
        }
        private async Task ShowWaitingMessage(ConsoleString message, DelayedWaitingPrompt signal)
        {
            await Task.Delay(signal.Delay);

            if (signal.PromptIsStillNeeded == false)
            {
                return;
            }

            await QueueAction(() =>
            {
                Dialog.Show(new ControlDialogOptions()
                {
                    Content = new Label()
                    {
                        Text = message
                    },
                    MaxHeight = 4
                });
                signal.Dialog = Dialog.Current;
            }).AsAwaitable();
        }