-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIDOCDetector.cs
247 lines (201 loc) · 6.53 KB
/
IDOCDetector.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
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
/* Dan and Dexter of the Heritage Shard (https://trueuo.com) */
using Server.Multis;
using Server.Network;
using System;
using System.Collections.Generic;
using Server.Regions;
namespace Server.Items
{
public class IDOCDetector : Item
{
private const int _Red = 0x26;
private const int _Yellow = 0x35;
private const int _Green = 0x55B;
public override bool ForceShowProperties => true;
private static Timer Timer { get; set; }
private static List<IDOCDetector> Detectors { get; set; }
[Constructable]
public IDOCDetector()
: base(8928)
{
Name = "Housing Assessor";
Movable = false;
Weight = 0;
if (Detectors == null)
{
Detectors = new List<IDOCDetector>();
}
Detectors.Add(this);
if (Timer == null)
{
Timer = Timer.DelayCall(TimeSpan.FromMinutes(5), TimeSpan.FromMinutes(5), OnTick);
Timer.Start();
}
}
public override void OnDoubleClick(Mobile m)
{
if (Cooldown == null || !Cooldown.ContainsKey(m) || Cooldown[m] < DateTime.UtcNow)
{
if (m.InRange(Location, 3))
{
var count = GetHouseCount();
var iCount = GetIDOCCount();
var gCount = GetGreatlyCount();
PrivateOverheadMessage(MessageType.Regular, 453, false, $"There are currently {count} placed houses with {iCount} in danger of collapsing and {gCount} that are greatly worn.", m.NetState);
if (Cooldown == null)
{
Cooldown = new Dictionary<Mobile, DateTime>();
}
Cooldown[m] = DateTime.UtcNow + TimeSpan.FromSeconds(10);
Defrag();
}
else
{
m.SendLocalizedMessage(500446); // That is too far away.
}
}
else
{
m.SendMessage("You must wait a moment before using this again.");
}
}
private static Dictionary<Mobile, DateTime> Cooldown { get; set; }
private static void Defrag()
{
if (Cooldown == null)
{
return;
}
var remove = new List<Mobile>();
foreach (var kvp in Cooldown)
{
if (kvp.Value < DateTime.UtcNow)
{
remove.Add(kvp.Key);
}
}
for (var index = 0; index < remove.Count; index++)
{
var m = remove[index];
Cooldown.Remove(m);
}
ColUtility.Free(remove);
}
private static int GetHouseCount()
{
int count = 0;
for (var index = 0; index < BaseHouse.AllHouses.Count; index++)
{
var h = BaseHouse.AllHouses[index];
if (h.Map != null && h.Map != Map.Internal && !h.Region.IsPartOf<GreenAcres>())
{
count++;
}
}
return count;
}
private static int GetIDOCCount()
{
int count = 0;
for (var index = 0; index < BaseHouse.AllHouses.Count; index++)
{
var h = BaseHouse.AllHouses[index];
if (h.DecayLevel == DecayLevel.IDOC && h.Map != null && h.Map != Map.Internal && !h.Region.IsPartOf<GreenAcres>())
{
count++;
}
}
return count;
}
private static int GetGreatlyCount()
{
int count = 0;
for (var index = 0; index < BaseHouse.AllHouses.Count; index++)
{
var h = BaseHouse.AllHouses[index];
if (h.DecayLevel == DecayLevel.Greatly && h.Map != null && h.Map != Map.Internal && !h.Region.IsPartOf<GreenAcres>())
{
count++;
}
}
return count;
}
public override void GetProperties(ObjectPropertyList list)
{
base.GetProperties(list);
list.Add("Double Click: Assesses the state of house decay");
}
public override void Delete()
{
base.Delete();
if (Detectors != null)
{
Detectors.Remove(this);
if (Detectors.Count == 0)
{
if (Timer != null)
{
Timer.Stop();
Timer = null;
}
Detectors = null;
}
}
}
public IDOCDetector(Serial serial)
: base(serial)
{
}
public override void Serialize(GenericWriter writer)
{
base.Serialize(writer);
writer.Write(0);
Defrag();
}
public override void Deserialize(GenericReader reader)
{
base.Deserialize(reader);
reader.ReadInt();
if (Detectors == null)
{
Detectors = new List<IDOCDetector>();
}
Detectors.Add(this);
if (Timer == null)
{
Timer = Timer.DelayCall(TimeSpan.FromMinutes(5), TimeSpan.FromMinutes(5), OnTick);
Timer.Start();
}
}
private static void OnTick()
{
if (Detectors == null)
{
if (Timer != null)
{
Timer.Stop();
Timer = null;
}
}
else
{
for (var index = 0; index < Detectors.Count; index++)
{
var d = Detectors[index];
if (GetIDOCCount() > 0 && d.Hue != _Red)
{
d.Hue = _Red;
}
else if (GetGreatlyCount() > 0 && GetIDOCCount() == 0 && d.Hue != _Yellow)
{
d.Hue = _Yellow;
}
else if (GetGreatlyCount() == 0 && GetIDOCCount() == 0 && d.Hue != _Green)
{
d.Hue = _Green;
}
}
}
}
}
}