Instantly share code, notes, and snippets.

anonymous /challenge.sh Secret
Created Apr 5, 2017

Embed
What would you like to do?
ESSENCE Challenge Python Wrapper
#! /usr/bin/env bash
mkfifo pipe
guesser=$1
token=$2
game=$3
./wrapper.py $token $game < pipe | ./$guesser > pipe
rm -rf pipe
#! /usr/bin/env python
import sys
import requests
stdin = sys.stdin
stdout = sys.stdout
root_url = "http://challenge.essence-network.com/describer/v1/"
start_url = "start/"
game_url = "play?gameId={0}&guess={1}"
game_id = None
def parse_response(res_text, is_first=False):
response_parsed_data = [str(elem) for elem in res_text.split("---")]
if is_first:
game_id = response_parsed_data[0].split(":")[1].replace(" ", "")
first_hint = response_parsed_data[2].split(":")[1].replace(" ", "")
return first_hint, game_id
answer = None
if not "Target" in response_parsed_data[0]:
answer = response_parsed_data[0].replace(" ", "")
next_hint = None
if ":" in response_parsed_data[1]:
next_hint = response_parsed_data[1].split(":")[1].replace(" ","")
return answer, next_hint
def get_answer(guess):
url = (root_url + game_url).format(game_id, guess)
res = requests.get(url, headers = headers)
return parse_response(res.text)
def make_request(url):
return requests.get(url, headers = headers)
# 1. Get the game number
if len(sys.argv) < 3:
sys.stderr.write("ERROR! Usage: ./wrapper <user_token> <game_number>\n")
sys.exit(1)
game_number = sys.argv[2]
token = sys.argv[1]
headers = {
'token': token
}
# 2. Start game
url = root_url + start_url + game_number
res = make_request(url)
hint, game_id = parse_response(res.text, True)
is_game_ended = False
while not is_game_ended:
# 3. Give hint to guesser -> write to STDOUT
stdout.write("{0}\n".format(hint))
stdout.flush()
# 3.1 Get guess from guesser -> read from STDIN
guess = stdin.readline()
guess = guess.replace("\n", "")
# 4. Send guess to server and wait for answer
answer, next_hint = get_answer(guess)
# 5. Give answer to guesser and next hint -> write to STDOUT
if answer == 'YES!':
stdout.write("CITY_FOUND")
stdout.flush()
is_game_ended = True
city_found = True
else:
hint = next_hint
if not hint:
is_game_ended = True
city_found = False
stdout.write("NO_MORE_HINTS\n")
stdout.flush()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment