-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathlimit-database.ts
More file actions
272 lines (251 loc) · 6.89 KB
/
limit-database.ts
File metadata and controls
272 lines (251 loc) · 6.89 KB
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
/**
* Copyright (c) 2021 Hengyang Zhang
*
* This software is released under the MIT License.
* https://opensource.org/licenses/MIT
*/
import { formatTimeYMD, MILL_PER_DAY } from "@util/time"
import BaseDatabase from "./common/base-database"
import { REMAIN_WORD_PREFIX } from "./common/constant"
const KEY = REMAIN_WORD_PREFIX + 'LIMIT'
type DateRecords = {
[date: string]: {
mill: number
visit: number
delay?: number
}
}
type LimitRecord = timer.limit.Rule & {
records: DateRecords
}
type ItemValue = {
/**
* ID
*/
i: number
/**
* Condition
*/
c: string[]
/**
* Name
*/
n: string
/**
* Limited time, second
*/
t?: number
/**
* Limited count
*/
ct?: number
/**
* Limited time weekly, second
*/
wt?: number
/**
* Limited count weekly
*/
wct?: number
/**
* Limited time per visit, second
*/
v?: number
/**
* Forbidden periods
*/
p?: Vector<2>[]
/**
* Enabled flag
*/
e: boolean
/**
* Locked flag
*/
l: boolean
/**
* Allow to delay
*/
ad: boolean
/**
* Effective days
*/
wd?: number[]
/**
* Date records
*/
r?: {
[date: string]: {
/**
* Milliseconds
*/
m: number
/**
* Visit count
*/
c: number
/**
* Delay count
*/
d?: number
}
}
}
const cvtItem2Rec = (item: ItemValue): LimitRecord => {
const { i, n, c, t, v, p, e, l, ad, wd, wt, r, ct, wct, } = item
const records: DateRecords = {}
Object.entries(r || {}).forEach?.(([date, { m, d, c }]) => records[date] = { mill: m, delay: d, visit: c })
return {
id: i,
name: n,
cond: c,
time: t,
count: ct,
weekly: wt,
weeklyCount: wct,
visitTime: v,
periods: p?.map(i => [i?.[0], i?.[1]]),
enabled: e,
allowDelay: !!ad,
weekdays: wd,
records: records,
locked: l,
}
}
type Items = Record<number, ItemValue>
function migrate(exist: Items, toMigrate: any) {
const idBase = Object.keys(exist).map(parseInt).sort().reverse()?.[0] ?? 0 + 1
Object.values(toMigrate).forEach((value, idx) => {
const id = idBase + idx
const itemValue: ItemValue = value as ItemValue
const { c, n, t, e, l, ad, v, p } = itemValue
exist[id] = {
i: id, c, n, t, e: !!e, l: !!l, ad: !!ad, v, p,
r: {},
}
})
}
/**
* Time limit
*
* @since 0.2.2
*/
class LimitDatabase extends BaseDatabase {
private async getItems(): Promise<Items> {
let items = await this.storage.getOne<Items>(KEY) || {}
return items
}
private update(items: Items): Promise<void> {
const days10Ago = new Date(Date.now() - MILL_PER_DAY * 10)
const days10AgoStr = formatTimeYMD(days10Ago)
// Clear early date
Object.values(items).forEach(item => {
const records = item.r
if (!records) return
const keys2Del = Object.keys(records).filter(k => k <= days10AgoStr)
keys2Del.forEach(k => delete records[k])
})
return this.setByKey(KEY, items)
}
async all(): Promise<LimitRecord[]> {
const items = await this.getItems()
return Object.values(items).map(cvtItem2Rec)
}
async save(data: MakeOptional<timer.limit.Rule, 'id'>, rewrite?: boolean): Promise<number> {
const items = await this.getItems()
let {
id, name, weekdays,
enabled, locked, allowDelay,
cond,
time, count,
weekly, weeklyCount,
visitTime, periods,
} = data
if (!id) {
const lastId = Object.values(items)
.map(e => e.i)
.filter(i => !!i)
.sort((a, b) => b - a)?.[0] ?? 0
id = lastId + 1
}
const existItem = items[id]
if (existItem && !rewrite) return id
items[id] = {
// Can be overridden by existing
...(existItem || {}),
i: id, n: name, c: cond, wd: weekdays,
e: !!enabled, l: locked, ad: !!allowDelay,
t: time, ct: count,
wt: weekly, wct: weeklyCount,
v: visitTime, p: periods,
}
await this.update(items)
return id
}
async remove(id: number): Promise<void> {
const items = await this.getItems()
delete items[id]
await this.update(items)
}
async updateWaste(date: string, toUpdate: { [id: number]: number }): Promise<void> {
const items = await this.getItems()
Object.entries(toUpdate).forEach(([k, waste]) => {
const id = parseInt(k)
const entry = items[id]
if (!entry) return
const records = entry.r = entry.r || {}
const record = records[date] = records[date] || { m: 0, c: 0 }
record.m = waste
})
await this.update(items)
}
async increaseVisit(date: string, ids: number[]) {
const items = await this.getItems()
ids?.forEach(id => {
const entry = items[id]
if (!entry) return
const records = entry.r = entry.r || {}
const record = records[date] = records[date] || { m: 0, c: 0 }
record.c++
})
await this.update(items)
}
async updateDelayCount(date: string, toUpdate: timer.limit.Item[]): Promise<void> {
const items = await this.getItems()
toUpdate?.forEach(({ id, delayCount }) => {
const entry = items[id]
if (!entry) return
const records = entry.r = entry.r || {}
const record = records[date] = records[date] || { m: 0, c: 0 }
record.d = delayCount
})
await this.update(items)
}
async updateDelay(id: number, allowDelay: boolean) {
const items = await this.getItems()
if (!items[id]) return
items[id].ad = allowDelay
await this.update(items)
}
async updateEnabled(id: number, enabled: boolean) {
const items = await this.getItems()
if (!items[id]) return
items[id].e = !!enabled
await this.update(items)
}
async updateLocked(id: number, locked: boolean) {
const items = await this.getItems()
if (!items[id]) return
items[id].l = !!locked
await this.update(items)
}
async importData(data: any): Promise<void> {
let toImport = data[KEY] as Items
// Not import
if (typeof toImport !== 'object') return
const exists: Items = await this.getItems()
migrate(exists, toImport)
this.setByKey(KEY, exists)
}
}
export default LimitDatabase