forked from semyon422/open-ripple
-
Notifications
You must be signed in to change notification settings - Fork 0
/
score.pyx
279 lines (250 loc) · 8.8 KB
/
score.pyx
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
import time
from objects import beatmap
from common import generalUtils
from common.constants import gameModes
from common.log import logUtils as log
from common.ripple import userUtils
from constants import rankedStatuses
from common.ripple import scoreUtils
from objects import glob
from pp import rippoppai
from pp import omppc
from pp import cicciobello
class score:
PP_CALCULATORS = {
gameModes.STD: rippoppai.oppai,
gameModes.TAIKO: rippoppai.oppai,
gameModes.CTB: cicciobello.Cicciobello,
gameModes.MANIA: omppc.Omppc
}
__slots__ = ["scoreID", "playerName", "score", "maxCombo", "c50", "c100", "c300", "cMiss", "cKatu", "cGeki",
"fullCombo", "mods", "playerUserID","rank","date", "hasReplay", "fileMd5", "passed", "playDateTime",
"gameMode", "completed", "accuracy", "pp", "oldPersonalBest", "rankedScoreIncrease"]
def __init__(self, scoreID = None, rank = None, setData = True):
"""
Initialize a (empty) score object.
scoreID -- score ID, used to get score data from db. Optional.
rank -- score rank. Optional
setData -- if True, set score data from db using scoreID. Optional.
"""
self.scoreID = 0
self.playerName = "nospe"
self.score = 0
self.maxCombo = 0
self.c50 = 0
self.c100 = 0
self.c300 = 0
self.cMiss = 0
self.cKatu = 0
self.cGeki = 0
self.fullCombo = False
self.mods = 0
self.playerUserID = 0
self.rank = rank # can be empty string too
self.date = 0
self.hasReplay = 0
self.fileMd5 = None
self.passed = False
self.playDateTime = 0
self.gameMode = 0
self.completed = 0
self.accuracy = 0.00
self.pp = 0.00
self.oldPersonalBest = 0
self.rankedScoreIncrease = 0
if scoreID is not None and setData == True:
self.setDataFromDB(scoreID, rank)
def calculateAccuracy(self):
"""
Calculate and set accuracy for that score
"""
if self.gameMode == 0:
# std
totalPoints = self.c50*50+self.c100*100+self.c300*300
totalHits = self.c300+self.c100+self.c50+self.cMiss
if totalHits == 0:
self.accuracy = 1
else:
self.accuracy = totalPoints/(totalHits*300)
elif self.gameMode == 1:
# taiko
totalPoints = (self.c100*50)+(self.c300*100)
totalHits = self.cMiss+self.c100+self.c300
if totalHits == 0:
self.accuracy = 1
else:
self.accuracy = totalPoints / (totalHits * 100)
elif self.gameMode == 2:
# ctb
fruits = self.c300+self.c100+self.c50
totalFruits = fruits+self.cMiss+self.cKatu
if totalFruits == 0:
self.accuracy = 1
else:
self.accuracy = fruits / totalFruits
elif self.gameMode == 3:
# mania
totalPoints = self.c50*50+self.c100*100+self.cKatu*200+self.c300*300+self.cGeki*300
totalHits = self.cMiss+self.c50+self.c100+self.c300+self.cGeki+self.cKatu
self.accuracy = totalPoints / (totalHits * 300)
else:
# unknown gamemode
self.accuracy = 0
def setRank(self, rank):
"""
Force a score rank
rank -- new score rank
"""
self.rank = rank
def setDataFromDB(self, scoreID, rank = None):
"""
Set this object's score data from db
Sets playerUserID too
scoreID -- score ID
rank -- rank in scoreboard. Optional.
"""
data = glob.db.fetch("SELECT scores.*, users.username FROM scores LEFT JOIN users ON users.id = scores.userid WHERE scores.id = %s LIMIT 1", [scoreID])
if data is not None:
self.setDataFromDict(data, rank)
def setDataFromDict(self, data, rank = None):
"""
Set this object's score data from dictionary
Doesn't set playerUserID
data -- score dictionarty
rank -- rank in scoreboard. Optional.
"""
#print(str(data))
self.scoreID = data["id"]
if "username" in data:
self.playerName = data["username"]
else:
self.playerName = userUtils.getUsername(data["userid"])
self.playerUserID = data["userid"]
self.score = data["score"]
self.maxCombo = data["max_combo"]
self.gameMode = data["play_mode"]
self.c50 = data["50_count"]
self.c100 = data["100_count"]
self.c300 = data["300_count"]
self.cMiss = data["misses_count"]
self.cKatu = data["katus_count"]
self.cGeki = data["gekis_count"]
self.fullCombo = True if data["full_combo"] == 1 else False
self.mods = data["mods"]
self.rank = rank if rank is not None else ""
self.date = data["time"]
self.fileMd5 = data["beatmap_md5"]
self.completed = data["completed"]
#if "pp" in data:
self.pp = data["pp"]
self.calculateAccuracy()
def setDataFromScoreData(self, scoreData):
"""
Set this object's score data from scoreData list (submit modular)
scoreData -- scoreData list
"""
if len(scoreData) >= 16:
self.fileMd5 = scoreData[0]
self.playerName = scoreData[1].strip()
# %s%s%s = scoreData[2]
self.c300 = int(scoreData[3])
self.c100 = int(scoreData[4])
self.c50 = int(scoreData[5])
self.cGeki = int(scoreData[6])
self.cKatu = int(scoreData[7])
self.cMiss = int(scoreData[8])
self.score = int(scoreData[9])
self.maxCombo = int(scoreData[10])
self.fullCombo = True if scoreData[11] == 'True' else False
#self.rank = scoreData[12]
self.mods = int(scoreData[13])
self.passed = True if scoreData[14] == 'True' else False
self.gameMode = int(scoreData[15])
#self.playDateTime = int(scoreData[16])
self.playDateTime = int(time.time())
self.calculateAccuracy()
#osuVersion = scoreData[17]
# Set completed status
self.setCompletedStatus()
def getData(self, pp=False):
"""Return score row relative to this score for getscores"""
return "{}|{}|{}|{}|{}|{}|{}|{}|{}|{}|{}|{}|{}|{}|{}|1\n".format(
self.scoreID,
self.playerName,
int(self.pp) if pp else self.score,
self.maxCombo,
self.c50,
self.c100,
self.c300,
self.cMiss,
self.cKatu,
self.cGeki,
self.fullCombo,
self.mods,
self.playerUserID,
self.rank,
self.date)
def setCompletedStatus(self):
"""
Set this score completed status and rankedScoreIncrease
"""
self.completed = 0
if self.passed == True and scoreUtils.isRankable(self.mods):
# Get userID
userID = userUtils.getID(self.playerName)
# Make sure we don't have another score identical to this one
# TODO: time check
duplicate = glob.db.fetch("SELECT id FROM scores WHERE userid = %s AND beatmap_md5 = %s AND play_mode = %s AND score = %s LIMIT 1", [userID, self.fileMd5, self.gameMode, self.score])
if duplicate is not None:
# Found same score in db. Don't save this score.
self.completed = -1
return
# No duplicates found.
# Get right "completed" value
personalBest = glob.db.fetch("SELECT id,{}score FROM scores WHERE userid = %s AND beatmap_md5 = %s AND play_mode = %s AND completed = 3 LIMIT 1".format(
glob.conf.extra["lets"]["submit"]["score-overwrite"] == "score" and " " or " {}, ".format(glob.conf.extra["lets"]["submit"]["score-overwrite"])
),
[userID, self.fileMd5, self.gameMode])
if personalBest is None:
# This is our first score on this map, so it's our best score
self.completed = 3
self.rankedScoreIncrease = self.score
self.oldPersonalBest = 0
else:
# Compare personal best's score with current score
if getattr(self, glob.conf.extra["lets"]["submit"]["score-overwrite"]) > personalBest[glob.conf.extra["lets"]["submit"]["score-overwrite"]]:
# New best score
self.completed = 3
self.rankedScoreIncrease = self.score-personalBest["score"]
self.oldPersonalBest = personalBest["id"]
else:
self.completed = 2
self.rankedScoreIncrease = 0
self.oldPersonalBest = 0
log.debug("Completed status: {}".format(self.completed))
def saveScoreInDB(self):
"""
Save this score in DB (if passed and mods are valid)
"""
# Add this score
self.setCompletedStatus()
if self.completed >= 2:
query = "INSERT INTO scores (id, beatmap_md5, userid, score, max_combo, full_combo, mods, 300_count, 100_count, 50_count, katus_count, gekis_count, misses_count, time, play_mode, completed, accuracy, pp) VALUES (NULL, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s);"
self.scoreID = int(glob.db.execute(query, [self.fileMd5, userUtils.getID(self.playerName), self.score, self.maxCombo, 1 if self.fullCombo == True else 0, self.mods, self.c300, self.c100, self.c50, self.cKatu, self.cGeki, self.cMiss, self.playDateTime, self.gameMode, self.completed, self.accuracy * 100, self.pp]))
# Set old personal best to completed = 2
if self.oldPersonalBest != 0:
glob.db.execute("UPDATE scores SET completed = 2 WHERE id = %s", [self.oldPersonalBest])
def calculatePP(self, b = None):
"""
Calculate this score's pp value if completed == 3
"""
# Create beatmap object
if b is None:
b = beatmap.beatmap(self.fileMd5, 0)
# Calculate pp
if b.rankedStatus >= rankedStatuses.RANKED and b.rankedStatus != rankedStatuses.UNKNOWN \
and scoreUtils.isRankable(self.mods) and self.passed and self.gameMode in score.PP_CALCULATORS:
calculator = score.PP_CALCULATORS[self.gameMode](b, self)
self.pp = calculator.pp
else:
self.pp = 0