Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
7047CEM-Software-Development-Project/app.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
78 lines (63 sloc)
2.79 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from flask import Flask, render_template, url_for, request, redirect | |
from flask_sqlalchemy import SQLAlchemy | |
app = Flask(__name__) | |
app.app_context().push() | |
#Configuring the databases and the database binding | |
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///test.db' | |
app.config['SQLALCHEMY_BINDS'] = { | |
'items': 'sqlite:///items.db' #bind key | |
} | |
db = SQLAlchemy(app) | |
class Character(db.Model): | |
""" | |
Creating an object of class Character used for the player's stats and actions. | |
The object attributes are initialised as columns of a database that can be later on used to store information | |
about the user inside the database. | |
""" | |
id = db.Column(db.Integer, primary_key=True) | |
username = db.Column(db.String(20), nullable=False) | |
health = db.Column(db.Integer, default=100) | |
attack = db.Column(db.Integer, default=15) | |
defence = db.Column(db.Integer, default=10) | |
def __repr__(self): | |
""" | |
Method used for visualising | |
""" | |
return '<User %r, attack: %r, health: %r, defence: %r>' % self.username, self.health, self.attack, self.defence | |
class Items(db.Model): | |
""" | |
Items class written to product items as objects. | |
The objects are stored inside a database. | |
The class is binded to "items.db. | |
""" | |
__bind_key__ = 'items' | |
item_id = db.Column(db.Integer, primary_key=True) | |
item_name = db.Column(db.String(10), nullable=False) | |
item_description = db.Column(db.String(20)) | |
item_health = db.Column(db.Integer) | |
item_attack = db.Column(db.Integer) | |
item_defence = db.Column(db.Integer) | |
def __repr__(self): | |
return '<Item %r, AT: %r, HP: %r, DEF: %r>' % self.item_description, self.item_description, self.item_attack, self.item_defence | |
@app.route('/', methods = ['POST', 'GET']) | |
def index(): | |
if request.method == 'POST': | |
user_content = request.form['username'] #store the information from the form into a variable user_content | |
new_player = Character(username=user_content) #creating a new object of the Character class | |
#setting the username of the object to the given input | |
#further implementation: Set the character username to the user's username | |
try: | |
#append the object created to the database and redirect the page | |
db.session.add(new_player) | |
db.session.commit() | |
return redirect('/') | |
except: | |
#return an error message | |
return 'There was an issue creating the new player' | |
else: | |
#query to the database | |
#further implementation to return the stats of the player logged. | |
stats = Character.query.all() | |
return render_template('index.html', stats=stats) | |
if __name__ == "__main__": | |
app.run(debug=True) |