Ejemplo n.º 1
0
 def __init__(self, game_events, game_type: GameType,
              players: List[TenhouPlayer], *, context):
     self.players = players
     self.context_ = context
     game_events = list(game_events)
     for event in game_events:
         event.context_ = self
     init, *playing = game_events
     playing = list(playing)
     first_end_index = next(i for (i, x) in enumerate(playing)
                            if is_game_end(x))
     end = [x for x in playing[first_end_index:] if is_game_end(x)]
     self.end_events = end
     self._meta = list_of_xml_configs([init])
     self._end_meta = list_of_xml_configs([game_events[-1]])
     self.events = game_events
     self.playing = playing
     self.game_type = game_type
     self.seeds = number_list(self._meta.INIT.seed)
     self.east_index = int(self._meta.INIT.oya)
     self.prevailing_and_game = SubCategory(
         self.game_type.play_wind_count() + 1,
         4,
         caption="prevailing_and_game",
         names=["prevailing", "game_index"])
Ejemplo n.º 2
0
def test_named_category(cat, index):
    category = SubCategory(2, 3, 4, caption="test", names=["a", "b", "c"])
    named = category.category(index)
    a, b, c = cat
    assert named.a == a
    assert named.b == b
    assert named.c == c
Ejemplo n.º 3
0
def game_type_command(event: TenhouEvent, ctx):
    cmd = Builder(GameCommand)
    game_type = GameType(event.attrib["type"])
    ctx["_game_indexer"] = SubCategory(game_type.play_wind_count() + 1,
                                       4,
                                       caption="prevailing_and_game",
                                       names=["prevailing", "game_index"])
    with cmd.when(update=Update.REPLACE):
        yield cmd(prop=RecordView.play_level,
                  value=if_value(game_type.with_tips(), "若銀琥孔",
                                 "般上特鳳")[game_type.play_level()])
        yield cmd(prop=RecordView.show_discard_shadow,
                  value=game_type.show_discard_shadow())
        yield cmd(prop=RecordView.play_wind_count,
                  value=game_type.play_wind_count())
        yield cmd(prop=RecordView.allow_tanyao_open,
                  value=game_type.allow_tanyao_open())
        yield cmd(prop=RecordView.speed_up, value=game_type.speed_up())
        yield cmd(prop=RecordView.player_count, value=game_type.player_count())
        yield cmd(prop=RecordView.using_tiles,
                  value=initial_tiles(game_type.player_count(),
                                      game_type.has_aka_dora()))
Ejemplo n.º 4
0
def init_named():
    a, b, c = SubCategory(3, caption='a_sub', names=["a"]), \
              SubCategory(2, caption="b_sub", names=["b"]), \
              SubCategory(4, caption="c_sub", names=["c"])
    return MixedCategory([a, b, c], caption="mixed_cat", field_name="mixed")
Ejemplo n.º 5
0
def init_normal():
    a, b, c = SubCategory(3), SubCategory(2), SubCategory(4)
    return MixedCategory([a, b, c])

def n_a_r(n, r):
    return reduce(op.mul, range(n, n - r, -1), 1)


def n_c_r(n, r):
    r = min(r, n - r)
    return n_a_r(n, r) / n_a_r(r, r)


UNICODE_TILE_ORDER = "zmsp"

UNICODE_HONOR_ORDER = [int(x) for x in "01234765"]

TILE_CATEGORY = MixedCategory([SubCategory(7)] + ([SubCategory(9)] * 3))


def to_unicode_tile(tile: Tile) -> str:
    color = UNICODE_TILE_ORDER.index(tile.color)
    if tile.color == 'z':
        number = UNICODE_HONOR_ORDER[tile.number] - 1
    else:
        number = tile.number - 1
    index = TILE_CATEGORY.index((color, number))
    return chr(0x1f000 + index)


def to_plain_tile(tile: Tile) -> str:
    return str(tile)
Ejemplo n.º 7
0
class TenhouGame:
    def __init__(self, game_events, game_type: GameType,
                 players: List[TenhouPlayer], *, context):
        self.players = players
        self.context_ = context
        game_events = list(game_events)
        for event in game_events:
            event.context_ = self
        init, *playing = game_events
        playing = list(playing)
        first_end_index = next(i for (i, x) in enumerate(playing)
                               if is_game_end(x))
        end = [x for x in playing[first_end_index:] if is_game_end(x)]
        self.end_events = end
        self._meta = list_of_xml_configs([init])
        self._end_meta = list_of_xml_configs([game_events[-1]])
        self.events = game_events
        self.playing = playing
        self.game_type = game_type
        self.seeds = number_list(self._meta.INIT.seed)
        self.east_index = int(self._meta.INIT.oya)
        self.prevailing_and_game = SubCategory(
            self.game_type.play_wind_count() + 1,
            4,
            caption="prevailing_and_game",
            names=["prevailing", "game_index"])

    def game_index(self):
        return self.prevailing_and_game.category(self.seeds[0])

    def sub_game_index(self):
        return self.seeds[1]

    def richii_counts(self):
        return self.seeds[2]

    def initial_dora(self):
        return self.seeds[-1]

    def end_stringify(self, event):
        if is_somebody_win_game(event):
            who, from_who = (int(event.attrib[s]) for s in ['who', 'fromWho'])
            score = number_list(event.attrib['ten'])[1]
            if who == from_who:
                return r"%s:ツモ(%d)" % (self.players[who].name, score)
            else:
                return "%s<-%s:ロン(%d)" % (self.players[who].name,
                                          self.players[from_who].name, score)
        elif is_nobody_win_game(event):
            type_of_draw = ""
            if "type" in event.attrib:
                type_of_draw = DRAWN_TYPES[event.attrib["type"]]
            return " ".join([r"流局", type_of_draw])

    def __str__(self):
        prevailing, game_index = self.game_index()
        return "%s%d局%d本 %s" % ("東南西北"[prevailing], game_index + 1,
                                self.sub_game_index(), ",".join(
                                    self.end_stringify(e)
                                    for e in self.end_events))

    def __repr__(self):
        return "<%s>" % self

    def to_paifu(self):
        paifu_data = []
        prevailing, game_index = self.game_index()
        game_str = "{0}-{1}-{2}".format("ESWN"[prevailing], game_index + 1,
                                        self.sub_game_index())
        for i in self.events:
            paifu_data.extend(i.to_paifu())
        for i in paifu_data:
            i['game_str'] = game_str
        return paifu_data
Ejemplo n.º 8
0
def test_index(cat, index):
    category = SubCategory(2, 3, 4)
    assert category.index(cat) == index
Ejemplo n.º 9
0
def test_category(cat, index):
    category = SubCategory(2, 3, 4)
    assert category.category(index) == cat