corrade-vassal – Diff between revs 14 and 16
?pathlinks?
Rev 14 | Rev 16 | |||
---|---|---|---|---|
Line 12... | Line 12... | |||
12 | using System.Threading.Tasks; |
12 | using System.Threading.Tasks; |
|
13 | using System.Xml.Serialization; |
13 | using System.Xml.Serialization; |
|
Line 14... | Line 14... | |||
14 | |
14 | |
|
15 | namespace wasSharp |
15 | namespace wasSharp |
|
16 | { |
16 | { |
|
17 | public class Time |
17 | public static class Time |
|
18 | { |
18 | { |
|
Line 19... | Line 19... | |||
19 | public delegate void TimerCallback(object state); |
19 | public delegate void TimerCallback(object state); |
|
20 | |
20 | |
|
Line 129... | Line 129... | |||
129 | /// Given a number of allowed events per seconds, this class allows you |
129 | /// Given a number of allowed events per seconds, this class allows you |
|
130 | /// to determine via the IsSafe property whether it is safe to trigger |
130 | /// to determine via the IsSafe property whether it is safe to trigger |
|
131 | /// another lined-up event. This is mostly used to check that throttles |
131 | /// another lined-up event. This is mostly used to check that throttles |
|
132 | /// are being respected. |
132 | /// are being respected. |
|
133 | /// </summary> |
133 | /// </summary> |
|
134 | public class TimedThrottle : IDisposable |
134 | public sealed class TimedThrottle : IDisposable |
|
135 | { |
135 | { |
|
136 | private readonly uint EventsAllowed; |
136 | private readonly uint EventsAllowed; |
|
137 | private readonly object LockObject = new object(); |
137 | private readonly object LockObject = new object(); |
|
138 | private Timer timer; |
138 | private Timer timer; |
|
139 | private uint TriggeredEvents; |
139 | private uint TriggeredEvents; |
|
Line 168... | Line 168... | |||
168 | { |
168 | { |
|
169 | Dispose(true); |
169 | Dispose(true); |
|
170 | GC.SuppressFinalize(this); |
170 | GC.SuppressFinalize(this); |
|
171 | } |
171 | } |
|
Line -... | Line 172... | |||
- | 172 | |
||
- | 173 | ~TimedThrottle() |
||
- | 174 | { |
||
- | 175 | Dispose(false); |
||
- | 176 | } |
||
172 | |
177 | |
|
173 | protected virtual void Dispose(bool dispose) |
178 | private void Dispose(bool dispose) |
|
174 | { |
179 | { |
|
175 | if (timer != null) |
180 | if (timer != null) |
|
176 | { |
181 | { |
|
177 | timer.Dispose(); |
182 | timer.Dispose(); |
|
Line 188... | Line 193... | |||
188 | /// of a decaying timer that tracks the time between rescheduling. |
193 | /// of a decaying timer that tracks the time between rescheduling. |
|
189 | /// </summary> |
194 | /// </summary> |
|
190 | /// <remarks> |
195 | /// <remarks> |
|
191 | /// (C) Wizardry and Steamworks 2013 - License: GNU GPLv3 |
196 | /// (C) Wizardry and Steamworks 2013 - License: GNU GPLv3 |
|
192 | /// </remarks> |
197 | /// </remarks> |
|
193 | public class DecayingAlarm : IDisposable |
198 | public sealed class DecayingAlarm : IDisposable |
|
194 | { |
199 | { |
|
195 | [Flags] |
200 | [Flags] |
|
196 | public enum DECAY_TYPE |
201 | public enum DECAY_TYPE |
|
197 | { |
202 | { |
|
198 | [XmlEnum(Name = "none")] NONE = 0, |
203 | [Reflection.NameAttribute("none")] [XmlEnum(Name = "none")] NONE = 0, |
|
199 | [XmlEnum(Name = "arithmetic")] ARITHMETIC = 1, |
204 | [Reflection.NameAttribute("arithmetic")] [XmlEnum(Name = "arithmetic")] ARITHMETIC = 1, |
|
200 | [XmlEnum(Name = "geometric")] GEOMETRIC = 2, |
205 | [Reflection.NameAttribute("geometric")] [XmlEnum(Name = "geometric")] GEOMETRIC = 2, |
|
201 | [XmlEnum(Name = "harmonic")] HARMONIC = 4, |
206 | [Reflection.NameAttribute("harmonic")] [XmlEnum(Name = "harmonic")] HARMONIC = 4, |
|
202 | [XmlEnum(Name = "weighted")] WEIGHTED = 5 |
207 | [Reflection.NameAttribute("weighted")] [XmlEnum(Name = "weighted")] WEIGHTED = 5 |
|
203 | } |
208 | } |
|
Line 204... | Line 209... | |||
204 | |
209 | |
|
205 | private readonly DECAY_TYPE decay = DECAY_TYPE.NONE; |
210 | private readonly DECAY_TYPE decay = DECAY_TYPE.NONE; |
|
206 | private readonly Stopwatch elapsed = new Stopwatch(); |
211 | private readonly Stopwatch elapsed = new Stopwatch(); |
|
Line 232... | Line 237... | |||
232 | { |
237 | { |
|
233 | Dispose(true); |
238 | Dispose(true); |
|
234 | GC.SuppressFinalize(this); |
239 | GC.SuppressFinalize(this); |
|
235 | } |
240 | } |
|
Line -... | Line 241... | |||
- | 241 | |
||
- | 242 | ~DecayingAlarm() |
||
- | 243 | { |
||
- | 244 | Dispose(false); |
||
- | 245 | } |
||
236 | |
246 | |
|
237 | public void Alarm(double deadline) |
247 | public void Alarm(double deadline) |
|
238 | { |
248 | { |
|
239 | lock (LockObject) |
249 | lock (LockObject) |
|
240 | { |
250 | { |
|
Line 262... | Line 272... | |||
262 | case DECAY_TYPE.ARITHMETIC: |
272 | case DECAY_TYPE.ARITHMETIC: |
|
263 | alarm?.Change( |
273 | alarm?.Change( |
|
264 | (int) ((deadline + times.Aggregate((a, b) => b + a))/(1f + times.Count)), 0); |
274 | (int) ((deadline + times.Aggregate((a, b) => b + a))/(1f + times.Count)), 0); |
|
265 | break; |
275 | break; |
|
266 | case DECAY_TYPE.GEOMETRIC: |
276 | case DECAY_TYPE.GEOMETRIC: |
|
267 | alarm?.Change((int) (Math.Pow(deadline*times.Aggregate((a, b) => b*a), |
277 | alarm?.Change((int) Math.Pow(deadline*times.Aggregate((a, b) => b*a), |
|
268 | 1f/(1f + times.Count))), 0); |
278 | 1f/(1f + times.Count)), 0); |
|
269 | break; |
279 | break; |
|
270 | case DECAY_TYPE.HARMONIC: |
280 | case DECAY_TYPE.HARMONIC: |
|
271 | alarm?.Change((int) ((1f + times.Count)/ |
281 | alarm?.Change((int) ((1f + times.Count)/ |
|
272 | (1f/deadline + times.Aggregate((a, b) => 1f/b + 1f/a))), 0); |
282 | (1f/deadline + times.Aggregate((a, b) => 1f/b + 1f/a))), 0); |
|
273 | break; |
283 | break; |
|
274 | case DECAY_TYPE.WEIGHTED: |
284 | case DECAY_TYPE.WEIGHTED: |
|
275 | HashSet<double> d = new HashSet<double>(times) {deadline}; |
285 | var d = new HashSet<double>(times) {deadline}; |
|
276 | double total = d.Aggregate((a, b) => b + a); |
286 | var total = d.Aggregate((a, b) => b + a); |
|
277 | alarm?.Change( |
287 | alarm?.Change( |
|
278 | (int) (d.Aggregate((a, b) => Math.Pow(a, 2)/total + Math.Pow(b, 2)/total)), 0); |
288 | (int) d.Aggregate((a, b) => Math.Pow(a, 2)/total + Math.Pow(b, 2)/total), 0); |
|
279 | break; |
289 | break; |
|
280 | default: |
290 | default: |
|
281 | alarm?.Change((int) deadline, 0); |
291 | alarm?.Change((int) deadline, 0); |
|
282 | break; |
292 | break; |
|
283 | } |
293 | } |
|
Line 286... | Line 296... | |||
286 | break; |
296 | break; |
|
287 | } |
297 | } |
|
288 | } |
298 | } |
|
289 | } |
299 | } |
|
Line 290... | Line 300... | |||
290 | |
300 | |
|
291 | protected virtual void Dispose(bool dispose) |
301 | private void Dispose(bool dispose) |
|
292 | { |
302 | { |
|
293 | if (alarm != null) |
303 | if (alarm != null) |
|
294 | { |
304 | { |
|
295 | alarm.Dispose(); |
305 | alarm.Dispose(); |
|
296 | alarm = null; |
306 | alarm = null; |
|
297 | } |
307 | } |
|
- | 308 | } |
||
- | 309 | |
||
- | 310 | public DecayingAlarm Clone() |
||
- | 311 | { |
||
- | 312 | return new DecayingAlarm(decay); |
||
298 | } |
313 | } |
|
299 | } |
314 | } |
|
300 | } |
315 | } |
|
301 | } |
316 | } |