| 1 |
using System; |
| 2 |
using System.Collections.Generic; |
| 3 |
using System.Linq; |
| 4 |
using System.Net.Sockets; |
| 5 |
using System.Text; |
| 6 |
using System.Threading.Tasks; |
| 7 |
|
| 8 |
namespace TELTest { |
| 9 |
public class BaseQuizContext { |
| 10 |
public BaseQuizContext(ConnectionHandler handler, NetworkStream stream, QuizContainer quiz) { |
| 11 |
Handler = handler; |
| 12 |
Stream = stream; |
| 13 |
Quiz = quiz; |
| 14 |
} |
| 15 |
public ConnectionHandler Handler { get; } |
| 16 |
public NetworkStream Stream { get; } |
| 17 |
public QuizContainer Quiz { get; } |
| 18 |
public async Task Handle() { |
| 19 |
int selection = 0; |
| 20 |
var selected = QuizGamemode.Registry[selection]; |
| 21 |
while (true) { |
| 22 |
selected = QuizGamemode.Registry[selection < QuizGamemode.Registry.Count? selection : 0]; |
| 23 |
await Handler.TryClear(Stream); |
| 24 |
await Handler.WriteASCII(Stream, Localization.UI["SELECTGAME"] + "\r\n"); |
| 25 |
StringBuilder sb = new StringBuilder(); |
| 26 |
for (int i = 0; i < QuizGamemode.Registry.Count + 1; i++) { |
| 27 |
var item = (QuizGamemode.Registry.Count < 1 || i >= QuizGamemode.Registry.Count) ? null : QuizGamemode.Registry[i < QuizGamemode.Registry.Count? i : 0]; |
| 28 |
if (i == selection) |
| 29 |
sb.Append(" -> "); |
| 30 |
else |
| 31 |
sb.Append(" "); |
| 32 |
sb.AppendLineRN(item == null? "Powrot..." : item.Name); |
| 33 |
} |
| 34 |
sb.AppendLineRN("\r\n"); |
| 35 |
sb.AppendLineRN(selection == QuizGamemode.Registry.Count? "Powrot do menu." : (selected.Name + ": " + selected.Description)); |
| 36 |
sb.Append("\r\n[W/S/ENTER] "); |
| 37 |
await Handler.WriteASCII(Stream, sb.ToString()); |
| 38 |
char c = char.ToUpper(await Handler.ReadChar(Stream, false)); |
| 39 |
if (c == '\n' || c == '\r') { |
| 40 |
Handler.Flush(Stream); |
| 41 |
break; |
| 42 |
} |
| 43 |
if (c == 'W') { |
| 44 |
if (selection == 0) { |
| 45 |
selection = QuizGamemode.Registry.Count; |
| 46 |
} else { |
| 47 |
selection--; |
| 48 |
} |
| 49 |
} |
| 50 |
if (c == 'S') { |
| 51 |
if (selection == QuizGamemode.Registry.Count) { |
| 52 |
selection = 0; |
| 53 |
} else { |
| 54 |
selection++; |
| 55 |
} |
| 56 |
} |
| 57 |
} // ENDOF While |
| 58 |
if (selection == QuizGamemode.Registry.Count) return; |
| 59 |
var sel = selected.CreateCheckInstance(Quiz, this); |
| 60 |
//Console.WriteLine(Quiz.SerializeXML()); |
| 61 |
await Handler.TryClear(Stream); |
| 62 |
await sel.Begin(); |
| 63 |
} |
| 64 |
} |
| 65 |
} |