Пример #1
0
    def GET(self):
        web.header("Content-Type", "text/html; charset=utf-8")
        web.header("Access-Control-Allow-Origin", "*")
        query_dict = dict(urlparse.parse_qsl(web.ctx.env['QUERY_STRING']))
        # params:
        # targets, opt
        # cond1, opt
        # cond2, opt
        # format? json, csv?
        db = utils.get_mongo_database()
        targets = query_dict.get('targets', '').split(',')
        if sum(len(t) for t in targets) == 0:
            targets = card_info.card_names()

        # print targets
        def str_card_index(card_name):
            title = card_info.sane_title(card_name)
            if title:
                return str(card_info.card_index(title))
            return ''
        target_inds = map(str_card_index, targets)
        # print targets, target_inds

        cond1 = str_card_index(query_dict.get('cond1', ''))
        cond2 = str_card_index(query_dict.get('cond2', ''))
        
        if cond1 < cond2:
            cond1, cond2 = cond2, cond1

        card_stats = {}
        for target_ind in target_inds:
            key = target_ind + ';'
            if cond1:
                key += cond1
            if cond2:
                key += ',' + cond2

            db_val = db.card_supply.find_one({'_id': key})
            if db_val:
                small_gain_stat = SmallGainStat()
                small_gain_stat.from_primitive_object(db_val['vals'])
                card_name = card_info.card_names()[int(target_ind)]
                card_stats[card_name] = small_gain_stat
        
        format = query_dict.get('format', 'json')
        if format == 'json':
            readable_card_stats = {}
            for card_name, card_stat in card_stats.iteritems():
                readable_card_stats[card_name] = (
                    card_stat.to_readable_primitive_object())
            return json.dumps(readable_card_stats)
        return 'unsupported format ' + format
Пример #2
0
 def update_db(self, mongo_db_inst):
     for event_type_name, stats_dict in self.event_stats.iteritems():
         mongo_collection = mongo_db_inst[event_type_name]
         for full_key, gain_stats_obj in sorted(stats_dict.iteritems()):
             existing_raw_obj = mongo_collection.find_one({'_id': full_key})
             if existing_raw_obj:
                 existing_stat = SmallGainStat()
                 existing_stat.from_primitive_object(
                     existing_raw_obj['vals'])
                 gain_stats_obj.merge(existing_stat)
             key_wrap_obj = {'vals': gain_stats_obj.to_primitive_object()}
             utils.write_object_to_db(key_wrap_obj, mongo_collection,
                                      full_key)
Пример #3
0
 def update_db(self, mongo_db_inst):
     for event_type_name, stats_dict in self.event_stats.iteritems():
         mongo_collection = mongo_db_inst[event_type_name]
         for full_key, gain_stats_obj in sorted(stats_dict.iteritems()):
             existing_raw_obj = mongo_collection.find_one(
                 {'_id': full_key})
             if existing_raw_obj:
                 existing_stat = SmallGainStat()
                 existing_stat.from_primitive_object(
                     existing_raw_obj['vals'])
                 gain_stats_obj.merge(existing_stat)
             key_wrap_obj = {'vals': gain_stats_obj.to_primitive_object()}
             utils.write_object_to_db(key_wrap_obj, mongo_collection,
                                      full_key)
Пример #4
0
 def update_db(self, mongo_db_inst):
     for event_type_name, stats_dict in self.event_stats.iteritems():
         log.debug('Updating database for event type %s, %d stats',
                   event_type_name, len(stats_dict))
         mongo_collection = mongo_db_inst[event_type_name]
         inserts = 0
         updates = 0
         for full_key, gain_stats_obj in sorted(stats_dict.iteritems()):
             existing_raw_obj = mongo_collection.find_one({'_id': full_key})
             if existing_raw_obj:
                 updates += 1
                 existing_stat = SmallGainStat()
                 existing_stat.from_primitive_object(
                     existing_raw_obj['vals'])
                 gain_stats_obj.merge(existing_stat)
             else:
                 inserts += 1
             key_wrap_obj = {'vals': gain_stats_obj.to_primitive_object()}
             utils.write_object_to_db(key_wrap_obj, mongo_collection,
                                      full_key)
         log.debug('Database update results for %s: %d inserts, %d updates',
                   event_type_name, inserts, updates)
