ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/public/Qzhteln/trunk/TELTest/GUIUtilities.cs
Revision: 2
Committed: Wed Feb 11 13:07:01 2026 UTC (8 weeks, 4 days ago) by figdor32
File size: 9392 byte(s)
Log Message:
Initial check-in

File Contents

# User Rev Content
1 figdor32 2 using System;
2     using System.Collections.Generic;
3     using System.IO;
4     using System.Linq;
5     using System.Net.Sockets;
6     using System.Reflection.Emit;
7     using System.Runtime.InteropServices;
8     using System.Text;
9     using System.Threading.Tasks;
10     using TELTest.Matchmaking;
11    
12     namespace TELTest {
13     internal class GUIUtilities {
14     public static async Task<string> Promptfull(ConnectionHandler handler, NetworkStream stream, string prompt, string @default = null, int nMax = 32, bool noClear = false) {
15     if (!noClear)
16     await handler.TryClear(stream);
17    
18     await handler.WriteASCII(stream, "\r\n" + prompt + ": ");
19     var res = await handler.Read(stream, new ReadConfiguration() {
20     Echo = true,
21     MaxLength = nMax,
22     FlushAfterReading = true,
23     Terminator = '\r',
24     Whitelist = Utilities.ALPHANUMERIC + " ?,.{}[]()/\\:;'\"'\b"
25     });
26     return string.IsNullOrWhiteSpace(res) ? @default : res;
27     }
28     public static async Task AskUsernameIfNeeded(ConnectionHandler handler, NetworkStream stream, bool force = false) {
29     bool usrset = string.IsNullOrWhiteSpace(handler.PlayerName);
30     if (!usrset && !force) return;
31     while (true) {
32     await handler.TryClear(stream);
33     //await handler.WriteASCII(stream, "Musisz wybrac nick zanim rozpoczniesz gre multiplayer.\r\n\r\n");
34     await handler.WriteASCII(stream, "Nick: ");
35     string nam = await handler.Read(stream, new ReadConfiguration() {
36     Terminator = '\r',
37     Echo = true,
38     MaxLength = 32,
39     Whitelist = Utilities.ALPHANUMERIC,
40     FlushAfterReading = true,
41     });
42     handler.PlayerName = nam.Trim();
43     if (!string.IsNullOrWhiteSpace(nam)) break;
44     }
45     }
46     public static async Task<bool> LobbyAwait(ConnectionHandler handler, NetworkStream stream, Lobby lobby) {
47     while (true) {
48     string[][] menu = new string[1][];
49     List<string> players = new List<string>();
50     players.Add("Odswiez");
51     players.Add("Gotowe...");
52     players.Add("Wyjdz...");
53     foreach (var item in lobby.Players) {
54     players.Add(item.PlayerName + (item.PlayerReady? "*" : ""));
55     }
56     menu[0] = players.ToArray();
57     var res = await Create2DMenu(handler, stream, menu, new[] { "Identyfikator: [" + lobby.Identifier + "]" });
58     if (res.Item2 == 1) return true;
59     if (res.Item2 == 2) return false;
60     }
61     }
62     public static async Task LobbySettings(ConnectionHandler handler, NetworkStream stream, Lobby lobby) {
63     int lastsel = 7;
64     while (true) {
65     string[][] menu = new string[1][];
66     menu[0] = new string[] {
67     /* 0 */ "Tryb gry: " + lobby.Gamemode.Name,
68     /* 1 */ "Quiz: " + lobby.Subject.Title,
69     /* 2 */ "----------------",
70     /* 3 */ "Nazwa: " + lobby.Name,
71     /* 4 */ "Publiczne? " + (lobby.AnnouncePublic? "Tak" : "Nie"),
72     /* 5 */ "Haslo: " + (lobby.Password != null? new string('*', lobby.Password.Length) : "[Brak]"),
73     /* 6 */ "Max graczy: " + (lobby.PlayerLimit == -1? "Bez limitu" : lobby.PlayerLimit + ""),
74     /* 7 */ "Gotowe."
75     };
76     var res = await Create2DMenu(handler, stream, menu, new[] { "Identyfikator: [" + lobby.Identifier + "]" }, defaulty: lastsel);
77     var sel = res.Item2;
78     if (sel == 3) {
79     await handler.TryClear(stream);
80     await handler.WriteASCII(stream, "Nazwa: ");
81     string nam = await handler.Read(stream, new ReadConfiguration() {
82     Terminator = '\r',
83     Echo = true,
84     MaxLength = 32,
85     Whitelist = Utilities.ALPHANUMERIC + " ",
86     FlushAfterReading = true,
87     });
88     lobby.Name = nam;
89     }
90     if (sel == 4) lobby.AnnouncePublic = !lobby.AnnouncePublic;
91     if (sel == 5) {
92     await handler.TryClear(stream);
93     await handler.WriteASCII(stream, "Haslo: ");
94     string nam = await handler.Read(stream, new ReadConfiguration() {
95     Terminator = '\r',
96     MaxLength = 32,
97     Echo = true,
98     EchoPasswordChars = true,
99     PasswordChar= '*',
100     FlushAfterReading = true,
101    
102     });
103     nam = nam.Trim();
104     lobby.Password = string.IsNullOrEmpty(nam) ? null : nam;
105    
106     }
107     if (sel == 6) {
108     await handler.TryClear(stream);
109     await handler.WriteASCII(stream, "Max: ");
110     string nam = await handler.Read(stream, new ReadConfiguration() {
111     Terminator = '\r',
112     Echo = true,
113     MaxLength = 32,
114     Whitelist = "0123456789",
115     FlushAfterReading = true,
116     });
117     if (string.IsNullOrWhiteSpace(nam)) {
118     lobby.PlayerLimit = -1;
119     } else {
120     lobby.PlayerLimit = int.Parse(nam);
121     }
122     }
123     if (sel == 7) break;
124     lastsel = sel;
125     }
126     }
127     public static async Task<Tuple<int, int>> Create2DMenu(ConnectionHandler handler, NetworkStream stream, string[][] menu, string[] labels, string caption = null, int defaultx = 0, int defaulty = 0) {
128     int xsel = defaultx;
129     int ysel = defaulty;
130     while (true) {
131     await handler.TryClear(stream);
132     StringBuilder sb = new StringBuilder();
133     if (caption != null)
134     sb.AppendLineRN(caption);
135     //sb.AppendLineRN("Wybierz menu/widok/katalog klawiszami A/D.\r\n");
136     if (labels.Length > xsel) {
137     if (xsel != 0)
138     sb.Append(labels[xsel - 1] + " <-- ");
139    
140     sb.Append("[" + (labels[xsel] != null ? labels[xsel] : "Bez nazwy") + "]");
141    
142     if (xsel != menu.Length - 1)
143     sb.AppendLineRN(" --> " + labels[xsel + 1]);
144     else
145     sb.AppendLineRN();
146    
147     sb.AppendLineRN();
148     }
149     //for (int i = 0; i < menu.Length; i++) {
150     int off = 0;
151     if (ysel > 20) {
152     off = ysel / 20 * 20;
153     }
154     if (off != 0) sb.AppendLineRN(" [... + " + off + "]");
155     for (int j = off; j < menu[xsel].Length; j++) {
156     bool t = ysel == j;
157     string name = menu[xsel][j];
158     if (name != null && name.StartsWith("FILE:")) name = Path.GetFileName(name.Substring(5));
159     sb.AppendLineRN((t ? " -> " : " ") + name + " ");
160     if (j - off > 20) {
161     sb.AppendLineRN("[... +" + (menu[xsel].Length - (off + 20)) + "]");
162     break;
163     }
164     }
165     //sb.AppendLineRN();
166     //}
167     sb.Append("\r\n[W/A/S/D/ENTER] ");
168     await handler.WriteASCII(stream, sb.ToString());
169     char c = char.ToUpper(await handler.ReadChar(stream, false));
170     if (c == '\n' || c == '\r') {
171     handler.Flush(stream);
172     break;
173     }
174     if (c == 'W') {
175     if (ysel == 0) {
176     ysel = menu[xsel].Length - 1;
177     } else {
178     ysel--;
179     }
180     }
181     if (c == 'S') {
182     if (ysel == menu[xsel].Length - 1) {
183     ysel = 0;
184     } else {
185     ysel++;
186     }
187     }
188    
189     if (c == 'A') {
190     if (xsel == 0) {
191     xsel = menu.Length - 1;
192     } else {
193     xsel--;
194     }
195     }
196     if (c == 'D') {
197     if (xsel == menu.Length - 1) {
198     xsel = 0;
199     } else {
200     xsel++;
201     }
202     }
203     if (menu[xsel].Length - 1 < ysel) ysel = menu[xsel].Length - 1;
204     }
205     return new Tuple<int, int>(xsel, ysel);
206     }
207     }
208     }