-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathutils.es
More file actions
250 lines (216 loc) · 6.24 KB
/
Copy pathutils.es
File metadata and controls
250 lines (216 loc) · 6.24 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
import Promise from 'bluebird'
import fs from 'fs-extra'
import { join } from 'path-extra'
import CSON from 'cson'
import { reduce, uniqBy, forEach, pick, omit } from 'lodash'
import FileWriter from 'views/utils/file-writer'
const { APPDATA_PATH, i18n } = window
export const storePath = 'plugin-senka'
export const __ = i18n["poi-plugin-senka-calc"].__.bind(i18n["poi-plugin-senka-calc"])
const fileWriter = new FileWriter()
export function saveHistoryData(historyData) {
fileWriter.write(
getFilePath(true),
CSON.stringify(uniqBy(historyData, 'time'))
)
}
export function loadHistoryData() {
let historyData = []
try {
fs.ensureDirSync(getFilePath())
historyData = CSON.parseCSONFile(getFilePath(true))
if (!(historyData instanceof Array)) {
historyData = []
}
} catch (e) {
historyData = []
}
return historyData
}
export function pickStoreData(storeData, initialData) {
const pickedData = {
...pick(storeData, Object.keys(initialData)),
...omit(initialData, Object.keys(storeData)),
}
forEach(pickedData, (value, key) => {
if (Object.prototype.toString.call(value) !== Object.prototype.toString.call(initialData[key])) {
pickedData[key] = initialData[key]
}
})
return pickedData
}
export function estimateSenka(exp, baseExp) {
return (exp - baseExp) / 1428 - 0.0499
}
export const requestAsyncMagicNums = async () => {
const requestAsync = Promise.promisify(
Promise.promisifyAll(require('request')),
{ multiArgs: true }
)
let response
let body
try {
[response, body] = await requestAsync('https://rui.ai/poi/data/tutu.json', {
method: 'GET',
json: true,
})
} catch (e) {
error(e.stack)
console.warn('Check update error.')
}
localStorage.setItem(`${storePath}.MAGIC_NUMS`, JSON.stringify(body))
return body
}
export function getRate(rankNo, obfsRate, memberId, MAGIC_NUMS) {
const [ MAGIC_R_NUMS, MAGIC_L_NUMS ] = MAGIC_NUMS
const rate = obfsRate / MAGIC_R_NUMS[rankNo % 13] / MAGIC_L_NUMS[memberId % 10] - 73 - 18
return rate > 0 ? rate : 0
}
export function getMemberId() {
return window.getStore('info.basic.api_member_id')
}
export function getUserNickname() {
return window.getStore('info.basic.api_nickname')
}
export function getFilePath(filename = false) {
const userPath = join(APPDATA_PATH, 'senka-calc', getMemberId().toString())
return filename
? join(userPath, timeToString(getRefreshTime(), true))
: userPath
}
export function getFinalTime(type, nextMonth) {
const offset = nextMonth ? 2 : 1
const finalDate = new Date()
finalDate.setUTCHours(finalDate.getUTCHours() + 9) // mapping Tokyo(00:00) to UTC(00:00)
finalDate.setUTCDate(1) // in case next month's day less than this month
finalDate.setUTCMonth(finalDate.getUTCMonth() + offset)
finalDate.setUTCDate(0)
switch (type) {
case 'pm':
finalDate.setUTCHours(13)
break
case 'am':
default:
finalDate.setUTCHours(6)
}
finalDate.setUTCMinutes(0)
finalDate.setUTCSeconds(0)
finalDate.setUTCMilliseconds(0)
return finalDate.getTime()
}
export function isLastDay() {
const today = new Date()
today.setUTCHours(today.getUTCHours() + 9) // mapping Tokyo(00:00) to UTC(00:00)
const tomorrow = new Date(today)
tomorrow.setUTCDate(today.getUTCDate() + 1)
return today.getUTCMonth() !== tomorrow.getUTCMonth()
}
export function isNewMonth(time) {
const updatedDay = new Date(time)
const today = new Date()
today.setUTCHours(today.getUTCHours() + 9)
updatedDay.setUTCHours(updatedDay.getUTCHours() + 9)
return today.getUTCMonth() !== updatedDay.getUTCMonth()
}
export function getRefreshTime(type) {
const date = new Date()
const hour = date.getUTCHours()
const offset =
type === 'next' ? 12 :
type === 'account' ? 12 - 1 :
0
const freshHour =
hour < 6 ? -6 :
hour < 18 ? 6 :
18
date.setUTCHours(freshHour + offset)
date.setUTCMinutes(0)
date.setUTCSeconds(0)
date.setUTCMilliseconds(0)
return date.getTime()
}
export function timeToString(time, isFilename = false) {
const date = new Date(time || 0)
return isFilename
? `${date.getFullYear()}-${date.getMonth() + 1}`
: `${date.getFullYear()}-${date.getMonth() + 1}-${date.getDate()} ${date.getHours()}:00`
}
export function getStatusStyle(flag) {
return flag ? {} : { opacity: 0.4 }
}
export function dateToString(time) {
if (!time) {
time = 0
}
const date = new Date(time)
return `${date.getMonth() + 1}-${date.getDate()}`
}
// return true if all true
export function checkIsUpdated(activeRank, updatedList) {
return !reduce(activeRank, function(sum, data, key) {
if (data.active && !updatedList[key]) sum++
return sum
}, 0)
}
export function accountTimeout(timerState) {
const { counter, updateTime, finalTimes } = timerState
let { status, str, nextTime } = counter.accounted
const now = Date.now()
// some logic to determine account Stage
if (now >= finalTimes.pm) {
status = true
} else if (now >= finalTimes.am) {
str = __('Normal map final time')
nextTime = finalTimes.pm
} else if (now >= updateTime + 11 * 3600 * 1000) {
status = true
} else {
str = __('Account time')
status = false
}
return {
counter: {
...counter,
accounted: {
...counter.accounted,
status,
str,
nextTime,
},
},
}
}
export function refreshTimeout(timerState) {
const { updatedList, counter, isTimeUp } = timerState
let { finalTimes } = timerState
const { accounted, refreshed } = counter
if (!isTimeUp) {
forEach(updatedList, (value, key) => {
updatedList[key] = false
})
} else {
refreshed.nextTime = getRefreshTime('next')
accounted.nextTime = getRefreshTime('account')
accounted.status = false
accounted.str = __('Account time')
if (Date.now() >= finalTimes.pm) {
finalTimes = {
am: getFinalTime('am', true),
pm: getFinalTime('pm', true),
}
} else if (Date.now() >= finalTimes.am) {
accounted.str = __('Normal map final time')
accounted.nextTime = finalTimes.pm
}
}
return {
updatedList,
finalTimes,
isTimeUp: !isTimeUp,
counter: {
...counter,
accounted,
refreshed,
},
}
}