def test_set_units(): """ Test - Sets units """ game = Game() game.clear_units() game.set_units('FRANCE', ['A PAR', 'A MAR', '*A GAS'], reset=False) game.set_units('ENGLAND', ['A PAR', 'A LON']) assert game.get_power('AUSTRIA').units == [] assert game.get_power('ENGLAND').units == ['A PAR', 'A LON'] assert game.get_power('FRANCE').units == ['A MAR'] assert 'A GAS' in game.get_power('FRANCE').retreats assert game.get_power('GERMANY').units == [] assert game.get_power('ITALY').units == [] assert game.get_power('RUSSIA').units == [] assert game.get_power('TURKEY').units == [] # Adding F PIC to England without resetting game.set_units('ENGLAND', ['F PIC'], reset=False) assert game.get_power('ENGLAND').units == ['A PAR', 'A LON', 'F PIC'] # Adding F PIC to England with resetting game.set_units('ENGLAND', ['F PIC'], reset=True) assert game.get_power('ENGLAND').units == ['F PIC'] # Adding F PAR (Illegal unit) to England without resetting game.set_units('ENGLAND', ['F PAR'], reset=False) assert game.get_power('ENGLAND').units == ['F PIC']
def test_set_centers(): """ Tests - Sets centers """ game = Game() game.clear_centers() game.set_centers('FRANCE', ['PAR', 'MAR', 'GAS']) # GAS is not a valid SC loc game.set_centers('ENGLAND', ['PAR', 'LON']) assert game.get_power('AUSTRIA').centers == [] assert game.get_power('ENGLAND').centers == ['PAR', 'LON'] assert game.get_power('FRANCE').centers == ['MAR'] assert game.get_power('GERMANY').centers == [] assert game.get_power('ITALY').centers == [] assert game.get_power('RUSSIA').centers == [] assert game.get_power('TURKEY').centers == [] # Adding BUD to England without resetting game.set_centers('ENGLAND', 'BUD', reset=False) assert game.get_power('ENGLAND').centers == ['PAR', 'LON', 'BUD'] # Adding BUD to England with resetting game.set_centers('ENGLAND', ['BUD'], reset=True) assert game.get_power('ENGLAND').centers == ['BUD'] # Adding UKR to England (illegal SC) game.set_centers('ENGLAND', 'UKR', reset=False) assert game.get_power('ENGLAND').centers == ['BUD']
def test_get_centers(): """ Test - get centers """ game = Game() centers = game.get_centers() assert centers['AUSTRIA'] == ['BUD', 'TRI', 'VIE'] assert centers['ENGLAND'] == ['EDI', 'LON', 'LVP'] assert centers['FRANCE'] == ['BRE', 'MAR', 'PAR'] assert centers['GERMANY'] == ['BER', 'KIE', 'MUN'] assert centers['ITALY'] == ['NAP', 'ROM', 'VEN'] assert centers['RUSSIA'] == ['MOS', 'SEV', 'STP', 'WAR'] assert centers['TURKEY'] == ['ANK', 'CON', 'SMY'] assert game.get_centers('AUSTRIA') == ['BUD', 'TRI', 'VIE'] assert game.get_centers('ENGLAND') == ['EDI', 'LON', 'LVP'] assert game.get_centers('FRANCE') == ['BRE', 'MAR', 'PAR'] assert game.get_centers('GERMANY') == ['BER', 'KIE', 'MUN'] assert game.get_centers('ITALY') == ['NAP', 'ROM', 'VEN'] assert game.get_centers('RUSSIA') == ['MOS', 'SEV', 'STP', 'WAR'] assert game.get_centers('TURKEY') == ['ANK', 'CON', 'SMY'] # Making sure we got a copy, and not a direct game reference austria = game.get_power('AUSTRIA') austria.centers.remove('BUD') centers_2 = game.get_centers() assert centers['AUSTRIA'] == ['BUD', 'TRI', 'VIE'] assert centers_2['AUSTRIA'] == ['TRI', 'VIE']
def test_get_orders(): """ Test - get orders """ check_sorted = lambda list_1, list_2: sorted(list_1) == sorted(list_2) game = Game() # Movement phase game.set_orders('FRANCE', ['A PAR H', 'A MAR - BUR']) game.set_orders('ENGLAND', ['LON H']) orders = game.get_orders() assert check_sorted(orders['AUSTRIA'], []) assert check_sorted(orders['ENGLAND'], ['F LON H']) assert check_sorted(orders['FRANCE'], ['A PAR H', 'A MAR - BUR']) assert check_sorted(orders['GERMANY'], []) assert check_sorted(orders['ITALY'], []) assert check_sorted(orders['RUSSIA'], []) assert check_sorted(orders['TURKEY'], []) assert check_sorted(game.get_orders('AUSTRIA'), []) assert check_sorted(game.get_orders('ENGLAND'), ['F LON H']) assert check_sorted(game.get_orders('FRANCE'), ['A PAR H', 'A MAR - BUR']) assert check_sorted(game.get_orders('GERMANY'), []) assert check_sorted(game.get_orders('ITALY'), []) assert check_sorted(game.get_orders('RUSSIA'), []) assert check_sorted(game.get_orders('TURKEY'), []) # Making sure we got a copy, and not a direct game reference france = game.get_power('FRANCE') del france.orders['A PAR'] orders_2 = game.get_orders() assert check_sorted(orders['FRANCE'], ['A PAR H', 'A MAR - BUR']) assert check_sorted(orders_2['FRANCE'], ['A MAR - BUR']) # Moving to W1901A game.clear_units('FRANCE') game.set_centers('FRANCE', 'SPA') game.process() game.process() assert game.get_current_phase() == 'W1901A' # Adjustment phase game.set_orders('FRANCE', ['A MAR B', 'F MAR B']) game.set_orders('AUSTRIA', 'A PAR H') orders = game.get_orders() assert check_sorted(orders['AUSTRIA'], []) assert check_sorted(orders['ENGLAND'], []) assert check_sorted(orders['FRANCE'], ['A MAR B']) assert check_sorted(orders['GERMANY'], []) assert check_sorted(orders['ITALY'], []) assert check_sorted(orders['RUSSIA'], []) assert check_sorted(orders['TURKEY'], []) assert check_sorted(game.get_orders('AUSTRIA'), []) assert check_sorted(game.get_orders('ENGLAND'), []) assert check_sorted(game.get_orders('FRANCE'), ['A MAR B']) assert check_sorted(game.get_orders('GERMANY'), []) assert check_sorted(game.get_orders('ITALY'), []) assert check_sorted(game.get_orders('RUSSIA'), []) assert check_sorted(game.get_orders('TURKEY'), [])
def test_set_current_phase(): """ Tests - set current phase""" game = Game() power = game.get_power('FRANCE') power.units.remove('A PAR') game.set_current_phase('W1901A') game.clear_cache() assert game.get_current_phase() == 'W1901A' assert game.phase_type == 'A' assert 'A PAR B' in game.get_all_possible_orders()['PAR']
def on_vote_updated(game, notification): """ Manage notification VoteUpdated (for omniscient game). :param game: a Network game :param notification: notification received :type game: diplomacy.client.network_game.NetworkGame :type notification: diplomacy.communication.notifications.VoteUpdated """ assert Game.is_omniscient_game(game) for power_name, vote in notification.vote.items(): Game.get_power(game, power_name).vote = vote
def test_unit_owner(): """ Test Unit Owner Resolver making sure the cached results are correct """ game = Game() print(game.get_units('RUSSIA')) assert game._unit_owner('F STP/SC', coast_required=1) is game.get_power( 'RUSSIA') # pylint: disable=protected-access assert game._unit_owner('F STP/SC', coast_required=0) is game.get_power( 'RUSSIA') # pylint: disable=protected-access assert game._unit_owner('F STP', coast_required=1) is None # pylint: disable=protected-access assert game._unit_owner('F STP', coast_required=0) is game.get_power( 'RUSSIA') # pylint: disable=protected-access assert game._unit_owner('A WAR', coast_required=0) is game.get_power( 'RUSSIA') # pylint: disable=protected-access assert game._unit_owner('A WAR', coast_required=1) is game.get_power( 'RUSSIA') # pylint: disable=protected-access assert game._unit_owner('F SEV', coast_required=0) is game.get_power( 'RUSSIA') # pylint: disable=protected-access assert game._unit_owner('F SEV', coast_required=1) is game.get_power( 'RUSSIA') # pylint: disable=protected-access
def test_get_orders_no_check(): """ Test - get orders NO_CHECK """ check_sorted = lambda list_1, list_2: sorted(list_1) == sorted(list_2) game = Game() game.add_rule('NO_CHECK') # Movement phase game.set_orders('FRANCE', ['A PAR H', 'A MAR - BUR']) game.set_orders('ENGLAND', ['LON H']) orders = game.get_orders() assert check_sorted(orders['AUSTRIA'], []) assert check_sorted(orders['ENGLAND'], ['LON H']) # Should not be fixed assert check_sorted(orders['FRANCE'], ['A PAR H', 'A MAR - BUR']) assert check_sorted(orders['GERMANY'], []) assert check_sorted(orders['ITALY'], []) assert check_sorted(orders['RUSSIA'], []) assert check_sorted(orders['TURKEY'], []) assert check_sorted(game.get_orders('AUSTRIA'), []) assert check_sorted(game.get_orders('ENGLAND'), ['LON H']) # Should not be fixed assert check_sorted(game.get_orders('FRANCE'), ['A PAR H', 'A MAR - BUR']) assert check_sorted(game.get_orders('GERMANY'), []) assert check_sorted(game.get_orders('ITALY'), []) assert check_sorted(game.get_orders('RUSSIA'), []) assert check_sorted(game.get_orders('TURKEY'), []) # Making sure we got a copy, and not a direct game reference france = game.get_power('FRANCE') france.orders = { order_ix: order_value for order_ix, order_value in france.orders.items() if not order_value.startswith('A PAR') } orders_2 = game.get_orders() assert check_sorted(orders['FRANCE'], ['A PAR H', 'A MAR - BUR']) assert check_sorted(orders_2['FRANCE'], ['A MAR - BUR']) # Moving to W1901A game.clear_units('FRANCE') game.set_centers('FRANCE', 'SPA') game.process() game.process() assert game.get_current_phase() == 'W1901A' # Adjustment phase game.set_orders('FRANCE', ['A MAR B', 'F MAR B']) game.set_orders('AUSTRIA', 'A PAR H') orders = game.get_orders() assert check_sorted(orders['AUSTRIA'], []) # 'A PAR H' is VOID assert check_sorted(orders['ENGLAND'], []) assert check_sorted(orders['FRANCE'], ['A MAR B']) # 'F MAR B' is VOID assert check_sorted(orders['GERMANY'], []) assert check_sorted(orders['ITALY'], []) assert check_sorted(orders['RUSSIA'], []) assert check_sorted(orders['TURKEY'], []) assert check_sorted(game.get_orders('AUSTRIA'), []) assert check_sorted(game.get_orders('ENGLAND'), []) assert check_sorted(game.get_orders('FRANCE'), ['A MAR B']) assert check_sorted(game.get_orders('GERMANY'), []) assert check_sorted(game.get_orders('ITALY'), []) assert check_sorted(game.get_orders('RUSSIA'), []) assert check_sorted(game.get_orders('TURKEY'), [])