Beispiel #1
0
def get_seed_for(p):
    if Game.SERVER_SIDE:
        seed = long(Game.getgame().random.randint(1, 10**20))
    else:
        seed = 0L

    return sync_primitive(seed, p)
Beispiel #2
0
def get_seed_for(p):
    if Game.SERVER_SIDE:
        seed = long(Game.getgame().random.randint(1, 10 ** 20))
    else:
        seed = 0L

    return sync_primitive(seed, p)
Beispiel #3
0
    def handle(self, evt_type, act):
        if evt_type == 'action_after' and isinstance(act, Damage):
            g = Game.getgame()
            src, tgt = act.source, act.target
            if not (src and src.has_skill(Disarm)): return act
            if tgt.dead: return act
            pact = g.action_stack[-1]
            pcard = getattr(pact, 'associated_card', None)
            if not pcard: return act

            if not pcard.is_card(AttackCard) and not (pcard.is_card(DuelCard) and pact.source is src):
                return act

            if not user_input([src], ChooseOptionInputlet(self, (False, True))):
                return act

            cl = list(tgt.cards) + list(tgt.showncards)
            g.process_action(ShowCards(tgt, cl, [src]))

            if g.SERVER_SIDE:
                l = [bool(c.is_card(AttackCard) or 'spellcard' in c.category) for c in cl]
            else:
                l = [False for c in cl]

            l = sync_primitive(l, g.players)
            cl = list(itertools.compress(cl, l))
            g.process_action(DisarmHideAction(src, tgt, cl))

        elif evt_type == 'action_after' and isinstance(act, FinalizeStage):
            tgt = act.target
            g = Game.getgame()
            g.process_action(DisarmReturningAction(tgt, tgt))

        return act
Beispiel #4
0
    def handle(self, evt_type, act):
        if evt_type == 'action_after' and isinstance(act, Damage):
            g = Game.getgame()
            src, tgt = act.source, act.target
            if not (src and src.has_skill(Disarm)): return act
            if tgt.dead: return act
            pact = g.action_stack[-1]
            pcard = getattr(pact, 'associated_card', None)
            if not pcard: return act

            if not pcard.is_card(AttackCard) and not (pcard.is_card(DuelCard) and pact.source is src):
                return act

            if not user_input([src], ChooseOptionInputlet(self, (False, True))):
                return act

            cl = list(tgt.cards) + list(tgt.showncards)
            g.process_action(ShowCards(tgt, cl, [src]))

            if g.SERVER_SIDE:
                l = [c.is_card(AttackCard) or 'spellcard' in c.category for c in cl]
            else:
                l = [False for c in cl]

            l = sync_primitive(l, g.players)
            cl = list(itertools.compress(cl, l))
            g.process_action(DisarmHideAction(src, tgt, cl))

        elif evt_type == 'action_after' and isinstance(act, FinalizeStage):
            tgt = act.target
            g = Game.getgame()
            g.process_action(DisarmReturningAction(tgt, tgt))

        return act
Beispiel #5
0
def random_choose_card(cardlists):
    from itertools import chain
    allcards = list(chain.from_iterable(cardlists))
    if not allcards:
        return None

    g = Game.getgame()
    c = g.random.choice(allcards)
    v = sync_primitive(c.sync_id, g.players)
    cl = g.deck.lookupcards([v])
    assert len(cl) == 1
    c = cl[0]
    c.detach()
    return c
Beispiel #6
0
def random_choose_card(cardlists):
    from itertools import chain
    allcards = list(chain.from_iterable(cardlists))
    if not allcards:
        return None

    g = Game.getgame()
    c = g.random.choice(allcards)
    v = sync_primitive(c.sync_id, g.players)
    cl = g.deck.lookupcards([v])
    assert len(cl) == 1
    c = cl[0]
    c.detach()
    return c
Beispiel #7
0
def random_choose_card(categories):
    from itertools import chain
    allcards = list(chain.from_iterable(categories))
    if not allcards:
        return None

    g = Game.getgame()
    c = g.random.choice(allcards)
    v = sync_primitive(c.syncid, g.players)
    cl = g.deck.lookupcards([v])
    if len(cl)!=1:
        print cl
    assert len(cl) == 1
    return cl[0]
