Exemple #1
0
    def _sankey_other_cards(self, cutoff_threshold=.1):
        data = {i: {"total_observations": 0} for i in range(0, 11)}
        db = card_db()
        other_cards = set()
        all_observations = 0
        for data_point in self.data_points:
            cards = data_point["cards"]
            for dbf_id, count in cards.items():
                card = db[int(dbf_id)]
                card_cost = min(10, card.cost)
                all_observations += data_point["observations"]
                data[card_cost]["total_observations"] += data_point[
                    "observations"]

                if dbf_id not in data[card_cost]:
                    data[card_cost][dbf_id] = 0

                data[card_cost][dbf_id] += data_point["observations"]

        if all_observations > 0:
            for mana_cost, card_observations in data.items():
                total_observations = card_observations["total_observations"]
                if total_observations > 0:
                    for dbf_id, observations in card_observations.items():
                        if dbf_id != "total_observations":
                            prevalance = float(observations /
                                               total_observations)
                            if prevalance < cutoff_threshold:
                                other_cards.add(dbf_id)

        return other_cards
Exemple #2
0
    def display(self, out):
        from hsreplaynet.utils import card_db
        db = card_db()
        out.write("\n** %s - %s **" %
                  (self.player_class.name, self.format.name))

        discovered = set()
        stack = [self.tree.root]
        while len(stack):
            v = stack.pop(0)
            if str(v) not in discovered:
                discovered.add(str(v))
                if v.label == "ROOT":
                    card_name = "ROOT"
                else:
                    card_name = db[int(v.label)].name

                dist = self._popularity_distribution(v).distribution()
                size = len(dist)
                if size:
                    observations = sum(dist.values())
                    vals = ("\t" * v.depth, v.depth, card_name, size,
                            observations)
                    out.write("%s(%i) %s (%i Decks, %i Observations):" % vals)

                for child in v.children():
                    stack.insert(0, child)
Exemple #3
0
 def pretty_signature_html(self):
     db = card_db()
     components = list(
         sorted(self.signature.items(), key=lambda t: t[1], reverse=True))
     table = "<table><tr><th>Card</th><th>Weight</th></tr>%s</table>"
     row = "<tr><td>%s</td><td>%s</td></tr>"
     rows = [
         row % (db[int(dbf)].name, round(weight, 4))
         for dbf, weight in components
     ]
     return table % "".join(rows)
Exemple #4
0
    def sankey_visualization(self):
        nodes = set()
        links = collections.defaultdict(lambda: collections.defaultdict(int))
        db = card_db()
        other_cards = self._sankey_other_cards()
        for data_point in self.data_points:
            observations = data_point["observations"]
            cards = data_point["cards"]
            for source_dbf, count in cards.items():
                source_card = db[int(source_dbf)]
                source_cost = min(10, source_card.cost)
                for target_dbf, count in cards.items():
                    target_card = db[int(target_dbf)]
                    target_cost = min(10, target_card.cost)

                    if target_cost == source_cost + 1:
                        if source_dbf not in other_cards:
                            source = str(source_dbf)
                        else:
                            source = "-1:%i" % source_cost

                        if target_dbf not in other_cards:
                            target = str(target_dbf)
                        else:
                            target = "-1:%i" % target_cost

                        nodes.add(source)
                        nodes.add(target)
                        links[source][target] += observations

        result = {
            "links": [],
            "nodes": [{
                "name": n
            } for n in nodes],
        }
        for source_dbf, targets in links.items():
            for target_dbf, observations in targets.items():
                result["links"].append({
                    "source": source_dbf,
                    "target": target_dbf,
                    "value": observations
                })

        return result
Exemple #5
0
def create_deck_from_deckstring(deckstring, archetype_id=None):
    db = card_db()
    cards, heroes, format = parse_deckstring(deckstring)

    card_ids = []
    for card in cards:
        for _ in range(card[1]):
            card_ids.append(db[card[0]].id)

    deck_list, _ = Deck.objects.get_or_create_from_id_list(
        card_ids,
        hero_id=heroes[0],
        game_type=enums.BnetGameType.BGT_RANKED_STANDARD)

    if archetype_id:
        archetype = Archetype(id=archetype_id)
        archetype.save()

        deck_list.archetype = archetype
        deck_list.save()

    return deck_list