def __init__(self, connection, user_id, auth_key, access_token, user_prompt, game_item_reader=None, vk=None): self.__connection = connection self.__session = Session(user_id, auth_key, client_version=Game.CLIENT_VERSION ) factory = message_factory.Factory(self.__session, None) self.__request_sender = RequestSender(factory, self.__connection) self.__game_events_sender = GameEventsSender(self.__request_sender) self.__timer = GameTimer() self.__game_initializer = GameInitializer(self.__timer, self.get_request_sender(), factory, access_token, self.__session, vk) # load items dictionary if game_item_reader is None: self.__itemReader = GameItemReader() self.__itemReader.download('items.txt') self.__itemReader.read('items.txt') else: self.__itemReader = game_item_reader self.__user_prompt = user_prompt self.__selected_seed = None self.__receive_gifts_with_messages = False self.__receive_non_free_gifts = False
from game_state.item_reader import GameItemReader if __name__ == '__main__': reader = GameItemReader() reader.read('items.txt') reader.pretty_write('pretty_items.txt')
def setUpClass(cls): cls.game_item_reader = GameItemReader() cls.game_item_reader.read('items.txt')
def testGameState(self): start_response = dict2obj({ "cmd": "START", "id": "1", "state": { "registerDate": "-2147483648", "playerStatus": "@PS_ZOMBIE", "dailyBonus": {}, "mailBonus": { "bonuses": [], "acceptedBonuses": [] }, "magic": { "expire": "80000000", "used": 0 }, "definedBonuses": [ "ITEM_ID", ], "playerSettings": { "userName": "******", "dressId": "ITEM_ID", "hatId": "ITEM_ID" }, "gameSettings": { "sound": False, "music": False, "tutorialState": 0 }, "brainsCount": 3, "buyedBrains": [], "experience": 50000, "level": 20, "gameMoney": 1000000, "cashMoney": 10, "gameMoneyReal": 0, "cashMoneyReal": 0, "burySlots": [ { "enabled": True }, ], "shopOpened": ["P_09"], "collectionItems": { "C_ITEM_ID": 2, }, "giftId": 3000, "gifts": [], "receivedGiftsExpire": "80000000", "receivedGiftsCoins": 0, "freeGiftUsers": [], "storageItems": [], "remoteTreasure": [], "treasureHide": "-80000000", "treasureExpire": "80000000", "treasureCount": 0, "remoteTrickTreating": [], "remoteThanksgiving": [], "remoteNewYearExpire": "80000000", "remoteNewYear": [], "remoteFertilizeFruitTreeCount": 0, "remoteFertilizeFruitTree": [], "wishlist": [None, None, None, None, None], "buyedClothing": ["ITEM_ID"], "retentionBonuses": False, "npcs": { "npcClientId": 1 }, "buffs": { "list": [] } }, "params": { "event": { "action": 'gameState', "locationId": 'main', "haveTreasure": True, "isAway": False, "haveAttempts": True, "treasureRehide": 100, "haveTrickTreating": True, "haveThanksgivingAttempt": True, "haveRemoteFertilizeFruit": True, "playerStatus": "@PS_ZOMBIE", "locationInfos": [], "wishlist": [], "location": {}, "playerSettings": { "userName": "******", "dressId": "ITEM_ID", "hatId": "ITEM_ID" }, }, "magicLimit": 500 } }) item = start_response.params.event.wishlist self.assertEqual([], item) game_items = GameItemReader() game_items.download('items.txt') game_items.read('items.txt') location = start_response.params.event.playerStatus self.assertEqual(u"Зомби", (game_items.get(location).name))
class Game(): CLIENT_VERSION = long(1352868088) def __init__(self, connection, user_id, auth_key, access_token, user_prompt, game_item_reader=None, vk=None): self.__connection = connection self.__session = Session(user_id, auth_key, client_version=Game.CLIENT_VERSION ) factory = message_factory.Factory(self.__session, None) self.__request_sender = RequestSender(factory, self.__connection) self.__game_events_sender = GameEventsSender(self.__request_sender) self.__timer = GameTimer() self.__game_initializer = GameInitializer(self.__timer, self.get_request_sender(), factory, access_token, self.__session, vk) # load items dictionary if game_item_reader is None: self.__itemReader = GameItemReader() self.__itemReader.download('items.txt') self.__itemReader.read('items.txt') else: self.__itemReader = game_item_reader self.__user_prompt = user_prompt self.__selected_seed = None self.__receive_gifts_with_messages = False self.__receive_non_free_gifts = False def select_plant_seed(self): level = self.get_game_state().level location = self.get_game_loc().get_location_id() seed_reader = GameSeedReader(self.__itemReader) available_seeds = seed_reader.getAvailablePlantSeedsDict(level, location) if self.__selected_seed is None: seed_name = self.__user_prompt.prompt_user('Plant to seed:', available_seeds.keys()) self.__selected_seed = available_seeds[seed_name] def start(self): while(True): try: start_response = self.__game_initializer.start() self.save_game_state(start_response) self.select_plant_seed() self.create_all_actors() # TODO send getMissions # TODO handle getMissions response self.eventLoop() except socket.timeout: seconds = 3 logger.error('Timeout occurred, retrying in %s seconds...' % seconds) time.sleep(seconds) def save_game_state(self, start_response): # parse game state self.__game_state_ = GameState(start_response, self.__itemReader) def get_game_loc(self): return self.__game_state_.get_game_loc() def get_game_state(self): return self.__game_state_.get_state() def eventLoop(self): ''' in a loop, every 30 seconds send EVT request handle EVT response ''' while(True): self.__game_events_sender.send_game_events() self.__game_events_sender.print_game_events() for event in self.__game_events_sender.get_game_events(): self.handleEvent(event) self.perform_all_actions() time.sleep(30) def create_all_actors(self): receive_options = {'with_messages': self.__receive_gifts_with_messages, 'non_free': self.__receive_non_free_gifts} options = {'GiftReceiverBot': receive_options, 'SeederBot': self.__selected_seed} events_sender = self.__game_events_sender timer = self._get_timer() item_reader = self.__itemReader game_state = self.__game_state_ actor_classes = [ Pickuper, BoxPickuper, GiftReceiverBot, HarvesterBot, SeederBot, RouletteRoller, WoodPicker, WoodTargetSelecter, ] self.__actors = [] for actor_class in actor_classes: self.__actors.append( actor_class(item_reader, game_state, events_sender, timer, options)) def perform_all_actions(self): ''' Assumes that create_all_actors is called before ''' for actor in self.__actors: actor.perform_action() def handleEvent(self, event_to_handle): if event_to_handle.action == 'addGift': AddGiftEventHandler(self.get_game_state()).handle(event_to_handle) elif event_to_handle.action == 'add': if event_to_handle.type == 'pickup': AddPickupHandler(self.get_game_loc()).handle(event_to_handle) elif event_to_handle.type == GameFertilizePlant.type: PlantEventHandler(self.get_game_loc()).handle(event_to_handle) elif event_to_handle.type == GamePlayGame.type: GameResultHandler(self.__itemReader, self.get_game_loc()).handle(event_to_handle) elif event_to_handle.type == GameStartGainMaterial.type: GainMaterialEventHandler(self.__itemReader, self.get_game_loc(), self.__timer).handle(event_to_handle) else: self.logUnknownEvent(event_to_handle) self.__game_events_sender.remove_game_event(event_to_handle) def logUnknownEvent(self, event_to_handle): logger = logging.getLogger('unknownEventLogger') logger.info(pprint.pformat(obj2dict(event_to_handle))) def _get_timer(self): return self.__timer def get_request_sender(self): return self.__request_sender
def testGameState(self): start_response = dict2obj({ "cmd": "START", "id": "1", "state": { "registerDate": "-2147483648", "playerStatus": "@PS_ZOMBIE", "dailyBonus": {}, "mailBonus": { "bonuses": [], "acceptedBonuses": [] }, "magic": {"expire": "80000000", "used": 0}, "definedBonuses": ["ITEM_ID", ], "playerSettings": {"userName": "******", "dressId": "ITEM_ID", "hatId": "ITEM_ID"}, "gameSettings": {"sound": False, "music": False, "tutorialState": 0}, "brainsCount": 3, "buyedBrains": [], "experience": 50000, "level": 20, "gameMoney": 1000000, "cashMoney": 10, "gameMoneyReal": 0, "cashMoneyReal": 0, "burySlots": [{"enabled": True}, ], "shopOpened": ["P_09"], "collectionItems": {"C_ITEM_ID": 2, }, "giftId": 3000, "gifts": [], "receivedGiftsExpire": "80000000", "receivedGiftsCoins": 0, "freeGiftUsers": [], "storageItems": [], "remoteTreasure": [], "treasureHide": "-80000000", "treasureExpire": "80000000", "treasureCount": 0, "remoteTrickTreating": [], "remoteThanksgiving": [], "remoteNewYearExpire": "80000000", "remoteNewYear": [], "remoteFertilizeFruitTreeCount": 0, "remoteFertilizeFruitTree": [], "wishlist": [None, None, None, None, None], "buyedClothing": ["ITEM_ID"], "retentionBonuses": False, "npcs": {"npcClientId": 1}, "buffs": {"list": []} }, "params": { "event": { "action": 'gameState', "locationId": 'main', "haveTreasure": True, "isAway": False, "haveAttempts": True, "treasureRehide": 100, "haveTrickTreating": True, "haveThanksgivingAttempt": True, "haveRemoteFertilizeFruit": True, "playerStatus": "@PS_ZOMBIE", "locationInfos": [], "wishlist": [], "location": {}, "playerSettings": {"userName": "******", "dressId": "ITEM_ID", "hatId": "ITEM_ID"}, }, "magicLimit": 500 } }) item = start_response.params.event.wishlist self.assertEqual([], item) game_items = GameItemReader() game_items.download('items.txt') game_items.read('items.txt') location = start_response.params.event.playerStatus self.assertEqual(u"Зомби", (game_items.get(location).name))