· 6 years ago · Mar 10, 2019, 05:44 PM
1def request_handler(request):
2 point = (float(request['values']['lon']), float(request['values']['lat']))
3 username = request['values']['username']
4 current_area = get_area(point, locations)
5 location_id = 0;#This will be stored in the database
6 conn = sqlite3.connect(db) #connect to that database (will create if it doesn't already exist)
7 c = conn.cursor() #make cursor into database (allows us to execute commands)
8 c.execute('CREATE TABLE IF NOT EXISTS player_table (user text,location_id int);') # run a CREATE TABLE command
9 response = c.execute('''SELECT * FROM player_table WHERE user = ?;''',(username,)).fetchone()
10 if response:
11 location_id = response[1]
12 if current_area == treasure_hunt_path[location_id]:
13 location_id += 1
14 c.execute('''UPDATE player_table SET location_id = ? WHERE user = ?;''',(location_id,username))
15 ret_string = 'You are in ' + str(current_area) + ", you should now go to " + str(treasure_hunt_path[location_id])
16 else:
17 ret_string = 'You have not progressed in the treasure hunt, please go to ' + str(treasure_hunt_path[location_id])
18 else:
19 c.execute('''INSERT into player_table VALUES (?,?);''',(username,location_id))
20 ret_string = 'Welcome to treasure hunt, please go to ' + str(treasure_hunt_path[location_id])
21 conn.commit() # commit commands
22 conn.close() # close connection to database
23 return ret_string