def create_challenge(self, name, lexicon, dt, numbertoselect, f, filetype):
     ch = DailyChallenge(lexicon=lexicon,
                         date=dt,
                         name=name,
                         seconds=name.timeSecs)
     if filetype == 'words':
         alpha_set = get_alphas_from_words(f)
         pk_list, msg = get_pks_from_alphas(alpha_set, lexicon.pk)
         random.shuffle(pk_list)
     elif filetype == 'pks':
         pk_list = []
         for line in f:
             try:
                 Alphagram.objects.get(pk=line)
             except Alphagram.DoesNotExist:
                 raise CommandError("Alphagram %s does not exist!" % line)
             pk_list.append(int(line))
         random.shuffle(pk_list)
     ch.alphagrams = json.dumps(pk_list[:numbertoselect])
     print "Got alphagrams, trying to save..."
     try:
         ch.save()
     except IntegrityError:
         raise CommandError("There was an error saving the challenge, "
                            "perhaps a duplicate entry?")
Esempio n. 2
0
 def initializeByDailyChallenge(self, user, challengeLex, challengeName):
     # does a daily challenge exist with this name and the current date? if not, create it.
     datenow = date.today()
     try:
         dc = DailyChallenge.objects.get(date=datenow, lexicon=challengeLex, name=challengeName)
         # pull out its indices
         pkIndices = []
         alphaPks = json.loads(dc.alphagrams)
         for alpha in alphaPks:
             pkIndices.append(alpha)                
         secs = dc.seconds
         random.shuffle(pkIndices)
     except:
         # does not exist!
         ret = self.generateDailyChallengePks(challengeName, challengeLex)
         if ret:
             pkIndices, secs = ret
             dc = DailyChallenge(date=datenow, lexicon=challengeLex, name=challengeName, 
                     seconds=secs, alphagrams=json.dumps(pkIndices))
             
             dc.save()
         else:
             return 0
     
     wgm = self.createGameModelInstance(user, GenericTableGameModel.SINGLEPLAYER_GAME, challengeLex, 
                                         len(pkIndices),
                                         json.dumps(pkIndices), 
                                         len(pkIndices),
                                         json.dumps(range(len(pkIndices))), 
                                         0,
                                         json.dumps([]), 
                                         0,
                                         json.dumps([]), 
                                         gameType='challenge',
                                         challengeId=dc.pk,
                                         timerSecs=secs)
     
     wgm.save()
     wgm.inTable.add(user)
     wgm.playing.add(user)
           
     return wgm.pk   # the table number
 def create_challenge(self, name, lexicon, dt, numbertoselect, f, filetype):
     ch = DailyChallenge(lexicon=lexicon, date=dt, name=name,
                         seconds=name.timeSecs)
     if filetype == 'words':
         alpha_set = get_alphas_from_words(f)
         pk_list, msg = get_pks_from_alphas(alpha_set, lexicon.pk)
         random.shuffle(pk_list)
     elif filetype == 'pks':
         pk_list = []
         for line in f:
             try:
                 Alphagram.objects.get(pk=line)
             except Alphagram.DoesNotExist:
                 raise CommandError("Alphagram %s does not exist!" % line)
             pk_list.append(int(line))
         random.shuffle(pk_list)
     ch.alphagrams = json.dumps(pk_list[:numbertoselect])
     print "Got alphagrams, trying to save..."
     try:
         ch.save()
     except IntegrityError:
         raise CommandError("There was an error saving the challenge, "
                            "perhaps a duplicate entry?")
Esempio n. 4
0
    def get_or_create_dc(self, ch_date, ch_lex, ch_name):
        """
        Get, or create, a daily challenge with the given parameters.

        """
        try:
            qs, secs, dc = self.get_dc(ch_date, ch_lex, ch_name)
        except DailyChallenge.DoesNotExist:
            ret = generate_dc_questions(ch_name, ch_lex, ch_date)
            if not ret:
                return None

            qs, secs = ret
            if qs.size() == 0:
                logger.info("Empty questions.")
                return None
            ch_category = DailyChallenge.CATEGORY_ANAGRAM
            if qs.build_mode:
                ch_category = DailyChallenge.CATEGORY_BUILD
            dc = DailyChallenge(
                date=ch_date,
                lexicon=ch_lex,
                name=ch_name,
                seconds=secs,
                alphagrams=qs.to_json(),
                category=ch_category,
            )
            try:
                dc.save()
            except IntegrityError:
                logger.exception("Caught integrity error")
                # This happens rarely if the DC gets generated twice
                # in very close proximity.
                qs, secs, dc = self.get_dc(ch_date, ch_lex, ch_name)

        return qs, secs, dc
