forked from Glaceon31/LLhelper
-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathupdateweight2.py
310 lines (280 loc) · 10.8 KB
/
updateweight2.py
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
import os
import os.path
import json
import threading
import sys
import ctypes
from lib.SyncPrinter import SyncPrinter
from lib.JsonUtil import JsonFile
if sys.version[0] == '2':
from urllib2 import urlopen
import Queue
else:
from urllib.request import urlopen
import queue as Queue
LLSIF_WIN_API_DOMAIN = 'http://a.llsif.win/'
LLSIF_WIN_API_ENDPOINT = 'live/json/'
LIVE_MAP_LOCAL_CACHE_DIR = 'static/live/json/'
NOTE_TYPE_RANDOM = 0
NOTE_TYPE_NORMAL = 1
NOTE_TYPE_EVENT = 2
NOTE_TYPE_HOLD = 3
NOTE_TYPE_BOMB_1 = 4
NOTE_TYPE_BOMB_3 = 5
NOTE_TYPE_BOMB_5 = 6
NOTE_TYPE_BOMB_9 = 7
NOTE_TYPE_SWING = 11
NOTE_TYPE_SWING_EVENT = 12
NOTE_TYPE_SWING_HOLD = 13
NOTE_WEIGHT_BASE = 1.0
NOTE_WEIGHT_HOLD_FACTOR = 1.25
NOTE_WEIGHT_SWING_FACTOR = 0.5
STATUS_SKIPPED = 0
STATUS_SUCCESSFUL = 1
STATUS_ERROR = 2
STATUS_KEYBOARD_INTERRUPT = 3
STATUS_EXIT = 4
NUMBER_OF_THREAD = 20
FILENAME_SONG_LIST_JSON = 'newsongsjson.txt'
liveDataKeysForNumber = ['time', 'star', 'slider', 'swing', 'swingslider']
liveDataKeysForPositiveNumber = ['time']
liveDataKeysForPosition = ['positionweight', 'positionnote', 'positionslider', 'positionswing', 'positionswingslider']
TITLE_WELCOME = '''# Welcome to LLHelper LLSIF Song Updater!
* Song data based on `%s`.
* Song data cached at `%s`.
* Program originally authored by **Chazeon** and the end of Sept 2017 in NY.
* Updated by **ben1222** at 2018 Dec
* We are now updating song list file: `%s`.
''' % (LLSIF_WIN_API_DOMAIN, LIVE_MAP_LOCAL_CACHE_DIR, FILENAME_SONG_LIST_JSON)
class Note:
def __init__(self, noteDict):
self.level = noteDict['notes_level']
self.type = noteDict['effect']
self.position = noteDict['position']
self.attribute = noteDict['notes_attribute']
self.appearTime = noteDict['timing_sec']
self.effectValue = noteDict['effect_value']
def isHold(self):
return self.type in [NOTE_TYPE_HOLD, NOTE_TYPE_SWING_HOLD]
def isSwing(self):
return self.type in [NOTE_TYPE_SWING, NOTE_TYPE_SWING_EVENT, NOTE_TYPE_SWING_HOLD]
def isStar(self):
return self.type in [NOTE_TYPE_BOMB_1, NOTE_TYPE_BOMB_3, NOTE_TYPE_BOMB_5, NOTE_TYPE_BOMB_9]
def getNoteWeightedValue(self):
weightValue = NOTE_WEIGHT_BASE
if self.isSwing():
weightValue *= NOTE_WEIGHT_SWING_FACTOR
if self.isHold():
weightValue *= NOTE_WEIGHT_HOLD_FACTOR
return weightValue
def getEndTime(self):
if self.isHold():
return float(self.appearTime) + float(self.effectValue)
else:
return float(self.appearTime)
class LiveMap:
def __init__(self, mapData):
self.mapData = mapData
positionWeight = [0.0] * 9
positionNote = [0] * 9
positionSlider = [0] * 9
positionSwing = [0] * 9
positionSwingSlider = [0] * 9
starCount = 0
sliderCount = 0
swingCount = 0
swingSliderCount = 0
endTime = 0.0
for note in mapData:
note = Note(note)
position = 9 - note.position
positionWeight[position] += note.getNoteWeightedValue()
if note.isHold():
if note.isSwing():
positionSwingSlider[position] += 1
swingSliderCount += 1
else:
positionSlider[position] += 1
sliderCount += 1
elif note.isSwing():
positionSwing[position] += 1
swingCount += 1
else:
positionNote[position] += 1
if note.isStar():
starCount += 1
curEndTime = note.getEndTime()
if curEndTime > endTime:
endTime = curEndTime
self.positionWeight = positionWeight
self.positionNote = positionNote
self.positionSlider = positionSlider
self.positionSwing = positionSwing
self.positionSwingSlider = positionSwingSlider
self.starCount = starCount
self.sliderCount = sliderCount
self.swingCount = swingCount
self.swingSliderCount = swingSliderCount
self.endTime = endTime
def updateLiveData(self, liveData):
liveData['positionweight'] = list(map(str, self.positionWeight))
liveData['positionnote'] = list(map(str, self.positionNote))
liveData['positionslider'] = list(map(str, self.positionSlider))
liveData['positionswing'] = list(map(str, self.positionSwing))
liveData['positionswingslider'] = list(map(str, self.positionSwingSlider))
liveData['star'] = str(self.starCount)
liveData['slider'] = str(self.sliderCount)
liveData['swing'] = str(self.swingCount)
liveData['swingslider'] = str(self.swingSliderCount)
liveData['time'] = str(self.endTime)
def getLiveMapJsonUrl(liveId):
if liveId > 1500:
raise Exception('llsif no longer updating new live maps')
else:
return LLSIF_WIN_API_DOMAIN + LLSIF_WIN_API_ENDPOINT + str(liveId)
def getLiveMapJsonPath(liveId):
return LIVE_MAP_LOCAL_CACHE_DIR + str(liveId) + '.json'
def getLiveMap(liveId):
liveJsonPath = getLiveMapJsonPath(liveId)
if os.path.isfile(liveJsonPath):
with open(liveJsonPath, 'r') as f:
liveJson = json.load(f)
else:
liveJsonUrl = getLiveMapJsonUrl(liveId)
# timeout for 10 seconds
liveJsonFp = urlopen(liveJsonUrl, None, 10)
liveJson = json.load(liveJsonFp)
with open(liveJsonPath, 'w') as f:
json.dump(liveJson, f, sort_keys=True)
return LiveMap(liveJson)
def isLiveDataComplete(live):
for positionKey in liveDataKeysForPosition:
if positionKey not in live:
return False
positionValue = live[positionKey]
if len(positionValue) != 9:
return False
if len(str(positionValue[0])) == 0:
return False
for numberKey in liveDataKeysForNumber:
if numberKey not in live:
return False
numberValue = live[numberKey]
if len(str(numberValue)) == 0:
return False
if float(numberValue) <= 0 and (numberKey in liveDataKeysForPositiveNumber):
return False
return True
class positionWeightUpdateThread (threading.Thread):
def __init__(self, liveQueue, messageQueue, printer):
threading.Thread.__init__(self)
self.liveQueue = liveQueue
self.messageQueue = messageQueue
self.printer = printer
def run(self):
while True:
try:
live = self.liveQueue.get(False)
self.messageQueue.put(self.processLive(live))
except Queue.Empty:
self.printer.myPrint('* Queue is empty, exiting... *')
break
except Exception as e:
self.printer.myPrint('* Unknown exception *')
self.printer.myPrint(e)
break
def processLive(self, live):
liveId = int(live['liveid'])
try:
result = getLiveMap(liveId)
self.printer.myPrint('* Successfully processed %d' % (liveId))
return (liveId, STATUS_SUCCESSFUL, result)
except KeyboardInterrupt:
self.printer.myPrint('* User trying to exit program')
return (liveId, STATUS_KEYBOARD_INTERRUPT, None)
except Exception as e:
self.printer.myPrint('* Failed to process %d, json=%s' % (liveId, live['jsonpath']))
self.printer.myPrint(e)
return (liveId, STATUS_ERROR, None)
def interrupt(self, e):
if not self.isAlive():
return
ex = ctypes.py_object(e)
ret = ctypes.pythonapi.PyThreadState_SetAsyncExc(ctypes.c_long(self.ident), ex)
if ret == 0:
self.printer.myPrint('thread already exit')
elif ret > 1:
self.printer.myPrint('Failed to interrupt thread')
def main(threadCount):
songs = {}
prevLoadedLives = []
updatedLives = []
messageQueue = Queue.Queue()
liveQueue = Queue.Queue()
liveQueueCount = 0
totalLiveCount = 0
threads = []
lives = {}
printer = SyncPrinter()
jsonFile = JsonFile(FILENAME_SONG_LIST_JSON, printer)
songs = jsonFile.load()
if not os.path.isdir(LIVE_MAP_LOCAL_CACHE_DIR):
os.makedirs(LIVE_MAP_LOCAL_CACHE_DIR)
for song in songs.values():
for liveSettingId in song['settings'].keys():
totalLiveCount += 1
live = song['settings'][liveSettingId]
liveId = int(live['liveid'])
if not isLiveDataComplete(live):
liveQueue.put(live)
lives[live['liveid']] = live
liveQueueCount += 1
# do not create more thread than number of lives to update
if threadCount > liveQueueCount:
threadCount = liveQueueCount
if threadCount <= 1:
printer.myPrint('* Single thread mode, processing %d lives' % liveQueueCount)
while not liveQueue.empty():
live = liveQueue.get()
liveId = int(live['liveid'])
try:
liveMap = getLiveMap(liveId)
liveMap.updateLiveData(live)
updatedLives.append(liveId)
printer.myPrint('* Successfully processed %d' % (liveId))
except KeyboardInterrupt:
printer.myPrint('* User trying to exit program')
except Exception as e:
printer.myPrint('* Failed to process %d, json=%s' % (liveId, live['jsonpath']))
printer.myPrint(e)
else:
printer.myPrint('* Multi-thread mode, %d threads processing %d lives' % (threadCount, liveQueueCount))
try:
for i in range(threadCount):
newThread = positionWeightUpdateThread(liveQueue, messageQueue, printer)
newThread.start()
threads.append(newThread)
for i in range(liveQueueCount):
liveId, status, result = messageQueue.get()
if status == STATUS_SUCCESSFUL:
updatedLives.append(liveId)
result.updateLiveData(lives[str(liveId)])
except KeyboardInterrupt:
try:
while True:
liveQueue.get(False)
except Queue.Empty:
printer.myPrint('* Cleared queue *')
for curThread in threads:
curThread.interrupt(KeyboardInterrupt)
jsonFile.save(songs)
printer.myPrint('* Successfully processed lives: `%s`.' % (str(updatedLives)))
printer.myPrint('* Updated %s , live count = %d (old: %d, new: %d).' % (FILENAME_SONG_LIST_JSON, totalLiveCount, (totalLiveCount - liveQueueCount), len(updatedLives)))
for curThread in threads:
curThread.join()
if __name__ == '__main__':
print(TITLE_WELCOME)
threadCount = NUMBER_OF_THREAD
if len(sys.argv) > 1:
threadCount = int(sys.argv[1])
main(threadCount)