async def chat(request, sysid): payload = server.unprocess_payload(sysid, request.body) text_message = payload['text'] for _, player in PLAYERS.items(): player['chat_queue'] += f'{player["char"]} says: {text_message}\n' chat_queue = PLAYERS[sysid]['chat_queue'] PLAYERS[sysid]['chat_queue'] = '' return_payload = server.process_payload(sysid, {'message': chat_queue}) return response.text(return_payload)
async def init(request, sysid): payload = server.unprocess_payload(sysid, request.body) char = payload['char'] # random starting location between (1, 1) and (8, 8) x, y = random.randint(1, 8), random.randint(1, 8) # register character PLAYERS[sysid] = {'char': char, 'location': (x, y), 'chat_queue': ''} return_payload = server.process_payload(sysid, {'message': get_map_payload()}) return response.text(return_payload)
async def move(request, sysid): payload = server.unprocess_payload(sysid, request.body) direction = payload['direction'] x, y = PLAYERS[sysid]['location'] if direction == 'north': my, mx = y - 1, x elif direction == 'south': my, mx = y + 1, x elif direction == 'east': my, mx = y, x + 1 elif direction == 'west': my, mx = y, x - 1 if MAP[my][mx] in ['+', '-', '|']: return_payload = server.process_payload( sysid, {'message': 'You cannot move that way!'}) else: PLAYERS[sysid]['location'] = (mx, my) return_payload = server.process_payload( sysid, {'message': 'You moved {d}!'.format(d=direction)}) return response.text(return_payload)
async def logout(request, sysid): PLAYERS.pop(sysid) return_payload = server.process_payload(sysid, {'message': 'logout'}) return response.text(return_payload)
async def look(request, sysid): return_payload = server.process_payload(sysid, {'message': get_map_payload()}) return response.text(return_payload)
async def test(request, sysid): payload = server.unprocess_payload(sysid, request.body) print('RECV: ' + str(payload)) return_payload = server.process_payload(sysid, {'Look Mom': 'No Hands!'}) print('SENT: ' + str(return_payload)) return response.text(return_payload)