forked from SHADE-AI/diplomacy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_comm_2.py
155 lines (122 loc) · 5.86 KB
/
test_comm_2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import asyncio
import random
from diplomacy.client.connection import connect
from diplomacy.utils import exceptions
from diplomacy_research.players import RuleBasedPlayer
from diplomacy_research.players.rulesets import dumbbot_ruleset
from random import *
from diplomacy.utils import strings
import time
POWERS = ['AUSTRIA', 'FRANCE', 'GERMANY', 'ITALY', 'RUSSIA', 'TURKEY']
STATUS = [strings.BUSY, strings.READY, strings.INACTIVE]
async def create_game(game_id, hostname='localhost', port=8432):
""" Creates a game on the server """
connection = await connect(hostname, port)
channel = await connection.authenticate('random_user', 'password')
await channel.create_game(game_id=game_id, rules={'REAL_TIME', 'NO_DEADLINE', 'POWER_CHOICE'})
def on_game_processed(network_game, notification):
print("({}/{}): notification received {} for role = {}".format(network_game.get_current_phase(), network_game.power.name, notification.name, notification.game_role))
def on_message_received(network_game, notification):
msg = notification.message
sender = msg.sender
recipient = msg.recipient
message = msg.message
print("({}/{}): {} received the following message from {}: \n\t{}".format(network_game.get_current_phase(), notification.game_role, recipient, sender, message))
async def play(game_id, power_name, hostname='localhost', port=8432):
""" Play as the specified power """
connection = await connect(hostname, port)
channel = await connection.authenticate("bot_"+power_name,'password')
bot = RuleBasedPlayer(dumbbot_ruleset)
# Waiting for the game, then joining it
while not (await channel.list_games(game_id=game_id)):
await asyncio.sleep(1.)
TYPES = [strings.HUMAN, strings.NO_PRESS_BOT, strings.PRESS_BOT]
type = TYPES[randint(0,2)]
print("({}): joining as type = {}".format(power_name, strings.PRESS_BOT))
game = await channel.join_game(game_id=game_id, power_name=power_name, player_type=type)
game.add_on_game_processed(on_game_processed)
game.add_on_game_message_received(on_message_received)
temp = {}
for p in game.powers.values():
temp[p.name] = [p.player_type, p.comm_status]
print(power_name)
print(temp)
allPlayersJoined = False
while game.is_game_active == False & allPlayersJoined == False:
#all player_type must be not strings.NONE to proceed
playerTypes = [pow.player_type for pow in game.powers.values()]
if strings.NONE not in playerTypes:
allPlayersJoined = True
print("{}: everyone is ready!".format(power_name))
await asyncio.sleep(0.1)
temp = {}
for p in game.powers.values():
temp[p.name] = [p.player_type, p.comm_status]
print(power_name)
print(temp)
# Playing game
while not game.is_game_done:
beginNeg = False
current_phase = game.get_current_phase()
temp = {}
for p in game.powers.values():
temp[p.name] = [p.player_type, p.comm_status]
print(power_name)
print(temp)
#simulate a delay for pinging model
#set flag for communication
thinking = randint(0,20)
print("({}/{}) thinking for {}s".format(current_phase, power_name, str(thinking)))
await asyncio.sleep(thinking)
await game.set_comm_status(comm_status=strings.READY)
print("({}/{}) set comm_status to READY".format(current_phase, power_name))
#INITIAL STRATEGY DECISION
#wait until everyone is ready to communicate (simple case: check all 7 are ready
while beginNeg == False:
status = [pow.comm_status == strings.READY for pow in game.powers.values()]
if sum(status) == 7:
beginNeg = True
print("({}/{}): everyone is ready to communicate".format(current_phase, power_name))
await asyncio.sleep(4)
#LOGGING
if random() > 0.5:
msg = current_phase + "\t" + "LOG CHECK from " + power_name
await game.send_log_data(log=game.new_log_data(body=msg))
await asyncio.sleep(2)
#DIPLOMACY
diplomacy = True
dipTime = 10
t1 = time.time()
while diplomacy:
#SEND MESSAGES
if random() > 0.5:
temp = [rec for rec in POWERS if not rec == power_name]
recipient = temp[randint(0,len(temp)-1)]
msg = "({}/{}): sending message to {}".format(current_phase, power_name, recipient)
print(msg)
await game.send_game_message(message=game.new_power_message(recipient, msg))
await asyncio.sleep(1)
t2 = time.time()
if t2 - t1 > dipTime:
print("({}/{}): diplomacy phase complete".format(current_phase, power_name))
diplomacy = False
# Submitting orders
orders = await bot.get_orders(game, power_name)
print("({}/{}): orders {}".format(current_phase, power_name, orders))
await game.set_orders(power_name=power_name, orders=orders, wait=False)
# Waiting for game to be processed
while current_phase == game.get_current_phase():
await asyncio.sleep(0.1)
# A local copy of the game can be saved with to_saved_game_format
# To download a copy of the game with messages from all powers, you need to export the game as an admin
# by logging in as 'admin' / 'password'
print(power_name + "----------------------------")
print(game.log_history)
async def launch(game_id):
""" Creates and plays a network game """
game_id = "test10"
#await create_game(game_id, hostname="localhost")
await play(game_id, "FRANCE", hostname="localhost")
#await asyncio.gather(*[play(game_id, power_name, hostname="localhost") for power_name in POWERS])
if __name__ == '__main__':
asyncio.run(launch(game_id=str(randint(1, 1000))))