Skip to content

first commit #22

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.env


api_key.py
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"python.pythonPath": "/home/ann/.local/share/virtualenvs/CS-Build-Week-2-BJXtAkqp/bin/python"
}
13 changes: 13 additions & 0 deletions Pipfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[[source]]
name = "pypi"
url = "https://pypi.org/simple"
verify_ssl = true

[dev-packages]

[packages]
request = "*"
requests = "*"

[requires]
python_version = "3.6"
88 changes: 88 additions & 0 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added __pycache__/api_key.cpython-36.pyc
Binary file not shown.
183 changes: 183 additions & 0 deletions adv.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
import random
import requests
import json
from api_key import API_KEY
import time
from random import randint, choice

url = 'https://lambda-treasure-hunt.herokuapp.com/api'

headers = {
'Authorization': API_KEY
}

def init():
r = requests.get(f'{url}/adv/init/', headers=headers)
data = r.json()
if 'errors' in data and len(data['errors']) > 0:
print(data)
return False
with open('current_state.txt', 'w') as f:
f.write(json.dumps(data, indent=4))
print(data)
return data


def move(payload):
r_move = requests.post(f'{url}/adv/move', data=json.dumps(payload), headers=headers)
data = r_move.json()

# if 'errors' in data and len(data['errors']) > 0:
# print(data)
# return False
with open('current_state.txt', 'w') as f:
f.write(json.dumps(data, indent=4))
current_state = get_contents()
# print(f'current_state, {current_state}\n')

if str(data['coordinates']) not in current_state.keys():
current_state[data['coordinates']] = data
save_content(current_state)
print(data)
return data

def sleep_print(seconds):
while seconds > 0:
print('Wait', seconds, ' seconds!')
time.sleep(1)
seconds = seconds - 1

def save_content(data):
with open('map.txt', 'w') as f:
f.write(json.dumps(data, indent=4))

def get_contents():
map_file = open('map.txt', 'rb').read()
contents = json.loads(map_file)
# print(f'Visited rooms: {len(contents)}')
return contents

def get_item(item):
r = requests.post(f'{url}/adv/take', data=json.dumps(item), headers=headers)
data = r.json()
print(data)
return data

def drop_item(item):
r = requests.post(f'{url}/adv/drop', data=json.dumps(item), headers=headers)
data = r.json()
print(data)
return data

def change_name():
name = {"name":"[ANN]", 'confirm':'aye'}
r = requests.post(f'{url}/adv/change_name', data=json.dumps(name), headers=headers)
data = r.json()
print(data)
return data

def status():
r = requests.post(f'{url}/adv/status', headers=headers)
data = r.json()
print(data)
return data

def wear(item):
r = requests.post(f'{url}/adv/wear', data=json.dumps(item), headers=headers)
data = r.json()
print(data)
return data



traversal_path = []
previous_room = [None]
opposites_direction = {'n': 's', 'e': 'w', 's': 'n', 'w': 'e'}

room_track = {}
visited = {}

def travel():
current_room = init()
sleep_print(1)
def check_directions(room_id):
direction = []
if 'e' in current_room['exits']:
direction.append('e')
if 'n' in current_room['exits']:
direction.append('n')
if 'w' in current_room['exits']:
direction.append('w')
if 's' in current_room['exits']:
direction.append('s')
return direction

while len(visited) < len(get_contents()):
cooldown = current_room['cooldown']
print('----cooldown---', cooldown)
room_id = current_room['room_id']
print(f'\nmap----: {len(get_contents())}\n')
sleep_print(cooldown)
if room_id not in room_track:
visited[room_id] = room_id
print('visited room ', len(visited))
room_track[room_id] = check_directions(room_id)

if len(room_track[room_id]) < 1:
print('room track-----if ', len(room_track))
previous_direction = previous_room.pop()
traversal_path.append(previous_direction)
current_room = move({"direction": previous_direction})
print('move pre')
# print('coor', current_room['coordinates'])
sleep_print(cooldown)
else:
print('room track-----else', len(room_track))
print('room_track[room_id]: ', len(room_track[room_id]))
print('exits if', current_room['exits'][0])
print(room_track[room_id])
next_direction = room_track[room_id].pop(0)
previous_room.append(opposites_direction[next_direction])
traversal_path.append(next_direction)
current_room = move({"direction": next_direction})
print('next_direction',next_direction)
sleep_print(cooldown+1)



def travel_2():
while True:
current_room = move({"direction":'e'})
if len(current_room['errors']) > 0:
current_room = move({"direction":'w'})






# init()
# move({'direction': "s"})
# move({'direction': "e", "next_room_id": "55"})
# sleep_print(10)

# change_name()
status()
# get_item({'name': 'tiny treasure'})
# drop_item({'name': 'shiny treasure'})
# sleep_print(8)
# print(f'visited :{len(get_contents())} rooms')
# travel()
# travel_2()

# wear({"name":"[tiny treasure]"})

# curl -X POST -H 'Authorization: Token acd50be286c9bd90d6f6b57922a3493725f26c28' -H "Content-Type: application/json" -d '{"name":"[ANN]"}' https://lambda-treasure-hunt.herokuapp.com/api/adv/examine/








24 changes: 24 additions & 0 deletions current_state.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{
"room_id": 55,
"title": "Wishing Well",
"description": "You are standing besides a large well. A sign next the well reads 'EXAMINE WELL, FIND WEALTH'.",
"coordinates": "(63,61)",
"elevation": 0,
"terrain": "NORMAL",
"players": [
"[matthew_harshman]",
"Andy",
"Sean Wu",
"Kathryn",
"['Matt']"
],
"items": [],
"exits": [
"w"
],
"cooldown": 20.0,
"errors": [
"You cannot move that way: +5s CD"
],
"messages": []
}
Loading