Beispiel #8
0
def roll(g, items):
    from thb.item import European
    roll = range(len(g.players))
    g.random.shuffle(roll)
    pl = g.players
    for i, p in enumerate(pl):
        if European.is_european(g, items, p):
            g.emit_event('european', p)
            roll.remove(i)
            roll.insert(0, i)
            break

    roll = sync_primitive(roll, pl)
    roll = [pl[i] for i in roll]
    g.emit_event('game_roll', roll)
    return roll
Beispiel #9
0
def roll(g, items):
    from thb.item import European
    roll = range(len(g.players))
    g.random.shuffle(roll)
    pl = g.players
    for i, p in enumerate(pl):
        if European.is_european(g, items, p):
            g.emit_event('european', p)
            roll.remove(i)
            roll.insert(0, i)
            break

    roll = sync_primitive(roll, pl)
    roll = [pl[i] for i in roll]
    g.emit_event('game_roll', roll)
    return roll
Beispiel #10
0
    def handle(self, evt_type, act):
        if evt_type == 'action_before' and isinstance(act, SpellCardAction):
            if act.cancelled: return act  # some other thing have done the job
            if act.non_responsive:
                return act

            g = Game.getgame()

            has_reject = False
            while g.SERVER_SIDE:
                # from thb.characters.baseclasses import Character
                # from thb.characters.reimu import SpiritualAttack
                # for p in g.players:
                #     if isinstance(p, Character) and p.has_skill(SpiritualAttack):
                #         has_reject = True
                #         break

                # if has_reject: break

                from thb.cards.definition import RejectCard
                for c in flatten([[p.cards, p.showncards] for p in g.players]):
                    if isinstance(c, RejectCard):
                        has_reject = True
                        break

                break

            has_reject = sync_primitive(has_reject, g.players)
            if not has_reject: return act

            self.target_act = act

            pl = BatchList(p for p in g.players if not p.dead)

            with InputTransaction('AskForRejectAction', pl) as trans:
                p, rst = ask_for_action(self,
                                        pl, ('cards', 'showncards'), [],
                                        trans=trans)

            if not p: return act
            cards, _ = rst
            assert cards and self.cond(cards)
            g.process_action(
                LaunchCard(p, [act.target], cards[0], Reject(p, act)))

        return act
Beispiel #11
0
    def handle(self, evt_type, act):
        if evt_type == 'action_before' and isinstance(act, SpellCardAction):
            if act.cancelled: return act  # some other thing have done the job
            if act.non_responsive:
                return act

            g = Game.getgame()

            has_reject = False
            while g.SERVER_SIDE:
                # from thb.characters.baseclasses import Character
                # from thb.characters.reimu import SpiritualAttack
                # for p in g.players:
                #     if isinstance(p, Character) and p.has_skill(SpiritualAttack):
                #         has_reject = True
                #         break

                # if has_reject: break

                from thb.cards.definition import RejectCard
                for c in flatten([[p.cards, p.showncards] for p in g.players]):
                    if isinstance(c, RejectCard):
                        has_reject = True
                        break

                break

            has_reject = sync_primitive(has_reject, g.players)
            if not has_reject: return act

            self.target_act = act

            pl = BatchList(p for p in g.players if not p.dead)

            with InputTransaction('AskForRejectAction', pl) as trans:
                p, rst = ask_for_action(self, pl, ('cards', 'showncards'), [], trans=trans)

            if not p: return act
            cards, _ = rst
            assert cards and self.cond(cards)
            g.process_action(LaunchCard(p, [act.target], cards[0], Reject(p, act)))

        return act
Beispiel #12
0
    def shuffle(self, cl):
        if Game.SERVER_SIDE:
            seed = long(Game.getgame().random.randint(1, 27814431486575L))
        else:
            seed = 0L

        owner = cl.owner
        seed = sync_primitive(seed, owner)

        if seed:  # cardlist owner & server
            shuffler = random.Random(seed)
            shuffler.shuffle(cl)
        else:  # others
            for c in cl:
                c.__class__ = HiddenCard
                c.suit = c.number = 0

        for c in cl:
            c.syncid = 0
            self.register_card(c)
