diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3e82497 --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +# Ignoring compiled python code +*.py[cod] + +# Ignoring the config file's +*.cfg + +# Ignoring the database +*.db diff --git a/README.md b/README.md new file mode 100644 index 0000000..de2ba70 --- /dev/null +++ b/README.md @@ -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://:5000 + +## Debug mode +You can enable the debug mode by running: + +./run.sh debug diff --git a/nfgame.cfg-example b/nfgame.cfg-example new file mode 100644 index 0000000..59a97e8 --- /dev/null +++ b/nfgame.cfg-example @@ -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!' diff --git a/nfgame.py b/nfgame.py new file mode 100644 index 0000000..130d7cc --- /dev/null +++ b/nfgame.py @@ -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/') +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() diff --git a/run.sh b/run.sh new file mode 100755 index 0000000..a235142 --- /dev/null +++ b/run.sh @@ -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 diff --git a/schema.sql b/schema.sql new file mode 100644 index 0000000..8c4c353 --- /dev/null +++ b/schema.sql @@ -0,0 +1,6 @@ +drop table if exists score; +create table score ( + id integer primary key autoincrement, + username text not null, + tags text null +); diff --git a/templates/layout.html b/templates/layout.html new file mode 100644 index 0000000..dfa5fed --- /dev/null +++ b/templates/layout.html @@ -0,0 +1,6 @@ + +NFGame +
+ {% block body %}{% endblock %} +
+ diff --git a/templates/newuser.html b/templates/newuser.html new file mode 100644 index 0000000..75d968c --- /dev/null +++ b/templates/newuser.html @@ -0,0 +1,12 @@ +{% extends "layout.html" %} +{% block body %} +
+
+
Username: +
+
+
+
+{% endblock %} + + diff --git a/templates/newuser_done.html b/templates/newuser_done.html new file mode 100644 index 0000000..dc1a067 --- /dev/null +++ b/templates/newuser_done.html @@ -0,0 +1,7 @@ +{% extends "layout.html" %} +{% block body %} +

New user

+ New user has been made. + Back to overview +{% endblock %} + diff --git a/templates/overview.html b/templates/overview.html new file mode 100644 index 0000000..31d7460 --- /dev/null +++ b/templates/overview.html @@ -0,0 +1,25 @@ +{% extends "layout.html" %} +{% block body %} +

Overview

+ New user
+ Delete score
+ Delete user
+ + + + {% for tag in tags %} + + {% endfor %} + + {% for entry in entries %} + + + {% for tag in tags %} + + {% endfor %} + {% endfor %} + +
Name{{ tags.get(tag) }}
{{ entry.username }}{{ user[entry.id][tag] }}
+ +{% endblock %} + diff --git a/templates/tagalreadyfound.html b/templates/tagalreadyfound.html new file mode 100644 index 0000000..27a7f8c --- /dev/null +++ b/templates/tagalreadyfound.html @@ -0,0 +1,7 @@ +{% extends "layout.html" %} +{% block body %} +

Hmmm!

+ You already found the tag {{ tagname }}! + Back to overview +{% endblock %} + diff --git a/templates/tagfound.html b/templates/tagfound.html new file mode 100644 index 0000000..ecf5818 --- /dev/null +++ b/templates/tagfound.html @@ -0,0 +1,7 @@ +{% extends "layout.html" %} +{% block body %} +

Found tag!

+ You found tag {{ tagname }}! + Back to overview +{% endblock %} + diff --git a/templates/tagnotfound.html b/templates/tagnotfound.html new file mode 100644 index 0000000..0594f60 --- /dev/null +++ b/templates/tagnotfound.html @@ -0,0 +1,7 @@ +{% extends "layout.html" %} +{% block body %} +

Error!

+ This is not a valid tag! + Back to overview +{% endblock %} +