-
Notifications
You must be signed in to change notification settings - Fork 98
/
client.py
288 lines (274 loc) · 13.8 KB
/
client.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
280
281
282
283
284
285
286
287
288
import time
import json
import asyncio
import websockets
commands = {
"command.auth.Register": {"nickname": str, "email": str, "cryptoPWD": str},
"command.building.Create": {"building_type": int, "name": str, "x": int, "y": int, "rotation": int},
"command.building.GetBuildingInfo": {"buildingID": int},
"command.building.GetBuildings": {},
"command.chat.ChatToNPC": {"NPCID": int, "content": str},
"command.config.GetBuildingsConfig": {},
"command.config.GetEquipmentsConfig": {},
"command.config.GetNPCsConfig": {},
"command.map.GetMapScene": {},
"command.map.GetMapTown": {},
"command.map.Navigate": {"x": int, "y": int},
"command.npc.Create": {"asset": str, "model": str, "memorySystem": str, "planSystem": str, "nickname": str,
"bio": str, "goal": str, "cash": int},
"command.npc.GetNPCInfo": {"NPCID": int},
"command.npc.GetNPCs": {},
"command.player.GetPlayerInfo": {},
"command.timetick.Tick": {},
}
async def listen_server(websocket):
while True:
msg = await websocket.recv()
print(f"Received: {msg}")
async def send_input(websocket):
# info = json.dumps({"uri": "command.auth.Register", "method": "POST", "data": {"nickname": "fisher", "email": "abc@def.com", "cryptoPWD": "WWW"}}, ensure_ascii=False, separators=(",", ":"))
# await websocket.send(info)
uid = "Player-10001"
while True:
data = dict()
command_list = ",".join([x for x in commands.keys()])
command = input(f"Enter command to send from: {command_list}")
if command not in commands:
print("command not in commands list")
continue
for key, func in commands[command].items():
param = func(input(f"Enter param {key}: "))
data[key] = param
request = json.dumps({"uid": uid, "uri": command, "data": data, "method": "POST"}, ensure_ascii=False,
separators=(",", ":"))
print(f"Send: {request}")
await websocket.send(request)
async def ping(websocket):
# info = json.dumps({"uri": "command.auth.Register", "method": "POST", "data": {"nickname": "fisher", "email": "abc@def.com", "cryptoPWD": "WWW"}}, ensure_ascii=False, separators=(",", ":"))
# await websocket.send(info)
uid = "Player-10001"
while True:
info = json.dumps({"uid": uid, "uri": "ping", "method": "GET", "data": {}}, ensure_ascii=False,
separators=(",", ":"))
print(f"Send: {info}")
await websocket.send(info)
await asyncio.sleep(10)
async def debug(websocket):
# register
info = json.dumps({"uri": "command.auth.Register", "method": "POST",
"data": {"nickname": "Lixing", "email": "Lixing@163.com", "cryptoPWD": "123456"}},
ensure_ascii=False, separators=(",", ":"))
print(f"Send: {info}")
await websocket.send(info)
msg = await websocket.recv()
print(f"Received: {msg}")
uid = "Player-10001"
# GetPlayerInfo
# time.sleep(3)
# get_player_info = json.dumps({"uid": uid, "uri": "command.player.GetPlayerInfo", "method": "POST", "data": {}}, ensure_ascii=False, separators=(",", ":"))
# print(f"Send: {get_player_info}")
# await websocket.send(get_player_info)
# msg = await websocket.recv()
# print(f"Received: {msg}")
# # GetBuildings
# # time.sleep(3)
# get_buildings = json.dumps({"uid": uid, "uri": "command.building.GetBuildings", "method": "POST", "data": {}}, ensure_ascii=False, separators=(",", ":"))
# print(f"Send: {get_buildings}")
# await websocket.send(get_buildings)
# msg = await websocket.recv()
# print(f"Received: {msg}")
# # GetBuildingInfo
# # time.sleep(3)
# building_id = 1
# get_building_info = json.dumps({"uid": uid, "uri": "command.building.GetBuildingInfo", "method": "POST", "data": {"buildingID": building_id}}, ensure_ascii=False, separators=(",", ":"))
# print(f"Send: {get_building_info}")
# await websocket.send(get_building_info)
# msg = await websocket.recv()
# print(f"Received: {msg}")
# # GetBuildingsConfig
# # time.sleep(3)
# get_buildings_config = json.dumps({"uid": uid, "uri": "command.config.GetBuildingsConfig", "method": "POST", "data": {}}, ensure_ascii=False, separators=(",", ":"))
# print(f"Send: {get_buildings_config}")
# await websocket.send(get_buildings_config)
# msg = await websocket.recv()
# print(f"Received: {msg}")
# # GetEquipmentsConfig
# # time.sleep(3)
# get_equipments_config = json.dumps({"uid": uid, "uri": "command.config.GetEquipmentsConfig", "method": "POST", "data": {}}, ensure_ascii=False, separators=(",", ":"))
# print(f"Send: {get_equipments_config}")
# await websocket.send(get_equipments_config)
# msg = await websocket.recv()
# print(f"Received: {msg}")
# # GetNPCsConfig
# # time.sleep(3)
# get_npcs_config = json.dumps({"uid": uid, "uri": "command.config.GetNPCsConfig", "method": "POST", "data": {}}, ensure_ascii=False, separators=(",", ":"))
# print(f"Send: {get_npcs_config}")
# await websocket.send(get_npcs_config)
# msg = await websocket.recv()
# print(f"Received: {msg}")
# # GetMapScene
# # time.sleep(3)
# # GetMapTown
# # time.sleep(3)
# get_map_town = json.dumps({"uid": uid, "uri": "command.map.GetMapTown", "method": "POST", "data": {}}, ensure_ascii=False, separators=(",", ":"))
# print(f"Send: {get_map_town}")
# await websocket.send(get_map_town)
# msg = await websocket.recv()
# print(f"Received: {msg}")
# GetNPCs
# time.sleep(3)
# get_npcs = json.dumps({"uid": uid, "uri": "command.npc.GetNPCs", "method": "POST", "data": {}}, ensure_ascii=False, separators=(",", ":"))
# print(f"Send: {get_npcs}")
# await websocket.send(get_npcs)
# msg = await websocket.recv()
# print(f"Received: {msg}")
# # GetNPCInfo
# # time.sleep(3)
# npc_id = 10001
# get_npc_info = json.dumps({"uid": uid, "uri": "command.npc.GetNPCInfo", "method": "POST", "data": {"NPCID": npc_id}}, ensure_ascii=False, separators=(",", ":"))
# print(f"Send: {get_npc_info}")
# await websocket.send(get_npc_info)
# msg = await websocket.recv()
# print(f"Received: {msg}")
# testName = "changeCash"
# fake_sendings = json.dumps({"uid": uid, "uri": "command.gm.FakeSendings", "method": "POST", "data": {"testName": testName}}, ensure_ascii=False, separators=(",", ":"))
# print(f"Send: {fake_sendings}")
# await websocket.send(fake_sendings)
# msg = await websocket.recv()
# print(f"Received: {msg}")
# start_mayor = json.dumps({"uid": uid, "uri": "command.starter.MayorStarter", "method": "POST", "data": {}}, ensure_ascii=False, separators=(",", ":"))
# print(f"Send: {start_mayor}")
# await websocket.send(start_mayor)
# msg = await websocket.recv()
# print(f"Received: {msg}")
# await asyncio.sleep(10)
# start_ticks = json.dumps({"uid": uid, "uri": "command.starter.TickStarter", "method": "POST", "data": {}}, ensure_ascii=False, separators=(",", ":"))
# print(f"Send: {start_ticks}")
# await websocket.send(start_ticks)
# msg = await websocket.recv()
# print(f"Received: {msg}")
# building.Create
# time.sleep(3)
# building_type = 3
# building_name = "dessert shop"
# building_x = 3
# building_y = 1
# building_rotation = 0
# create_building = json.dumps({"uid": uid, "uri": "command.building.Create", "method": "POST", "data": {"building_type": building_type, "name": building_name, "x": building_x, "y": building_y, "rotation": building_rotation}}, ensure_ascii=False, separators=(",", ":"))
# print(f"Send: {create_building}")
# await websocket.send(create_building)
# msg = await websocket.recv()
# print(f"Received: {msg}")
# building_type = 4
# building_name = "gym"
# building_x = 3
# building_y = 2
# building_rotation = 0
# create_building = json.dumps({"uid": uid, "uri": "command.building.Create", "method": "POST", "data": {"building_type": building_type, "name": building_name, "x": building_x, "y": building_y, "rotation": building_rotation}}, ensure_ascii=False, separators=(",", ":"))
# print(f"Send: {create_building}")
# await websocket.send(create_building)
# msg = await websocket.recv()
# print(f"Received: {msg}")
# building_type = 5
# building_name = "houseZ"
# building_x = 3
# building_y = 4
# building_rotation = 0
# create_building = json.dumps({"uid": uid, "uri": "command.building.Create", "method": "POST", "data": {"building_type": building_type, "name": building_name, "x": building_x, "y": building_y, "rotation": building_rotation}}, ensure_ascii=False, separators=(",", ":"))
# print(f"Send: {create_building}")
# await websocket.send(create_building)
# msg = await websocket.recv()
# print(f"Received: {msg}")
# building_type = 6
# building_name = "park"
# building_x = 4
# building_y = 2
# building_rotation = 0
# create_building = json.dumps({"uid": uid, "uri": "command.building.Create", "method": "POST", "data": {"building_type": building_type, "name": building_name, "x": building_x, "y": building_y, "rotation": building_rotation}}, ensure_ascii=False, separators=(",", ":"))
# print(f"Send: {create_building}")
# await websocket.send(create_building)
# msg = await websocket.recv()
# print(f"Received: {msg}")
# building_type = 10
# building_name = "houseA"
# building_x = 2
# building_y = 3
# building_rotation = 0
# create_building = json.dumps({"uid": uid, "uri": "command.building.Create", "method": "POST", "data": {"building_type": building_type, "name": building_name, "x": building_x, "y": building_y, "rotation": building_rotation}}, ensure_ascii=False, separators=(",", ":"))
# print(f"Send: {create_building}")
# await websocket.send(create_building)
# msg = await websocket.recv()
# print(f"Received: {msg}")
# get_map_scene = json.dumps({"uid": uid, "uri": "command.map.GetMapScene", "method": "POST", "data": {}}, ensure_ascii=False, separators=(",", ":"))
# print(f"Send: {get_map_scene}")
# await websocket.send(get_map_scene)
# msg = await websocket.recv()
# print(f"Received: {msg}")
# npc.Create
# time.sleep(3)
# npc_asset = "premade_01"
# npc_model = "gpt-3.5"
# npc_memory = "LongShortTermMemories"
# npc_plan = "QAFramework"
# npc_home = 3
# npc_work = 5
# npc_nickname = "Alan"
# npc_bio = "Alan is a genius with outstanding talents and the inventor of computers. Allen has an introverted personality and is only interested in the research he focuses on."
# npc_goal = "Allen is committed to conducting more work and research."
# npc_cash = 10000
# create_npc = json.dumps({"uid": uid, "uri": "command.npc.Create", "method": "POST", "data": {"asset": npc_asset, "model": npc_model, "memorySystem": npc_memory, "planSystem": npc_plan, "homeBuilding": npc_home, "workBuilding": npc_work, "nickname": npc_nickname, "bio": npc_bio, "goal": npc_goal, "cash": npc_cash}}, ensure_ascii=False, separators=(",", ":"))
# print(f"Send: {create_npc}")
# await websocket.send(create_npc)
# msg = await websocket.recv()
# print(f"Received: {msg}")
# npc_asset = "premade_04"
# npc_model = "gpt-3.5"
# npc_memory = "LongShortTermMemories"
# npc_plan = "QAFramework"
# npc_home = 3
# npc_work = 5
# npc_nickname = "Fei"
# npc_bio = "Fei is a talented researcher with a research focus on artificial intelligence. As a university professor, she has a passion for open research in the field of artificial intelligence."
# npc_goal = "Share with others the progress in the field of artificial intelligence, as well as helping others and answering their questions."
# npc_cash = 10000
# create_npc = json.dumps({"uid": uid, "uri": "command.npc.Create", "method": "POST", "data": {"asset": npc_asset, "model": npc_model, "memorySystem": npc_memory, "planSystem": npc_plan, "homeBuilding": npc_home, "workBuilding": npc_work, "nickname": npc_nickname, "bio": npc_bio, "goal": npc_goal, "cash": npc_cash}}, ensure_ascii=False, separators=(",", ":"))
# print(f"Send: {create_npc}")
# await websocket.send(create_npc)
# msg = await websocket.recv()
# print(f"Received: {msg}")
# Navigate
# time.sleep(3)
# content = "Hi, how do you feel abount the town?"
# target_x = 91
# target_y = 70
# navigate = json.dumps({"uid": uid, "uri": "command.map.Navigate", "method": "POST", "data": {"x": target_x, "y": target_y}}, ensure_ascii=False, separators=(",", ":"))
# print(f"Send: {navigate}")
# await websocket.send(navigate)
# msg = await websocket.recv()
# print(f"Received: {msg}")
# # ChatToNPC
# # time.sleep(3)
npc_id = 10001
content = "Hi, how do you feel abount the town?"
chat = json.dumps({"uid": uid, "uri": "command.chat.ChatWithNPC", "method": "POST",
"data": {"NPCID": f"NPC-{npc_id}", "content": content}}, ensure_ascii=False,
separators=(",", ":"))
print(f"Send: {chat}")
await websocket.send(chat)
msg = await websocket.recv()
print(f"Received: {msg}")
server_task = asyncio.create_task(listen_server(websocket))
await server_task
async def main():
async with websockets.connect("ws://localhost:8000/ws", ping_interval=None) as websocket:
msg = await websocket.recv()
print(f"Received: {msg}")
# server_task = asyncio.create_task(listen_server(websocket))
debug_task = asyncio.create_task(debug(websocket))
# input_task = asyncio.create_task(send_input(websocket))
ping_task = asyncio.create_task(ping(websocket))
# await server_task
await debug_task
# await input_task
await ping_task
asyncio.run(main())