class MeoTech(ShowBase): """MeoTech Main Class. Handles the setup of the whole app. Basically connects the MeoTech Engine with the Game Engine. """ def __init__(self): # Create the main app Log file log.basicConfig(filename="MeoTech.log", level=log.DEBUG, format="%(asctime)s %(levelname)s: %(message)s", datafmt="%d-%m-%Y %H:%M:%S") # Init Panda ShowBase.__init__(self) # Init Engine self.engine = Engine(self) # Init Game self.game = Game(self) # Debug stuff wantDebug = True if wantDebug: self.engine.showBulletDebug() print " " print "Panda Render.ls()" print "--------------------------------------------------------" print render.ls() print "--------------------------------------------------------"
def load_game(file_path, is_save, scr_wd=80, scr_ht=40, fov_mode=False, debug=False): with open(file_path, 'r', encoding='utf-8') as file: data = json.load(file) if is_save: engine = Serializer.deserialize_engine(data) else: engine = Engine(screen_width=scr_wd, screen_height=scr_ht, fov_mode=fov_mode, debug=debug) engine.map.walls = [] engine.map.free_all_cells() for x, y in data['coords']: new_wall = Wall(x, y, 1, 1) engine.map.walls.append(new_wall) engine.map.cells[y][x].block() engine.player = EngineInitializer.init_player( engine, engine.load_type) engine.mobs = EngineInitializer.init_entities( engine, engine.load_type) return engine
def test_player_stats(self): e = Engine( 'CoinPoker', 1, { 1: { 'name': 'joe', 'balance': 1000, 'status': 1 }, 2: { 'name': 'jane', 'balance': 1000, 'status': 1 }, }, 50, 100, 0, ) e.available_actions() stats = ES.player_stats(e, e.s) assert 'actions' in stats assert len(stats['actions']) >= 4 assert 0 < stats['hs'] < 100
def retrieve_image(self): Engine.retrieve_image(self) self.web_driver.get(Baidu.HOME_URL) key_input_element = self.web_driver.find_element_by_id('kw') key_input_element.send_keys(self.key) key_input_element.submit() self.web_driver.find_elements_by_css_selector( "[class='imgbox']")[0].click() # navi_url = self.web_driver.find_elements_by_class_name('main_img img-hover') # self.web_driver.get(navi_url.get_attribute('src')) time.sleep(2) self.web_driver.switch_to.window(self.web_driver.window_handles[-1]) url = self.web_driver.find_element_by_class_name("currentImg") current_index = 0 while url is not None: url = url.get_attribute('src') # image_format = url.split('.')[-1] image_format = re.findall(r'fmt=.*\?', url)[0][4:-1] print('IMAGE URL: ', url) image_name = str(current_index) + '.' + image_format if Util.download_image(url, self.storing_folder, image_name): current_index += 1 self.web_driver.find_elements_by_class_name( "img-switch-btn")[1].click() time.sleep(1.5) url = self.web_driver.find_element_by_class_name("currentImg")
def autopilot_mode(): print("Autopilot mode has been activated.") configuration_parser.read('./config/configuration.txt') configuration = configuration_parser['Files'] counter = read_actions(configuration) engine = Engine(configuration, counter, actions) engine.execute()
def launch_engine(): if request.method == 'POST' and check_request(request.json): url = request.json['url'] username = request.json['username'] password = request.json['password'] location = request.json['location'] text = ScraperManager().get_text_content(url) # if the scrap is too long if text is None: return json.dumps({'send': False, 'error': 'scrap'}) engine = Engine(text=text, username=username, password=password, location=location) data = engine.run() if data is not None: return json.dumps({ 'send': True, 'success': True, 'text': data.get('text'), 'pure': data.get('pure'), 'yago': data.get('yago') }) return json.dumps({'send': True, 'success': False}) return json.dumps({'send': False, 'error': 'error'})
def __init__(self): welcome("Server") arguments = Arguments() file_manager = File_Manager(dir=arguments.destination) print(arguments.video) graphical_user_interface = GUI() ENGINE = Engine(self, graphical_user_interface, file_manager, arguments) fps_counter = FPS_extractor() data_acquisition = DAQ_extractor(file_manager.new_folderpath) extractors = [fps_counter, data_acquisition] ENGINE.load_extractors(extractors) try: print("Initiating tracking via {}".format(arguments.importer)) import_command = "from importers.{} import Importer".format( arguments.importer) exec(import_command, globals()) except Exception as e: print("Invalid importer selected.\n", e) importer = Importer(ENGINE) importer.route()
def main(library_file: str, scenario_file: str, query_file: str = None): domain_desc = parsing.domain_description.parse_file(library_file) print('Parsed domain description:') for statement in domain_desc.statements: print(statement) scenario = parsing.scenario.parse_file(scenario_file) print('Parsed scenario:') for statement in scenario.observations: print(statement) for statement in scenario.action_occurrences: print(statement) queries = parsing.query.parse_file(query_file) print('Parsed queries:') for statement in queries: print(statement) engine = Engine() if len(queries) == 0: print("The library and the scenario are valid.") if engine.run(scenario=scenario, domain_desc=domain_desc): print('After the engine ran We found', len(engine.models), 'models') i = 0 for model in engine.models: print('Final model:', i, '\n', model) print('Action history for model', i, 'is:', model.action_history) i += 1 if len(engine.models) != 0: for query in queries: print('Query:', query, 'was evaluated to:', query.validate(engine.models, scenario)) else: for query in queries: print('Query:', query, 'was evaluated to:', query.validate(engine.models, scenario))
def test_longer_runs_are_better(self): e = Engine( 'CoinPoker', 1, { 1: {'name': 'joe', 'balance': 1000, 'status': 1}, 2: {'name': 'jane', 'balance': 1000, 'status': 1}, }, 50, 100, 0, ) e.available_actions() mc = MonteCarlo(e, 1) mc.run(10) assert len(mc.current_actions) > 0 total_traversions = sum(a[2] for a in mc.current_actions) assert total_traversions > 1, 'Must have traversed something!' total_traversions_batches = 0 for n in range(1, 11): mc = MonteCarlo(e, 1) mc.run(1) assert len(mc.current_actions) > 0 total_traversions_batches += sum(a[2] for a in mc.current_actions) assert total_traversions_batches > 1, 'Must have traversed something!' assert total_traversions > total_traversions_batches
def test_click_bomb(): e = Engine(10, 10, 10) for i in range(10): for j in range(10): if not e.check_location(i, j): return assert False, "Clicking bomb spaces did not return false."
class TestCityUnits(unittest.TestCase): def setUp(self): self.engine = Engine(DummyRequest(TEST_UID)) self.engine.start() def testMoveUnits(self): post = {'city_id': 1, 'unit_1': 1, 'unit_2': '1', } self.assertTrue(self.engine.city.id) # Validate start_city_id post = {'city_id': '3dupa', 'unit_1': 1, 'unit_2': '1', } self.assertFalse(self.engine.city.move_units(post)) # Validate units (POST) post = {'city_id': 1, 'unit_1': '3dupa', 'unit_2': '1', } self.assertFalse(self.engine.city.move_units(post)) post = {'city_id': 1, 'unit_1': 0, 'unit_2': -10, } self.assertFalse(self.engine.city.move_units(post)) post = {'city_id': 1, 'unit_1': 3, 'unit_2': 6, } self.assertFalse(self.engine.city.move_units(post)) post = {'city_id': 1, 'unit_1': 1, 'unit_2': '1', 'unit_66': 2, } self.assertFalse(self.engine.city.move_units(post)) post = {'city_id': 1, 'unit_1': 1, 'unit_2': '1', } self.assertTrue(self.engine.city.move_units(post)) def testMoveRoute(self): from common.helpers import calc_route self.assertEqual(calc_route(2, 1, 2, 100), 19) self.assertEqual(calc_route(200, 55, 55, 1), 1460) self.assertEqual(calc_route(1, 1, 55, 100), 559)
def retrieve_image(self): Engine.retrieve_image(self) self.web_driver.get(Bing.HOME_URL) search_box = self.web_driver.find_element_by_class_name("b_searchbox") search_box.send_keys(self.key) search_box.submit() self.web_driver.find_elements_by_css_selector( "[class='mimg']")[0].click() time.sleep(2) current_url = self.web_driver.current_url self.web_driver.get(current_url) img = self.web_driver.find_elements_by_css_selector( "div.imgContainer > img")[0] current_index = 0 while img is not None: img_src = img.get_attribute('src') image_format = 'jpg' print('IMAGE URL: ', img_src) image_name = str(current_index) + '.' + image_format if Util.download_image(img_src, self.storing_folder, image_name): current_index += 1 self.web_driver.find_element_by_id("navr").click() time.sleep(2) img = self.web_driver.find_elements_by_css_selector( "div.imgContainer > img")[0]
def __init__(self): # Create the main app Log file log.basicConfig(filename="MeoTech.log", level=log.DEBUG, format="%(asctime)s %(levelname)s: %(message)s", datafmt="%d-%m-%Y %H:%M:%S") # Init Panda ShowBase.__init__(self) # Init Engine self.engine = Engine(self) # Init Game self.game = Game(self) # Debug stuff wantDebug = True if wantDebug: self.engine.showBulletDebug() print " " print "Panda Render.ls()" print "--------------------------------------------------------" print render.ls() print "--------------------------------------------------------"
def test_model_impossible_condition_case(self): scenario = parsing.scenario.parse_file( "../test_models/model_impossible_condition_case_scenario.txt") domain = parsing.domain_description.parse_file( "../test_models/model_impossible_condition_case_domain.txt") engine = Engine() engine.run(scenario, domain) self.assertEqual(1, len(engine.models))
def test_three_initial_model_case(self): scenario = parsing.scenario.parse_file( "../test_models/three_initial_model_case_scenario.txt") domain = parsing.domain_description.parse_file( "../test_models/three_initial_model_case_domain.txt") engine = Engine() engine.run(scenario, domain) self.assertEqual(3, len(engine.models))
def refreshReferenceData(self): self.custodyDictOID = Engine.getCustodyDictOID() self.corporateEventTypeOID = Engine.getCorporateEventTypeDictOID() self.assetDictOID = Engine.getAssetDictOID() USDMXN = PricingInterface.getExchangeRateByCurrency('USD', 'MXN') if USDMXN == 0: USDMXN = 1 self.setUSDMXN(USDMXN)
def test_model_destruction_observation_case(self): scenario = parsing.scenario.parse_file( "../test_models/model_destruction_observation_case_scenario.txt") domain = parsing.domain_description.parse_file( "../test_models/model_destruction_observation_case_domain.txt") engine = Engine() engine.run(scenario, domain) self.assertEqual(0, len(engine.models))
class Chess: def __init__(self): self.engine = Engine() self.board = Board() self.engine.add_entities(self.board) def start_game(self): self.engine.run()
def main(clean_directory="resources/clean_data"): initial_time = time.time() clean_files = [ f for f in listdir(clean_directory) if isfile(join(clean_directory, f)) and "2" in f ] for file in clean_files: results = [] print(f"processing {file} file") dataset = Dataset(f"{clean_directory}/{file}") data_points = dataset.get_data() count = 1 for data_point in data_points: start = time.time() print(f"\tlooking at {count}:{len(data_points)}") chess_engine = Engine(data_point.board, data_point.color, depth=data_point.mate_in * 2, algorithm="alpha-beta") result_moves = chess_engine.find_next_move() moves_to_mate = len(result_moves) // 2 + 1 duration = (time.time() - start) print( f"\t\tfound mate in {moves_to_mate} moves; expecting {data_point.mate_in} in {duration} seconds" ) result_moves_string = [str(move) for move in result_moves] results.append( (data_point.board.fen(), "->".join(result_moves_string), moves_to_mate, data_point.mate_in, duration)) count += 1 # if count > 10: # break output_name = file.split("/")[-1].split(".")[0] output_path = f"resources/results/{output_name}_results.csv" print(f"downloading results to {output_path}") with open(output_path, "w") as f: f.write( "fen,sequence_of_moves,found_mate,best_mate,difference,compute_duration\n" ) for result in results: f.write( f"{result[0]},{result[1]},{result[2]},{result[3]},{int(result[3]) - result[2]},{result[4]}\n" ) print("_" * 80) print(f"total time: {(time.time() - initial_time)/60} minutes")
def setUp(self): self.controller_1 = ManualController(0) self.controller_2 = ManualController(1) self.user_1 = User(0, self.controller_1) self.user_2 = User(1, self.controller_2) self.engine = Engine() self.engine.add_user(self.user_1) self.engine.add_user(self.user_2) self.engine.deck = Deck(shuffle=False, is_small=False)
def setPositionDict(self, fromDate, toDate, setLastMarketData): from engine.engine import Engine today = datetime.now().date() self.startPositionDict = Engine.buildPositions(date(1900, 1, 1), fromDate, False) if len(self.startPositionDict) == 0: logging.warning("Empty start position dict") self.finalPositionDict = Engine.buildPositions(date(1900, 1, 1), toDate, setLastMarketData) if len(self.finalPositionDict) == 0: logging.warning("Empty final position dict")
def test_display_board(): b1 = np.array([[Spaces.BOMB, Spaces.UNKNOWN], [Spaces.BOMB, Spaces.BOMB]]) e = Engine(10, 10, 10) e._board = b1 dboard = e.get_display_board() assert dboard[0, 0] == Spaces.UNKNOWN assert dboard[0, 1] == Spaces.UNKNOWN assert dboard[1, 0] == Spaces.UNKNOWN assert dboard[1, 1] == Spaces.UNKNOWN
def test_click_flag(): e = Engine(10, 10, 1) orig_space = e.get_display_board()[0, 0] e.toggle_flag(0, 0) assert e.get_display_board()[0, 0] == Spaces.FLAG e.check_location(0, 0) assert e.get_display_board()[0, 0] == orig_space
def test_toggle_flag(): e = Engine(10, 10, 10) orig_space = e.get_display_board()[0, 0] e.toggle_flag(0, 0) assert e.get_display_board()[0, 0] == Spaces.FLAG e.toggle_flag(0, 0) assert e.get_display_board()[0, 0] == orig_space
def _generate_one_scenerio(self): self._games += 1 if self._games % 1000 == 0: print('{:,} complete'.format(self._games)) self._engine = Engine(12, 12, 30) first_move = np.floor( np.multiply(np.random.rand(1, 2), np.array([[10, 10]]))).astype(int)[0] self._engine.check_location(first_move[0], first_move[1]) daters = [self._get_next_data(self._engine) for i in range(10)] return daters
def __init__(self, table): ''' Create an engine for this game monte carlo will use the engine instances for iterations Game will only keep track of this current game First get actions from engine, then the repr ''' self.engine = Engine(table.site_name, table.button, table.players, table.sb, table.bb) self.history = {} self.cursor = 0
def __init__(self): self.preprocess = PreProcessing() self.engine = Engine() # model model = self.engine.nn_train_fit() test_data = self.preprocess.get_test_data() prediction = model.predict(test_data) print(prediction) self.save_submit(prediction)
def start_threads(): global frozen if not frozen: return 'Already running' frozen = False for i in range(1): e = Engine(event=event, tid=i) e.start() threads.append(e) return 'Started'
def main(): e = Engine(Settings()) r = SdlRenderer(e.renderer_core) c = SdlActionListener(e.controller) c.start() r.start() e.execute() r.stop() c.stop()
class TestCityUnits(unittest.TestCase): def setUp(self): self.engine = Engine(DummyRequest(TEST_UID)) self.engine.start() def tearDown(self): pass def testMoveUnits1(self): post = {} self.assertTrue(self.engine.city.id) self.assertTrue(self.engine.city.move_units(post))
def test_player_stats(self): e = Engine( 'CoinPoker', 1, { 1: {'name': 'joe', 'balance': 1000, 'status': 1}, 2: {'name': 'jane', 'balance': 1000, 'status': 1}, }, 50, 100, 0, ) e.available_actions() stats = ES.player_stats(e, e.s) assert 'actions' in stats assert len(stats['actions']) >= 4 assert 0 < stats['hs'] < 100
def start_game(self, deck=None): self.master.geometry('1100x250') deck = Deck() if deck is None else deck self.engine = Engine(deck) for user in self.users: self.engine.add_user(user) self.engine.init_game() self.engine.update_users() for user in self.engine.get_active_users(): self.active_users_queue.put(user) user = self.active_users_queue.get() self.engine.process_turn( user, user.make_turn(*self.engine.get_game_table_info())) if self.engine.is_ended(): self.engine.outcomes_notify(self.engine.generate_outcomes())
class TestCityUnits(unittest.TestCase): def setUp(self): self.engine = Engine(DummyRequest(TEST_UID)) self.engine.start() def tearDown(self): pass def testMoveUnits1(self): post = { } self.assertTrue(self.engine.city.id) self.assertTrue(self.engine.city.move_units(post))
def finish_auction(self): # Refund if len(self.bids) == 0: if self.auction.seller != SELLER: self.give_item(self.seller) return True # Buyer if not self.auction.is_buy_now or self.auction.buyer_id == 0: bid = self.bids[0] self.buyer = Engine(DummyRequest(bid.buyer.id)) self.buyer.start() self.buyer.user.profile.earn('cash', bid.max_price - bid.price) bid.is_refunded = True bid.save() else: self.buyer = Engine(DummyRequest(self.auction.buyer_id)) self.buyer.start() self.give_item(self.buyer) # Seller if not self.auction.is_buy_now or self.auction.buyer_id == 0: price = self.auction.current_price self.seller.user.profile.earn('cash', self.auction.current_price) else: price = self.auction.buy_it_now_price self.seller.user.profile.earn('cash', self.auction.buy_it_now_price) self.seller.notify.add(type='auction', key='income', value=price, date=None) self.buyer.notify.add(type='auction', key='outcome', value=price, date=None) self.seller.notify.add(type='auction', key='transaction', value=1, date=None) self.buyer.notify.add(type='auction', key='transaction', value=1, date=None) if len(self.bids) == 1: return True if not self.auction.is_buy_now or self.auction.buyer_id == 0: self.bids = self.bids[1:] for bid in self.bids: if bid.is_refunded == True: continue profile = UserProfile.objects.get_by_id(bid.buyer.id) profile.earn('cash', bid.max_price) bid.is_refunded = True bid.save()
def __init__(self): # Create the main app Log file log.basicConfig( filename="MeoTech.log", level=log.DEBUG, format="%(asctime)s %(levelname)s: %(message)s", datafmt="%d-%m-%Y %H:%M:%S") # Init Panda ShowBase.__init__(self) # Init Engine self.engine = Engine(self) # Init Game self.game = Game(self) # Debug stuff if wantDebug: self.engine.showBulletDebug() print " " print "Panda Render.ls()" print "--------------------------------------------------------" #print render.ls() print "--------------------------------------------------------" print base.win.getGsg().getSupportsShadowFilter() print base.win.getGsg().getSupportsBasicShaders()
def __init__(self): # Create the main app Log file log.basicConfig( filename="main.log", level=log.DEBUG, format="%(asctime)s %(levelname)s: %(message)s", datafmt="%d-%m-%Y %H:%M:%S") # Init Panda ShowBase.__init__(self) self.disableMouse() # Init Engine self.engine = Engine(self) # Init Game self.game = Game(self) # Debug stuff wantDebug = False # Run the debug stuffww aswell. #? Setup a proper debug if wantDebug: #do = DirectObject() #do.accept("f11", self.engine.gm.setFullscreen, [True]) #do.accept("f12", self.engine.gm.setFullscreen, [False]) self.engine.showBulletDebug() print " " print "Panda Render.ls()" print "--------------------------------------------------------" print render.ls() print "--------------------------------------------------------"
def end_session(self, engine=None): from common.helpers.core import exp_mod from common.models import DummyRequest from engine.engine import Engine self.is_active = False self.save() attacker, defender = UserProfile.objects.get_by_id(self.attacker.id), UserProfile.objects.get_by_id( self.defender.id) attacker_mod, defender_mod = exp_mod(attacker.level, defender.level), exp_mod(defender.level, attacker.level) attacker_engine = engine defender_engine = Engine(DummyRequest(self.defender.id)) defender_engine.start() for r in self.result: if r == 'W': v1 = settings.EXP['attacker_win'] + (settings.EXP['attacker_win'] * attacker_mod) v2 = settings.EXP['defender_lost'] + (settings.EXP['defender_lost'] * defender_mod) attacker_engine.notify.add(user=attacker.user, type='fight', key='win', date='today') attacker_engine.notify.add(user=attacker.user, type='fight', key='win', date=None) defender_engine.notify.add(user=defender.user, type='fight', key='lost', date='today') defender_engine.notify.add(user=defender.user, type='fight', key='lost', date=None) elif r == 'D': v1 = settings.EXP['attacker_draw'] + (settings.EXP['attacker_draw'] * attacker_mod) v2 = settings.EXP['defender_draw'] + (settings.EXP['defender_draw'] * defender_mod) attacker_engine.notify.add(user=attacker.user, type='fight', key='draw', date='today') attacker_engine.notify.add(user=attacker.user, type='fight', key='draw', date=None) defender_engine.notify.add(user=defender.user, type='fight', key='draw', date='today') defender_engine.notify.add(user=defender.user, type='fight', key='draw', date=None) elif r == 'L': v1 = settings.EXP['attacker_lost'] + (settings.EXP['attacker_lost'] * attacker_mod) v2 = settings.EXP['defender_win'] + (settings.EXP['defender_win'] * defender_mod) attacker_engine.notify.add(user=attacker.user, type='fight', key='lost', date='today') attacker_engine.notify.add(user=attacker.user, type='fight', key='lost', date=None) defender_engine.notify.add(user=defender.user, type='fight', key='win', date='today') defender_engine.notify.add(user=defender.user, type='fight', key='win', date=None) attacker.earn('exp', int(v1), False, engine=attacker_engine) defender.earn('exp', int(v2), False, engine=defender_engine) attacker.job_id = 0 attacker.save() defender.save()
def main(): parser = Parser() cities = parser.parse_cities("data/city.json") flights = parser.parse_flights("data/flight.json") customers = parser.parse_customers("data/customer.json") articles = parser.parse_articles("data/article.json") tags = parser.parse_tags("data/tags.json") engine = Engine(customers, articles, tags, flights, cities) print_customers(customers, parser.customer_schema) cid = dialog_customer(customers, parser.customer_schema) ar = engine.articlerating_customer(cid) show_best_articles(ar, articles, parser.article_schema) return True
def test_threading(self): e = Engine( 'CoinPoker', 1, { 1: {'name': 'joe', 'balance': 1000, 'status': 1}, 2: {'name': 'jane', 'balance': 1000, 'status': 1}, }, 50, 100, 0, ) e.available_actions() perfs = {} for n in range(1, 4): mc = MonteCarlo(e, 1) mc.N_THREADS = n mc.run(2) assert len(mc.current_actions) > 0 total_traversions = sum(a[2] for a in mc.current_actions) assert total_traversions > 1, 'Must have traversed something!' perfs[n] = total_traversions assert perfs
def test_removing_path_earlier_is_easier(self): e = Engine( 'CoinPoker', 1, { 1: {'name': 'joe', 'balance': 1000, 'status': 1}, 2: {'name': 'jane', 'balance': 1000, 'status': 1}, 3: {'name': 'mike', 'balance': 1000, 'status': 1}, 4: {'name': 'mark', 'balance': 1000, 'status': 1}, }, 50, 100, 0, ) e.available_actions() total_traversions = [] mc = MonteCarlo(e, 1) for _ in range(10): mc.run(1) total_traversions.append(sum(a[2] for a in mc.current_actions)) # assert len(mc.current_actions) > 0 traversed_leaves = list(mc.tree.filter_nodes(lambda n: n.is_leaf() and n.data['traversed'] > 0)) assert len(traversed_leaves) == total_traversions[-1] assert total_traversions[-1] > 5, 'Must have traversed something!'
class MeoTech(ShowBase): """MeoTech Main Class. Handles the setup of the whole app. Basically connects the MeoTech Engine with the Game Engine. """ def __init__(self): # Create the main app Log file log.basicConfig( filename="MeoTech.log", level=log.DEBUG, format="%(asctime)s %(levelname)s: %(message)s", datafmt="%d-%m-%Y %H:%M:%S") # Init Panda ShowBase.__init__(self) # Init Engine self.engine = Engine(self) # Init Game self.game = Game(self) # Debug stuff wantDebug = True if wantDebug: self.engine.showBulletDebug() print " " print "Panda Render.ls()" print "--------------------------------------------------------" print render.ls() print "--------------------------------------------------------"
def start(self): auctions = AuctionModel.objects.filter(end_at__lt=datetime.datetime.now, is_refunded=False) for self.auction in auctions: logging.debug("Finishing auction of %s" % self.auction.title) self.seller = Engine(DummyRequest(self.auction.seller.id)) self.seller.start() self.buyer = None self.bids = AuctionOffer.objects.get_by_auction(auction=self.auction) self.finish_auction() self.auction.is_refunded = True if self.buyer: self.auction.buyer_id = self.buyer.user.profile.user_id self.auction.save()
from algorithms.image.gaussian_filter_factory import GaussianFilterFactory from skimage import io as skio if __name__ == '__main__': print("=== Gaussian Filter Example ===") sigma = 2. run_type = HADOOP data_file = 'hdfs:///user/linda/ml/data/test-images' if run_type == HADOOP else '../data/test-images' print( "\n data: " + data_file + "\n sigma: " + str(sigma) + "\n run type: " + run_type + "\n" ) # 1. define algorithm pred_nn = GaussianFilterFactory(sigma) # 2. set data handler (pre-processing, normalization, data set creation) data_handler = ImageDataHandler() # 3. run engine = Engine(pred_nn, data_file, data_handler=data_handler, verbose=True) gaussian_filter = engine.start(_run_type=run_type) skio.imsave('cat_gaussian.png', gaussian_filter.filtered[0])
def setUp(self): self.engine = Engine(DummyRequest(TEST_UID)) self.engine.start()
def catch_all(path): engine = Engine() return engine.run()
def test_hs_percentile(self): e = Engine( 'CoinPoker', 1, { 1: {'name': 'joe', 'balance': 1000, 'status': 1}, 2: {'name': 'jane', 'balance': 1000, 'status': 1}, }, 50, 100, 0, ) avail = e.available_actions() # p1 sb e.do(['c']) avail = e.available_actions() # p2 bb e.do(['k']) avail = e.available_actions() # p2 e.do(['b', 50]) avail = e.available_actions() # p1 e.do(['c']) avail = e.available_actions() # p2 e.do(['b', 100]) avail = e.available_actions() # # p1 # e.do('c') # avail = e.available_actions() # # # p2 # e.do(['b', 150]) # avail = e.available_actions() percentile = 50 history = [] ev = 1 while ev > 0 and percentile <= 60: mc = MonteCarlo(e, 1) mc.PERCENTILE = percentile mc.run(100) actions = mc.current_actions actions.sort(key=itemgetter(1), reverse=True) best_action = actions[0] ev = best_action[1] history.append((percentile, int(ev))) percentile += 1 assert 0 <= percentile <= 100
def handle_noargs(self, **options): from userprofile.models import UserProfile from common.models import Car from album.models import Album from engine.engine import Engine from common.models import DummyRequest from django.core.urlresolvers import reverse from common.helpers.slughifi import slughifi profile = UserProfile.objects.get_by_id('1') engine = Engine(DummyRequest('1')) engine.start() # --- Save Achievements for key, achieves in engine.achieve.achievements.iteritems(): type, key = key.split(',') for achieve in achieves: print type, key, achieve achieve.save(type, key) # --- Save avatars # from shutil import copyfile # for p in UserProfile.objects.all(): # path = settings.MEDIA_ROOT + 'avatars/' + str(p.user.id / 1000) # if not os.path.isdir(path): # os.makedirs(path) # copyfile(settings.MEDIA_ROOT + 'avatars/0.jpg', path + '/' + str(p.user.id) + '.jpg') # --- Set up domain for p in UserProfile.objects.all(): if p.domain != '': continue if p.partner == 'fb': p.domain = 'http://apps.facebook.com/car_battle/' else: p.domain = 'http://www.car-battle.com/' p.save() # --- # MSG all from msg.models import Msg txt = "hi, just want to let you know that we rolled out few new features like, if you need assistance just msg me" for p in UserProfile.objects.all()[1:]: Msg.send.send_to(profile.user, p.user, '@%s %s' % (p.username, txt)) return # --- album = Album.objects.get(pk=1) car = Car.objects.get_by_id(16333) # engine.notify.add(user=profile.user, type='album', key='buy', date=None, stream='Nazwa albumu') engine.notify.add(user=profile.user, type='album', key='stick', date=None, stream='%s|%s|%s|%s' % ( reverse('encyclopedia_car', args=[slughifi(car.manuf), slughifi(car.name), str(car.id)]), car.display_short_name(), reverse('encyclopedia_album', args=[slughifi(album.name), album.id]), album.name ))
def scan(): engine=Engine() return engine.execute()
def test_showdown_hs(self): e = Engine( 'CoinPoker', 1, { 1: {'name': 'joe', 'balance': 1000, 'status': 1}, 2: {'name': 'joe', 'balance': 1000, 'status': 1}, }, 50, 100, 0, ) e.available_actions() # p1 e.do(['r', 100]) e.available_actions() # p2 e.do(['c']) e.available_actions() # p2 e.do(['k']) e.available_actions() # p1 e.do(['b', 200]) e.available_actions() # p2 has: # preflop_1 = l # preflop_2 = c # flop_1 = k hs = ES.showdown_hs(e, e.s, percentile=50) assert hs is not None assert 0 < hs < 1 hs2 = ES.showdown_hs(e, e.s, percentile=10) assert hs2 < hs hs3 = ES.showdown_hs(e, e.s, percentile=90) assert hs3 > hs res = ES.showdown_hs(e, e.s, 200) hits = res.hits.hits assert len(hits) == 200 assert hits[0]['_score'] > 4 assert hits[-1]['_score'] > 0
class Scraper(View): """Runs a scraper for a site. Will use the site to do the scraping, and with the data calculate what action needs to be taken and pass that into the engine and MC""" PATH_DEBUG = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'debug') ACTIONS_MAP = { 'f': 'fold', 'k': 'check', 'c': 'call', 'b': 'bet', 'r': 'raise', 'a': 'allin', } def __init__(self, site_name, seats, debug=False, replay=False, observe=False): self.debug = debug logger.debug('Debug {}'.format(self.debug)) self.observe = observe logger.debug('Observing {}'.format(self.observe)) self.replay = replay logger.debug('Replay {}'.format(self.replay)) if replay: self.load_files() if site_name == 'ps': self.site = PokerStars(seats, debug) elif site_name == 'pp': self.site = PartyPoker(seats, debug) elif site_name == 'zg': self.site = Zynga(seats, debug) elif site_name == 'cp': self.site = CoinPoker(seats, debug) else: raise NotImplementedError('{} is not implemented'.format(site_name)) self.img = None # starting balance zero for ante on init self.players = { s: { 'name': 'joe', 'balance': 1000000, 'status': 1, } for s in range(1, seats + 1) } # do not add engine & mc self.btn = None # not currently in a game self.waiting_for_new_game = True # button moved self.button_moved = False # board moved to help finish phase self.board_moved = False # if tlc/button cannot be found, drop existing game after 5 seconds self.drop_game_start = None # on each player's turn, update balances self.last_thinking_seat = None # orphan call from previous first require thinking self.last_thinking_phase = None def load_files(self): """Replays through images saved during debug run""" files = [] for entry in os.scandir(self.PATH_DEBUG): if not entry.is_file() or entry.name.startswith('.'): logger.debug('skipping file {}'.format(entry.name)) continue files.append(entry.path) logger.info(f'loading {len(files)} files') self.files = sorted(files) def take_screen(self): """Get screen image Takes screen shot or load file if replaying Stats: 9.58% """ logger.info('taking screen shot') while True: if not self.replay: # 3840 x 2400 mac retina # pokerstars # img = ImageGrab.grab((1920, 600, 3840, 2400)) # coinpoker img = ImageGrab.grab((1760, 700, 3840, 2300)) img_file = os.path.join(self.PATH_DEBUG, '{}.png'.format(datetime.datetime.utcnow())) img.save(img_file) logger.debug('file saved locally to {}'.format(img_file)) else: img_path = self.files.pop(0) logger.debug('loading file: {}'.format(img_path)) img = Image.open(img_path) if not self.observe: img_full = img.convert('L') try: self.img = self.site.parse_top_left_corner(img_full) if self.drop_game_start: self.drop_game_start = None logger.info('Continuing existing game') except SiteException as e: logger.info(e) if not self.waiting_for_new_game: if not self.drop_game_start: self.drop_game_start = time.time() logger.warning('Drop game beginning...') elif time.time() - self.drop_game_start > 5: self.waiting_for_new_game = True self.button_moved = False logger.error('Game state aborted!') if self.debug: logger.warning('TLC not found in image!') time.sleep(1) continue try: btn = self.site.parse_dealer(self.img) except NoDealerButtonError as e: logger.warning(e) time.sleep(0.6) break else: if not self.btn: logger.debug('button initialised at {} on joining table'.format(btn)) self.btn = btn elif btn != self.btn: self.button_moved = True self.btn_next = btn logger.debug(f'button moved to {btn}!') self.check_board() # DONE # if self.debug: # input('$ check hands if lucky:') # for s, d in self.engine.data.items(): # if 'in' in d['status']: # self.site.parse_pocket_region(self.img, s) # always break (only continue when tlc & button found) break def check_board(self): """Check board and handles exception raised if card not identified. Card animation could be covering the cards. Instead of retrying rather return existing board""" if not hasattr(self, 'engine'): logger.info('Not checking board when no engine present') return if len(self.engine.board) >= 5: logger.info(f'Board already identified as {self.engine.board}') return logger.info('checking board...') board = self.site.parse_board(self.img) # works like this on CP # if not self.button_moved and not self.waiting_for_new_game and len(board) < len(self.engine.board): # raise BoardError('Board cannot be removed without button moving') if len(board) > len(self.engine.board): logger.debug(f'The board changed with {set(board) - set(self.engine.board)}') self.engine.board = board self.board_moved = True logger.debug('board: {}'.format(self.engine.board)) def run(self): """Run application""" while True: self.take_screen() if self.debug: self.img.show() time.sleep(1) if self.observe: continue # we are ready for a new game if self.waiting_for_new_game: logger.debug('waiting for a new game...') if self.button_moved: logger.debug('button moved and waiting for new game: create new game') self.start_new_game() else: logger.info('just waiting for new game to start...') time.sleep(1) continue # else game in progress else: logger.debug('still playing same game') if self.button_moved: logger.debug('button moved! we need to finish up what engine is') self.finish_it() elif self.engine.phase == self.engine.PHASE_SHOWDOWN: self.check_showdown_winner() time.sleep(1) elif self.engine.phase == self.engine.PHASE_GG: pot, total = self.site.parse_pot_and_total(self.img) if not pot and not total: logger.info(f'Game completed and pot allocated') self.finish_it() else: logger.info('Game completed but waiting for button move') time.sleep(0.5) else: logger.debug('button not moved, just continue loop') self.wait_player_action() logger.info('loop finished') def run_mc(self, timeout): """Runs MC analysis till timeout. This catches error where the amounts have varied too much from the current board by making the closes action""" logger.info('Running MC analysis') # if self.debug: # timeout = 0.4 if 'in' not in self.engine.data[self.site.HERO]['status']: time.sleep(0.2) else: # profiler = Profiler() # time_start = time.time() try: # if self.debug: # profiler.start() self.mc.run(timeout) # if self.debug: # profiler.stop() except EngineError as e: # if self.debug: # profiler.stop() logger.error(e) self.mc.init_tree() self.mc.run(timeout) # duration = time.time() - time_start # if not self.mc.queue.empty() and duration > timeout * 2: # with open(f'profile_{timeout}_{duration}.html', 'w') as f: # f.write(profiler.output_html()) # logger.warning(f'MC {timeout}s run took way longer at {duration}s') self.print() def wait_player_action(self): """Think a little. Always think at least 1 second after every player actioned. First detect if the phase haven't moved on by checking board cards Secondly detect if current player isn't still thinking""" self.run_mc(0.3) # todo check if gg in expected # todo then scan for foe pockets and exit # check actions till phase has caught up # board will not trigger (as cards cleanedup) as we will have button_moved flag # this works when card dealt and players still need to act # this fails when normally finished last action, then when turn comes the whole phase is # considered board phase... # check if phase from number of cards on board is what current player expected phase is if self.board_moved: # this works only if 'board_moved' (looking to catch up if card added to board) mapped_board = self.engine.BOARD_MAP[len(self.engine.board)] while self.engine.phase != mapped_board: logger.debug(f'board at: {mapped_board} and engine at {self.engine.phase}') # if second last person folded, or others allin if self.engine.phase in [self.engine.PHASE_SHOWDOWN, self.engine.PHASE_GG]: logger.info(f'Engine caught up to {mapped_board}, but gg & sd handled separately') return self.check_player_action() self.run_mc(0.3) self.board_moved = False # do not update last_thinking_phase as previous phase text still shows, giving wrong action # an allin would end here if self.engine.phase == self.engine.PHASE_SHOWDOWN: logger.debug('Game in phase showdown, not checking players') time.sleep(0.8) return # as long as someone else is thinking, run analysis logger.info('Checking parsing player...') try: current_s = self.site.parse_thinking_player(self.img) logger.debug(f'Current thinking player is {current_s}') except ThinkingPlayerError as e: logger.debug(f'Current thinking player is unknown, estimating action for {self.engine.s}') # during any phase, if pot removed, then have to loop to find winner with contrib pot, total = self.site.parse_pot_and_total(self.img) if not pot: while self.engine.phase not in [self.engine.PHASE_SHOWDOWN, self.engine.PHASE_GG] \ and self.engine.phase == self.last_thinking_phase: try: self.check_player_action(expect_text=True) except PlayerActionError as exc: # happened when player did not move, but thinking already moved. wtf return self.run_mc(0.3) if self.engine.phase == self.engine.PHASE_SHOWDOWN: self.check_showdown_winner() elif self.engine.phase == self.engine.PHASE_GG: self.finish_it() # else we can check for action considering we are in same phase elif self.last_thinking_phase == self.engine.phase: try: self.check_player_action(expect_text=True) except PlayerActionError as exc: # happened when player did not move, but thinking already moved. wtf # 2: caught thinking elapsed, but folded cards not yet removed logger.info(str(exc)) return self.run_mc(0.3) else: self.last_thinking_phase = self.engine.phase # if player is still thinking, then so can we if current_s == self.engine.q[0][0]: logger.debug(f'player to act {current_s} is still thinking, so can we...') if current_s != self.last_thinking_seat: self.check_player_name(current_s) self.check_player_balance(current_s) self.last_thinking_seat = current_s # longer thinking time for hero thinking_time = 1 if current_s == self.site.HERO else 0.4 self.run_mc(thinking_time) # player (in engine) to act is not the one thinking on screen # whilst it is not the expected player to act use the same current img to catch up else: while current_s != self.engine.q[0][0]: logger.debug(f'taking action for {self.engine.q[0][0]} as he is not thinking on screen') if self.engine.phase in [self.engine.PHASE_SHOWDOWN, self.engine.PHASE_GG]: logger.warning(f'exiting board phase as engine is now in {self.engine.phase}') return try: self.check_player_action() except (BalancesError, PlayerActionError) as e: logger.warning(e) # retry with new screen return self.run_mc(0.3) def check_player_action(self, expect_text=False): """It is certain the expected player is not thinking, thus he finished his turn: check the player action. Based on expected moves we can infer what he did Balance would change for: call, bet, raise, allin and not for: fold, check Fold is most common and easy to detect pocket cards Check (if so) is easy if balance did not change Otherwise balance has changed: balance and contrib change should match, just send bet to engine, it'll correct the action """ s = self.engine.q[0][0] phase = self.engine.phase logger.info(f'check player {s} action') logger.info(f'expecting one of {self.expected} during {phase}') if 'fold' not in self.expected: logger.error(f'fold is not in {self.expected}') raise PlayerActionError('End of game should not be here: player cannot fold') pocket = self.check_players_pockets(s) contrib = self.site.parse_contribs(self.img, s) cmd = self.site.infer_player_action(self.img, s, phase, pocket, contrib or 0, self.engine, self.engine.current_pot, self.board_moved, self.expected, expect_text) # do action, rotate, and get next actions logger.debug(f'parsed action {cmd}') action = self.engine.do(cmd) action_name = self.ACTIONS_MAP[action[0]] logger.info(f'Player {s} did {action_name} {action}') # cut tree based on action # do not have to cut tree when button moved if not self.button_moved: # self.mc.analyze_tree() child_nodes = self.mc.tree.children(self.mc.tree.root) logger.debug(f'{len(child_nodes)} child nodes on tree {self.mc.tree.root}') # logger.info('nodes:\n{}'.format(json.dumps([n.tag for n in child_nodes], indent=4, default=str))) action_nodes = [n for n in child_nodes if n.data['action'].startswith(action_name)] # create new if not action_nodes: logger.warning(f'action {action_name} not found in nodes {[n.data["action"] for n in child_nodes]}') self.mc.init_tree() logger.debug('tree recreated') # subtree else: # direct if len(action_nodes) == 1: node = action_nodes[0] self.mc.tree = self.mc.tree.subtree(node.identifier) logger.debug(f'Tree branched from single node {node.tag}') logger.debug(f'tree branched from single node {node.data}') # proximity else: nodes_diffs = {abs(n.data['amount'] - action[1]): n for n in action_nodes} node = nodes_diffs[min(nodes_diffs.keys())] self.mc.tree = self.mc.tree.subtree(node.identifier) logger.debug(f'tree recreated from closest node {node.tag}') logger.debug(f'tree recreated from closest node {node.data}') # increment traversed level if not node.tag.endswith('_{}_{}'.format(s, phase)): logger.error(f'Finished player {s} in {phase} not in subtree tag {node.tag}') self.mc.init_tree() logger.debug('get next actions') self.expected = self.engine.available_actions() def check_players_pockets(self, filter_seat=None): """Check if player has back side of pocket, otherwise check what his cards are""" pockets = {} for i in range(self.site.seats): s = i + 1 if filter_seat and filter_seat != s: continue # could already have hand # but cannot check here # as it does not pick up when hero folded # if hasattr(self, 'engine') and self.engine.data[s]['hand'] not in [['__', '__'], [' ', ' ']]: # pockets[s] = self.engine.data[s] # continue # check for back side if self.site.parse_pocket_back(self.img, s): logger.info(f'Player {s} has hole cards') pockets[s] = self.site.HOLE_CARDS continue # check if showing cards pocket = self.site.parse_pocket_cards(self.img, s) if pocket: logger.info(f'Player {s} is showing {pocket}') pockets[s] = pocket if hasattr(self, 'engine'): self.engine.data[s]['hand'] = pocket continue logger.info(f'Player {s} has no cards') if filter_seat: return pockets.get(filter_seat) return pockets def check_player_name(self, seat): """Get name hashes and set it to players""" name = self.site.parse_names(self.img, seat) if name: self.players[seat]['name'] = name logger.info(f'Player {seat} name is {name}') def check_player_balance(self, seat): """Update player balances""" balance = self.site.parse_balances(self.img, seat) if balance: self.players[seat]['balance'] = balance logger.debug(f'Player {seat} balance is {balance}') def start_new_game(self): """Dealer button moved: create new game! Get players that are still IN: some sites have balances always available, but some are blocked by text. Thus at least 2 pockets are required. Get small blind and big blind and ante. Create new engine Create new MC Not waiting for a new game anymore. """ # for joining table first time only if not self.btn: logger.info(f'init button at {self.btn_next}') self.btn = self.btn_next return logger.info('button moved: creating new game...') self.btn = self.btn_next # there must be pockets for a game to have started pockets = self.check_players_pockets() if len(pockets) < 2: logger.debug(f'Game has not started as there are {len(pockets)} pockets right now') time.sleep(0.1) return # pot and total are required for a game to have started # cannot check pot anymore for when no ante, no pot in middle :( pot, total = self.site.parse_pot_and_total(self.img) if not total: logger.debug(f'Game has not started as there are no pot or total right now') time.sleep(0.1) return # contribs are required for blinds! contribs = self.site.parse_contribs(self.img) # seems there can be 1 blind, probably when blind fell out during prev round # if len(contribs) == 1: # logger.error('Found only 1 blind. Not supported.') # self.button_moved = False # return # requires 2 blinds to get sb and bb if not len(contribs): logger.debug('Game has not started as there are no contribs right now') time.sleep(0.1) return # not doing any more checks, since if they have cards and there is a pot: game on! # self.check_player_name() # checked at flop to save time during start # always have text on it, so who cares # balances = self.site.parse_balances(self.img) vs_players = set(list(pockets.keys()) + list(contribs.keys())) ante = self.check_ante(len(vs_players), pot) sb, bb = self.check_blinds(contribs) self.pre_start(vs_players, contribs, ante) logger.debug('creating engine...') self.engine = Engine(self.site.NAME, self.btn, self.players, sb, bb, ante) # why does 'hand' return? self.expected = self.engine.available_actions() logger.debug(f'seat {self.engine.q[0][0]} available actions: {self.expected}') logger.debug('creating MC...') self.mc = MonteCarlo(engine=self.engine, hero=self.site.HERO) self.post_start(pockets) logger.info('new game created!') self.waiting_for_new_game = False self.button_moved = False self.board_moved = False def check_ante(self, vs, pot): """Check ante from contrib total before gathering/matched. Using player balances instead of recently scraped balances.""" # no pot so there is no ante paid yet if not pot: return 0 ante = pot / vs logger.debug(f'Ante {ante} from current pot {pot} and vs {vs}') if not ante.is_integer(): logger.warning(f'Ante {ante} is not integer') ante = int(round(ante)) logger.info(f'Ante: {ante}') return ante def check_blinds(self, contribs): """SB and BB from ante structure. Since hero blocks the play, this should be always found.""" # skip SB in tournament fallout if len(contribs) == 3 and contribs.get(5) == 7: logger.info('Fixed player 5 wrong contrib of 7') del contribs[5] contribs_sorted = sorted([c for c in contribs.values() if c]) if len(contribs_sorted) == 1: return None, contribs_sorted[0] sb, bb, *others = contribs_sorted logger.info(f'Blinds found: SB {sb} and BB {bb}') if sb * 2 != bb: # revert to seating order logger.warning('Smallest SB is not 1:2 to BB') sb, bb, *_ = contribs.values() # small blind is all-in if sb < bb / 2: s = list(contribs.keys())[0] self.players[s]['balance'] = sb sb = bb / 2 # small blind is all-in if bb < sb * 2: s = list(contribs.keys())[1] self.players[s]['balance'] = bb bb = sb * 2 return sb, bb def pre_start(self, vs_players, contribs, ante): """Add contribs back to balances for when engine take action it will subtract it again from matched/contrib. The status of players can be set if they have any balance/contrib""" for s in range(1, self.site.seats + 1): status = s in vs_players self.players[s]['status'] = status self.players[s]['sitout'] = False if not status: continue # balance might not be scraped as it might be obscured by text # if s in balances: # balance = balances[s] # balance_diff = self.players[s]['balance'] - balance # if balance_diff: # logger.warning(f'player {s} balance {balance} out by {balance_diff}') # self.players[s]['balance'] = balance # if player is blind, then return money for engine to subtract on init. same for ante. # BUT with coinpoker, the balances are not scraped # so no need to add it back # contrib = contribs.get(s, 0) # if contrib: # self.players[s]['balance'] += contrib # logger.debug(f'player {s} contrib {contrib} added back to balance') # if ante: # self.players[s]['balance'] += ante # logger.debug(f'player {s} ante {ante} added back to balance') logger.info('pre start done') if self.debug: logger.info('Is all the contribs/ante correct?') def post_start(self, pockets): """Post start. Engine and MC has been created. Set hero hand immediately for analysis of EV""" hero = self.site.HERO pocket = pockets.get(hero) if pocket: self.engine.data[hero]['hand'] = pocket logger.info(f'Hero (p{hero}) hand set to {pocket}') else: logger.error('No hero pocket found!') self.print() def finish_it(self): """Finish the game. Should already have winner.""" logger.info('finish the game') # todo BUG: game does get here in showdown # todo BUG: cannot go to showdown, as the game has ended long time ago, has to finish it # if not self.engine.winner: # raise GamePhaseError(f'Game does not have winner but in phase {self.engine.phase}') # if self.engine.phase == self.engine.PHASE_SHOWDOWN: # return self.check_showdown_winner() self.waiting_for_new_game = True self.save_game() logger.info(f'Game over! Player {self.engine.winner} won!') if self.debug: raise Exception(f'Game over: {self.engine.winner} won!') def check_showdown_winner(self): """Winner can be identified by no pot amounts but one contrib""" if 'gg' not in self.expected: logger.debug(f'gg not in expected {self.expected}') return for s, d in self.engine.data.items(): if 'in' in d['status'] and d['hand'] == self.site.HOLE_CARDS: self.check_players_pockets(s) if len(self.engine.board) != 5: logger.info(f'Still drawing on board {self.engine.board}') return pot, total = self.site.parse_pot_and_total(self.img) if pot or total: logger.debug(f'pot {pot} or total {total} has values') return contribs = self.site.parse_contribs(self.img) # can contain pot and sidepots if len(contribs) < 1: logger.debug(f'There is not a single contrib but {contribs}') return # todo support split pot winners winner = max(contribs.items(), key=itemgetter(1))[0] logger.info(f'Winner of showdown is {winner}') cmd = ['gg', winner] self.engine.do(cmd) self.finish_it() def cards(self): """Generate cards for a site""" self.site.generate_cards() def chips(self): """Generate cards for a site""" self.site.generate_chips() def calc_board_to_pocket_ratio(self): self.site.calc_board_to_pocket_ratio() def save_game(self): """Check for players that sit out, and do not save their game""" for s, d in self.engine.data.items(): if 'f' in [i['action'] for i in d['preflop']]: balance_txt = self.site.parse_balances(self.img, s, True) if balance_txt == 'sit out': d['sitout'] = True ES.save_game(self.players, self.engine.data, self.engine.site_name, self.engine.vs, self.engine.board)
+ "\n layers: " + str(arr_layer_sizes) + "\n iterations: " + str(iterations) + "\n run type: " + run_type + "\n input scalling: " + str(input_scalling) + "\n target scalling: " + str(target_scalling) + "\n" ) # 1. define algorithm pred_nn = PredictionNNFactory(arr_layer_sizes, iterations) # 2. set data handler (pre-processing, normalization, data set creation) data_handler = NumericalDataHandler(nr_params, nr_label_dim, input_scalling=input_scalling, target_scalling=target_scalling) # 3. run engine = Engine(pred_nn, data_file, data_handler=data_handler) trained_alg = engine.start(_run_type=run_type) # 4. validate result validation_stats = engine.validate(trained_alg, PredictionValidator(), _run_type=run_type) targets = np.array(validation_stats['targets']) pred = np.array(validation_stats['pred']) plot(targets, 'go') plot(pred, 'r+') show() data_processor = data_handler.get_data_processor() data_processor.set_data(np.array([[7.4, 0.7, 0 , 1.9, 0.076, 11, 34, 0.9978, 3.51, 0.56, 9.4, 5]])) data_processor.normalize_data(data_handler.get_statistics()) test_set = data_processor.get_data_set() pred = trained_alg.predict(test_set)
class Auction(object): def __init__(self): pass def start(self): auctions = AuctionModel.objects.filter(end_at__lt=datetime.datetime.now, is_refunded=False) for self.auction in auctions: logging.debug("Finishing auction of %s" % self.auction.title) self.seller = Engine(DummyRequest(self.auction.seller.id)) self.seller.start() self.buyer = None self.bids = AuctionOffer.objects.get_by_auction(auction=self.auction) self.finish_auction() self.auction.is_refunded = True if self.buyer: self.auction.buyer_id = self.buyer.user.profile.user_id self.auction.save() def finish_auction(self): # Refund if len(self.bids) == 0: if self.auction.seller != SELLER: self.give_item(self.seller) return True # Buyer if not self.auction.is_buy_now or self.auction.buyer_id == 0: bid = self.bids[0] self.buyer = Engine(DummyRequest(bid.buyer.id)) self.buyer.start() self.buyer.user.profile.earn('cash', bid.max_price - bid.price) bid.is_refunded = True bid.save() else: self.buyer = Engine(DummyRequest(self.auction.buyer_id)) self.buyer.start() self.give_item(self.buyer) # Seller if not self.auction.is_buy_now or self.auction.buyer_id == 0: price = self.auction.current_price self.seller.user.profile.earn('cash', self.auction.current_price) else: price = self.auction.buy_it_now_price self.seller.user.profile.earn('cash', self.auction.buy_it_now_price) self.seller.notify.add(type='auction', key='income', value=price, date=None) self.buyer.notify.add(type='auction', key='outcome', value=price, date=None) self.seller.notify.add(type='auction', key='transaction', value=1, date=None) self.buyer.notify.add(type='auction', key='transaction', value=1, date=None) if len(self.bids) == 1: return True if not self.auction.is_buy_now or self.auction.buyer_id == 0: self.bids = self.bids[1:] for bid in self.bids: if bid.is_refunded == True: continue profile = UserProfile.objects.get_by_id(bid.buyer.id) profile.earn('cash', bid.max_price) bid.is_refunded = True bid.save() def give_item(self, engine): car = self.auction.car engine.user.get_garage() engine.user.garage.add_car(car.id) engine.user.profile.cars = len(engine.user.garage) engine.user.profile.save() return True
def test_player_stats_on_hand(self): e = Engine( 'CoinPoker', 1, { 1: {'name': 'joe', 'balance': 1000, 'status': 1}, 2: {'name': 'jane', 'balance': 1000, 'status': 1}, 3: {'name': 'jane', 'balance': 1000, 'status': 1}, 4: {'name': 'jane', 'balance': 1000, 'status': 1}, 5: {'name': 'jane', 'balance': 1000, 'status': 1}, 6: {'name': 'jane', 'balance': 1000, 'status': 1}, }, 50, 100, 0, ) e.available_actions() # p4 e.do(['r', 100]) e.available_actions() # p5 e.do(['f']) e.available_actions() # p6 e.do(['f']) e.available_actions() # p1 e.do(['c']) e.available_actions() # p2 e.do(['c']) e.available_actions() # p3 e.do(['k']) e.available_actions() # p2 e.do(['b', 100]) e.available_actions() # p3 stats = ES.player_stats(e, e.s) # hs = res.aggregations['hs']['hs_agg']['values']['50.0'] assert len(stats['actions']) >= 4
class Game: def __init__(self, table): ''' Create an engine for this game monte carlo will use the engine instances for iterations Game will only keep track of this current game First get actions from engine, then the repr ''' self.engine = Engine(table.site_name, table.button, table.players, table.sb, table.bb) self.history = {} self.cursor = 0 @retrace.retry() def play(self): """iterative process of inputting commands from table engine saved after available actions have been executed""" while True: available_actions = self.engine.available_actions() if not available_actions: logger.info('No actions received from engine!') break self.engine.save() r = repr(self.engine) o = ' | '.join(available_actions) u = self.replay() i = input('\n\n\n' + r + '\n\n' + o + u + '\n$ ').strip() # exit? if i == 'X': return # undo? if i == 'U': self.undo() continue # redo? if i == 'R': self.replay(True) continue # analyse? if i == 'M': MonteCarlo(self.engine).run() continue cmd = i.split() # overwrite board? if cmd[0] == 'B': self.engine.board = cmd[1:] logger.info('set board to {}'.format(cmd[1:])) continue if cmd[0] == 't': s = self.engine.q[0][0] p_contrib = self.engine.data[s]['contrib'] bet_from_contrib = int(cmd[1]) - p_contrib logger.info('extracted {} bet/raise from contrib for player {}'.format(bet_from_contrib, s)) cmd = ['b', bet_from_contrib] # play self.handle_input(cmd) self.save_game() def handle_input(self, cmd): logger.info('input = {}'.format(cmd)) # send expected engine cmd e_copy = deepcopy(self.engine) self.engine.do(cmd) # action successful, save in history self.history[self.cursor] = (e_copy, cmd) logger.info('keeping history at {} (before cmd {})'.format(self.cursor, cmd)) self.cursor += 1 logger.debug('input handled and done') def save_game(self): logger.info('saving game...') game_id = ''.join(random.choice(ascii_uppercase + digits) for _ in range(8)) for s, d in self.engine.data.items(): doc = {} for phase in ['preflop', 'flop', 'turn', 'river']: for i, action_info in enumerate(d[phase]): doc['{}_{}'.format(phase, i+1)] = action_info['action'] doc['{}_{}_rvl'.format(phase, i+1)] = action_info['rvl'] if i == 0: doc['{}_aggro'.format(phase)] = action_info['aggro'] if 'bet_to_pot' in action_info: doc['{}_{}_btp'.format(phase, i+1)] = action_info['bet_to_pot'] if action_info.get('pot_odds'): doc['{}_{}_po'.format(phase, i+1)] = action_info['pot_odds'] if doc: doc.update({ 'player': self.engine.players[s]['name'], 'site': self.engine.site_name, 'game': game_id, 'vs': self.engine.vs, 'created_at': datetime.datetime.utcnow(), }) GameAction(**doc).save() logger.info('saved {}'.format(doc)) def undo(self): """Restore previous engine state (before that cmd) Since current state is last entry in history, restore second to last item""" logger.info('undo last action') if self.cursor <= 0: logger.warn('cannot restore snapshot! cursor at {}'.format(self.cursor)) return self.cursor -= 1 snapshot = self.history[self.cursor] self.engine = snapshot[0] logger.info('previous snapshot restored. cursor back at {}'.format(self.cursor)) def replay(self, force=False): """allow same commands to be replayed if in history""" if len(self.history) <= self.cursor: return '' past_state = self.history[self.cursor] if not force: return '\nReplay: {}?'.format(past_state[1]) self.handle_input(past_state[1])
def start_new_game(self): """Dealer button moved: create new game! Get players that are still IN: some sites have balances always available, but some are blocked by text. Thus at least 2 pockets are required. Get small blind and big blind and ante. Create new engine Create new MC Not waiting for a new game anymore. """ # for joining table first time only if not self.btn: logger.info(f'init button at {self.btn_next}') self.btn = self.btn_next return logger.info('button moved: creating new game...') self.btn = self.btn_next # there must be pockets for a game to have started pockets = self.check_players_pockets() if len(pockets) < 2: logger.debug(f'Game has not started as there are {len(pockets)} pockets right now') time.sleep(0.1) return # pot and total are required for a game to have started # cannot check pot anymore for when no ante, no pot in middle :( pot, total = self.site.parse_pot_and_total(self.img) if not total: logger.debug(f'Game has not started as there are no pot or total right now') time.sleep(0.1) return # contribs are required for blinds! contribs = self.site.parse_contribs(self.img) # seems there can be 1 blind, probably when blind fell out during prev round # if len(contribs) == 1: # logger.error('Found only 1 blind. Not supported.') # self.button_moved = False # return # requires 2 blinds to get sb and bb if not len(contribs): logger.debug('Game has not started as there are no contribs right now') time.sleep(0.1) return # not doing any more checks, since if they have cards and there is a pot: game on! # self.check_player_name() # checked at flop to save time during start # always have text on it, so who cares # balances = self.site.parse_balances(self.img) vs_players = set(list(pockets.keys()) + list(contribs.keys())) ante = self.check_ante(len(vs_players), pot) sb, bb = self.check_blinds(contribs) self.pre_start(vs_players, contribs, ante) logger.debug('creating engine...') self.engine = Engine(self.site.NAME, self.btn, self.players, sb, bb, ante) # why does 'hand' return? self.expected = self.engine.available_actions() logger.debug(f'seat {self.engine.q[0][0]} available actions: {self.expected}') logger.debug('creating MC...') self.mc = MonteCarlo(engine=self.engine, hero=self.site.HERO) self.post_start(pockets) logger.info('new game created!') self.waiting_for_new_game = False self.button_moved = False self.board_moved = False