APIs for Accessing Server

Minimum Working Example

import diplomacy
from diplomacy.client.connection import connect
from diplomacy.engine.message import Message
import asyncio

GAME_ID = "admin" # The game's id you want to join
IP = 'localhost' #"diplomacy.umiacs.umd.edu" the server IP without port specification
PHASE = "S1901M" # you should retrieve the phase from the game directly

async def co():
    # connect to the server, default port 8432
    connection = await connect(IP, 8432)
    # authenticate; to use specific API you need admin permissions
    channel = await connection.authenticate('admin', 'password')
    print('Authenticated', channel)
    # accessing a specific game
    game = await channel.join_game(game_id=GAME_ID)
    print('Joined game', game)
    
    # an example suggestions, where type must be in ["suggested_move_full", "suggested_move_partial", "suggested_message"]
    msg_dict = {
            "sender": "omniscient_type",
            "recipient": "GLOBAL",
            "phase": PHASE,
            "message": "AUSTRIA-F: A VIE - BOH, F TRI - ADR, A BUD - TRI",
            "type": "suggested_move"
        },
        
    # message wrapper, note the moves must be in the same format for the regex parser to work
    msg_obj = Message(
        sender="omniscient_type",
        recipient="GLOBAL",
        phase=PHASE,
        message="AUSTRIA-F: A VIE - BOH, F TRI - ADR, A BUD - TRI",
        type="suggested_move"
    )
    
    # the suggested messages must also be in the same format
    reply = Message(
        sender="omniscient_type",
        recipient="GLOBAL",
        phase=PHASE,
        message="AUSTRIA-TURKEY: meow",
        type="suggested_message"
    )
    # send message API
    game.send_game_message(message=reply)

if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    loop.run_until_complete(co())

game.get_current_phase()

returns information about the current phase

example:

S1901M

game.set_wait(power_name, wait=True)

sets player wait/no wait

game.set_orders(power_name=power_name, orders=agent_orders, wait=True)

sets order for a given player

game.get_units(power_name=NAME)

returns a list of current units

['A BUD', 'A VIE', 'F TRI']

game.get_centers(power_name=NAME)

returns a list of controlled centers

['BUD', 'TRI', 'VIE']

game.get_orders(power_name='AUSTRIA')

['A BUD - TRI', 'A VIE - BOH', 'F TRI - ADR']

game.get_phase_data()

returns a GamePhaseData object, which contains information about orders, messages, etc for a game

data = game.get_phase_data()    
print(data.orders)

# output: {'AUSTRIA': ['A BUD - TRI', 'A VIE - BOH', 'F TRI - ADR'], 'ENGLAND': None, 'FRANCE': None, 'GERMANY': None, 'ITALY': None, 'RUSSIA': None, 'TURKEY': None}

Last updated