Beispiel #13
0
    def shuffle(self, cl):
        if Game.SERVER_SIDE:
            seed = long(Game.getgame().random.randint(1, 27814431486575L))
        else:
            seed = 0L

        owner = cl.owner
        seed = sync_primitive(seed, owner)

        if seed:  # cardlist owner & server
            shuffler = random.Random(seed)
            shuffler.shuffle(cl)
        else:  # others
            for c in cl:
                c.__class__ = HiddenCard
                c.suit = c.number = 0

        for c in cl:
            c.syncid = 0
            self.register_card(c)
Beispiel #14
0
 def is_type(self, t):
     g = Game.getgame()
     pl = g.players
     return sync_primitive(self.type == t, pl)
Beispiel #15
0
    def apply_action(self):
        g = Game.getgame()

        from cards import Deck

        g.deck = Deck()
        g.ehclasses = list(action_eventhandlers) + g.game_ehs.values()

        H, M, A = Identity.TYPE.HAKUREI, Identity.TYPE.MORIYA, Identity.TYPE.ADMIN
        idlist = [A, H, H, M, M]
        del H, M, A

        pl = g.players[1:]
        seed = sync_primitive(g.random.getrandbits(32), g.players)
        random.Random(seed).shuffle(pl)
        g.players[1:] = pl
        g.emit_event('reseat', None)

        for p, identity in zip(g.players, idlist):
            p.identity = Identity()
            p.identity.type = identity
            g.process_action(RevealIdentity(p, g.players))

        force_hakurei = BatchList(g.players[1:3])
        force_moriya  = BatchList(g.players[3:5])
        g.forces = BatchList([force_hakurei, force_moriya])

        from . import characters

        g.switch_character(g.players[0], CharChoice(characters.koakuma.Koakuma))
        g.koakuma = koakuma = g.players[0]
        koakuma.tags['books'] = g.total_books
        koakuma.maxlife += 4
        koakuma.life += 4

        # choose girls -->
        chars = characters.get_characters('book')
        try:
            chars.remove(characters.koakuma.Koakuma)
        except:
            pass

        g.random.shuffle(chars)

        testing = list(settings.TESTING_CHARACTERS)
        testing = filter_out(chars, lambda c: c.__name__ in testing)
        chars = g.random.sample(chars, 24)

        if Game.SERVER_SIDE:
            choices = [CharChoice(cls) for cls in chars[-20:]]
        else:
            choices = [CharChoice(None) for _ in xrange(20)]

        del chars[-20:]

        for p in g.players[1:]:
            c = choices[-4:]
            del choices[-4:]
            akari = CharChoice(characters.akari.Akari)
            akari.real_cls = chars.pop()
            c.append(akari)
            c.extend([CharChoice(cls) for cls in testing])
            p.choices = c
            p.reveal(c)

        pl = g.players[1:]
        mapping = {p: p.choices for p in pl}

        with InputTransaction('ChooseGirl', pl, mapping=mapping) as trans:
            ilet = ChooseGirlInputlet(g, mapping)
            ilet.with_post_process(lambda p, rst: trans.notify('girl_chosen', (p, rst)) or rst)
            rst = user_input(pl, ilet, timeout=30, type='all', trans=trans)

        for p in pl:
            c = rst[p] or p.choices[0]
            g.switch_character(p, c)

        g.emit_event('game_begin', g)

        for p in g.players:
            g.process_action(DistributeCards(p, amount=4))

        pl = g.players
        for i, idx in enumerate(cycle(range(len(pl)))):
            if i >= 6000: break
            p = pl[idx]
            if p.dead:
                g.process_action(PlayerRevive(p, p, 2))
                g.process_action(DrawCards(p, 2))
                continue

            g.emit_event('player_turn', p)
            try:
                g.process_action(PlayerTurn(p))
            except InterruptActionFlow:
                pass
Beispiel #16
0
 def is_type(self, t):
     g = Game.getgame()
     pl = g.players
     return sync_primitive(self.type == t, pl)