about summary refs log tree commit diff stats
path: root/AnodyneArchipelago/Menu/ConnectionState.cs
blob: 877213ff3c10e129ef8a451247c01de93db3482e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
using AnodyneSharp.Input;
using AnodyneSharp.Resources;
using AnodyneSharp.Sounds;
using AnodyneSharp.States;
using AnodyneSharp.UI;
using AnodyneSharp.UI.Font;
using AnodyneSharp.UI.Text;
using Archipelago.MultiClient.Net;
using Archipelago.MultiClient.Net.Enums;
using Microsoft.Xna.Framework;
using System.Reflection;
using System.Threading.Tasks;

namespace AnodyneArchipelago.Menu
{
    internal class ConnectionState : State
    {
        public delegate void SuccessEvent(ArchipelagoManager archipelagoManager);

        private readonly SuccessEvent _successFunc;

        private Task<LoginResult> _connectionTask;
        private ArchipelagoManager _archipelago = new();

        private TextWriter _textWriter;
        private UIEntity _bgBox;
        private readonly SpriteFont _font;

        private string _text = "Connecting...";

        public ConnectionState(string apServer, string apSlot, string apPassword, SuccessEvent successFunc)
        {
            _successFunc = successFunc;

            _connectionTask = Task.Run(() => _archipelago.Connect(apServer, apSlot, apPassword));

            _font = FontManager.InitFont(new Color(226, 226, 226), true);

            _textWriter = new(20, 44, 128, 100)
            {
                drawLayer = AnodyneSharp.Drawing.DrawOrder.TEXT
            };
            _textWriter.SetSpriteFont(_font, ResourceManager.GetTexture("consoleButtons"));

            _bgBox = new UIEntity(new Vector2(16f, 40f), "pop_menu", 16, 16, AnodyneSharp.Drawing.DrawOrder.TEXTBOX);
            UpdateDisplay();
        }

        public override void Update()
        {
            if (_connectionTask != null && _connectionTask.IsCompleted)
            {
                LoginResult result = _connectionTask.Result;

                if (result.Successful)
                {
                    Exit = true;
                    _successFunc(_archipelago);
                    return;
                }
                else
                {
                    LoginFailure failure = result as LoginFailure;
                    string errorMessage = "";
                    foreach (string error in failure.Errors)
                    {
                        errorMessage += error;
                        errorMessage += "\n";
                    }
                    foreach (ConnectionRefusedError error in failure.ErrorCodes)
                    {
                        errorMessage += error.ToString();
                        errorMessage += "\n";
                    }

                    if (errorMessage.Length > 0)
                    {
                        errorMessage = errorMessage.Substring(0, errorMessage.Length - 1);
                    }
                    else
                    {
                        errorMessage = "Unknown error during connection.";
                    }

                    _text = errorMessage;
                    _connectionTask = null;

                    UpdateDisplay();
                }
            }

            if (KeyInput.JustPressedRebindableKey(KeyFunctions.Accept) || KeyInput.JustPressedRebindableKey(KeyFunctions.Cancel))
            {
                Exit = true;
                SoundManager.PlaySoundEffect("menu_select");
            }
        }

        public override void DrawUI()
        {
            _bgBox.Draw();
            _textWriter.Draw();
        }

        private void UpdateDisplay()
        {
            _textWriter.Text = _text;
            _textWriter.ProgressTextToEnd();

            FieldInfo linesField = typeof(TextWriter).GetField("_line", BindingFlags.NonPublic | BindingFlags.Instance);
            int lineValue = (int)linesField.GetValue(_textWriter);

            int innerHeight = (lineValue + 1) * _font.lineSeparation;

            _bgBox = new UIEntity(new Vector2(16f, 40f), "pop_menu", 136, innerHeight + 8, AnodyneSharp.Drawing.DrawOrder.TEXTBOX);
        }
    }
}