def leaderboard(msg, group, day: str = 'today') -> str: ''' ''' if day is 'today': day = formatToday() # build shades shades = buildShades(group, day) bs = getBiggestShade(group, shades) ls = getLongestShade(group, shades) # init initUserScore(group) # basic addBasicScore(group) # bs/ls bonus bs_participants = getShadeParticipants(group, bs[0]) for u in getShadeParticipants(group, bs[0]): u[0].bonus_score += bs[1] / \ len(bs_participants) * (1 - (random.random()/len(bs_participants))) ls_participants = getShadeParticipants(group, ls[0]) for u in getShadeParticipants(group, ls[0]): u[0].bonus_score += len(ls[0]) / \ len(ls_participants) * (1 - (random.random()/len(ls_participants))) # participation bonus addBonusScore(bs_participants) addBonusScore(ls_participants) return parse(group, day)
def buildLattice(day: str='today') -> list: ''' :param day: like `20180705` ''' if day is 'today': day = formatToday() start = getStartSecOfTheDay(day) n_lattice = int((time.time() - start) / 300) return [start + i*300 for i in range(n_lattice)]
def getSecByLattice(lattice: int, day: str='today') -> float: ''' :param lattice: `int`, range(288) ''' if lattice in range(288): if day is 'today': day = formatToday() start = getStartSecOfTheDay(day) return start + lattice*300
def countUserShowUps(user, day: str='today') -> list: ''' Return a `list` of all the lattices a user has appeared ''' if day is 'today': day = formatToday() user.buildUserCorpus(day) lattices = buildLattice(day) # :key: `timestamp` user.show_ups = [lattices.index( [i for i in lattices if i < key][-1]) for key in user.corpus_keys] return user.show_ups
def buildUserCorpus(self, day: str = 'today'): ''' Must has `uuid` Manually zip(self.corpus_keys, self.corpus_values) when using :param day: like `20180705` ''' if day is 'today': day = formatToday() if not hasattr(self, 'uuid'): try: self.group.getUserUUID(self) except AttributeError: raise theGroupNotDynamicallyLoaded() self.corpus_keys = [ float(key) for key in tuple(self.r.lrange(f'{self.uuid}{day}', 0, -1)) ] self.corpus_values = (self.r.hget(self.group.uuid, key) for key in self.corpus_keys)
def saveOnDisk(msg): ''' Deprecated. Just for backward compatible TODO data migration ''' group = theGroup(msg) bot = msg.bot def getNamePuidDict(): bot.enable_puid() name_list = [m.nick_name for m in group.members] puid_list = [m.puid for m in group.members] return dict(zip(name_list, puid_list)) def getUserByPuid(puid: str) -> str: return tuple(getNamePuidDict())[list( getNamePuidDict().values()).index(puid)] hoy = formatToday() def genMsgId() -> str: with open('db', 'r') as f: return str(len(f.readlines()) + 1) serial = genMsgId() content = {} content['serial'] = serial content['user'] = getUserByPuid(msg.member.puid) content['date'] = hoy content['time'] = msg.create_time.timestamp() content['type'] = msg.type content['text'] = msg.text msg_name = 'msg{}_{}'.format(hoy, serial) with open('db', 'a') as f: f.write(msg_name + ' = ' + str(content) + '\n') print(msg_name + ' saved on disk.') return True
def getHistoryGroupName(cls, msg, group, day): raw = group.r.hgetall('group_name_history') content = [(formatToday(timestamp=float(i[0])), i[1]) for i in raw.items()] return '\n'.join([' '.join(i) for i in content])
def parseDate(self, msg): match = re.search(r'20\d\d\d\d\d\d', msg.text) if not hasattr(match, 'group'): return formatToday() else: return match.group()
def getStartSecOfTheDay(day: str='today') -> float: if day is 'today': day = formatToday() tup = (int(day[:4]), int(day[4:6]), int(day[6:8]), 0, 0, 0, 0, 0, 0) return time.mktime(tup)