Esempio n. 1
0
    def match_pair(cls, game, pair_1, pair_2):
        """Match a pair and update game state"""
        game.attempts += 1
        game.put()
        card_1 = Card.query(Card.game == game.key).filter(Card.index == pair_1).get()
        card_2 = Card.query(Card.game == game.key).filter(Card.index == pair_2).get()

        if card_1.matched or card_2.matched:
            raise RuntimeError('Could not rematch a matched card')

        form = MatchResultForm()
        if card_1.value == card_2.value:
            card_1.matched = True
            card_1.put()
            card_2.matched = True
            card_2.put()
            game.matched += 2
            game.put()
            form.message = 'Success'
        else:
            form.message = 'Fail'

        # Construct return info form
        form.card_1 = card_1.to_form()
        form.card_2 = card_2.to_form()
        form.matched_count = game.matched

        if game.matched == 52:
            game.game_over = True
            game.put()
            Card.delete_cards_for_game(game)

            # Update average attempts of user
            user = game.user.get()
            games = Game.get_user_finished_games(user)
            count = len(games)
            if user.average_attempts == float('inf'):
                user.average_attempts = 0
            user.average_attempts = ((count - 1) * user.average_attempts + game.attempts) / count
            user.put()

            score = Score(user=game.user, date=date.today(), attempts=game.attempts)
            score.put()
            form.message = 'Win'

        # Create history log
        History.create_history(game=game,
                               card_1=card_1,
                               card_2=card_2,
                               message=form.message)
        return form
Esempio n. 2
0
 def end_game(self, won=False):
     """Ends the game - if won is True, the player won. - if won is False,
     the player lost. Creates a Score object and stores it in the datastore.
     Args:
        won: Indicates whether the player wins or loses.
     """
     self.game_over = True
     self.put()
     # Add the game to the score 'board'
     score = Score(
             user=self.user, date=date.today(), won=won,
             attempts_used=self.attempts - self.attempts_remaining,
             attempts=self.attempts)
     score.put()
Esempio n. 3
0
 def end_game(self, won=False):
     """Ends the game - if won is True, the player won. - if won is False,
     the player lost. Creates a Score object and stores it in the datastore.
     Args:
        won: Indicates whether the player wins or loses.
     """
     self.game_over = True
     self.put()
     # Add the game to the score 'board'
     score = Score(user=self.user,
                   date=date.today(),
                   won=won,
                   attempts_used=self.attempts - self.attempts_remaining,
                   attempts=self.attempts)
     score.put()
Esempio n. 4
0
    def _save_scores(self, week, scores):
        query = Score.all()
        scorebox = {}
        result = {}

        query.filter('week =', week)
        result = query.fetch(25)

        if len(result) <= 0:
            # Completely new save
            for game in scores:
                scorebox = Score(
                    year = self.get_season_year(),
                    week = week,
                    away_name = game[AWAY_NAME].encode('ascii', 'ignore'),
                    away_score = int(game[AWAY_SCORE]),
                    game_clock = str(game[GAME_CLOCK]),
                    game_day = game[GAME_DAY].encode('ascii', 'ignore'),
                    game_id = int(game[GAME_ID]),
                    game_status = game[GAME_STATUS],
                    game_time = game[GAME_TIME],
                    home_name = game[HOME_NAME].encode('ascii', 'ignore'),
                    home_score = int(game[HOME_SCORE]),
                    timestamp = datetime.datetime.now()
                    )

                scorebox.put()
        else:
            current = {}
            for scorebox in result:
                # Find the related game score
                for game in scores:
                    if game[AWAY_NAME] == scorebox.away_name:
                        current = game
                        break

                key = scorebox.key()
                matchup = Score.get(key)

                # Update
                matchup.away_score = int(current[AWAY_SCORE])
                matchup.home_score = int(current[HOME_SCORE])
                matchup.game_clock = str(current[GAME_CLOCK])
                matchup.game_status = current[GAME_STATUS]
                matchup.timestamp = datetime.datetime.now()
                
                #Push update
                matchup.put()
