splash.py 1.42 KB
Newer Older
Sorrel Harriet's avatar
Sorrel Harriet committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#!/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' ) )