def update_event_handlers(g): ehclasses = list(action_eventhandlers) + g.game_ehs.values() ehclasses += g.ehclasses g.set_event_handlers(EventHandler.make_list(ehclasses))
def update_event_handlers(self): ehclasses = list(action_eventhandlers) + self.game_ehs.values() ehclasses += self.ehclasses self.event_handlers = EventHandler.make_list(ehclasses)
def update_event_handlers(g): ehclasses = list(action_eventhandlers) + g.game_ehs.values() ehclasses += g.ehclasses ehclasses.remove(ShuffleHandler) # disable shuffling g.set_event_handlers(EventHandler.make_list(ehclasses))
def game_start(g): # game started, init state from cards import Deck g.deck = Deck() g.ehclasses = ehclasses = list( action_eventhandlers) + g.game_ehs.values() for i, p in enumerate(g.players): p.identity = Identity() p.identity.type = (Identity.TYPE.HAKUREI, Identity.TYPE.MORIYA)[i % 2] g.forces = forces = BatchList([BatchList(), BatchList()]) for i, p in enumerate(g.players): f = i % 2 p.force = f forces[f].append(p) # choose girls --> from . import characters chars = list(characters.characters) seed = get_seed_for(g.players) random.Random(seed).shuffle(chars) # ANCHOR(test) chars.extend([ # characters.kokoro.Kokoro, ]) choices = [CharChoice(cls) for cls in chars[-16:]] del chars[-16:] for c in choices[:4]: c.char_cls = characters.akari.Akari if Game.SERVER_SIDE: for c, cls in zip(choices[:4], g.random.sample(chars, 4)): # yes, must random.sample c.real_cls = cls # ----- roll ------ roll = range(len(g.players)) g.random.shuffle(roll) pl = g.players roll = sync_primitive(roll, pl) roll = [pl[i] for i in roll] g.emit_event('game_roll', roll) first = roll[0] g.emit_event('game_roll_result', first) # ---- first_index = g.players.index(first) n = len(g.order_list) order = [g.players[(first_index + i) % n] for i in g.order_list] # akaris = {} # DO NOT USE DICT! THEY ARE UNORDERED! akaris = [] mapping = {p: choices for p in g.players} with InputTransaction('ChooseGirl', g.players, mapping=mapping) as trans: for p in order: c = user_input([p], ChooseGirlInputlet(g, mapping), timeout=30, trans=trans) c = c or [_c for _c in choices if not _c.chosen][0] c.chosen = p if issubclass(c.char_cls, characters.akari.Akari): akaris.append((p, c)) else: g.set_character(p, c.char_cls) trans.notify('girl_chosen', c) # reveal akaris if akaris: for p, c in akaris: c.char_cls = c.real_cls g.players.reveal([i[1] for i in akaris]) for p, c in akaris: g.set_character(p, c.char_cls) g.event_handlers = EventHandler.make_list(ehclasses) # ------- for p in g.players: log.info(u'>> Player: %s:%s %s', p.__class__.__name__, Identity.TYPE.rlookup(p.identity.type), p.account.username) # ------- first = g.players[first_index] try: pl = g.players for p in pl: g.process_action(RevealIdentity(p, pl)) g.emit_event('game_begin', g) for p in g.players: g.process_action(DrawCards(p, amount=3 if p is first else 4)) pl = g.players.rotate_to(first) for i, p in enumerate(cycle(pl)): if i >= 6000: break if not p.dead: g.emit_event('player_turn', p) try: g.process_action(PlayerTurn(p)) except InterruptActionFlow: pass except GameEnded: pass log.info(u'>> Winner: %s', Identity.TYPE.rlookup(g.winners[0].identity.type))
def game_start(g): # game started, init state from cards import Card, Deck, CardList g.deck = Deck() ehclasses = list(action_eventhandlers) + _game_ehs.values() np = g.n_persons for p in g.players: p.cards = CardList(p, 'handcard') # Cards in hand p.showncards = CardList(p, 'showncard') # Cards which are shown to the others, treated as 'Cards in hand' p.equips = CardList(p, 'equips') # Equipments p.fatetell = CardList(p, 'fatetell') # Cards in the Fatetell Zone p.special = CardList(p, 'special') # used on special purpose p.showncardlists = [p.showncards, p.fatetell] # cardlists should shown to others p.tags = defaultdict(int) p.dead = False # choose girls init --> from characters import characters as chars if Game.SERVER_SIDE: choice = [ CharChoice(cls, cid) for cls, cid in zip(g.random.sample(chars, 3*np+2), xrange(3*np+2)) ] elif Game.CLIENT_SIDE: choice = [ CharChoice(None, i) for i in xrange(3*np+2) ] chosen_girls = [] pl = PlayerList(g.players) def process(p, cid): try: retry = p._retry except AttributeError: retry = 3 retry -= 1 try: check(isinstance(cid, int)) i = p._choose_tag if p is boss: assert p is pl[-1] check(i*3 <= cid< (i+1)*3+2) else: check(i*3 <= cid < (i+1)*3) c = choice[cid] if c.chosen and retry > 0: p._retry = retry raise ValueError c.chosen = p chosen_girls.append(c) g.emit_event('girl_chosen', c) pl.remove(p) return c except CheckFailed as e: return None finally: try: del p._retry del p._choose_tag except AttributeError: pass # choose boss idx = sync_primitive(g.random.randrange(len(g.players)), g.players) pl[-1], pl[idx] = pl[idx], pl[-1] boss = g.boss = pl[-1] boss.identity = Identity() boss.identity.type = Identity.TYPE.BOSS g.process_action(RevealIdentity(boss, g.players)) for i, p in enumerate(pl): p._choose_tag = i p.reveal(choice[i*3:(i+1)*3]) boss.reveal(choice[-2:]) g.emit_event('choose_girl_begin', ([boss], choice)) PlayerList([boss]).user_input_all('choose_girl', process, choice, timeout=30) if not chosen_girls: # didn't choose offs = sync_primitive(g.random.randrange(5), g.players) c = choice[(len(pl)-1)*3+offs] c.chosen = boss g.emit_event('girl_chosen', c) pl.remove(boss) else: c = chosen_girls.pop() assert c.chosen is boss g.players.reveal(c) # mix it in advance # so the others could see it mixin_character(boss, c.char_cls) boss.skills = list(boss.skills) # make it instance variable ehclasses.extend(boss.eventhandlers_required) # boss's hp bonus if len(g.players) > 5: boss.maxlife += 1 boss.life = boss.maxlife g.emit_event('choose_girl_end', None) # reseat opl = g.players loc = range(len(opl)) g.random.shuffle(loc) loc = sync_primitive(loc, opl) npl = opl[:] for i, l in zip(range(len(opl)), loc): npl[i] = opl[l] g.players[:] = npl g.emit_event('reseat', None) # tell the others their own identity il = list(g.identities) g.random.shuffle(il) for p in g.players.exclude(boss): p.identity = Identity() id = il.pop() if Game.SERVER_SIDE: p.identity.type = id g.process_action(RevealIdentity(p, p)) pl = g.players.exclude(boss) g.emit_event('choose_girl_begin', (pl, choice)) pl.user_input_all('choose_girl', process, choice, timeout=30) g.emit_event('choose_girl_end', None) # now you can have them all. g.players.reveal(choice) # if there's any person didn't make a choice --> # FIXME: this can choose girl from the other force! if pl: choice = [c for c in choice if not c.chosen] sample = sync_primitive( g.random.sample(xrange(len(choice)), len(pl)), g.players ) for p, i in zip(pl, sample): c = choice[i] c.chosen = p chosen_girls.append(c) g.emit_event('girl_chosen', c) # mix char class with player --> for c in chosen_girls: p = c.chosen mixin_character(p, c.char_cls) p.skills = list(p.skills) # make it instance variable p.life = p.maxlife ehclasses.extend(p.eventhandlers_required) g.event_handlers = EventHandler.make_list(ehclasses) g.emit_event('game_begin', g) try: for p in g.players: g.process_action(DrawCards(p, amount=4)) pl = g.players.rotate_to(boss) for i, p in enumerate(cycle(pl)): if i >= 6000: break if not p.dead: try: g.process_action(PlayerTurn(p)) except InterruptActionFlow: pass except GameEnded: pass
def game_start(g): # game started, init state from cards import Deck, CardList g.deck = Deck() ehclasses = list(action_eventhandlers) + g.game_ehs.values() for i, p in enumerate(g.players): p.cards = CardList(p, 'cards') # Cards in hand p.showncards = CardList(p, 'showncards') # Cards which are shown to the others, treated as 'Cards in hand' p.equips = CardList(p, 'equips') # Equipments p.fatetell = CardList(p, 'fatetell') # Cards in the Fatetell Zone p.special = CardList(p, 'special') # used on special purpose p.showncardlists = [p.showncards, p.fatetell] p.tags = defaultdict(int) p.dead = False p.identity = Identity() p.identity.type = (Identity.TYPE.HAKUREI, Identity.TYPE.MORIYA)[i % 2] g.forces = forces = BatchList([BatchList(), BatchList()]) for i, p in enumerate(g.players): f = i % 2 p.force = f forces[f].append(p) # choose girls --> from . import characters chars = list(characters.characters) seed = get_seed_for(g.players) random.Random(seed).shuffle(chars) # ANCHOR(test) chars.extend([ ]) choices = [CharChoice(cls) for cls in chars[-16:]] del chars[-16:] for c in choices[:4]: c.char_cls = characters.akari.Akari if Game.SERVER_SIDE: for c, cls in zip(choices[:4], g.random.sample(chars, 4)): # yes, must random.sample c.real_cls = cls # ----- roll ------ roll = range(len(g.players)) g.random.shuffle(roll) pl = g.players roll = sync_primitive(roll, pl) roll = [pl[i] for i in roll] g.emit_event('game_roll', roll) first = roll[0] g.emit_event('game_roll_result', first) # ---- first_index = g.players.index(first) n = len(g.order_list) order = [g.players[(first_index + i) % n] for i in g.order_list] def mix(p, c): # mix char class with player --> mixin_character(p, c.char_cls) p.skills = list(p.skills) # make it instance variable p.life = p.maxlife ehclasses.extend(p.eventhandlers_required) # akaris = {} # DO NOT USE DICT! THEY ARE UNORDERED! akaris = [] mapping = {p: choices for p in g.players} with InputTransaction('ChooseGirl', g.players, mapping=mapping) as trans: for p in order: c = user_input([p], ChooseGirlInputlet(g, mapping), timeout=30, trans=trans) c = c or [_c for _c in choices if not _c.chosen][0] c.chosen = p if issubclass(c.char_cls, characters.akari.Akari): akaris.append((p, c)) else: mix(p, c) trans.notify('girl_chosen', c) # reveal akaris if akaris: for p, c in akaris: c.char_cls = c.real_cls g.players.reveal([i[1] for i in akaris]) for p, c in akaris: mix(p, c) first_actor = first g.event_handlers = EventHandler.make_list(ehclasses) # ------- log.info(u'>> Game info: ') log.info(u'>> First: %s:%s ', first.char_cls.__name__, Identity.TYPE.rlookup(first.identity.type)) for p in g.players: log.info(u'>> Player: %s:%s %s', p.char_cls.__name__, Identity.TYPE.rlookup(p.identity.type), p.account.username) # ------- try: pl = g.players for p in pl: g.process_action(RevealIdentity(p, pl)) g.emit_event('game_begin', g) for p in g.players: g.process_action(DrawCards(p, amount=3 if p is first_actor else 4)) pl = g.players.rotate_to(first_actor) for i, p in enumerate(cycle(pl)): if i >= 6000: break if not p.dead: g.emit_event('player_turn', p) try: g.process_action(PlayerTurn(p)) except InterruptActionFlow: pass except GameEnded: pass log.info(u'>> Winner: %s', Identity.TYPE.rlookup(g.winners[0].identity.type))
def game_start(self): # game started, init state from cards import Card, Deck, CardList self.deck = Deck() ehclasses = list(action_eventhandlers) + self.game_ehs.values() for i, p in enumerate(self.players): p.cards = CardList(p, "handcard") # Cards in hand p.showncards = CardList(p, "showncard") # Cards which are shown to the others, treated as 'Cards in hand' p.equips = CardList(p, "equips") # Equipments p.fatetell = CardList(p, "fatetell") # Cards in the Fatetell Zone p.special = CardList(p, "special") # used on special purpose p.showncardlists = [p.showncards, p.fatetell] p.tags = defaultdict(int) p.dead = False p.identity = Identity() p.identity.type = (Identity.TYPE.HAKUREI, Identity.TYPE.MORIYA)[i % 2] self.forces = forces = BatchList([PlayerList(), PlayerList()]) for i, p in enumerate(self.players): f = i % 2 p.force = f forces[f].append(p) # choose girls --> from characters import characters as chars from characters.akari import Akari if Game.SERVER_SIDE: choice = [CharChoice(cls, cid) for cls, cid in zip(self.random.sample(chars, 16), xrange(16))] for c in self.random.sample(choice, 4): c.real_cls = c.char_cls c.char_cls = Akari elif Game.CLIENT_SIDE: choice = [CharChoice(None, i) for i in xrange(16)] # ----------- self.players.reveal(choice) # roll roll = range(len(self.players)) self.random.shuffle(roll) pl = self.players roll = sync_primitive(roll, pl) roll = [pl[i] for i in roll] self.emit_event("game_roll", roll) first = roll[0] self.emit_event("game_roll_result", first) # ---- first_index = self.players.index(first) n = len(self.order_list) order = [self.players[(first_index + i) % n] for i in self.order_list] def mix(p, c): # mix char class with player --> mixin_character(p, c.char_cls) p.skills = list(p.skills) # make it instance variable p.life = p.maxlife ehclasses.extend(p.eventhandlers_required) # akaris = {} # DO NOT USE DICT! THEY ARE UNORDERED! akaris = [] self.emit_event("choose_girl_begin", (self.players, choice)) for i, p in enumerate(order): cid = p.user_input("choose_girl", choice, timeout=(n - i + 1) * 5) try: check(isinstance(cid, int)) check(0 <= cid < len(choice)) c = choice[cid] check(not c.chosen) c.chosen = p except CheckFailed: # first non-chosen char for c in choice: if not c.chosen: c.chosen = p break if issubclass(c.char_cls, Akari): akaris.append((p, c)) else: mix(p, c) self.emit_event("girl_chosen", c) self.emit_event("choose_girl_end", None) # reveal akaris if akaris: for p, c in akaris: c.char_cls = c.real_cls self.players.reveal([i[1] for i in akaris]) for p, c in akaris: mix(p, c) first_actor = first self.event_handlers = EventHandler.make_list(ehclasses) # ------- log.info(u">> Game info: ") log.info(u">> First: %s:%s ", first.char_cls.__name__, Identity.TYPE.rlookup(first.identity.type)) for p in self.players: log.info( u">> Player: %s:%s %s", p.char_cls.__name__, Identity.TYPE.rlookup(p.identity.type), p.account.username ) # ------- try: pl = self.players for p in pl: self.process_action(RevealIdentity(p, pl)) self.emit_event("game_begin", self) for p in self.players: self.process_action(DrawCards(p, amount=3 if p is first_actor else 4)) pl = self.players.rotate_to(first_actor) for i, p in enumerate(cycle(pl)): if i >= 6000: break if not p.dead: self.emit_event("player_turn", p) try: self.process_action(PlayerTurn(p)) except InterruptActionFlow: pass except GameEnded: pass log.info(u">> Winner: %s", Identity.TYPE.rlookup(self.winners[0].identity.type))
def game_start(g): # game started, init state from cards import Deck g.deck = Deck() g.ehclasses = ehclasses = list(action_eventhandlers) + _game_ehs.values() # choose girls init --> from .characters import characters as chars from .characters.akari import Akari chars = list(chars) if Game.CLIENT_SIDE: chars = [None] * len(chars) g.random.shuffle(chars) # choose boss idx = sync_primitive(g.random.randrange(len(g.players)), g.players) boss = g.boss = g.players[idx] boss.identity = Identity() boss.identity.type = Identity.TYPE.BOSS g.process_action(RevealIdentity(boss, g.players)) boss.choices = [CharChoice(c) for c in chars[:4]] boss.choices.append(CharChoice(Akari)) del chars[:4] for p in g.players.exclude(boss): p.choices = [CharChoice(c) for c in chars[:3]] p.choices.append(CharChoice(Akari)) del chars[:3] for p in g.players: p.reveal(p.choices) mapping = {boss: boss.choices} with InputTransaction('ChooseGirl', [boss], mapping=mapping) as trans: c = user_input([boss], ChooseGirlInputlet(g, mapping), 30, 'single', trans) c = c or boss.choices[-1] c.chosen = boss g.players.reveal(c) trans.notify('girl_chosen', c) if c.char_cls is Akari: c = CharChoice(chars.pop()) g.players.reveal(c) # mix it in advance # so the others could see it boss = g.switch_character(boss, c.char_cls) # boss's hp bonus if len(g.players) > 5: boss.maxlife += 1 boss.life = boss.maxlife # reseat seed = get_seed_for(g.players) random.Random(seed).shuffle(g.players) g.emit_event('reseat', None) # tell the others their own identity il = list(g.identities) g.random.shuffle(il) for p in g.players.exclude(boss): p.identity = Identity() id = il.pop() if Game.SERVER_SIDE: p.identity.type = id g.process_action(RevealIdentity(p, p)) pl = g.players.exclude(boss) mapping = {p: p.choices for p in pl} # CAUTION, DICT HERE with InputTransaction('ChooseGirl', pl, mapping=mapping) as trans: ilet = ChooseGirlInputlet(g, mapping) ilet.with_post_process(lambda p, rst: trans.notify('girl_chosen', rst) or rst) result = user_input(pl, ilet, type='all', trans=trans) # not enough chars for random, reuse unselected for p in pl: if result[p]: result[p].chosen = p chars.extend([i.char_cls for i in p.choices if not i.chosen and i.char_cls is not Akari]) seed = get_seed_for(g.players) random.Random(seed).shuffle(chars) # mix char class with player --> for p in pl: c = result[p] c = c or p.choices[-1] g.players.reveal(c) if c.char_cls is Akari: c = CharChoice(chars.pop()) g.players.reveal(c) p = g.switch_character(p, c.char_cls) g.event_handlers = EventHandler.make_list(ehclasses) g.emit_event('game_begin', g) try: for p in g.players: g.process_action(DrawCards(p, amount=4)) pl = g.players.rotate_to(boss) for i, p in enumerate(cycle(pl)): if i >= 6000: break if not p.dead: try: g.process_action(PlayerTurn(p)) except InterruptActionFlow: pass except GameEnded: pass
def game_start(g): # game started, init state from cards import Deck, CardList g.deck = Deck() ehclasses = list(action_eventhandlers) + _game_ehs.values() for p in g.players: p.cards = CardList(p, 'cards') # Cards in hand p.showncards = CardList(p, 'showncards') # Cards which are shown to the others, treated as 'Cards in hand' p.equips = CardList(p, 'equips') # Equipments p.fatetell = CardList(p, 'fatetell') # Cards in the Fatetell Zone p.special = CardList(p, 'special') # used on special purpose p.showncardlists = [p.showncards, p.fatetell] # cardlists should shown to others p.tags = defaultdict(int) p.dead = False # choose girls init --> from characters import characters as chars chars = list(chars) if Game.CLIENT_SIDE: chars = [None] * len(chars) g.random.shuffle(chars) # choose boss idx = sync_primitive(g.random.randrange(len(g.players)), g.players) boss = g.boss = g.players[idx] boss.identity = Identity() boss.identity.type = Identity.TYPE.BOSS g.process_action(RevealIdentity(boss, g.players)) boss.choices = [CharChoice(c) for c in chars[:4]] del chars[:4] for p in g.players.exclude(boss): p.choices = [CharChoice(c) for c in chars[:3]] del chars[:3] for p in g.players: p.reveal(p.choices) mapping = {boss: boss.choices} with InputTransaction('ChooseGirl', [boss], mapping=mapping) as trans: c = user_input([boss], ChooseGirlInputlet(g, mapping), 30, 'single', trans) c = c or CharChoice(chars.pop()) c.chosen = boss g.players.reveal(c) trans.notify('girl_chosen', c) # mix it in advance # so the others could see it mixin_character(boss, c.char_cls) boss.skills = list(boss.skills) # make it instance variable ehclasses.extend(boss.eventhandlers_required) # boss's hp bonus if len(g.players) > 5: boss.maxlife += 1 boss.life = boss.maxlife # reseat seed = get_seed_for(g.players) random.Random(seed).shuffle(g.players) g.emit_event('reseat', None) # tell the others their own identity il = list(g.identities) g.random.shuffle(il) for p in g.players.exclude(boss): p.identity = Identity() id = il.pop() if Game.SERVER_SIDE: p.identity.type = id g.process_action(RevealIdentity(p, p)) pl = g.players.exclude(boss) mapping = {p: p.choices for p in pl} # CAUTION, DICT HERE with InputTransaction('ChooseGirl', pl, mapping=mapping) as trans: ilet = ChooseGirlInputlet(g, mapping) ilet.with_post_process(lambda p, rst: trans.notify('girl_chosen', rst) or rst) result = user_input(pl, ilet, type='all', trans=trans) # not enough chars for random, reuse unselected for p in pl: if result[p]: result[p].chosen = p chars.extend([i.char_cls for i in p.choices if not i.chosen]) seed = get_seed_for(g.players) random.Random(seed).shuffle(chars) # mix char class with player --> for p in pl: c = result[p] c = c or CharChoice(chars.pop()) g.players.reveal(c) mixin_character(p, c.char_cls) p.skills = list(p.skills) # make it instance variable p.life = p.maxlife ehclasses.extend(p.eventhandlers_required) g.event_handlers = EventHandler.make_list(ehclasses) g.emit_event('game_begin', g) try: for p in g.players: g.process_action(DrawCards(p, amount=4)) pl = g.players.rotate_to(boss) for i, p in enumerate(cycle(pl)): if i >= 6000: break if not p.dead: try: g.process_action(PlayerTurn(p)) except InterruptActionFlow: pass except GameEnded: pass
def game_start(g): # game started, init state from cards import Deck g.deck = Deck() g.ehclasses = ehclasses = list(action_eventhandlers) + g.game_ehs.values() for i, p in enumerate(g.players): p.identity = Identity() p.identity.type = (Identity.TYPE.HAKUREI, Identity.TYPE.MORIYA)[i % 2] g.forces = forces = BatchList([BatchList(), BatchList()]) for i, p in enumerate(g.players): f = i % 2 p.force = f forces[f].append(p) # choose girls --> from . import characters chars = list(characters.characters) seed = get_seed_for(g.players) random.Random(seed).shuffle(chars) # ANCHOR(test) chars.extend([ # characters.kokoro.Kokoro, ]) choices = [CharChoice(cls) for cls in chars[-16:]] del chars[-16:] for c in choices[:4]: c.char_cls = characters.akari.Akari if Game.SERVER_SIDE: for c, cls in zip(choices[:4], g.random.sample(chars, 4)): # yes, must random.sample c.real_cls = cls # ----- roll ------ roll = range(len(g.players)) g.random.shuffle(roll) pl = g.players roll = sync_primitive(roll, pl) roll = [pl[i] for i in roll] g.emit_event('game_roll', roll) first = roll[0] g.emit_event('game_roll_result', first) # ---- first_index = g.players.index(first) n = len(g.order_list) order = [g.players[(first_index + i) % n] for i in g.order_list] # akaris = {} # DO NOT USE DICT! THEY ARE UNORDERED! akaris = [] mapping = {p: choices for p in g.players} with InputTransaction('ChooseGirl', g.players, mapping=mapping) as trans: for p in order: c = user_input([p], ChooseGirlInputlet(g, mapping), timeout=30, trans=trans) c = c or [_c for _c in choices if not _c.chosen][0] c.chosen = p if issubclass(c.char_cls, characters.akari.Akari): akaris.append((p, c)) else: g.set_character(p, c.char_cls) trans.notify('girl_chosen', c) # reveal akaris if akaris: for p, c in akaris: c.char_cls = c.real_cls g.players.reveal([i[1] for i in akaris]) for p, c in akaris: g.set_character(p, c.char_cls) g.event_handlers = EventHandler.make_list(ehclasses) # ------- for p in g.players: log.info(u'>> Player: %s:%s %s', p.__class__.__name__, Identity.TYPE.rlookup(p.identity.type), p.account.username) # ------- first = g.players[first_index] try: pl = g.players for p in pl: g.process_action(RevealIdentity(p, pl)) g.emit_event('game_begin', g) for p in g.players: g.process_action(DrawCards(p, amount=3 if p is first else 4)) pl = g.players.rotate_to(first) for i, p in enumerate(cycle(pl)): if i >= 6000: break if not p.dead: g.emit_event('player_turn', p) try: g.process_action(PlayerTurn(p)) except InterruptActionFlow: pass except GameEnded: pass log.info(u'>> Winner: %s', Identity.TYPE.rlookup(g.winners[0].identity.type))
def game_start(g): # game started, init state from cards import Deck g.deck = Deck() g.ehclasses = ehclasses = list( action_eventhandlers) + _game_ehs.values() # choose girls init --> from .characters import characters as chars from .characters.akari import Akari chars = list(chars) if Game.CLIENT_SIDE: chars = [None] * len(chars) g.random.shuffle(chars) # choose boss idx = sync_primitive(g.random.randrange(len(g.players)), g.players) boss = g.boss = g.players[idx] boss.identity = Identity() boss.identity.type = Identity.TYPE.BOSS g.process_action(RevealIdentity(boss, g.players)) boss.choices = [CharChoice(c) for c in chars[:4]] boss.choices.append(CharChoice(Akari)) del chars[:4] for p in g.players.exclude(boss): p.choices = [CharChoice(c) for c in chars[:3]] p.choices.append(CharChoice(Akari)) del chars[:3] for p in g.players: p.reveal(p.choices) mapping = {boss: boss.choices} with InputTransaction('ChooseGirl', [boss], mapping=mapping) as trans: c = user_input([boss], ChooseGirlInputlet(g, mapping), 30, 'single', trans) c = c or boss.choices[-1] c.chosen = boss g.players.reveal(c) trans.notify('girl_chosen', c) if c.char_cls is Akari: c = CharChoice(chars.pop()) g.players.reveal(c) # mix it in advance # so the others could see it boss = g.switch_character(boss, c.char_cls) # boss's hp bonus if len(g.players) > 5: boss.maxlife += 1 boss.life = boss.maxlife # reseat seed = get_seed_for(g.players) random.Random(seed).shuffle(g.players) g.emit_event('reseat', None) # tell the others their own identity il = list(g.identities) g.random.shuffle(il) for p in g.players.exclude(boss): p.identity = Identity() id = il.pop() if Game.SERVER_SIDE: p.identity.type = id g.process_action(RevealIdentity(p, p)) pl = g.players.exclude(boss) mapping = {p: p.choices for p in pl} # CAUTION, DICT HERE with InputTransaction('ChooseGirl', pl, mapping=mapping) as trans: ilet = ChooseGirlInputlet(g, mapping) ilet.with_post_process( lambda p, rst: trans.notify('girl_chosen', rst) or rst) result = user_input(pl, ilet, type='all', trans=trans) # not enough chars for random, reuse unselected for p in pl: if result[p]: result[p].chosen = p chars.extend([ i.char_cls for i in p.choices if not i.chosen and i.char_cls is not Akari ]) seed = get_seed_for(g.players) random.Random(seed).shuffle(chars) # mix char class with player --> for p in pl: c = result[p] c = c or p.choices[-1] g.players.reveal(c) if c.char_cls is Akari: c = CharChoice(chars.pop()) g.players.reveal(c) p = g.switch_character(p, c.char_cls) g.event_handlers = EventHandler.make_list(ehclasses) g.emit_event('game_begin', g) try: for p in g.players: g.process_action(DrawCards(p, amount=4)) pl = g.players.rotate_to(boss) for i, p in enumerate(cycle(pl)): if i >= 6000: break if not p.dead: try: g.process_action(PlayerTurn(p)) except InterruptActionFlow: pass except GameEnded: pass
def game_start(self): # game started, init state from cards import Card, Deck, CardList self.deck = Deck() ehclasses = list(action_eventhandlers) + self.game_ehs.values() for i, p in enumerate(self.players): p.cards = CardList(p, 'handcard') # Cards in hand p.showncards = CardList( p, 'showncard' ) # Cards which are shown to the others, treated as 'Cards in hand' p.equips = CardList(p, 'equips') # Equipments p.fatetell = CardList(p, 'fatetell') # Cards in the Fatetell Zone p.special = CardList(p, 'special') # used on special purpose p.showncardlists = [p.showncards, p.fatetell] p.tags = defaultdict(int) p.dead = False p.identity = Identity() p.identity.type = (Identity.TYPE.HAKUREI, Identity.TYPE.MORIYA)[i % 2] self.forces = forces = BatchList([PlayerList(), PlayerList()]) for i, p in enumerate(self.players): f = i % 2 p.force = f forces[f].append(p) # choose girls --> from characters import characters as chars from characters.akari import Akari if Game.SERVER_SIDE: choice = [ CharChoice(cls, cid) for cls, cid in zip(self.random.sample(chars, 16), xrange(16)) ] for c in self.random.sample(choice, 4): c.real_cls = c.char_cls c.char_cls = Akari elif Game.CLIENT_SIDE: choice = [CharChoice(None, i) for i in xrange(16)] # ----------- self.players.reveal(choice) # roll roll = range(len(self.players)) self.random.shuffle(roll) pl = self.players roll = sync_primitive(roll, pl) roll = [pl[i] for i in roll] self.emit_event('game_roll', roll) first = roll[0] self.emit_event('game_roll_result', first) # ---- first_index = self.players.index(first) n = len(self.order_list) order = [self.players[(first_index + i) % n] for i in self.order_list] def mix(p, c): # mix char class with player --> mixin_character(p, c.char_cls) p.skills = list(p.skills) # make it instance variable p.life = p.maxlife ehclasses.extend(p.eventhandlers_required) # akaris = {} # DO NOT USE DICT! THEY ARE UNORDERED! akaris = [] self.emit_event('choose_girl_begin', (self.players, choice)) for i, p in enumerate(order): cid = p.user_input('choose_girl', choice, timeout=(n - i + 1) * 5) try: check(isinstance(cid, int)) check(0 <= cid < len(choice)) c = choice[cid] check(not c.chosen) c.chosen = p except CheckFailed: # first non-chosen char for c in choice: if not c.chosen: c.chosen = p break if issubclass(c.char_cls, Akari): akaris.append((p, c)) else: mix(p, c) self.emit_event('girl_chosen', c) self.emit_event('choose_girl_end', None) # reveal akaris if akaris: for p, c in akaris: c.char_cls = c.real_cls self.players.reveal([i[1] for i in akaris]) for p, c in akaris: mix(p, c) first_actor = first self.event_handlers = EventHandler.make_list(ehclasses) # ------- log.info(u'>> Game info: ') log.info(u'>> First: %s:%s ', first.char_cls.__name__, Identity.TYPE.rlookup(first.identity.type)) for p in self.players: log.info(u'>> Player: %s:%s %s', p.char_cls.__name__, Identity.TYPE.rlookup(p.identity.type), p.account.username) # ------- try: pl = self.players for p in pl: self.process_action(RevealIdentity(p, pl)) self.emit_event('game_begin', self) for p in self.players: self.process_action( DrawCards(p, amount=3 if p is first_actor else 4)) pl = self.players.rotate_to(first_actor) for i, p in enumerate(cycle(pl)): if i >= 6000: break if not p.dead: self.emit_event('player_turn', p) try: self.process_action(PlayerTurn(p)) except InterruptActionFlow: pass except GameEnded: pass log.info(u'>> Winner: %s', Identity.TYPE.rlookup(self.winners[0].identity.type))