def testToughieChallengeDates(self): self.assertEqual(toughies_challenge_date(date(2020, 12, 9)), date(2020, 12, 8)) # wed, tues self.assertEqual(toughies_challenge_date(date(2020, 12, 8)), date(2020, 12, 8)) # tues, tues self.assertEqual(toughies_challenge_date(date(2012, 1, 9)), date(2012, 1, 3)) # mon, prev tuesday self.assertEqual(toughies_challenge_date(date(2012, 1, 7)), date(2012, 1, 3)) # sat, prev tues self.assertEqual(toughies_challenge_date(date(2012, 1, 2)), date(2011, 12, 27)) # mon, prev tuesday
def handle(self, *args, **options): today = date.today() toughies = DailyChallengeName.objects.get( name=DailyChallengeName.WEEKS_BINGO_TOUGHIES) blankies = DailyChallengeName.objects.get( name=DailyChallengeName.BLANK_BINGOS) marathons = DailyChallengeName.objects.get( name=DailyChallengeName.BINGO_MARATHON) lbs = DailyChallengeLeaderboard.objects.filter( medalsAwarded=False, challenge__date__lt=today) for lb in lbs: award = True if lb.challenge.name == toughies: chDate = toughies_challenge_date(today) # Toughies challenge still ongoing if chDate == lb.challenge.date: award = False if award: lbes = DailyChallengeLeaderboardEntry.objects.filter( board=lb, qualifyForAward=True) if len(lbes) < 8: lb.medalsAwarded = True lb.save() continue # do not award lbes = sorted(lbes, cmp=sort_cmp) if lb.challenge.name == toughies: # Award extra medal. medals = ['Platinum', 'Gold', 'Silver', 'Bronze'] elif lb.challenge.name in (blankies, marathons): medals = ['GoldStar', 'Gold', 'Silver', 'Bronze'] else: medals = ['Gold', 'Silver', 'Bronze'] for i in range(len(medals)): try: lbes[i].additionalData = json.dumps( {'medal': medals[i]}) except IndexError: break lbes[i].save() profile = lbes[i].user.aerolithprofile try: userMedals = json.loads(profile.wordwallsMedals) except (TypeError, ValueError): userMedals = {'medals': {}} if 'medals' not in userMedals: userMedals = {'medals': {}} if medals[i] in userMedals['medals']: userMedals['medals'][medals[i]] += 1 else: userMedals['medals'][medals[i]] = 1 profile.wordwallsMedals = json.dumps(userMedals) profile.save() print profile.user.username, profile.wordwallsMedals print 'awarded medals', lb lb.medalsAwarded = True lb.save()
def get_leaderboard_data(lex, chName, challengeDate, tiebreaker): if chName.name == DailyChallengeName.WEEKS_BINGO_TOUGHIES: chdate = toughies_challenge_date(challengeDate) else: chdate = challengeDate try: dc = DailyChallenge.objects.get(lexicon=lex, date=chdate, name=chName) except DailyChallenge.DoesNotExist: return None # daily challenge doesn't exist return get_leaderboard_data_for_dc_instance(dc, tiebreaker)
def getLeaderboardData(lex, chName, challengeDate): if chName.name == DailyChallengeName.WEEKS_BINGO_TOUGHIES: chdate = toughies_challenge_date(challengeDate) else: chdate = challengeDate try: dc = DailyChallenge.objects.get(lexicon=lex, date=chdate, name=chName) except DailyChallenge.DoesNotExist: return None # daily challenge doesn't exist return getLeaderboardDataDcInstance(dc)
def challenges_played(request): """ Get the challenges for the given day, played by the logged-in user. """ lex = request.GET.get("lexicon") ch_date = date_from_request_dict(request.GET) try: lex = Lexicon.objects.get(pk=lex) except Lexicon.DoesNotExist: return bad_request("Bad lexicon.") challenges = DailyChallenge.objects.filter(date=ch_date, lexicon=lex) entries = DailyChallengeLeaderboardEntry.objects.filter( board__challenge__in=challenges, user=request.user ) resp = [] for entry in entries: resp.append({"challengeID": entry.board.challenge.name.pk}) # Search for toughies challenge as well. toughies_date = toughies_challenge_date(ch_date) try: relevant_toughie = DailyChallenge.objects.get( date=toughies_date, lexicon=lex, name__name=DailyChallengeName.WEEKS_BINGO_TOUGHIES, ) except DailyChallenge.DoesNotExist: return response(resp) # If the toughies date is not the date in question, we need to see if # the user has an entry for this challenge. if toughies_date != ch_date: try: entry = DailyChallengeLeaderboardEntry.objects.get( board__challenge=relevant_toughie, user=request.user ) except DailyChallengeLeaderboardEntry.DoesNotExist: return response(resp) resp.append({"challengeID": entry.board.challenge.name.pk}) return response(resp)
def initialize_daily_challenge(self, user, ch_lex, ch_name, ch_date, use_table=None): """ Initializes a WordwallsGame daily challenge. """ # Does a daily challenge exist with this name and date? # If not, create it. today = timezone.localtime(timezone.now()).date() qualify_for_award = False if ch_name.name == DailyChallengeName.WEEKS_BINGO_TOUGHIES: # Repeat on Tuesday at midnight local time (ie beginning of # the day, 0:00) Tuesday is an isoweekday of 2. Find the # nearest Tuesday back in time. isoweekday goes from 1 to 7. ch_date = toughies_challenge_date(ch_date) if ch_date == toughies_challenge_date(today): qualify_for_award = True # otherwise, it's not a 'bingo toughies', but a regular challenge. else: if ch_date == today: qualify_for_award = True ret = self.get_or_create_dc(ch_date, ch_lex, ch_name) if ret is None: if ch_name.name == DailyChallengeName.WEEKS_BINGO_TOUGHIES: raise GameInitException( "Unable to create Toughies challenge. If this is a new " "lexicon, please wait until Tuesday for new words to be " "available.") raise GameInitException( "Unable to create daily challenge {0}".format(ch_name)) qs, secs, dc = ret list_category = WordList.CATEGORY_ANAGRAM if dc.category == DailyChallenge.CATEGORY_BUILD: list_category = WordList.CATEGORY_BUILD wl = self.initialize_word_list(qs, ch_lex, user, list_category) visible_name = dc.visible_name if not visible_name: visible_name = dc.name.name temp_list_name = "{0} {1} - {2}".format(ch_lex.lexiconName, visible_name, ch_date.strftime("%Y-%m-%d")) wgm = self.create_or_update_game_instance( user, ch_lex, wl, use_table, False, # Extra parameters to be put in 'state' gameType="challenge", questionsToPull=qs.size(), challengeId=dc.pk, timerSecs=secs, temp_list_name=temp_list_name, qualifyForAward=qualify_for_award, ) return wgm.pk # the table number