-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathViews.cs
More file actions
848 lines (723 loc) · 31.3 KB
/
Views.cs
File metadata and controls
848 lines (723 loc) · 31.3 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Sockets;
using System.Security;
using System.Text;
using System.Threading.Tasks;
using ToSParser;
namespace ToSTextClient
{
public class ViewRegistry
{
public IExceptionView Exception { get; protected set; }
public IAuthView Auth { get; protected set; }
public IHomeView Home { get; protected set; }
public IView GameModes { get; protected set; }
public IInputView Settings { get; protected set; }
public IView Friends { get; protected set; }
public IView Notifications { get; protected set; }
public IView Party { get; protected set; }
public IGameView Game { get; protected set; }
public IView Players { get; protected set; }
public IView Roles { get; protected set; }
public IView Graveyard { get; protected set; }
public IView Team { get; protected set; }
public IWillView LastWill { get; protected set; }
public IView Winners { get; protected set; }
public EditableWillView MyLastWill { get; protected set; }
public EditableWillView MyDeathNote { get; protected set; }
public EditableWillView MyForgedWill { get; protected set; }
public HelpView Help { get; protected set; }
public ViewRegistry(ITextUI ui)
{
TextClient GetGame() => ui.Game;
ui.RegisterMainView(Exception = new ExceptionView(), "exception");
ui.RegisterMainView(Auth = new AuthView(), "auth", "authentication", "login");
ui.RegisterMainView(Home = new HomeView(CommandContext.HOME.Set(), 60, 2, GetGame), "home");
ui.RegisterSideView(GameModes = new ListView(" # Game Modes", () => ui.Game.ActiveGameModes.Select(gm => ui.Game.Resources.GetMetadata(gm)).Where(gm => gm.PermissionLevel <= ui.Game.PermissionLevel).Select(gm => (FormattedString)gm.Name), CommandContext.HOME.Set(), 25), "modes", "gamemodes", "game modes");
ui.RegisterSideView(Settings = new SettingsView(GetGame), "settings", "options");
ui.RegisterSideView(Friends = new ListView(" # Friends", () => ui.Game.Friends.Values.Select(f => f.ToDisplay()), CommandContext.HOME.Set(), 22), "friends", "friendlist", "friend list");
ui.RegisterSideView(Notifications = new ListView(" # Notifications", () => ui.Game.PendingFriendRequests.Keys.Select(fr => (FormattedString)string.Format("FR {0}", fr)).Concat(ui.Game.PendingPartyInvitations.Keys.Select(pi => (FormattedString)string.Format("PI {0}", pi))), CommandContext.HOME.Set(), 23), "notifications");
ui.RegisterSideView(Party = new PartyView(CommandContext.HOME.Set().And(c => ui.Game.Party != null), GetGame), "party");
ui.RegisterMainView(Game = new GameView(CommandExtensions.IsInLobbyOrGame, 60, 20, GetGame), "game");
ui.RegisterSideView(Players = new ListView(" # Players", () => ui.Game.GameState.Players.Select(ps => (FormattedString)(ps.Dead ? "" : ui.Game.GameState.ToName(ps.ID, true))), CommandExtensions.IsInLobbyOrGame, 25), "players", "playerlist", "player list");
ui.RegisterSideView(Roles = new ListView(" # Roles", () => ui.Game.GameState.Roles.Select(r => ui.Game.Resources.Of(r)), CommandExtensions.IsInLobbyOrGame, 25), "roles", "rolelist", "role list");
ui.RegisterSideView(Graveyard = new ListView(" # Graveyard", () => ui.Game.GameState.Graveyard.Select(ps => ui.Game.GameState.ToName(ps, true)), CommandExtensions.IsInGame, 40), "graveyard", "deaths");
ui.RegisterSideView(Team = new ListView(" # Team", () => ui.Game.GameState.Team.Select(ps => !ps.Dead || ps.Role == Role.DISGUISER ? ui.Game.GameState.ToName(ps, true) : ""), CommandExtensions.IsInGame, 40), "team", "teammates");
ui.RegisterSideView(LastWill = new WillView(CommandExtensions.IsInGame), "lw", "dn", "lastwill", "deathnote", "last will", "death note");
ui.RegisterSideView(Winners = new ListView(" # Winners", () => ui.Game.GameState.Winners.Select(p => (FormattedString)ui.Game.GameState.ToName(p, true)), CommandContext.GAME_END.Set(), 25), "winners", "winnerlist", "winner list");
ui.RegisterSideView(MyLastWill = new EditableWillView(CommandExtensions.IsInGame), "mlw", "mylastwill", "my lastwill", "my last will");
ui.RegisterSideView(MyDeathNote = new EditableWillView(context => CommandExtensions.IsInGame(context) && ui.Game.GameState.Role.HasDeathNote()), "mdn", "mydeathnote", "my deathnote", "my death note");
ui.RegisterSideView(MyForgedWill = new EditableWillView(context => CommandExtensions.IsInGame(context) && ui.Game.GameState.Role == Role.FORGER), "mfw", "myforgedwill", "my forgedwill", "my forged will");
ui.RegisterSideView(Help = new HelpView(ui.Commands, () => ui.CommandContext, 40, 1), "?", "h", "help");
}
}
public abstract class BaseView : IView
{
public virtual int MinimumWidth { get; protected set; }
public virtual int MinimumHeight { get; protected set; }
public virtual IPinnedView PinnedView { get; set; }
public event Action<string, Exception> OnException;
public event Action OnTextChange;
protected Func<CommandContext, bool> isAllowed;
protected BaseView(Func<CommandContext, bool> isAllowed, int minimumWidth, int minimumHeight, IPinnedView pinned)
{
this.isAllowed = isAllowed;
MinimumWidth = minimumWidth;
MinimumHeight = minimumHeight;
PinnedView = pinned;
}
public abstract IEnumerable<FormattedString> Lines(int width);
public virtual void Redraw() => OnTextChange?.Invoke();
public virtual bool IsAllowed(CommandContext activeContext) => isAllowed(activeContext);
protected void Handle(string msg, Exception ex) => OnException?.Invoke(msg, ex);
}
public abstract class BasePinnedView : BaseView, IPinnedView
{
protected BasePinnedView(Func<CommandContext, bool> isAllowed, int minimumWidth, int minimumHeight, IPinnedView pinned) : base(isAllowed, minimumWidth, minimumHeight, pinned) { }
public virtual IEnumerable<INamedTimer> Timers => Enumerable.Empty<INamedTimer>();
protected class BaseNamedTimer : INamedTimer
{
public virtual string Name { get; protected set; }
public virtual int Value { get => _Value; set { _Value = value; parent.Redraw(); } }
protected readonly IPinnedView parent;
protected int _Value;
private int timerIndex;
public BaseNamedTimer(IPinnedView parent, string name)
{
this.parent = parent;
Name = name;
}
public virtual void Set(int value)
{
Value = value;
Task.Run(UpdateTimer);
}
protected virtual async Task UpdateTimer()
{
for (int thisInc = ++timerIndex; timerIndex == thisInc && Value > 0; Value--) await Task.Delay(1000);
}
}
protected class BaseEditableNamedTimer : BaseNamedTimer, IEditableNamedTimer
{
public BaseEditableNamedTimer(IPinnedView parent, string name) : base(parent, name) { }
public void Set(string name, int value)
{
Name = name;
Set(value);
}
}
}
public abstract class BaseInputView : BaseView, IInputView
{
public virtual (int x, int y) Cursor => (0, 0);
public event Action OnCursorChange;
protected event Action<char> OnChar;
protected event Action OnBackspace;
protected event Action OnDelete;
protected event Action OnUpArrow;
protected event Action OnDownArrow;
protected event Action OnLeftArrow;
protected event Action OnRightArrow;
protected event Action OnHome;
protected event Action OnEnd;
protected event Action OnEnter;
protected BaseInputView(Func<CommandContext, bool> isAllowed, int minimumWidth, int minimumHeight, IPinnedView pinned) : base(isAllowed, minimumWidth, minimumHeight, pinned) { }
public virtual void Close() { }
public virtual void KeyPress(ConsoleKeyInfo key)
{
switch (key.Key)
{
default:
if (OnChar != null && !char.IsControl(key.KeyChar)) OnChar(key.KeyChar);
break;
case ConsoleKey.Backspace:
OnBackspace?.Invoke();
break;
case ConsoleKey.Delete:
OnDelete?.Invoke();
break;
case ConsoleKey.UpArrow:
OnUpArrow?.Invoke();
break;
case ConsoleKey.DownArrow:
OnDownArrow?.Invoke();
break;
case ConsoleKey.LeftArrow:
OnLeftArrow?.Invoke();
break;
case ConsoleKey.RightArrow:
OnRightArrow?.Invoke();
break;
case ConsoleKey.Home:
OnHome?.Invoke();
break;
case ConsoleKey.End:
OnEnd?.Invoke();
break;
case ConsoleKey.Enter:
OnEnter?.Invoke();
break;
}
}
protected void CursorChange() => OnCursorChange?.Invoke();
}
public class AuthView : BaseInputView, IAuthView
{
public FormattedString Status { set { _Status = value; Redraw(); } }
public override (int x, int y) Cursor => (selectedLine == 1 ? usernameCursor + 10 : selectedLine == 2 && password.Length == 0 ? 10 : 18, selectedLine);
public bool AllowGeneralInput => false;
public event Action<Socket, string, SecureString> OnAuthenticate;
private int selectedLine;
private Host selectedHost;
private StringBuilder username;
private int usernameCursor;
private SecureString password;
private int passwordCursor;
private FormattedString _Status;
public AuthView() : base(CommandContext.AUTHENTICATING.Set(), 30, 4, null)
{
selectedLine = 1;
username = new StringBuilder(20);
password = new SecureString();
OnChar += Insert;
OnBackspace += Backspace;
OnDelete += Delete;
OnUpArrow += UpArrow;
OnDownArrow += DownArrow;
OnLeftArrow += LeftArrow;
OnRightArrow += RightArrow;
OnHome += Home;
OnEnd += End;
OnEnter += Enter;
}
public override IEnumerable<FormattedString> Lines(int width)
{
yield return FormattedString.Concat(ToName(Host.Live), " / ", ToName(Host.PTR), " / ", ToName(Host.Local));
yield return FormattedString.Concat("Username: ", username.ToString());
yield return password.Length > 0 ? "Password: (hidden)" : "Password:";
if (_Status != null) yield return _Status;
}
public override void Close()
{
selectedLine = 1;
username.Clear();
usernameCursor = 0;
password.Clear();
passwordCursor = 0;
}
public void GeneralInput(string input) => throw new NotImplementedException();
private void Insert(char c)
{
if (selectedLine == 1 && username.Length < 20 && c != ' ') username.Insert(usernameCursor++, c);
else if (selectedLine == 2) password.InsertAt(passwordCursor++, c);
else return;
Redraw();
}
private void Backspace()
{
if (selectedLine == 1 && usernameCursor > 0) username.Remove(--usernameCursor, 1);
else if (selectedLine == 2 && passwordCursor > 0) password.RemoveAt(--passwordCursor);
else return;
Redraw();
}
private void Delete()
{
if (selectedLine == 1 && usernameCursor < username.Length) username.Remove(usernameCursor, 1);
else if (selectedLine == 2 && passwordCursor < password.Length) password.RemoveAt(passwordCursor);
else return;
Redraw();
}
private void UpArrow()
{
if (selectedLine <= 0) return;
selectedLine--;
CursorChange();
}
private void DownArrow()
{
if (selectedLine >= 2) return;
selectedLine++;
CursorChange();
}
private void LeftArrow()
{
if (selectedLine == 0 && selectedHost > 0)
{
selectedHost--;
Redraw();
return;
}
else if (selectedLine == 1 && usernameCursor > 0) usernameCursor--;
else if (selectedLine == 2 && passwordCursor > 0) passwordCursor--;
else return;
CursorChange();
}
private void RightArrow()
{
if (selectedLine == 0 && selectedHost < Host.Local)
{
selectedHost++;
Redraw();
return;
}
else if (selectedLine == 1 && usernameCursor < username.Length) usernameCursor++;
else if (selectedLine == 2 && passwordCursor < password.Length) passwordCursor++;
else return;
CursorChange();
}
private void Home()
{
if (selectedLine == 1) usernameCursor = 0;
else if (selectedLine == 2) passwordCursor = 0;
else return;
CursorChange();
}
private void End()
{
if (selectedLine == 1) usernameCursor = username.Length;
else if (selectedLine == 2) passwordCursor = password.Length;
else return;
CursorChange();
}
private void Enter()
{
if (selectedLine < 2) DownArrow();
else
{
try
{
Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
socket.Connect(GetHost(selectedHost), 3600);
OnAuthenticate?.Invoke(socket, username.ToString(), password);
Close();
}
catch (SocketException)
{
Status = ("Failed to connect to the server: check your internet connection", TextClient.RED);
Close();
}
catch (Exception e)
{
Handle("Failed to authenticate", e);
}
}
}
private FormattedString ToName(Host host) => (host.ToString(), host == selectedHost ? TextClient.GREEN : TextClient.WHITE, TextClient.BLACK);
private string GetHost(Host host)
{
switch (host)
{
case Host.Live:
return "live4.tos.blankmediagames.com";
case Host.PTR:
return "ptr.tos.blankmediagames.com";
case Host.Local:
return "localhost";
}
throw new ArgumentOutOfRangeException("unknown host value " + host);
}
private enum Host
{
Live, PTR, Local
}
}
public class TextView : BaseView, ITextView
{
public event Action<int, FormattedString> OnAppend;
public event Action<int, FormattedString> OnReplace;
private readonly IList<FormattedString> _Lines;
public TextView(Func<CommandContext, bool> isAllowed, int minimumWidth, int minimumHeight, IPinnedView pinned = null) : base(isAllowed, minimumWidth, minimumHeight, pinned) => _Lines = new List<FormattedString>();
public override IEnumerable<FormattedString> Lines(int width) => _Lines;
public void AppendLine(FormattedString text)
{
_Lines.Add(text);
OnAppend?.Invoke(_Lines.Count - 1, text);
}
public void AppendLine(FormattedString format, params object[] args) => AppendLine(FormattedString.Format(format, args));
public void ReplaceLine(int index, FormattedString text)
{
_Lines.SafeReplace(index, text);
OnReplace?.Invoke(index, text);
}
public void ReplaceLine(int index, FormattedString format, params object[] args) => ReplaceLine(index, FormattedString.Format(format, args));
public void Clear()
{
_Lines.Clear();
Redraw();
}
}
public class HomeView : TextView, IHomeView
{
public INamedTimer FWotDTimer { get; protected set; }
public INamedTimer QueueTimer { get; protected set; }
public bool AllowGeneralInput => getGame().Party != null;
private readonly Func<TextClient> getGame;
public HomeView(Func<CommandContext, bool> isAllowed, int minimumWidth, int minimumHeight, Func<TextClient> getGame) : base(isAllowed, minimumWidth, minimumHeight, null) => PinnedView = new UserInfoView(this, this.getGame = getGame);
public void GeneralInput(string input) => getGame().Parser.SendPartyMessage(input);
protected class UserInfoView : BasePinnedView
{
public override IEnumerable<INamedTimer> Timers
{
get
{
yield return parent.FWotDTimer;
yield return parent.QueueTimer;
}
}
private readonly HomeView parent;
private readonly Func<TextClient> getGame;
public UserInfoView(HomeView parent, Func<TextClient> getGame) : base(CommandContext.HOME.Set(), 20, 2, null)
{
this.parent = parent;
this.getGame = getGame;
parent.FWotDTimer = new BaseNamedTimer(this, "FWotD Bonus");
parent.QueueTimer = new BaseNamedTimer(this, "Queue");
}
public override IEnumerable<FormattedString> Lines(int width)
{
TextClient game = getGame();
if (game.Username == null) yield break;
yield return game.Username;
yield return string.Format("{0} TP / {1} MP", game.TownPoints, game.MeritPoints);
}
}
}
public class GameView : TextView, IGameView
{
private readonly Func<TextClient> getGame;
public IEditableNamedTimer PhaseTimer { get; protected set; }
public bool AllowGeneralInput => true;
public GameView(Func<CommandContext, bool> isAllowed, int minimumWidth, int minimumHeight, Func<TextClient> getGame) : base(isAllowed, minimumWidth, minimumHeight, null) => PinnedView = new NameView(this, this.getGame = getGame);
public void GeneralInput(string input) => getGame().Parser.SendChatBoxMessage(input);
protected class NameView : BasePinnedView
{
public override IEnumerable<INamedTimer> Timers
{
get
{
yield return parent.PhaseTimer;
}
}
private readonly GameView parent;
private readonly Func<TextClient> getGame;
public NameView(GameView parent, Func<TextClient> getGame) : base(CommandExtensions.IsInLobbyOrGame, 23, 2, null)
{
this.parent = parent;
this.getGame = getGame;
parent.PhaseTimer = new BaseEditableNamedTimer(this, null);
}
public override IEnumerable<FormattedString> Lines(int width)
{
GameState state = getGame().GameState;
if (state.Self == null) yield break;
yield return state.ToName(state.Self, true);
}
}
}
public class ListView : BaseView
{
private readonly FormattedString title;
private readonly Func<IEnumerable<FormattedString>> _Lines;
public ListView(FormattedString title, Func<IEnumerable<FormattedString>> lines, Func<CommandContext, bool> isAllowed, int minimumWidth) : base(isAllowed, minimumWidth, 1, null)
{
this.title = title;
_Lines = lines;
}
public override IEnumerable<FormattedString> Lines(int width) => _Lines().Prepend(title);
}
public class PartyView : BaseView
{
private readonly Func<TextClient> getGame;
public PartyView(Func<CommandContext, bool> isAllowed, Func<TextClient> getGame) : base(isAllowed, 30, 4, null) => this.getGame = getGame;
public override IEnumerable<FormattedString> Lines(int width)
{
TextClient game = getGame();
PartyState party = game.Party;
if (party == null) yield break;
yield return " # Party";
yield return party.Brand.ToString().ToDisplayName();
yield return game.Resources.GetMetadata(party.SelectedMode).Name;
yield return "";
yield return " # Members";
foreach (PartyMember member in party.Members.Values) yield return member.ToDisplay();
if (party.Invitations.Count == 0) yield break;
yield return "";
yield return " # Invitations";
foreach (PartyInvitation invitation in party.Invitations.Values) yield return invitation.ToDisplay();
}
}
public class WillView : BaseView, IWillView
{
internal const int WILL_WIDTH = 40;
internal const int WILL_HEIGHT = 18;
public string Title { get => _Title; set { _Title = value; Redraw(); } }
public string Value { get => _Value; set { _Value = value; Redraw(); } }
private string _Title;
private string _Value;
public WillView(Func<CommandContext, bool> isAllowed) : base(isAllowed, WILL_WIDTH, WILL_HEIGHT + 1, null) => _Title = _Value = "";
public override IEnumerable<FormattedString> Lines(int width) => Value.Split('\r').SelectMany(s => s.Wrap(width).Select(s2 => (FormattedString)s2)).Prepend(Title);
}
public class EditableWillView : BaseInputView, IEditableWillView
{
public override (int x, int y) Cursor => (cursorX, cursorY);
public string Title { get => _Title; set { _Title = value; Redraw(); } }
public event Action<string> OnSave;
private readonly StringBuilder value;
private int cursorIndex;
private string _Title;
private int cursorX;
private int cursorY;
private int lastWidth;
public EditableWillView(Func<CommandContext, bool> isAllowed) : base(isAllowed, WillView.WILL_WIDTH, WillView.WILL_HEIGHT, null)
{
value = new StringBuilder();
OnChar += Insert;
OnBackspace += Backspace;
OnDelete += Delete;
OnUpArrow += UpArrow;
OnDownArrow += DownArrow;
OnLeftArrow += LeftArrow;
OnRightArrow += RightArrow;
OnHome += Home;
OnEnd += End;
OnEnter += Enter;
}
public override IEnumerable<FormattedString> Lines(int width)
{
lastWidth = width;
return value.ToString().Split('\r').SelectMany(s => s.Wrap(width).Select(s2 => (FormattedString)s2)).Prepend(Title);
}
public override void Close() => OnSave?.Invoke(value.ToString());
private void Insert(char c)
{
value.Insert(cursorIndex++, c);
Redraw();
}
private void Backspace()
{
if (cursorIndex <= 0) return;
value.Remove(--cursorIndex, 1);
Redraw();
}
private void Delete()
{
value.Remove(cursorIndex, 1);
Redraw();
}
private void UpArrow()
{
if (cursorY == 0) return;
cursorY--;
int willIndex = 0;
int lineIndex = 0;
foreach (string line in value.ToString().Split('\r'))
{
foreach (string segment in line.Wrap(lastWidth))
{
if (++lineIndex > cursorY)
{
lineIndex = Math.Min(line.Length, cursorX);
cursorIndex = willIndex + lineIndex;
return;
}
willIndex += segment.Length;
}
willIndex++;
}
CursorChange();
}
private void DownArrow()
{
cursorY++;
int willIndex = 0;
int lineIndex = 0;
foreach (string line in value.ToString().Split('\r'))
{
foreach (string segment in line.Wrap(lastWidth))
{
if (++lineIndex > cursorY)
{
lineIndex = Math.Min(line.Length, cursorX);
cursorIndex = willIndex + lineIndex;
return;
}
willIndex += segment.Length;
}
willIndex++;
}
cursorY--;
CursorChange();
}
private void LeftArrow()
{
if (cursorIndex <= 0) return;
cursorIndex--;
CursorChange();
}
private void RightArrow()
{
if (cursorIndex >= value.Length) return;
cursorIndex++;
CursorChange();
}
private void Home()
{
cursorX = 0;
int willIndex = 0;
int lineIndex = 0;
foreach (string line in value.ToString().Split('\r'))
{
int startLine = lineIndex;
foreach (string segment in line.Wrap(lastWidth))
{
if (++lineIndex > cursorY)
{
cursorIndex = willIndex;
return;
}
}
willIndex += line.Length;
willIndex++;
}
CursorChange();
}
private void End()
{
int willIndex = 0;
int lineIndex = 0;
foreach (string line in value.ToString().Split('\r'))
{
bool found = false;
foreach (string segment in line.Wrap(lastWidth))
{
if (++lineIndex > cursorY)
{
cursorX = segment.Length;
found = true;
}
}
willIndex += line.Length;
if (found)
{
cursorIndex = willIndex;
cursorY = lineIndex - 1;
return;
}
willIndex++;
}
CursorChange();
}
private void Enter() => Insert('\r');
}
public class HelpView : BaseView, IHelpView
{
public (IDocumented cmd, string[] names)? Topic { get => _Topic; set { _Topic = value; OnShowHelp?.Invoke(); } }
public event Action OnShowHelp;
protected readonly IReadOnlyDictionary<string, Command> commands;
protected Func<CommandContext> getContext;
protected (IDocumented cmd, string[] names)? _Topic;
public HelpView(IReadOnlyDictionary<string, Command> commands, Func<CommandContext> getContext, int minimumWidth, int minimumHeight) : base(context => true, minimumWidth, minimumHeight, null)
{
this.commands = commands;
this.getContext = getContext;
}
public override IEnumerable<FormattedString> Lines(int width)
{
CommandContext context = getContext();
if (Topic == null) return commands.GroupBy(c => c.Value).Select(c => c.First()).Where(c => c.Value.IsAllowed(context)).SelectMany(FormatCommand).Prepend(" # Help");
else return Topic.Value.cmd.Documentation.Prepend(string.Format(" # Help ({0})", string.Join(", ", Topic.Value.names)), Topic.Value.cmd.Description);
}
private IEnumerable<FormattedString> FormatCommand(KeyValuePair<string, Command> command, int width)
{
yield return string.Format(" /{0} {1}", command.Key, command.Value.UsageLine);
yield return command.Value.Description;
}
}
public class SettingsView : BaseInputView
{
public override (int x, int y) Cursor => (GetLength(), 2 * selected + 2);
private readonly Func<TextClient> getGame;
private int selected;
public SettingsView(Func<TextClient> getGame) : base(CommandContext.HOME.Set(), 20, 5, null)
{
this.getGame = getGame;
OnUpArrow += UpArrow;
OnDownArrow += DownArrow;
OnLeftArrow += LeftArrow;
OnRightArrow += RightArrow;
}
public override IEnumerable<FormattedString> Lines(int width)
{
TextClient game = getGame();
yield return " # Settings";
yield return " Share Skin";
yield return game.ShareSkin ? "Yes" : "No";
yield return " Queue Language";
yield return game.QueueLanguage.ToString().ToDisplayName();
}
private void UpArrow()
{
if (selected > 0) selected--;
CursorChange();
}
private void DownArrow()
{
if (selected < 1) selected++;
CursorChange();
}
private void LeftArrow()
{
TextClient game = getGame();
switch (selected)
{
case 0:
game.ShareSkin = !game.ShareSkin;
break;
case 1:
if (game.QueueLanguage > Language.UNSELECTED) game.QueueLanguage--;
break;
default:
return;
}
Redraw();
}
private void RightArrow()
{
TextClient game = getGame();
switch (selected)
{
case 0:
game.ShareSkin = !game.ShareSkin;
break;
case 1:
if (game.QueueLanguage < Language.SPANISH) game.QueueLanguage++;
break;
default:
return;
}
Redraw();
}
private int GetLength()
{
switch (selected)
{
case 0:
return getGame().ShareSkin ? 3 : 2;
case 1:
return getGame().QueueLanguage.ToString().Length;
}
return 0;
}
}
public class ExceptionView : BaseView, IExceptionView
{
public Exception Exception { get => _Exception; set { _Exception = value; Redraw(); } }
public bool AllowGeneralInput => false;
private Exception _Exception;
public ExceptionView() : base(context => true, 60, 10, null) { }
public override IEnumerable<FormattedString> Lines(int width) => Exception?.ToString()?.Split('\r').Select(s => (FormattedString)s) ?? Enumerable.Empty<FormattedString>().Append("False");
public void GeneralInput(string input) => throw new NotImplementedException();
}
}