Esempio n. 5
0
    def _save_scores(self, week, scores):
        query = Score.all()
        scorebox = {}
        result = {}

        query.filter('week =', week)
        result = query.fetch(25)

        if len(result) <= 0:
            # Completely new save
            for game in scores:
                scorebox = Score(
                    year=self.get_season_year(),
                    week=week,
                    away_name=game[AWAY_NAME].encode('ascii', 'ignore'),
                    away_score=int(game[AWAY_SCORE]),
                    game_clock=str(game[GAME_CLOCK]),
                    game_day=game[GAME_DAY].encode('ascii', 'ignore'),
                    game_id=int(game[GAME_ID]),
                    game_status=game[GAME_STATUS],
                    game_time=game[GAME_TIME],
                    home_name=game[HOME_NAME].encode('ascii', 'ignore'),
                    home_score=int(game[HOME_SCORE]),
                    timestamp=datetime.datetime.now())

                scorebox.put()
        else:
            current = {}
            for scorebox in result:
                # Find the related game score
                for game in scores:
                    if game[AWAY_NAME] == scorebox.away_name:
                        current = game
                        break

                key = scorebox.key()
                matchup = Score.get(key)

                # Update
                matchup.away_score = int(current[AWAY_SCORE])
                matchup.home_score = int(current[HOME_SCORE])
                matchup.game_clock = str(current[GAME_CLOCK])
                matchup.game_status = current[GAME_STATUS]
                matchup.timestamp = datetime.datetime.now()

                #Push update
                matchup.put()
Esempio n. 6
0
 def get(self):
     
     game_id = int(self.request.get('game_id'))
     load_game = self.request.get('load_game')
     
     jplayers = self.request.get('jplayers')
     
     logging.info("jplayers id is |%s|", jplayers)
     
     players = json.loads(jplayers) if jplayers != 'None' else []
         
     scores = []
     
     if load_game == "true":
         scores = Score.query(Score.game_id == game_id).fetch()
     else:
         for player in players:
             score = Score(player_name=player, game_id=game_id, score=int(self.request.get(player)))
             score.put()
             scores.append(score)
             
     final = True
     for score in scores:
         if score.score == None:
             final = False
     
     if final:
         game = get_game(int(game_id))
         
         owings = calculate_owings(scores, game)
         
         payments = calculate_payments_from_owings(owings, game)
     else:
         owings = None
         payments = None
     
     context = {
         'game_id': game_id,
         'scores': scores,
         'players': players,
         'payments': payments
     }
     
     template = JINJA_ENVIRONMENT.get_template('templates/payments.html')
     self.response.write(template.render(context))
Esempio n. 7
0
    def end_game(self, won=False):
        """Ends the game.

        Args:
            won: if won is True, the player won, else the player lost."""
        self.game_over = True

        user = self.user.get()
        if won:
            user.games_won += 1

        # Add the game to the score 'board'
        score = Score(user=self.user,
                      date=date.today(),
                      won=won,
                      harvest=self.plant.get().flowers)

        score.put()
        user.put()
        self.put()
Esempio n. 8
0
    def end_game(self, won=False):
        """Ends the game.

        Args:
            won: if won is True, the player won, else the player lost."""
        self.game_over = True

        user = self.user.get()
        if won:
            user.games_won += 1

        # Add the game to the score 'board'
        score = Score(user=self.user,
                      date=date.today(),
                      won=won,
                      harvest=self.plant.get().flowers)

        score.put()
        user.put()
        self.put()
