-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLockInstance.cs
306 lines (217 loc) · 8.85 KB
/
LockInstance.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
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
using ChasterSharp;
namespace ChasterUtil;
public sealed class LockInstance : IEquatable<LockInstance>
{
#region Properties
public bool IsFrozen { get; set; }
public bool DisplayRemainingTime { get; set; }
public bool HideTimeLogs { get; set; }
public bool IsLocked { get; private set; }
public bool IsArchived { get; private set; }
public bool IsTrusted { get; internal set; }
public bool IsKeyholderLock => _lock.Role == LockRole.Keyholder;
public bool HasTimeLimit => MaxLimitDateTime.HasValue;
public TimeSpan TimeLocked => ServerDateTime - _lock.StartDate;
public TimeSpan? TimeFrozen => ServerDateTime - _lock.FrozenAt;
public TimeSpan? TimeRemaining => GetTimeRemaining();
public DateTimeOffset? UnlockDate => TimeRemaining.HasValue ? ServerDateTime.Add(TimeRemaining.Value) : null;
public DateTimeOffset ServerDateTime => new(DateTime.UtcNow, _lock.StartDate.Offset);
public DateTimeOffset? MaxLimitDateTime { get; private set; }
public DateTimeOffset? ArchivedDateTime => IsKeyholderLock ? _lock.KeyholderArchivedAt : _lock.ArchivedAt;
public Lock Lock => _lock;
public string LockId => _lock.Id;
internal string Token { get; }
internal string TokenId { get; }
internal ChasterProcessor Processor { get; }
#endregion
#region Extensions
public DiceExtension Dice { get; }
public GuessTheTimerExtension GuessTheTimer { get; }
public HygieneOpeningExtension HygieneOpening { get; }
public PilloryExtension Pillory { get; }
public RandomEventsExtension RandomEvents { get; }
public ShareLinkExtension ShareLink { get; }
public WheelOfFortuneExtension WheelOfFortune { get; }
public VerificationPictureExtension VerificationPicture { get; }
public TasksExtension Tasks { get; }
public PenaltiesExtension Penalties { get; }
#endregion
#region Members
private TimeSpan _timeAdjustment;
private TimeSpan _pendingTimeChanges;
private readonly Lock _lock;
#endregion
public LockInstance(ChasterProcessor processor, Lock @lock, string token)
{
_lock = @lock;
Processor = processor;
Token = token;
TokenId = Processor.GetBearerTokenId(Token);
IsLocked = @lock.Status == LockStatus.Locked;
IsArchived = ArchivedDateTime.HasValue;
IsFrozen = @lock.IsFrozen;
DisplayRemainingTime = @lock.DisplayRemainingTime;
HideTimeLogs = @lock.HideTimeLogs;
IsTrusted = @lock.Trusted;
MaxLimitDateTime = @lock.MaxLimitDate;
Dice = new DiceExtension(@lock.Extensions.FirstOrDefault(x => x.GetExtensionSlug() == ExtensionSlug.Dice), this);
GuessTheTimer = new GuessTheTimerExtension(@lock.Extensions.FirstOrDefault(x => x.GetExtensionSlug() == ExtensionSlug.GuessTheTimer), this);
HygieneOpening = new HygieneOpeningExtension(@lock.Extensions.FirstOrDefault(x => x.GetExtensionSlug() == ExtensionSlug.HygieneOpening), this);
Pillory = new PilloryExtension(@lock.Extensions.FirstOrDefault(x => x.GetExtensionSlug() == ExtensionSlug.Pillory), this);
RandomEvents = new RandomEventsExtension(@lock.Extensions.FirstOrDefault(x => x.GetExtensionSlug() == ExtensionSlug.RandomEvents), this);
ShareLink = new ShareLinkExtension(@lock.Extensions.FirstOrDefault(x => x.GetExtensionSlug() == ExtensionSlug.ShareLink), this);
WheelOfFortune = new WheelOfFortuneExtension(@lock.Extensions.FirstOrDefault(x => x.GetExtensionSlug() == ExtensionSlug.WheelOfFortune), this);
VerificationPicture = new VerificationPictureExtension(@lock.Extensions.FirstOrDefault(x => x.GetExtensionSlug() == ExtensionSlug.VerificationPicture), this);
Tasks = new TasksExtension(@lock.Extensions.FirstOrDefault(x => x.GetExtensionSlug() == ExtensionSlug.Tasks), this);
Penalties = new PenaltiesExtension(@lock.Extensions.FirstOrDefault(x => x.GetExtensionSlug() == ExtensionSlug.Penalties), this);
}
public void Unlock()
{
if (!IsKeyholderLock || Lock.Status != LockStatus.Locked)
return;
IsLocked = false;
}
public void Archive()
{
Unlock();
IsArchived = true;
}
public void AddTime(TimeSpan timeToAdd)
{
_timeAdjustment += timeToAdd;
_pendingTimeChanges += timeToAdd;
}
public void RemoveTime(TimeSpan timeToRemove)
{
if (!IsKeyholderLock)
return;
_timeAdjustment -= timeToRemove;
_pendingTimeChanges -= timeToRemove;
}
public void TrustKeyholder()
{
if (IsKeyholderLock)
return;
IsTrusted = true;
}
public void IncreaseMaxLimitDate(TimeSpan duration)
{
if (IsKeyholderLock || !HasTimeLimit)
return;
MaxLimitDateTime += duration;
}
public void RemoveMaxLimitDate()
{
if (IsKeyholderLock)
return;
MaxLimitDateTime = null;
}
private TimeSpan? GetTimeRemaining()
{
var timeRemaining = _lock.EndDate - (_lock.IsFrozen ? _lock.FrozenAt : ServerDateTime) + _timeAdjustment;
if (MaxLimitDateTime.HasValue)
{
var maxTimeRemaining = MaxLimitDateTime - ServerDateTime;
if(maxTimeRemaining < timeRemaining)
timeRemaining = maxTimeRemaining;
}
return timeRemaining is { TotalSeconds: < 0 } ? TimeSpan.Zero : timeRemaining;
}
private ChasterExtension[] GetExtensions()
{
return [Dice, GuessTheTimer, HygieneOpening, Pillory, RandomEvents, ShareLink, WheelOfFortune, VerificationPicture, Tasks, Penalties];
}
public void CommitUpdates()
{
if (_lock.Status == LockStatus.Locked && !IsLocked)
Processor.LogUnlockAction(this);
if (IsKeyholderLock)
{
if (_lock.KeyholderArchivedAt.HasValue != IsArchived && !IsLocked)
Processor.LogArchiveAction(this);
}
else
{
if (_lock.ArchivedAt.HasValue != IsArchived)
Processor.LogArchiveAction(this);
}
if (!IsLocked)
return;
CommitLockUpdates();
CommitExtensionUpdates();
CommitTaskUpdates();
}
private void CommitLockUpdates()
{
if (IsKeyholderLock)
{
if (IsFrozen != _lock.IsFrozen)
Processor.LogUpdateFreezeAction(this, IsFrozen);
if (_lock.DisplayRemainingTime != DisplayRemainingTime || _lock.HideTimeLogs != HideTimeLogs)
Processor.LogUpdateSettingsAction(this, DisplayRemainingTime, HideTimeLogs);
}
else
{
if(IsTrusted != _lock.Trusted)
Processor.LogTrustKeyholderAction(this);
if (MaxLimitDateTime != _lock.MaxLimitDate)
Processor.LogUpdateMaxTimeLimitAction(this, MaxLimitDateTime);
}
if ((int)_pendingTimeChanges.TotalSeconds != 0)
{
Processor.LogAddRemoveTimeAction(this, _pendingTimeChanges);
_pendingTimeChanges = TimeSpan.Zero;
}
}
private void CommitExtensionUpdates()
{
if (!IsTrusted || !IsKeyholderLock)
return;
var extensions = GetExtensions().ToList();
if (!extensions.Any(x => x.IsModified))
return;
var dto = new EditLockExtensionsDto
{
Extensions = extensions.Where(x => x.IsEnabled).Select(x => x.GetLockExtensionConfig()).ToList()
};
Processor.LogUpdateExtensionsAction(this, dto);
}
private void CommitTaskUpdates()
{
//TODO: I'm not sure how wearer interaction works here... I think it's fine if they have ConfigureTasks enabled?
if (!Tasks.IsEnabled || !Tasks.TasksModified)
return;
var taskParams = Tasks.UserTasks.Select(x => new TaskActionParamsModel { Points = x.Points, Task = x.Task }).ToList();
Processor.LogUpdateTasksAction(this, taskParams);
}
public void OverrideTrusted()
{
IsTrusted = true;
}
#region Equality
public override bool Equals(object? obj)
{
return ReferenceEquals(this, obj) || obj is LockInstance other && LockId == other.LockId && TokenId == other.TokenId;
}
public bool Equals(LockInstance? other)
{
return ReferenceEquals(this, other) || other is not null && LockId == other.LockId && TokenId == other.TokenId;
}
public override int GetHashCode()
{
return HashCode.Combine(LockId, TokenId);
}
public static bool operator ==(LockInstance? obj1, LockInstance? obj2)
{
if (ReferenceEquals(obj1, obj2))
return true;
if (obj1 is null || obj2 is null)
return false;
return obj1.Equals(obj2);
}
public static bool operator !=(LockInstance? obj1, LockInstance? obj2)
{
return !(obj1 == obj2);
}
#endregion
}