Пример #5
0
 def fetch_conditional_stats(self, target_inds, interaction_tuples):
     db = utils.get_mongo_database()
     card_stats = []
     count_searched = 0
     for target_ind in target_inds:
         for interaction_tuple in interaction_tuples:
             count_searched += 1
             if count_searched > 1000:
                 return card_stats
             key = target_ind + ';' + (','.join(interaction_tuple))
             db_val = db.card_supply.find_one({'_id': key})
             if db_val:
                 small_gain_stat = SmallGainStat()
                 small_gain_stat.from_primitive_object(db_val['vals'])
                 def name_getter(ind_str):
                     return card_info.card_names()[int(ind_str)]
                 card_name = name_getter(int(target_ind))
                 condition = map(name_getter, interaction_tuple)
                 stat_with_context = {'card_name': card_name,
                                      'condition': condition,
                                      'stats': small_gain_stat}
                 card_stats.append(stat_with_context)
     return card_stats
Пример #6
0
 def fetch_conditional_stats(self, target_inds, interaction_tuples):
     db = utils.get_mongo_database()
     card_stats = []
     count_searched = 0
     for target_ind in target_inds:
         for interaction_tuple in interaction_tuples:
             count_searched += 1
             if count_searched > 1000:
                 return card_stats
             key = target_ind + ';' + (','.join(interaction_tuple))
             db_val = db.card_supply.find_one({'_id': key})
             if db_val:
                 small_gain_stat = SmallGainStat()
                 small_gain_stat.from_primitive_object(db_val['vals'])
                 def name_getter(ind_str):
                     return dominioncards.index_to_card(int(ind_str)).singular
                 card_name = name_getter(target_ind)
                 condition = map(name_getter, interaction_tuple)
                 stat_with_context = {'card_name': card_name,
                                      'condition': condition,
                                      'stats': small_gain_stat}
                 card_stats.append(stat_with_context)
     return card_stats
Пример #7
0
 def update_db(self, mongo_db_inst):
     for event_type_name, stats_dict in self.event_stats.iteritems():
         log.debug('Updating database for event type %s, %d stats',
                   event_type_name, len(stats_dict))
         mongo_collection = mongo_db_inst[event_type_name]
         inserts = 0
         updates = 0
         for full_key, gain_stats_obj in sorted(stats_dict.iteritems()):
             existing_raw_obj = mongo_collection.find_one(
                 {'_id': full_key})
             if existing_raw_obj:
                 updates += 1
                 existing_stat = SmallGainStat()
                 existing_stat.from_primitive_object(
                     existing_raw_obj['vals'])
                 gain_stats_obj.merge(existing_stat)
             else:
                 inserts += 1
             key_wrap_obj = {'vals': gain_stats_obj.to_primitive_object()}
             utils.write_object_to_db(key_wrap_obj, mongo_collection,
                                      full_key)
         log.debug('Database update results for %s: %d inserts, %d updates',
                   event_type_name, inserts, updates)
Пример #8
0
def accumulate_card_stats(games_stream, stats_accumulator, max_games=-1):
    ct = 0
    for game in games_stream:
        detected_events = detect_events(game)

        per_player_accum = game.cards_accumalated_per_player().iteritems()
        for player, accum_dict in per_player_accum:
            avail = analysis_util.available_cards(game, accum_dict.keys())
            win_points = game.get_player_deck(player).WinPoints()
            for card in avail:
                count = accum_dict.get(card, 0)
                small_gain_stat = SmallGainStat()
                if count:
                    small_gain_stat.win_given_any_gain.add_outcome(win_points)
                else:
                    small_gain_stat.win_given_no_gain.add_outcome(win_points)
                small_gain_stat.win_weighted_gain.add_many_outcomes(
                    win_points, count)
                card_index = str(card_info.card_index(card))
                stats_accumulator.merge_stats(detected_events, card_index,
                                              small_gain_stat)
        max_games -= 1
        if max_games == 0:
            break