Beispiel #1
0
        public NetContext(ContextManager manager) : base(manager, "Networking", "Common")
        {
            // Just close when anything is selected and "submitted"
            RegisterSelectListeners((s, i, v) => controller.CloseView(s), "EmptyFieldError", "IPError", "PortError", "ConnectionError");

            bool connecting = false;


            GetView <InputView>("NetConnect").OnBackEvent         = _ => Show("quit");
            GetView <InputView>("NetConnect").SubmissionsListener = i =>
            {
                if (connecting)
                {
                    controller.Popup("Already connecting!", 1000, ConsoleColor.DarkRed);
                    return;
                }
                bool
                    ip   = ParseIP(i.Inputs[0].Text) != null,
                    port = short.TryParse(i.Inputs[1].Text, out short prt) && prt > 0;

                if (ip && port)
                {
                    connecting = true;
                    // Connect to server here
                    BankNetInteractor ita = new BankNetInteractor(i.Inputs[0].Text, prt);

                    Promise verify;
                    try
                    {
                        verify = Promise.AwaitPromise(ita.CheckIdentity(new RSA(Resources.e_0x100, Resources.n_0x100), provider.NextUShort()));
                    }
                    catch
                    {
                        Show("ConnectionError");
                        connecting = false;
                        return;
                    }
                    verify.Subscribe =
                        p =>
                    {
                        void load() => manager.LoadContext(new WelcomeContext(manager, ita));

                        // Add condition check for remote peer verification
                        if (bool.Parse(p.Value))
                        {
                            controller.Popup(GetIntlString("NC_verified"), 1000, ConsoleColor.Green, load);
                        }
                        else
                        {
                            controller.Popup(GetIntlString("verror"), 5000, ConsoleColor.Red, load);
                        }
                    };
                    DialogView identityNotify = GetView <DialogView>("IdentityVerify");
                    identityNotify.RegisterSelectListener(
                        (vw, ix, nm) => {
                        Hide(identityNotify);
                        connecting       = false;
                        verify.Subscribe = null; // Clear subscription
#pragma warning disable CS4014                   // Because this call is not awaited, execution of the current method continues before the call is completed
                        ita.CancelAll();
#pragma warning restore CS4014                   // Because this call is not awaited, execution of the current method continues before the call is completed
                        connecting = false;
                    });
                    Show(identityNotify);
                }
                else if (i.Inputs[0].Text.Length == 0 || i.Inputs[1].Text.Length == 0)
                {
                    Show("EmptyFieldError");
                }
                else if (!ip)
                {
                    Show("IPError");
                }
                else
                {
                    Show("PortError");
                }
            };
        }
Beispiel #2
0
        public WelcomeContext(ContextManager manager, BankNetInteractor connection) : base(manager, "Setup", "Common")
        {
            this.interactor = connection;

            // Prepare events and stuff

            // Just close when anything is selected and "submitted"
            RegisterSelectListeners((s, i, v) => controller.CloseView(s), "DuplicateAccountError", "EmptyFieldError", "IPError", "PortError", "AuthError", "PasswordMismatchError");

            // If Escape key is pressed, suggest to controller to terminate
            GetView("WelcomeScreen").OnBackEvent = v => Show("quit");

            GetView <InputView>("Login").SubmissionsListener = i =>
            {
                bool success = true;

                foreach (var input in i.Inputs)
                {
                    if (input.Text.Length == 0)
                    {
                        success = false;
                        input.SelectBackgroundColor = ConsoleColor.Red;
                        input.BackgroundColor       = ConsoleColor.DarkRed;
                    }
                }


                if (success)
                {
                    // Authenticate against server here
                    Show("AuthWait");
                    try
                    {
                        promise = Promise.AwaitPromise(interactor.Authenticate(i.Inputs[0].Text, i.Inputs[1].Text));
                    }
                    catch
                    {
                        Hide("AuthWait");
                        Show("ConnectionError");
                        return;
                    }
                    //promise = prom.Result;
                    promise.Subscribe =
                        response =>
                    {
                        Hide("AuthWait");
                        if (response.Value.StartsWith("ERROR") || response.Value.Equals("False")) // Auth failure or general error
                        {
                            Show("AuthError");
                        }
                        else
                        {
                            forceDestroy = false;
                            manager.LoadContext(new SessionContext(manager, interactor));
                        }
                    };
                }
                else
                {
                    Show("EmptyFieldError");
                }
            };

            // For a smooth effect
            GetView <InputView>("Login").InputListener = (v, c, i, t) =>
            {
                c.BackgroundColor       = v.DefaultBackgroundColor;
                c.SelectBackgroundColor = v.DefaultSelectBackgroundColor;
                return(true);
            };

            GetView <InputView>("Register").SubmissionsListener = i =>
            {
                bool success = true, mismatch = false;

                foreach (var input in i.Inputs)
                {
                    if (input.Text.Length == 0)
                    {
                        success = false;
                        input.SelectBackgroundColor = ConsoleColor.Red;
                        input.BackgroundColor       = ConsoleColor.DarkRed;
                    }
                }

                mismatch = !i.Inputs[1].Text.Equals(i.Inputs[2].Text);
                if (success && !mismatch)
                {
                    void a()
                    {
                        Show("RegWait");
                        try
                        {
                            promise = Promise.AwaitPromise(interactor.Register(i.Inputs[0].Text, i.Inputs[1].Text));
                        }
                        catch
                        {
                            Hide("RegWait");
                            Show("ConnectionError");
                            return;
                        }
                        promise.Subscribe =
                            response =>
                        {
                            Hide("RegWait");
                            if (!bool.Parse(response.Value))
                            {
                                Show("DuplicateAccountError");
                            }
                            else
                            {
                                forceDestroy = false;
                                manager.LoadContext(new SessionContext(manager, interactor));
                            }
                        };
                    }

                    if (i.Inputs[1].Text.Length < 5 || i.Inputs[1].Text.StartsWith("asdfasdf") || i.Inputs[1].Text.StartsWith("asdf1234"))
                    {
                        var warning = GetView <DialogView>("WeakPasswordWarning");
                        warning.RegisterSelectListener((wrn, idx, sel) =>
                        {
                            Hide(warning);
                            if (idx == 0)
                            {
                                a();
                            }
                        });
                        Show(warning);
                    }
                    else
                    {
                        a();
                    }
                }
                else if (mismatch)
                {
                    Show("PasswordMismatchError");
                }
                else
                {
                    Show("EmptyFieldError");
                }
            };

            GetView <InputView>("Register").InputListener = (v, c, i, t) =>
            {
                c.BackgroundColor       = v.DefaultBackgroundColor;
                c.SelectBackgroundColor = v.DefaultSelectBackgroundColor;
                return(true);
            };
        }