Esempio n. 9
0
    def _save_scores(self, week, scores):
        '''
        Side-Effect: Appends a game's margin & odds
        '''
        current = {}
        _game_status = ''
        key = None
        matchup = None
        query = Score.all()
        query_select = {}
        scorebox = {}
        right_now = (datetime.datetime.now() - 
                datetime.timedelta(hours=constants.UTC_OFFSET))
        
        query.filter('week =', week)
        query_select = query.fetch(constants.TOTAL_TEAMS)

        if len(query_select) == 0:
            # Completely new save
            for game in scores:
                _game_status = game[constants.GAME_STATUS]
                if _game_status == 'final overtime':
                    # Workaround for formatting regarding overtime games
                    _game_status = 'Final Overtime'
                
                scorebox = Score(
                    year = int(game[constants.GAME_SEASON]),
                    week = int(week),
                    away_name = game[constants.AWAY_NAME].encode('ascii',
                                                                    'ignore'),
                    away_score = int(game[constants.AWAY_SCORE]),
                    game_clock = str(game[constants.GAME_CLOCK]),
                    game_day = game[constants.GAME_DAY].encode('ascii',
                                                                    'ignore'),
                    game_id = int(game[constants.GAME_ID]),
                    game_status = _game_status,
                    game_time = game[constants.GAME_TIME],
                    home_name = game[constants.HOME_NAME].encode('ascii',
                                                                    'ignore'),
                    home_score = int(game[constants.HOME_SCORE]),
                    spread_margin = float(game[constants.GAME_SPREAD_MARGIN]),
                    spread_odds = float(game[constants.GAME_SPREAD_ODDS]),
                    timestamp = right_now
                    )
                
                scorebox.put()
        else:
            # Update the scores
            for scorebox in query_select:
                # Find the related game score
                for game in scores:
                    if game[constants.AWAY_NAME] == scorebox.away_name:
                        current = game
                        break

                key = scorebox.key()
                matchup = Score.get(key)
                
                # Update
                matchup.away_score = int(game[constants.AWAY_SCORE])
                matchup.game_clock = str(game[constants.GAME_CLOCK])
                matchup.game_status = game[constants.GAME_STATUS]
                matchup.home_score = int(game[constants.HOME_SCORE])
                matchup.timestamp = right_now
                
                # Pull margin & odds data while we have the data
                game[constants.GAME_SPREAD_MARGIN] = matchup.spread_margin
                game[constants.GAME_SPREAD_ODDS] = matchup.spread_odds
                
                # Push update
                matchup.put()
Esempio n. 10
0
    def _save_scores(self, week, scores):
        '''
        Side-Effect: Appends a game's margin & odds
        '''
        current = {}
        _game_status = ''
        key = None
        matchup = None
        query = Score.all()
        query_select = {}
        scorebox = {}
        right_now = (datetime.datetime.now() -
                     datetime.timedelta(hours=constants.UTC_OFFSET))

        query.filter('week =', week)
        query_select = query.fetch(constants.TOTAL_TEAMS)

        if len(query_select) == 0:
            # Completely new save
            for game in scores:
                _game_status = game[constants.GAME_STATUS]
                if _game_status == 'final overtime':
                    # Workaround for formatting regarding overtime games
                    _game_status = 'Final Overtime'

                scorebox = Score(
                    year=int(game[constants.GAME_SEASON]),
                    week=int(week),
                    away_name=game[constants.AWAY_NAME].encode(
                        'ascii', 'ignore'),
                    away_score=int(game[constants.AWAY_SCORE]),
                    game_clock=str(game[constants.GAME_CLOCK]),
                    game_day=game[constants.GAME_DAY].encode(
                        'ascii', 'ignore'),
                    game_id=int(game[constants.GAME_ID]),
                    game_status=_game_status,
                    game_time=game[constants.GAME_TIME],
                    home_name=game[constants.HOME_NAME].encode(
                        'ascii', 'ignore'),
                    home_score=int(game[constants.HOME_SCORE]),
                    spread_margin=float(game[constants.GAME_SPREAD_MARGIN]),
                    spread_odds=float(game[constants.GAME_SPREAD_ODDS]),
                    timestamp=right_now)

                scorebox.put()
        else:
            # Update the scores
            for scorebox in query_select:
                # Find the related game score
                for game in scores:
                    if game[constants.AWAY_NAME] == scorebox.away_name:
                        current = game
                        break

                key = scorebox.key()
                matchup = Score.get(key)

                # Update
                matchup.away_score = int(game[constants.AWAY_SCORE])
                matchup.game_clock = str(game[constants.GAME_CLOCK])
                matchup.game_status = game[constants.GAME_STATUS]
                matchup.home_score = int(game[constants.HOME_SCORE])
                matchup.timestamp = right_now

                # Pull margin & odds data while we have the data
                game[constants.GAME_SPREAD_MARGIN] = matchup.spread_margin
                game[constants.GAME_SPREAD_ODDS] = matchup.spread_odds

                # Push update
                matchup.put()