-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplay_game.py
279 lines (259 loc) · 10.9 KB
/
play_game.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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
# coding=utf-8
# Copyright 2019 Google LLC
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Script allowing to play the game by multiple players."""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from absl import app
from absl import flags
from absl import logging
from gfootball.env import config
from gfootball.env import football_env
import os
import openai
import webbrowser
openai.api_key = "api_key"
list = openai.Model.list()
FLAGS = flags.FLAGS
flags.DEFINE_string('players', 'keyboard:left_players=1',
'Semicolon separated list of players, single keyboard '
'player on the left by default')
flags.DEFINE_string('level', '', 'Level to play')
flags.DEFINE_enum('action_set', 'default', ['default', 'full'], 'Action set')
flags.DEFINE_bool('real_time', True,
'If true, environment will slow down so humans can play.')
flags.DEFINE_bool('render', True, 'Whether to do game rendering.')
def gpt3_completion(prompt, engine='text-davinci-003', temp=0.7, top_p=1.0, tokens=300, freq_pen=0.5, pres_pen=0.5, stop=['DARIUS:', 'AGENT:']):
prompt = prompt.encode(encoding='ASCII', errors='ignore').decode()
response = openai.Completion.create(
engine = engine,
prompt = prompt,
temperature = temp,
max_tokens = tokens,
top_p = top_p,
frequency_penalty = freq_pen,
presence_penalty = pres_pen,
stop = stop
)
text = response['choices'][0]['text'].strip()
return text
def ttsCreator(response):
tts = response.replace(' ', '%20')
tts = tts.replace(',', '%2C')
tts = tts.replace('\'', '%27')
tts = tts.replace('!', '')
browser = 'http://localhost:5500/api/tts?voice=marytts%3Adfki-spike-hsmm&text=' + tts + '%21&vocoder=high&denoiserStrength=0.03&cache=false'
webbrowser.open(browser)
def main(_):
players = FLAGS.players.split(';') if FLAGS.players else ''
assert not (any(['agent' in player for player in players])
), ('Player type \'agent\' can not be used with play_game.')
cfg_values = {
'action_set': FLAGS.action_set,
'dump_full_episodes': True,
'players': players,
'real_time': FLAGS.real_time,
}
if FLAGS.level:
cfg_values['level'] = FLAGS.level
cfg = config.Config(cfg_values)
env = football_env.FootballEnv(cfg)
if FLAGS.render:
env.render()
env.reset()
try:
currentState = 0
currentBallPos = ''
currentBallTeam = -1
currentBallPlayer = -1
currentHomeYellow = 0
currentAwayYellow = 0
currentHomeScore = 0
currentAwayScore = 0
lastPlayer = ''
commentary = 'The following is a conversation between AGENT and DARIUS. DARIUS is a football commentator, who is getting information about the match from AGENT. The football match will be between Real Madrid and Manchester United.'
welcome = commentary + ' AGENT: The match will start in a second. Make a welcome talk, where you tell who is playing, in what stadium (Santiago Bernabeu), and who are the best players in both teams (Ronaldo, and Rashford)'
prompt = welcome + '\nDARIUS: '
response = gpt3_completion(prompt)
ttsCreator(response)
prompt = prompt + response
while True:
_, _, done, _ = env.step([])
state = env.observation()['game_mode']
state1 = env.observation()
timeInt = int(90-(env.observation()['steps_left']/33.33))
time = str(timeInt)
homeYellow = sum(env.observation()['left_team_yellow_card'])
awayYellow = sum(env.observation()['right_team_yellow_card'])
ballTeam = env.observation()['ball_owned_team']
ballPlayer = env.observation()['ball_owned_player']
score = env.observation()['score']
ballState = env.observation()['ball']
if ballPlayer != currentBallPlayer:
if ballTeam == 0 and ballPlayer == 0:
lastPlayer = 'Courtois'
currentBallPos = 'Real Madrid'
if ballTeam == 0 and ballPlayer == 1:
lastPlayer = 'Camavinga'
currentBallPos = 'Real Madrid'
if ballTeam == 0 and ballPlayer == 2:
lastPlayer = 'Sergio Ramos'
currentBallPos = 'Real Madrid'
if ballTeam == 0 and ballPlayer == 3:
lastPlayer = 'Militao'
currentBallPos = 'Real Madrid'
if ballTeam == 0 and ballPlayer == 4:
lastPlayer = 'Carvajal'
currentBallPos = 'Real Madrid'
if ballTeam == 0 and ballPlayer == 5:
lastPlayer = 'Modric'
currentBallPos = 'Real Madrid'
if ballTeam == 0 and ballPlayer == 6:
lastPlayer = 'Kross'
currentBallPos = 'Real Madrid'
if ballTeam == 0 and ballPlayer == 7:
lastPlayer = 'Valverde'
currentBallPos = 'Real Madrid'
if ballTeam == 0 and ballPlayer == 8:
lastPlayer = 'Vinicius'
currentBallPos = 'Real Madrid'
if ballTeam == 0 and ballPlayer == 9:
lastPlayer = 'Benzema'
currentBallPos = 'Real Madrid'
if ballTeam == 0 and ballPlayer == 10:
lastPlayer = 'Ronaldo'
currentBallPos = 'Real Madrid'
if ballTeam == 1 and ballPlayer == 0:
lastPlayer = 'De Gea'
currentBallPos = 'Manchester United'
if ballTeam == 1 and ballPlayer == 1:
lastPlayer = 'Martinez'
currentBallPos = 'Manchester United'
if ballTeam == 1 and ballPlayer == 2:
lastPlayer = 'Maguire'
currentBallPos = 'Manchester United'
if ballTeam == 1 and ballPlayer == 3:
lastPlayer = 'Sabitzer'
currentBallPos = 'Manchester United'
if ballTeam == 1 and ballPlayer == 4:
lastPlayer = 'Fred'
currentBallPos = 'Manchester United'
if ballTeam == 1 and ballPlayer == 5:
lastPlayer = 'Eriksen'
currentBallPos = 'Manchester United'
if ballTeam == 1 and ballPlayer == 6:
lastPlayer = 'Casemiro'
currentBallPos = 'Manchester United'
if ballTeam == 1 and ballPlayer == 7:
lastPlayer = 'Bruno Fernandes'
currentBallPos = 'Manchester United'
if ballTeam == 1 and ballPlayer == 8:
lastPlayer = 'Martial'
currentBallPos = 'Manchester United'
if ballTeam == 1 and ballPlayer == 9:
lastPlayer = 'Rashford'
currentBallPos = 'Manchester United'
if ballTeam == 1 and ballPlayer == 10:
lastPlayer = 'Garnacho'
currentBallPos = 'Manchester United'
currentBallPlayer = ballPlayer
if state != currentState:
if state == 1:
print('kickoff')
if state == 2:
prompt = prompt + '\nAGENT: In ' + time + ' minute, the ball was kicked out from the field by ' + currentBallPos + 'player, ' + lastPlayer
prompt = prompt + '\nDARIUS: '
response = gpt3_completion(prompt)
ttsCreator(response)
prompt = prompt + response
if state == 3:
prompt = prompt + '\nAGENT: In ' + time + ' minute ' + lastPlayer + ' from ' + currentBallPos + ' is on offside'
prompt = prompt + '\nDARIUS: '
response = gpt3_completion(prompt)
ttsCreator(response)
prompt = prompt + response
if state == 4:
prompt = prompt + '\nAGENT: In ' + time + ' minute, the ball is kicked on corner by ' + lastPlayer + ', ' + currentBallPos + ' will have a chance'
prompt = prompt + '\nDARIUS: '
response = gpt3_completion(prompt)
ttsCreator(response)
prompt = prompt + response
if state == 5:
prompt = prompt + '\nAGENT: In ' + time + ' minute, there is throwout made by ' + lastPlayer + ' from ' + currentBallPos
Prompt = prompt + '\nDARIUS: '
response = gpt3_completion(prompt)
ttsCreator(response)
prompt = prompt + response
if state == 6:
prompt = prompt + '\nAGENT: In ' + time + ' minute, ' + lastPlayer + ' is fouled in penalty box and ' + currentBallPos + ' will have a penalty'
prompt = prompt + '\nDARIUS: '
response = gpt3_completion(prompt)
ttsCreator(response)
prompt = prompt + response
if state == 0:
prompt = prompt + '\nAGENT: It is ' + time + ' minute of the match, the score is ' + str(score[0]) + ' to ' + str(score[1]) + ', talk briefly about the situation in the match '
prompt = prompt + '\nDARIUS: '
response = gpt3_completion(prompt)
print(response)
prompt = prompt + response
currentState = state
if homeYellow > currentHomeYellow:
prompt = prompt + '\nAGENT: In ' + time + ' minute, there is yellow card for United'
prompt = prompt + '\nDARIUS: '
response = gpt3_completion(prompt)
ttsCreator(response)
prompt = prompt + response
currentHomeYellow = homeYellow
if awayYellow > currentAwayYellow:
prompt = prompt + '\nAGENT: In ' + time + ' minute, there is yellow card for Real'
prompt = prompt + '\nDARIUS: '
response = gpt3_completion(prompt)
ttsCreator(response)
prompt = prompt + response
currentAwayYellow = awayYellow
if score[0] > currentHomeScore:
prompt = prompt + '\nAGENT: In ' + time + ' minute, there is goal for Real, scored by ' + lastPlayer
prompt = prompt + '\nDARIUS: '
response = gpt3_completion(prompt)
ttsCreator(response)
prompt = prompt + response
currentHomeScore = score[0]
if score[1] > currentAwayScore:
prompt = prompt + '\nAGENT: In ' + time + ' minute, there is goal for United, scored by ' + lastPlayer
prompt = prompt + '\nDARIUS: '
response = gpt3_completion(prompt)
ttsCreator(response)
prompt = prompt + response
currentAwayScore = score[1]
if done:
break
except KeyboardInterrupt:
pass
finally:
if score[0] > score[1]:
winner= 'for Real Madrid'
if score[0] < score[1]:
winner= 'for Manchester United'
if score[0] == score[1]:
winner= 'so it is a draw'
prompt = prompt + '\nAGENT: It is the end of the match, the score is ' + str(score[0]) + ' to ' + str(score[1]) + ' ' + winner + ' summarize the match'
prompt = prompt + '\nDARIUS: '
response = gpt3_completion(prompt)
prompt = prompt + response
print(prompt)
ttsCreator(response)
env.write_dump('shutdown')
exit(1)
if __name__ == '__main__':
app.run(main)