Rogier Neeleman
8 years ago
13 changed files with 303 additions and 0 deletions
@ -0,0 +1,8 @@
@@ -0,0 +1,8 @@
|
||||
# Ignoring compiled python code |
||||
*.py[cod] |
||||
|
||||
# Ignoring the config file's |
||||
*.cfg |
||||
|
||||
# Ignoring the database |
||||
*.db |
@ -0,0 +1,34 @@
@@ -0,0 +1,34 @@
|
||||
# NFGame, a simple FNC game |
||||
|
||||
## Installation |
||||
|
||||
### Requirements |
||||
The next software is needed to run this game: |
||||
|
||||
- Python 2.6 or higher OR |
||||
- Python 3.3 or higher |
||||
- Flask 0.11 or higher |
||||
|
||||
You can install this as root by doing the following: |
||||
|
||||
- Debian: apt-get install python-flask |
||||
- FreeBSD: pkg install py27-Flask |
||||
- pip: pip install Flask |
||||
|
||||
### Getting the software |
||||
Just clone the software from github: |
||||
|
||||
git clone https://github.com/sciuro/nfgame |
||||
|
||||
### Configure the software |
||||
Copy the file nfgame.cfg-example to nfgame.cfg and edit to your preferences. |
||||
|
||||
## Running the game |
||||
./run.sh |
||||
|
||||
The site is running on http://<ip>:5000 |
||||
|
||||
## Debug mode |
||||
You can enable the debug mode by running: |
||||
|
||||
./run.sh debug |
@ -0,0 +1,9 @@
@@ -0,0 +1,9 @@
|
||||
# All the tags and there human readable name |
||||
TAGS = {'taghash1': 'tree', |
||||
'taghash2': 'toilet', |
||||
'taghash3': 'tent', |
||||
'taghash4': 'water tap' |
||||
} |
||||
|
||||
# For storing the cookie information to the client |
||||
SECRET_KEY = 'Very secret key!' |
@ -0,0 +1,148 @@
@@ -0,0 +1,148 @@
|
||||
# all the imports |
||||
import os |
||||
import sqlite3 |
||||
from flask import Flask, request, g, redirect, url_for, abort, \ |
||||
render_template, flash, session |
||||
import random |
||||
|
||||
# create our little application :) |
||||
app = Flask(__name__) |
||||
app.config.from_object(__name__) |
||||
|
||||
# Load default config and override config from an environment variable |
||||
app.config.update(dict( |
||||
DATABASE = os.path.join(app.root_path, 'nfgame.db'), |
||||
TAGS = {'taghash1': 'tagname1', |
||||
'taghash2': 'tagname2', |
||||
'taghash3': 'tagname3', |
||||
'taghash4': 'tagname4' |
||||
}, |
||||
SECRET_KEY = 'Very secret key!' |
||||
)) |
||||
app.config.from_envvar('NFGAME_SETTINGS', silent=True) |
||||
app.secret_key = app.config['SECRET_KEY'] |
||||
|
||||
def connect_db(): |
||||
"""Connects to the database.""" |
||||
rv = sqlite3.connect(app.config['DATABASE']) |
||||
rv.row_factory = sqlite3.Row |
||||
return rv |
||||
|
||||
def init_db(): |
||||
db = get_db() |
||||
with app.open_resource('schema.sql', mode='r') as f: |
||||
db.cursor().executescript(f.read()) |
||||
db.commit() |
||||
|
||||
@app.cli.command('initdb') |
||||
def initdb_command(): |
||||
"""Initializes the database.""" |
||||
init_db() |
||||
print 'Initialized the database.' |
||||
|
||||
def get_db(): |
||||
"""Opens a new database connection if there is none yet for the |
||||
current application context. |
||||
""" |
||||
if not hasattr(g, 'sqlite_db'): |
||||
g.sqlite_db = connect_db() |
||||
return g.sqlite_db |
||||
|
||||
@app.teardown_appcontext |
||||
def close_db(error): |
||||
"""Closes the database again at the end of the request.""" |
||||
if hasattr(g, 'sqlite_db'): |
||||
g.sqlite_db.close() |
||||
|
||||
@app.route('/') |
||||
def index(): |
||||
db = get_db() |
||||
cur = db.execute('select * from score') |
||||
entries = cur.fetchall() |
||||
|
||||
user = {} |
||||
tags = app.config['TAGS'] |
||||
|
||||
for entry in entries: |
||||
if entry['tags'] == None: |
||||
found_tags = [] |
||||
else: |
||||
found_tags = entry['tags'].split(',') |
||||
|
||||
user[entry['id']] = {} |
||||
for tag in tags: |
||||
user[entry['id']][tag] = 'Not' |
||||
for found_tag in found_tags: |
||||
if found_tag == tag: |
||||
user[entry['id']][tag] = 'Found' |
||||
|
||||
return render_template('overview.html', entries=entries, tags=app.config['TAGS'], user=user) |
||||
|
||||
@app.route('/newuser', methods=['GET', 'POST']) |
||||
def new_user(): |
||||
"""If it's a GET request, no new user should be made""" |
||||
if request.method == 'GET': |
||||
return render_template('newuser.html') |
||||
|
||||
"""Now we got a POST request""" |
||||
db = get_db() |
||||
cur = db.execute("insert into score (username) values (?)", [request.form['username']]) |
||||
db.commit() |
||||
session['username'] = request.form['username'] |
||||
|
||||
db = get_db() |
||||
cur = db.execute('select * from score where username = ?', [session['username']]) |
||||
entries = cur.fetchall() |
||||
session['id'] = entries[0]['id'] |
||||
|
||||
return render_template('newuser_done.html') |
||||
|
||||
@app.route('/tag/<string:taghash>') |
||||
def tag_found(taghash): |
||||
if not 'id' in session: |
||||
return redirect(url_for('new_user')) |
||||
|
||||
tags = app.config['TAGS'] |
||||
|
||||
if not tags.has_key(taghash): |
||||
return render_template('tagnotfound.html') |
||||
|
||||
db = get_db() |
||||
cur = db.execute('select * from score where id = ?', [session['id']]) |
||||
entries = cur.fetchall() |
||||
|
||||
cur_score = entries[0]['tags'] |
||||
if cur_score == None: |
||||
cur_score = taghash |
||||
else: |
||||
found_tags = cur_score.split(',') |
||||
for found_tag in found_tags: |
||||
if taghash == found_tag: |
||||
return render_template('tagalreadyfound.html', tagname=tags.get(taghash)) |
||||
break |
||||
|
||||
cur_score = cur_score + "," + taghash |
||||
|
||||
db = get_db() |
||||
cur = db.execute('update score set tags = ? where id = ?', [cur_score, session['id']]) |
||||
db.commit() |
||||
|
||||
return render_template('tagfound.html', tagname=tags.get(taghash)) |
||||
|
||||
@app.route('/deletescore') |
||||
def delete_score(): |
||||
db = get_db() |
||||
cur = db.execute("delete from score") |
||||
db.commit() |
||||
|
||||
return redirect(url_for('index')) |
||||
|
||||
@app.route('/deleteuser') |
||||
def delete_user(): |
||||
session.pop('username', None) |
||||
session.pop('id', None) |
||||
|
||||
return redirect(url_for('index')) |
||||
|
||||
if __name__ == '__main__': |
||||
app.run() |
@ -0,0 +1,27 @@
@@ -0,0 +1,27 @@
|
||||
#!/bin/bash |
||||
|
||||
# Settings |
||||
RUNPORT=5000 |
||||
BINDIP=0.0.0.0 |
||||
|
||||
# Export the variables |
||||
export FLASK_APP=nfgame.py |
||||
export NFGAME_SETTINGS=nfgame.cfg |
||||
|
||||
# If working in debug mode |
||||
if [[ $1 == 'debug' ]]; then |
||||
export FLASK_DEBUG=1 |
||||
else |
||||
export FLASK_DEBUG=0 |
||||
fi |
||||
|
||||
# If the database does not exists, create it. |
||||
if [ ! -f nfgame.db ]; then |
||||
python -m flask initdb |
||||
fi |
||||
|
||||
if [ ! -f nfgame.cfg ]; then |
||||
echo "Please copy nfgame.cfg-example to nfgame.cfg and edit the options!" |
||||
else |
||||
python -m flask run --host=$BINDIP --port=$RUNPORT |
||||
fi |
@ -0,0 +1,6 @@
@@ -0,0 +1,6 @@
|
||||
drop table if exists score; |
||||
create table score ( |
||||
id integer primary key autoincrement, |
||||
username text not null, |
||||
tags text null |
||||
); |
@ -0,0 +1,6 @@
@@ -0,0 +1,6 @@
|
||||
<!doctype html> |
||||
<title>NFGame</title> |
||||
<div class=page> |
||||
{% block body %}{% endblock %} |
||||
</div> |
||||
|
@ -0,0 +1,12 @@
@@ -0,0 +1,12 @@
|
||||
{% extends "layout.html" %} |
||||
{% block body %} |
||||
<form action="{{ url_for('new_user') }}" method=post> |
||||
<dl> |
||||
<dt>Username: |
||||
<dd><input type=text name=username> |
||||
<dd><input type=submit value=Submit> |
||||
</dl> |
||||
</form> |
||||
{% endblock %} |
||||
|
||||
|
@ -0,0 +1,7 @@
@@ -0,0 +1,7 @@
|
||||
{% extends "layout.html" %} |
||||
{% block body %} |
||||
<h1>New user</h1> |
||||
New user has been made. |
||||
<a href='{{ url_for('index') }}'>Back to overview</a> |
||||
{% endblock %} |
||||
|
@ -0,0 +1,25 @@
@@ -0,0 +1,25 @@
|
||||
{% extends "layout.html" %} |
||||
{% block body %} |
||||
<h1>Overview</h1> |
||||
<a href='{{ url_for('new_user') }}'>New user</a><br> |
||||
<a href='{{ url_for('delete_score') }}'>Delete score</a></br> |
||||
<a href='{{ url_for('delete_user') }}'>Delete user</a></br> |
||||
<table border=1> |
||||
<tr> |
||||
<td>Name</td> |
||||
{% for tag in tags %} |
||||
<td>{{ tags.get(tag) }}</td> |
||||
{% endfor %} |
||||
</tr> |
||||
{% for entry in entries %} |
||||
<tr> |
||||
<td>{{ entry.username }}</td> |
||||
{% for tag in tags %} |
||||
<td>{{ user[entry.id][tag] }}</td> |
||||
{% endfor %} |
||||
{% endfor %} |
||||
</tr> |
||||
</table> |
||||
|
||||
{% endblock %} |
||||
|
@ -0,0 +1,7 @@
@@ -0,0 +1,7 @@
|
||||
{% extends "layout.html" %} |
||||
{% block body %} |
||||
<h1>Hmmm!</h1> |
||||
You already found the tag {{ tagname }}! |
||||
<a href='{{ url_for('index') }}'>Back to overview</a> |
||||
{% endblock %} |
||||
|
@ -0,0 +1,7 @@
@@ -0,0 +1,7 @@
|
||||
{% extends "layout.html" %} |
||||
{% block body %} |
||||
<h1>Found tag!</h1> |
||||
You found tag {{ tagname }}! |
||||
<a href='{{ url_for('index') }}'>Back to overview</a> |
||||
{% endblock %} |
||||
|
Loading…
Reference in new issue