<commit_before>from tota.game import Drawer
from tota import settings

from termcolor import colored


class TerminalDrawer(Drawer):
    def __init__(self, use_basic_icons=False, use_compressed_view=False):
        self.use_basic_icons = use_basic_icons
        self.use_compressed_view = use_compressed_view

    def position_draw(self, game, position):
        """Get the string to draw for a given position of the world."""
        # decorations first, then things over them
        thing = game.world.things.get(position)
        effect = game.world.effects.get(position)
        effect_color = settings.EFFECT_COLORS.get(effect)

        if thing is not None:
            if self.use_basic_icons:
                icon = thing.ICON_BASIC
            else:
                icon = thing.ICON

            color = settings.TEAM_COLORS[thing.team]
        else:
            icon = ' '
            color = None

        if effect_color is not None:
            on_color = 'on_' + effect_color
        else:
            on_color = None

        if self.use_compressed_view:
            widener = ''
        else:
            widener = ' '

        return colored(icon + widener, color, on_color)

    def draw(self, game):
        """Draw the world with 'ascii'-art ."""
        screen = ''

        # print the world
        screen += '\n'.join(u''.join(self.position_draw(game, (x, y))
                                     for x in range(game.world.size[0]))
                            for y in range(game.world.size[1]))

        # game stats
        screen += '\nticks:{}'.format(game.world.t)

        # print hero stats
        for hero in sorted(game.heroes, key=lambda x: x.name):
            if hero.alive:
                # a small "health bar" with unicode chars, from 0 to 10 chars
                life_chars_count = int((10.0 / hero.max_life) * hero.life)
                life_chars = life_chars_count * '\u2588'
                no_life_chars = (10 - life_chars_count) * '\u2591'
                if self.use_basic_icons:
                    heart = ''
                else:
                    heart = '\u2665 '
                life_bar = heart + '{}{}'.format(life_chars, no_life_chars)
            else:
                if self.use_basic_icons:
                    skull = ''
                else:
                    skull = '\u2620 '
                life_bar = skull + '[dead]'

            hero_template = '{bar}({life}) {name} ({level})                     '
            hero_stats = hero_template.format(bar=life_bar,
                                              name=hero.name,
                                              life=int(hero.life),
                                              level=hero.level)

            screen += '\n' + colored(hero_stats,
                                     settings.TEAM_COLORS[hero.team])

        # print events (of last step) for debugging
        if game.debug:
            screen += u'\n'
            screen += u'\n'.join([colored('{}: {}'.format(thing, event),
                                          settings.TEAM_COLORS[thing.team])
                                  for t, thing, event in game.events
                                  if t == game.world.t])
        GO_TO_TOP = '\033[0;0H'
        print(GO_TO_TOP + screen)
<commit_msg>Ancient bars, and nicer hero stats<commit_after>from tota.game import Drawer
from tota import settings

from termcolor import colored


def health_bar(length, life, max_life):
    """Create a small unicode health bar."""
    life_chars_count = int((length / max_life) * life)
    life_chars = life_chars_count * '\u2588'
    no_life_chars = (length - life_chars_count) * '\u2591'

    return life_chars + no_life_chars


class TerminalDrawer(Drawer):
    def __init__(self, use_basic_icons=False, use_compressed_view=False):
        self.use_basic_icons = use_basic_icons
        self.use_compressed_view = use_compressed_view

    def position_draw(self, game, position):
        """Get the string to draw for a given position of the world."""
        # decorations first, then things over them
        thing = game.world.things.get(position)
        effect = game.world.effects.get(position)
        effect_color = settings.EFFECT_COLORS.get(effect)

        if thing is not None:
            if self.use_basic_icons:
                icon = thing.ICON_BASIC
            else:
                icon = thing.ICON

            color = settings.TEAM_COLORS[thing.team]
        else:
            icon = ' '
            color = None

        if effect_color is not None:
            on_color = 'on_' + effect_color
        else:
            on_color = None

        if self.use_compressed_view:
            widener = ''
        else:
            widener = ' '

        return colored(icon + widener, color, on_color)

    def draw(self, game):
        """Draw the world with 'ascii'-art ."""
        screen = ''

        # print the world
        screen += '\n'.join(u''.join(self.position_draw(game, (x, y))
                                     for x in range(game.world.size[0]))
                            for y in range(game.world.size[1]))

        # game stats
        screen += '\nticks:{}'.format(game.world.t)

        # print teams stats
        for team in (settings.TEAM_RADIANT, settings.TEAM_DIRE):
            screen += '\n' + colored(team, settings.TEAM_COLORS[team])

            ancient = game.ancients[team]
            team_heroes = [hero for hero in game.heroes
                           if hero.team == team]

            ancient_template = '{icon} {bar}({life}) Ancient                              '
            ancient_stats = ancient_template.format(
                icon=ancient.ICON_BASIC if self.use_basic_icons else ancient.ICON,
                bar=health_bar(20, ancient.life, ancient.max_life),
                life=int(ancient.life) if ancient.alive else 'destroyed!',
            )

            screen += '\n' + colored(ancient_stats, settings.TEAM_COLORS[team])

            for hero in sorted(team_heroes, key=lambda x: x.name):
                hero_template = '{icon} {bar}({life}) Hero: {name} ({level})              '
                hero_stats = hero_template.format(
                    icon=hero.ICON_BASIC if self.use_basic_icons else hero.ICON,
                    bar=health_bar(20, hero.life, hero.max_life),
                    name=hero.name,
                    life=int(hero.life) if hero.alive else 'dead',
                    level=hero.level,
                )

                screen += '\n' + colored(hero_stats, settings.TEAM_COLORS[team])

        # print events (of last step) for debugging
        if game.debug:
            screen += u'\n'
            screen += u'\n'.join([colored('{}: {}'.format(thing, event),
                                          settings.TEAM_COLORS[thing.team])
                                  for t, thing, event in game.events
                                  if t == game.world.t])
        GO_TO_TOP = '\033[0;0H'
        print(GO_TO_TOP + screen)

{'fraction_non_alphanumeric': 0.10133409026397956, 'fraction_numerical': 0.00709622480840193, 'mean_word_length': 1.5303411131059246, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 21, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4520323', 'n_tokens_mistral': 2040, 'n_tokens_neox': 1922, 'n_words': 469}