def deal_cards(self): if 'pile_tag_to_pile_id' in self.other_properties: card_values = self.to_valuelist() destination_piles_and_cards = self.destination_piles_and_cards if destination_piles_and_cards is not None: all_cards_to_distribute_count = sum([ len(self.other_properties['pile_tag_to_pile_id'][key]) * self._eval_expression(val) for key, val in destination_piles_and_cards.items() ]) if all_cards_to_distribute_count <= len(card_values): starting_index = 0 for key, val in destination_piles_and_cards.items(): n_cards = self._eval_expression(val) for card_pile_id in self.other_properties[ 'pile_tag_to_pile_id'][key]: new_event = gamestate.Event( type='Move', src_pile=self.card_pile_id, dst_pile=card_pile_id, cards=card_values[ starting_index:starting_index + n_cards]) self._update_event_handle(new_event, local_fast_update=False) starting_index += n_cards
def _clear_card(self): new_event = gamestate.Event(type='Remove', src_pile=self.card_pile_id, cards=self.to_valuelist()) self.clear() self._update_event_handle(new_event)
def _on_change_destination_piles_and_cards(self, value): destination_piles_and_cards = self.destination_piles_and_cards if destination_piles_and_cards is not None: new_event = gamestate.Event(type='UIElementChange', dst_pile=self.card_pile_id, property={ 'destination_piles_and_cards': destination_piles_and_cards }) self._update_event_handle(new_event, local_fast_update=False)
def _on_change_num_of_decks_per_generation(self, value): num_of_decks_per_generation = self.num_of_decks_per_generation if num_of_decks_per_generation is not None: new_event = gamestate.Event(type='UIElementChange', dst_pile=self.card_pile_id, property={ 'num_of_decks_per_generation': num_of_decks_per_generation }) self._update_event_handle(new_event, local_fast_update=False)
def flip_card(self, card): new_face = card.face_flipped() new_event = gamestate.Event(type='Flip', player_index=self.self_player_index, cards=[card.value], cards_status={card.value: new_face}) self.event_buffer.append(new_event) self.game_state.update_from_event(new_event) card.face = new_face
def move_cards(self, cards, new_pile): old_pile = self.get_pile_for_card(cards[0]) for i, dropped_card in enumerate(cards): new_pile.add_card(dropped_card) old_pile.remove_card(dropped_card) new_event = gamestate.Event(type='Move', player_index=self.self_player_index, src_pile=old_pile.card_pile_id, dst_pile=new_pile.card_pile_id, cards=[card.value for card in cards]) self.event_buffer.append(new_event) self.game_state.update_from_event(new_event)
def _flip_all_card_face_up(self): """ flip all card face up :return: """ card_update_dict = {} for card in self: card_update_dict[card.value] = 'U' card.face = 'U' new_event = gamestate.Event( type='Flip', cards_status=card_update_dict, ) self._update_event_handle(new_event)
def _flip_all_card(self): card_update_dict = {} for card in self: new_face = card.face_flipped() card_update_dict[ card.value] = new_face #.update({card.value: new_face}) card.face = new_face new_event = gamestate.Event( type='Flip', #cards=list(card_update_dict.keys()), cards_status=card_update_dict, ) self._update_event_handle(new_event)
def _recover_removed_card(self): """ recover previously cleared cards""" card_recovered = self._last_removed_card_values face_status = self._last_removed_face_status for value in card_recovered: self.add_card( Card(value=value, face=self._last_removed_face_status[value])) self._last_removed_card_values = [] self._last_removed_face_status = {} new_event = gamestate.Event(type='Add', dst_pile=self.card_pile_id, cards=card_recovered, cards_status=face_status) self._update_event_handle(new_event)
async def update_from_client(gs: gamestate.GameState, gs_buffer: list, sock: Socket): try: while True: msg = await sock.recv_json(object_hook=gamestate.json_obj_hook) print(msg) counter = msg['counter'] event_dict = msg['event'] #print(event_dict) # update game sate #print({key: val for key, val in gs.cards_in_pile.items() if key != 0}) new_gs_ls = gs.update_from_event(gamestate.Event(**event_dict)) if new_gs_ls: gs_buffer += new_gs_ls #print('***') #print(gs) # event_dict = await sock.recv_json() #print(f'Got event dict: {event_dict}') except asyncio.CancelledError: pass except Exception as e: print(e)