Esempio n. 5
0
    def initializeByDailyChallenge(self, user, challengeLex, challengeName,
                                   challengeDate):
        # Does a daily challenge exist with this name and the current
        # date? If not, create it.
        today = date.today()
        qualifyForAward = False
        if challengeName.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.
            from wordwalls.management.commands.genMissedBingoChalls import (
                challengeDateFromReqDate)
            chDate = challengeDateFromReqDate(challengeDate)
            if chDate == challengeDateFromReqDate(today):
                qualifyForAward = True
        # otherwise, it's not a 'bingo toughies', but a regular challenge.
        else:
            chDate = challengeDate
            if chDate == today:
                qualifyForAward = True

        try:
            qs, secs, dc = self.getDc(chDate, challengeLex, challengeName)
        except DailyChallenge.DoesNotExist:
            # does not exist!
            try:
                ret = self.generateDailyChallengePks(challengeName,
                                                     challengeLex, chDate)
            except IOError:
                return 0
            if ret:
                qs, secs = ret
                dc = DailyChallenge(date=chDate,
                                    lexicon=challengeLex,
                                    name=challengeName,
                                    seconds=secs,
                                    alphagrams=json.dumps(qs))
                try:
                    dc.save()
                except IntegrityError:
                    logger.exception("Caught integrity error")
                    # This happens rarely if the DC gets generated twice
                    # in very close proximity.
                    qs, secs, dc = self.getDc(chDate, challengeLex,
                                              challengeName)
            else:
                return 0
        num_questions = len(qs)
        wgm = self.createGameModelInstance(
            user,
            GenericTableGameModel.SINGLEPLAYER_GAME,
            challengeLex,
            num_questions,
            json.dumps(qs),
            num_questions,
            json.dumps(range(num_questions)),
            0,
            json.dumps([]),
            0,
            json.dumps([]),
            gameType='challenge',
            challengeId=dc.pk,
            timerSecs=secs,
            qualifyForAward=qualifyForAward,
            questionsToPull=num_questions)
        wgm.save()
        wgm.inTable.add(user)
        return wgm.pk  # the table number
Esempio n. 6
0
    def initializeByDailyChallenge(self, user, challengeLex, challengeName,
                                   challengeDate):
        # Does a daily challenge exist with this name and the current
        # date? If not, create it.
        today = date.today()
        qualifyForAward = False
        if challengeName.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.
            from wordwalls.management.commands.genMissedBingoChalls import (
                challengeDateFromReqDate)
            chDate = challengeDateFromReqDate(challengeDate)
            if chDate == challengeDateFromReqDate(today):
                qualifyForAward = True
        # otherwise, it's not a 'bingo toughies', but a regular challenge.
        else:
            chDate = challengeDate
            if chDate == today:
                qualifyForAward = True

        try:
            qs, secs, dc = self.getDc(
                chDate, challengeLex, challengeName)
        except DailyChallenge.DoesNotExist:
            # does not exist!
            try:
                ret = self.generateDailyChallengePks(challengeName,
                                                     challengeLex,
                                                     chDate)
            except IOError:
                return 0
            if ret:
                qs, secs = ret
                dc = DailyChallenge(date=chDate, lexicon=challengeLex,
                                    name=challengeName, seconds=secs,
                                    alphagrams=json.dumps(qs))
                try:
                    dc.save()
                except IntegrityError:
                    logger.exception("Caught integrity error")
                    # This happens rarely if the DC gets generated twice
                    # in very close proximity.
                    qs, secs, dc = self.getDc(chDate, challengeLex,
                                              challengeName)
            else:
                return 0
        num_questions = len(qs)
        wgm = self.createGameModelInstance(
            user, GenericTableGameModel.SINGLEPLAYER_GAME, challengeLex,
            num_questions,
            json.dumps(qs),
            num_questions,
            json.dumps(range(num_questions)),
            0,
            json.dumps([]),
            0,
            json.dumps([]),
            gameType='challenge',
            challengeId=dc.pk,
            timerSecs=secs,
            qualifyForAward=qualifyForAward,
            questionsToPull=num_questions)
        wgm.save()
        wgm.inTable.add(user)
        return wgm.pk   # the table number