|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +import logging |
| 3 | + |
| 4 | +from terminaltables import AsciiTable |
| 5 | +from tqdm import trange |
| 6 | + |
| 7 | +from game.game_manager import GameManager |
| 8 | +from mahjong.client import Client |
| 9 | + |
| 10 | +TOTAL_HANCHANS = 100 |
| 11 | + |
| 12 | +def main(): |
| 13 | + # enable it for manual testing |
| 14 | + logger = logging.getLogger('game') |
| 15 | + logger.disabled = True |
| 16 | + |
| 17 | + # let's load three bots with old logic |
| 18 | + # and one copy with new logic |
| 19 | + clients = [Client(use_previous_ai_version=True) for _ in range(0, 3)] |
| 20 | + clients += [Client(use_previous_ai_version=False)] |
| 21 | + manager = GameManager(clients) |
| 22 | + |
| 23 | + total_results = {} |
| 24 | + for client in clients: |
| 25 | + total_results[client.id] = { |
| 26 | + 'name': client.player.name, |
| 27 | + 'version': client.player.ai.version, |
| 28 | + 'positions': [], |
| 29 | + 'played_rounds': 0, |
| 30 | + 'lose_rounds': 0, |
| 31 | + 'win_rounds': 0, |
| 32 | + 'riichi_rounds': 0, |
| 33 | + } |
| 34 | + |
| 35 | + for x in trange(TOTAL_HANCHANS): |
| 36 | + # yes, I know about tqdm.write |
| 37 | + # but it didn't work properly for our case |
| 38 | + print('\n') |
| 39 | + print('Hanchan #{0}'.format(x + 1)) |
| 40 | + |
| 41 | + result = manager.play_game(total_results) |
| 42 | + |
| 43 | + table_data = [ |
| 44 | + ['Position', 'Player', 'AI', 'Scores'], |
| 45 | + ] |
| 46 | + |
| 47 | + clients = sorted(clients, key=lambda i: i.player.scores, reverse=True) |
| 48 | + for client in clients: |
| 49 | + player = client.player |
| 50 | + table_data.append([player.position, |
| 51 | + player.name, |
| 52 | + 'v{0}'.format(player.ai.version), |
| 53 | + '{0:,d}'.format(int(player.scores)) |
| 54 | + ]) |
| 55 | + |
| 56 | + total_result_client = total_results[client.id] |
| 57 | + total_result_client['positions'].append(player.position) |
| 58 | + total_result_client['played_rounds'] += result['played_rounds'] |
| 59 | + |
| 60 | + table = AsciiTable(table_data) |
| 61 | + print(table.table) |
| 62 | + print('') |
| 63 | + |
| 64 | + print('\n') |
| 65 | + |
| 66 | + table_data = [ |
| 67 | + ['Player', 'AI', 'Played rounds', 'Average place', 'Win rate', 'Feed rate', 'Riichi rate'], |
| 68 | + ] |
| 69 | + |
| 70 | + # recalculate stat values |
| 71 | + for item in total_results.values(): |
| 72 | + played_rounds = item['played_rounds'] |
| 73 | + lose_rounds = item['lose_rounds'] |
| 74 | + win_rounds = item['win_rounds'] |
| 75 | + riichi_rounds = item['riichi_rounds'] |
| 76 | + |
| 77 | + item['average_place'] = sum(item['positions']) / len(item['positions']) |
| 78 | + item['feed_rate'] = (lose_rounds / played_rounds) * 100 |
| 79 | + item['win_rate'] = (win_rounds / played_rounds) * 100 |
| 80 | + item['riichi_rate'] = (riichi_rounds / played_rounds) * 100 |
| 81 | + |
| 82 | + calculated_clients = sorted(total_results.values(), key=lambda i: i['average_place']) |
| 83 | + |
| 84 | + for item in calculated_clients: |
| 85 | + table_data.append([ |
| 86 | + item['name'], |
| 87 | + item['version'], |
| 88 | + '{0:,d}'.format(item['played_rounds']), |
| 89 | + format(item['average_place'], '.2f'), |
| 90 | + format(item['win_rate'], '.2f') + '%', |
| 91 | + format(item['feed_rate'], '.2f') + '%', |
| 92 | + format(item['riichi_rate'], '.2f') + '%', |
| 93 | + ]) |
| 94 | + |
| 95 | + print('Final results:') |
| 96 | + table = AsciiTable(table_data) |
| 97 | + print(table.table) |
| 98 | + |
| 99 | + |
| 100 | +if __name__ == '__main__': |
| 101 | + main() |
0 commit comments