/
Face.cs
159 lines (139 loc) · 4.73 KB
/
Face.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace StateOfMindTest
{
public class Face
{
IScreenRenderer _renderer;
IGetInput _input;
public static readonly string AllQuestionsAsked = "I've asked all my questions.";
public static readonly string NoOneKnown = "Uh, never mind";
private IndexIncrement _incrementer;
public Face(IScreenRenderer renderer, IGetInput input)
{
_renderer = renderer;
_input = input;
}
public Interaction Talk(string text, int millisecondTimeout = 5000)
{
Interaction result = new Interaction();
_renderer.Clear();
_renderer.Write(text);
result.displayText = text;
Thread.Sleep(millisecondTimeout);
return result;
}
public Interaction RememberSingleValue(string text, bool longTerm = false, int millisecondTimeout = 5000)
{
Interaction result = new Interaction();
_renderer.Clear();
_renderer.Write(text);
result.displayText = text;
Task task = Task.Run(() =>
{
int intResult;
if (_input.TryGetInteger(out intResult))
{
result.resultValue = intResult;
}
});
task.Wait(millisecondTimeout);
if(result.resultValue != -1)
Memory.GetInstance().AddToMemory(result, longTerm);
return result;
}
public Interaction GetSingleValue(string text, int millisecondTimeout = 5000)
{
Interaction result = new Interaction();
_renderer.Clear();
_renderer.Write(text);
result.displayText = text;
Task task = Task.Run(() =>
{
int intResult;
if (_input.TryGetInteger(out intResult))
{
result.resultValue = intResult;
}
});
task.Wait(millisecondTimeout);
return result;
}
public Interaction YesNo(string text, bool longTerm = false, int millisecondTimeout = 10000)
{
Interaction result = new Interaction();
_renderer.Clear();
_renderer.Write(text);
result.displayText = text;
Task task = Task.Run(() =>
{
int intResult;
if (_input.TryGetInteger(out intResult))
{
result.resultValue = intResult;
}
});
task.Wait(millisecondTimeout);
if (result.resultValue != -1)
Memory.GetInstance().AddToMemory(result, longTerm);
return result;
}
public Interaction Remembrance(string textTemplate, bool longTerm = true, int millisecondTimeout = 5000, params string[] memoryKeys)
{
List<object> rememberedValues = new List<object>();
foreach (string s in memoryKeys)
{
Interaction i = Memory.GetInstance().Remember(s, longTerm);
if(null != i)
rememberedValues.Add(i.resultValue);
}
Interaction result = new Interaction();
result.displayText = string.Format(textTemplate, rememberedValues.ToArray());
_renderer.Clear();
_renderer.Write(result.displayText);
Thread.Sleep(millisecondTimeout);
return result;
}
public void ResetIncrementer()
{
_incrementer = null;
}
public Interaction TalkInCircles(int millisecondTimeout = 5000, params string[] textLines)
{
if (null == _incrementer) _incrementer = new IndexIncrement(textLines.Length, true);
string text = textLines[_incrementer.Next];
Interaction result = new Interaction();
_renderer.Clear();
_renderer.Write(text);
result.displayText = text;
Thread.Sleep(millisecondTimeout);
return result; ;
}
}
public class IndexIncrement
{
private int _current;
private int _max;
private bool _loop;
public IndexIncrement(int max, bool loop)
{
_current = -1;
_max = max;
_loop = loop;
}
public int Next
{
get
{
_current++;
if (_loop && _current >= _max)
_current = 0;
return _current;
}
}
}
}