-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathFirewall.cs
171 lines (155 loc) · 5.87 KB
/
Firewall.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
160
161
162
163
164
165
166
167
168
169
170
171
using System.Collections.Generic;
using System.Globalization;
using System.Text;
using System.Xml;
namespace Hacknet
{
public class Firewall
{
private const int MIN_SOLUTION_LENGTH = 6;
private const int OUTPUT_LINE_WIDTH = 20;
private const int CHARS_SOLVED_PER_PASS = 3;
private const string SOLVED_CHAR = "0";
private readonly float additionalDelay;
private int analysisPasses;
private readonly int complexity = 1;
private string solution;
private readonly int solutionLength = 6;
public bool solved;
public Firewall()
{
generateRandomSolution();
}
public Firewall(int complexity)
{
this.complexity = complexity;
solutionLength = 6 + complexity;
generateRandomSolution();
}
public Firewall(int complexity, string solution)
{
this.complexity = complexity;
this.solution = solution;
solutionLength = solution.Length;
}
public Firewall(int complexity, string solution, float additionalTime)
{
this.complexity = complexity;
this.solution = solution;
additionalDelay = additionalTime;
solutionLength = solution.Length;
}
private void generateRandomSolution()
{
var stringBuilder = new StringBuilder(solutionLength);
for (var index = 0; index < solutionLength; ++index)
stringBuilder.Append(Utils.getRandomChar());
solution = stringBuilder.ToString().ToUpperInvariant();
}
public static Firewall load(XmlReader reader)
{
while (reader.Name != "firewall")
reader.Read();
var complexity = 0;
string solution = null;
var additionalTime = 0.0f;
if (reader.MoveToAttribute("complexity"))
complexity = reader.ReadContentAsInt();
if (reader.MoveToAttribute("solution"))
solution = reader.ReadContentAsString();
if (reader.MoveToAttribute("additionalDelay"))
additionalTime = reader.ReadContentAsFloat();
return new Firewall(complexity, solution, additionalTime);
}
public string getSaveString()
{
return "<firewall complexity=\"" + complexity + "\" solution=\"" + solution + "\" additionalDelay=\"" +
additionalDelay.ToString(CultureInfo.InvariantCulture) + "\" />";
}
public void resetSolutionProgress()
{
analysisPasses = 0;
}
public bool attemptSolve(string attempt, object os)
{
if (attempt.Length != solution.Length)
{
var str = attempt.Length < solution.Length ? "Too few charachters" : "Too many charachters";
((OS) os).write("Solution Incorrect Length - " + str);
}
else if (attempt.ToLower().Equals(solution.ToLower()))
{
solved = true;
return true;
}
return false;
}
public void writeAnalyzePass(object os_object, object target_object)
{
var target = (Computer) target_object;
var os = (OS) os_object;
if (target.firewallAnalysisInProgress)
{
os.write("-Analysis already in Progress-");
}
else
{
os.delayer.PostAnimation(generateOutputPass(analysisPasses, os, target));
++analysisPasses;
}
}
private IEnumerator<ActionDelayer.Condition> generateOutputPass(int pass, OS os, Computer target)
{
target.firewallAnalysisInProgress = true;
os.write("Firewall Analysis Pass " + analysisPasses + "\n");
yield return ActionDelayer.Wait(0.03);
os.write("--------------------");
yield return ActionDelayer.Wait(0.03);
var preceedeString = " ";
var secondsDelayPerLine = 0.08 + 0.06*pass + additionalDelay;
for (var i = 0; i < solutionLength; ++i)
{
os.write(preceedeString + generateOutputLine(i));
yield return ActionDelayer.Wait(secondsDelayPerLine);
}
os.write("--------------------\n");
target.firewallAnalysisInProgress = false;
}
private string generateOutputLine(int location)
{
var stringBuilder = new StringBuilder();
for (var index = 0; index < 20; ++index)
stringBuilder.Append("0");
var num = 20 - 3*analysisPasses;
for (var index = 0; index < num; ++index)
stringBuilder[index] = string.Concat(Utils.getRandomChar()).ToLower()[0];
var index1 = Utils.random.Next(stringBuilder.Length);
if (location < solution.Length)
stringBuilder[index1] = solution[location];
var index2 = 0;
while (index2 < stringBuilder.Length)
{
stringBuilder.Insert(index2, " ");
index2 += 2;
}
return stringBuilder.ToString();
}
public override bool Equals(object obj)
{
var firewall = obj as Firewall;
if (firewall != null && firewall.additionalDelay == (double) additionalDelay &&
firewall.complexity == complexity)
return firewall.solution == solution;
return false;
}
public override int GetHashCode()
{
return base.GetHashCode();
}
public override string ToString()
{
return "Firewall: solution\"" + solution + "\" - time:" + additionalDelay + " - complexity:" +
complexity;
}
}
}