#!/usr/bin/env python3 # import modules from Python Standard library import cgi import cgitb cgitb.enable() # import custom modules from config import config import utils import components # tell browser to expect HTML print("Content-Type: text/html\n") # render header HTML print( utils.render_template( config['TEMPLATE_DIR'] + 'header.html') ) # render navigation HTML print( utils.render_template( config['TEMPLATE_DIR'] + 'nav.html') ) # get any data sent with a GET or POST request sent_data = cgi.FieldStorage() # for now, all players have the id of 1 player_id = 1 # connect to a database cnx = utils.db_connect( config ) # check there's an open connection to database if cnx: # create a cursor object cursor = cnx.cursor() # decide what component to serve according to page parameter from query string if 'page' in sent_data: if sent_data['page'].value == 'leaderboard': components.leaderboard() elif sent_data['page'].value == 'play': components.play_game(cursor, player_id) else: components.home() # respond to a gameplay form submission elif 'won' in sent_data: components.record_game(cursor, sent_data) # Make sure data is committed to the database cnx.commit() # close the connection to the database cursor.close() cnx.close() # render footer print( utils.render_template( config['TEMPLATE_DIR']+'footer.html' ) )