-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDBHelper.cs
818 lines (735 loc) · 25.2 KB
/
DBHelper.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
using LiteDB;
using RBQBot.Model;
using System;
using System.Collections.Generic;
namespace RBQBot
{
public sealed class DBHelper
{
private string DBPath = AppDomain.CurrentDomain.BaseDirectory + "db/database.db";
private string tempDBPath = AppDomain.CurrentDomain.BaseDirectory + "db/temp.db";
public DBHelper() {
InitDB();
}
public static readonly DBHelper Instance = new DBHelper();
private ILiteDatabase db = null;
private volatile bool CanAccess = false;
private ILiteCollection<AllowGroup> allowGroupCol = null;
private ILiteCollection<GagItem> gagItemCol = null;
private ILiteCollection<RBQ> rbqCol = null;
private ILiteCollection<RBQStatus> rbqStatusCol = null;
private ILiteCollection<MessageCount> messageCountCol = null;
/// <summary>[绒布球] 给自己带上了默认口塞! 咦?! 居然自己给自己戴? 真是个可爱的绒布球呢!</summary>
public readonly string[] defaultSelfLockMsg = { "咦?! 居然自己给自己戴? 真是个可爱的绒布球呢!" };
/// <summary>[Ta] 帮 [绒布球] 带上了默认口塞! 顺便在 Ta 身上画了一个正字~</summary>
public readonly string[] defaultLockMsg = { "顺便在 Ta 身上画了一个正字~" };
/// <summary>[Ta] 帮 [绒布球] 修好了口塞!\n顺便展示了钥匙并丢到了一边!</summary>
public readonly string[] defaultEnhancedLockMsg = { "修好了口塞!\n顺便展示了钥匙并丢到了一边!" };
/// <summary>[绒布球] 挣脱了被人们安装的 超大号默认口塞! Ta感觉自己可以容纳更大的尺寸了呢!</summary>
public readonly string[] defaultUnlockMsg = { "Ta感觉自己可以容纳更大的尺寸了呢!" };
private void InitDB()
{
if (System.IO.Directory.Exists($"{AppDomain.CurrentDomain.BaseDirectory}db/") != true) System.IO.Directory.CreateDirectory($"{AppDomain.CurrentDomain.BaseDirectory}db/");
db = new LiteDatabase(new ConnectionString() {
Filename = DBPath,
Connection = ConnectionType.Direct,
Password = null,
InitialSize = 0,
ReadOnly = false,
Upgrade = false,
Collation = new Collation("zh-CN/None") // 使用区域化 和 比较字符串时 不忽略任何字符去比较 http://www.litedb.org/docs/collation/
});
allowGroupCol = db.GetCollection<AllowGroup>("GroupList");
gagItemCol = db.GetCollection<GagItem>("GagList");
rbqCol = db.GetCollection<RBQ>("RBQList");
rbqStatusCol = db.GetCollection<RBQStatus>("RBQStatusList");
messageCountCol = db.GetCollection<MessageCount>("MessageCount");
CanAccess = true;
}
public void StopDatabase()
{
CanAccess = false;
db.Checkpoint();
db.Commit();
allowGroupCol = null;
gagItemCol = null;
rbqCol = null;
rbqStatusCol = null;
messageCountCol = null;
db.Dispose();
db = null;
}
public void GetDatabase()
{
CanAccess = false;
db.Checkpoint();
db.Commit();
allowGroupCol = null;
gagItemCol = null;
rbqCol = null;
rbqStatusCol = null;
messageCountCol = null;
db.Dispose();
db = null;
if (System.IO.File.Exists(tempDBPath) == true) System.IO.File.Delete(tempDBPath);
System.IO.File.Copy(DBPath, tempDBPath);
InitDB();
}
#region Group Operate
public int GetGroupCount()
{
if (CanAccess) return allowGroupCol.Count();
return -1;
}
public void AddAllowGroup(long groupId)
{
if (CanAccess)
{
var obj = new AllowGroup(groupId)
{
AllowGag = true,
AllowMsgCount = true,
AllowVerify = true,
SamplyMsgCountStr = "快乐的一天开始了!"
};
allowGroupCol.Insert(obj);
}
}
public void DelAllowGroup(long groupId)
{
if (CanAccess)
{
var result = allowGroupCol.FindOne(x => x.GroupId == groupId);
if (result != null) allowGroupCol.Delete(result.Id);
}
}
public IEnumerable<AllowGroup> GetAllowGroups()
{
if (CanAccess)
{
return allowGroupCol.FindAll();
}
return new AllowGroup[0];
}
public AllowGroup GetAllowGroup(long groupId)
{
if (CanAccess)
{
var result = allowGroupCol.FindOne(x => x.GroupId == groupId);
if (result != null) return result;
}
return null;
}
public string GetAllowMsgCountStr(long groupId)
{
if (CanAccess)
{
var result = allowGroupCol.FindOne(x => x.GroupId == groupId);
if (result != null)
{
return result.SamplyMsgCountStr;
}
}
return "快乐的一天开始了!";
}
public bool GetAllowFunctionStatus(long groupId, AllowFunction query)
{
if (CanAccess)
{
var result = allowGroupCol.FindOne(x => x.GroupId == groupId);
if (result != null)
{
switch (query)
{
case AllowFunction.AllowGag: return result.AllowGag;
case AllowFunction.AllowVerify: return result.AllowVerify;
case AllowFunction.AllowMsgCount: return result.AllowMsgCount;
default: return false;
}
}
}
return false;
}
public bool GetAllowGroupExists(long groupId)
{
if (CanAccess)
{
var result = allowGroupCol.FindOne(x => x.GroupId == groupId);
if (result != null) return true;
}
return false;
}
public void SetAllowGroup(AllowGroup allowGroup)
{
if (CanAccess)
{
allowGroupCol.Update(allowGroup);
}
}
//public long[] GetAllGroupId()
//{
// long[] l = new long[allowGroupCol.Count()];
// var result = allowGroupCol.FindAll();
// if (result != null)
// {
// var c = 0;
// foreach (var i in result)
// {
// l[c] = i.GroupId;
// c++;
// }
// return l;
// }
// return null;
//}
//public IEnumerable<AllowGroup> GetGroup(int startId, int stopId)
//{
// var results = allowGroupCol.Find(x => x.Id >= startId && x.Id <= stopId);
// if (results != null) return results;
// return null;
//}
#endregion
#region GagItem Operate
public int GetGagItemCount()
{
if (CanAccess)
{
return gagItemCol.Count();
}
return 0;
}
public void AddGagItem(string gagName, int limitPoint, int unLockCount, bool showLimit, bool showUnlock, string[] selfLockMsg = null, string[] lockMsg = null, string[] enhancedLockMsg = null, string[] unlockMsg = null)
{
if (CanAccess)
{
if (selfLockMsg == null) selfLockMsg = defaultSelfLockMsg;
if (lockMsg == null) lockMsg = defaultLockMsg;
if (enhancedLockMsg == null) enhancedLockMsg = defaultEnhancedLockMsg;
if (unlockMsg == null) unlockMsg = defaultUnlockMsg;
var obj = new GagItem()
{
Name = gagName,
LimitPoint = limitPoint,
UnLockCount = unLockCount,
SelfLockMsg = selfLockMsg,
LockMsg = lockMsg,
EnhancedLockMsg = enhancedLockMsg,
UnLockMsg = unlockMsg,
ShowLimit = showLimit,
ShowUnlock = showUnlock
};
gagItemCol.Insert(obj);
}
}
public void DelGagItem(string gagName)
{
if (CanAccess)
{
var result = gagItemCol.FindOne(x => x.Name == gagName);
if (result != null) gagItemCol.Delete(result.Id);
}
}
public IEnumerable<GagItem> GetAllGagItems()
{
if (CanAccess)
{
return gagItemCol.FindAll();
}
return new GagItem[0];
}
public bool GetGagItemExist(string gagName)
{
if (CanAccess)
{
var result = gagItemCol.FindOne(x => x.Name == gagName);
if (result != null) return true;
}
return false;
}
public GagItem GetGagItemInfo(string gagName)
{
if (CanAccess)
{
var result = gagItemCol.FindOne(x => x.Name == gagName);
return result;
}
return new GagItem()
{
Id = 1,
Name = "巧克力口塞",
LimitPoint = -100,
UnLockCount = 1,
ShowLimit = true,
ShowUnlock = true,
SelfLockMsg = defaultSelfLockMsg,
LockMsg = defaultLockMsg,
EnhancedLockMsg = defaultEnhancedLockMsg,
UnLockMsg = defaultUnlockMsg
};
}
public GagItem GetGagItemInfo(int id)
{
if (CanAccess)
{
var result = gagItemCol.FindOne(x => x.Id == id);
return result;
}
return new GagItem()
{
Id = 1,
Name = "巧克力口塞",
LimitPoint = -100,
UnLockCount = 1,
ShowLimit = true,
ShowUnlock = true,
SelfLockMsg = defaultSelfLockMsg,
LockMsg = defaultLockMsg,
EnhancedLockMsg = defaultEnhancedLockMsg,
UnLockMsg = defaultUnlockMsg
};
}
public void SetGagItem(string gagName, int limitPoint, int unLockCount, string[] selfLockMsg = null, string[] lockMsg = null, string[] enhancedLockMsg = null, string[] unlockMsg = null)
{
if (CanAccess)
{
var result = gagItemCol.FindOne(x => x.Name == gagName);
if (result != null)
{
result.LimitPoint = limitPoint;
result.UnLockCount = unLockCount;
result.SelfLockMsg = selfLockMsg;
result.LockMsg = lockMsg;
result.EnhancedLockMsg = enhancedLockMsg;
result.UnLockMsg = unlockMsg;
gagItemCol.Update(result);
}
}
}
public void SetGagItem(GagItem item)
{
if (CanAccess)
{
gagItemCol.Update(item);
}
}
#endregion
#region RBQ Operate
public int GetRBQCount()
{
if (CanAccess)
{
return rbqCol.Count();
}
return 0;
}
public void AddRBQ(long telegramId, long rbqPoint)
{
if (CanAccess)
{
var obj = new RBQ()
{
TelegramId = telegramId,
RBQPoint = rbqPoint
};
rbqCol.Insert(obj);
}
}
public void DelRBQ(long telegramId)
{
if (CanAccess)
{
var result = rbqCol.FindOne(x => x.TelegramId == telegramId);
if (result != null) rbqCol.Delete(result.Id);
}
}
public bool GetRBQExist(long telegramId)
{
if (CanAccess)
{
var result = rbqCol.FindOne(x => x.TelegramId == telegramId);
if (result != null) return true;
}
return false;
}
public RBQ GetRBQInfo(long telegramId)
{
if (CanAccess)
{
var result = rbqCol.FindOne(x => x.TelegramId == telegramId);
return result;
}
return new RBQ()
{
TelegramId = 777000,
RBQPoint = 0,
};
}
public long GetRBQPoint(long telegramId)
{
if (CanAccess)
{
var result = rbqCol.FindOne(x => x.TelegramId == telegramId);
if (result != null) return result.RBQPoint;
}
return -1;
}
public void SetRBQInfo(long telegramId, long rbqPoint)
{
if (CanAccess)
{
var result = rbqCol.FindOne(x => x.TelegramId == telegramId);
if (result != null)
{
result.RBQPoint = rbqPoint;
rbqCol.Update(result);
}
}
}
public void SetRBQPointAdd(long telegramId, long addRbqPoint)
{
if (CanAccess)
{
var result = rbqCol.FindOne(x => x.TelegramId == telegramId);
if (result != null)
{
result.RBQPoint = result.RBQPoint + addRbqPoint;
rbqCol.Update(result);
}
}
}
public void SetRBQPointAdd1(long telegramId)
{
if (CanAccess)
{
var result = rbqCol.FindOne(x => x.TelegramId == telegramId);
if (result != null)
{
result.RBQPoint++;
rbqCol.Update(result);
}
}
}
public void SetRBQPointDel(long telegramId, long delRbqPoint)
{
if (CanAccess)
{
var result = rbqCol.FindOne(x => x.TelegramId == telegramId);
if (result != null)
{
result.RBQPoint = result.RBQPoint - delRbqPoint;
rbqCol.Update(result);
}
}
}
public void SetRBQInfo(RBQ rbq)
{
if (CanAccess)
{
rbqCol.Update(rbq);
}
}
public IEnumerable<RBQ> GetAllRBQ()
{
if (CanAccess)
{
return rbqCol.FindAll();
}
return new RBQ[0];
}
#endregion
#region RBQStatus Operate
public int GetRBQStatusCount()
{
if (CanAccess)
{
return rbqStatusCol.Count();
}
return 0;
}
public bool AddRBQFroms(long groupId, long rbqId, long userId)
{
if (CanAccess)
{
var result = rbqStatusCol.FindOne(x => x.GroupId == groupId && x.RBQId == rbqId);
if (result != null)
{
if (result.FromId == null)
{
result.FromId = new long[] { userId };
rbqStatusCol.Update(result);
return true;
}
else
{
if (result.FromId.Length == 0)
{
result.FromId = new long[] { userId };
rbqStatusCol.Update(result);
return true;
}
else
{
long[] a = new long[result.FromId.Length + 1];
Array.Copy(result.FromId, 0, a, 0, result.FromId.Length);
a[result.FromId.Length] = userId;
result.FromId = a;
rbqStatusCol.Update(result);
return true;
}
}
}
}
return false;
}
public void AddRBQStatus(long groupId, long rbqId, long lockCount = 0, bool anyUsed = false)
{
if (CanAccess)
{
var obj = new RBQStatus()
{
GroupId = groupId,
RBQId = rbqId,
LockCount = lockCount,
AnyUsed = anyUsed,
StartLockTime = DateTime.UnixEpoch.Ticks,
};
rbqStatusCol.Insert(obj);
}
}
public void DelRBQStatus(long groupId, long rbqId)
{
if (CanAccess)
{
var result = rbqStatusCol.FindOne(x => x.GroupId == groupId && x.RBQId == rbqId);
if (result != null) rbqStatusCol.Delete(result.Id);
}
}
public IEnumerable<RBQStatus> GetAllRBQStatus()
{
if (CanAccess)
{
return rbqStatusCol.FindAll();
}
return new RBQStatus[0];
}
//public IEnumerable<RBQStatus> GetRBQStatusById(long rbqId)
//{
// var result = rbqStatusCol.Find(x => x.RBQId == rbqId);
// if (result != null)
// {
// return result;
// } return null;
//}
public bool GetRBQFromExits(long groupId, long rbqId, long userId)
{
if (CanAccess)
{
var result = rbqStatusCol.FindOne(x => x.GroupId == groupId && x.RBQId == rbqId);
if (result != null)
{
if (result.FromId == null) return false;
for (int i = 0; i < result.FromId.Length; i++)
{
if (result.FromId[i] == userId) return true;
}
}
}
return false;
}
public int GetRBQGagId(long groupId, long rbqId)
{
if (CanAccess)
{
var result = rbqStatusCol.FindOne(x => x.GroupId == groupId && x.RBQId == rbqId);
if (result != null) return result.GagId;
}
return 0;
}
public long GetRBQLockCount(long groupId, long rbqId)
{
if (CanAccess)
{
var result = rbqStatusCol.FindOne(x => x.GroupId == groupId && x.RBQId == rbqId);
if (result != null) return result.LockCount;
}
return 0;
}
public bool GetRBQStatusExist(long groupId, long rbqId)
{
if (CanAccess)
{
var result = rbqStatusCol.FindOne(x => x.GroupId == groupId && x.RBQId == rbqId);
if (result != null) return true;
}
return false;
}
public bool GetRBQCanLock(long groupId, long rbqId)
{
if (CanAccess)
{
var result = rbqStatusCol.FindOne(x => x.GroupId == groupId && x.RBQId == rbqId);
if (result != null)
{
var rbqP = GetRBQPoint(rbqId);
var cd = 0;
switch (rbqP)
{
case <= 100: cd = 60; break;
case <= 200: cd = 40; break;
case <= 500: cd = 25; break;
case <= 1000: cd = 15; break;
case > 1000: cd = 10; break;
default: cd = 60; break;
}
// (result.StartLockTime)开始锁定时间+(Program.LockTime)超时时间+CD < 当前时间
if (new DateTime(result.StartLockTime).AddMinutes(Program.LockTime).AddSeconds(cd) < DateTime.UtcNow.AddHours(8)) return true;
else return false;
}
}
return false;
}
public bool GetRBQAnyUsed(long groupId, long rbqId)
{
if (CanAccess)
{
var result = rbqStatusCol.FindOne(x => x.GroupId == groupId && x.RBQId == rbqId);
if (result != null) return result.AnyUsed;
}
return false;
}
public long[] GetRBQFromId(long groupId, long rbqId)
{
if (CanAccess)
{
var result = rbqStatusCol.FindOne(x => x.GroupId == groupId && x.RBQId == rbqId);
if (result != null) return result.FromId;
}
return new long[0];
}
public RBQStatus GetRBQStatus(int id)
{
if (CanAccess)
{
var result = rbqStatusCol.FindOne(x => x.Id == id);
if (result != null) return result;
}
return null;
}
public RBQStatus GetRBQStatus(long groupId, long rbqId)
{
if (CanAccess)
{
var result = rbqStatusCol.FindOne(x => x.GroupId == groupId && x.RBQId == rbqId);
if (result != null) return result;
}
return null;
}
public int GetRBQStatusId(long groupId, long rbqId)
{
if (CanAccess)
{
var result = rbqStatusCol.FindOne(x => x.GroupId == groupId && x.RBQId == rbqId);
if (result != null) return result.Id;
}
return -1;
}
public void SetRBQStatus(long groupId, long rbqId, long lockCount, bool anyUsed, int gagId, long startLockTime, long[] fromId)
{
if (CanAccess)
{
var result = rbqStatusCol.FindOne(x => x.GroupId == groupId && x.RBQId == rbqId);
if (result != null)
{
result.LockCount = lockCount;
result.AnyUsed = anyUsed;
result.GagId = gagId;
result.StartLockTime = startLockTime;
result.FromId = fromId;
rbqStatusCol.Update(result);
}
}
}
public void SetRBQUsed(long groupId, long rbqId, bool anyUsed)
{
if (CanAccess)
{
var result = rbqStatusCol.FindOne(x => x.GroupId == groupId && x.RBQId == rbqId);
if (result != null)
{
result.AnyUsed = anyUsed;
rbqStatusCol.Update(result);
}
}
}
public void SetRBQStatus(RBQStatus rbq)
{
if (CanAccess)
{
rbqStatusCol.Update(rbq);
}
}
#endregion
#region MessageCount Operate
public void AddMessageCountUser(long groupId, long userId)
{
if (CanAccess)
{
var obj = new MessageCount(messageCountCol.Count()+1, groupId, userId);
messageCountCol.Insert(obj);
}
}
public void AddMessageCountUser(long groupId, long userId, int count)
{
if (CanAccess)
{
var obj = new MessageCount(messageCountCol.Count()+1, groupId, userId, count);
messageCountCol.Insert(obj);
}
}
public bool GetMessageCountUserExist(long groupId, long userId)
{
if (CanAccess)
{
var result = messageCountCol.FindOne(x => x.GroupId == groupId && x.UserId == userId);
if (result.Id > 0) return true;
}
return false;
}
public void AddMessageCountUserCount(long groupId, long userId)
{
if (CanAccess)
{
var result = messageCountCol.FindOne(x => x.GroupId == groupId && x.UserId == userId);
if (result.Id > 0)
{
result.Count++;
messageCountCol.Update(result);
}
}
}
public IEnumerable<MessageCount> GetAllMessageCounts()
{
if (CanAccess)
{
return messageCountCol.FindAll();
}
return new MessageCount[0];
}
public int GetMessageCountTableCount()
{
if (CanAccess)
{
return messageCountCol.Count();
}
return 0;
}
public void DropMessageCountTable()
{
if (CanAccess)
{
db.DropCollection("MessageCount");
}
}
#endregion
}
}