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