Skip to content

Add files via upload #2

Open
wants to merge 1 commit into
base: tirthik
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
132 changes: 54 additions & 78 deletions app.py
Original file line number Diff line number Diff line change
@@ -1,78 +1,54 @@
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)
from flask import Flask, render_template, request
import random
import uuid

app = Flask(__name__)

game_stage = {}

@app.route("/")
def level():
return """
<h1>
This is your level where you will have go through a three enemy caves
</h1>
<a href="/select">Find out to select your luck</a>
"""

@app.route("/display")
def first():
return render_template('display.html')
def new():
id_game = str(uuid.uuid4())
win = random.randint(1, 3)
game_stage[id_game] = win

@app.route('/choice', methods=['POST'])
def choice():
selected = int(request.form["cave"])

id_game = request.args.get("id_game")
win = game_stage[id_game]

opened = set([1, 2, 3])
opened.discard(win)
opened.discard(selected)
opened = random.choice(list(opened))

return render_template("choice.html", id_game=id_game, selected=selected, opened=opened)

@app.route('/last', methods=['POST'])
def last():


selected = int(request.form["cave"])

# request.args contains the URL parameters, like the game_id
id_game = request.args.get("id_game")
win = game_stage[id_game]

won = selected == win
return render_template("last.html", won=won, win=win)

if __name__ =="__main__":
app.run(debug=True)
30 changes: 30 additions & 0 deletions choice.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<!doctype html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>

<title>Do you want try other way?</title>

<form action="/last?id_game={{ id_game }}" method="POST"></form>

<h1>You have selected Cave {{ selected }}</h1>

<h2>You enter the Cave {{ opened }}.</h2>
<span>Dead-END</span>

<h2>Well.... Do you want try other way?</h2>
<br/>
{% for i in [1, 2, 3] %}
<input type="radio" name="cave" value="{{ i }}"
{% if selected==i %} checked {% endif %}
{% if opened==i %} disabled {% endif %} >
Door {{ i }} {% if opened==i %} (opened) {% endif %}
</input>
<br/>
{% endfor %}
<input type="submit" value="Let's go">
</html>
28 changes: 28 additions & 0 deletions display.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>The level</title>
</head>
<body>

<form action="/choice?id_game={{ id_game }}" method="POST">

<h1>The choice of your lucky Path</h1>
Choose one of the 3 Caves:
<br/><br/>
<input type="radio" name="cave" value="1" checked>Cave 1
<br/><br/>
<input type="radio" name="cave" value="2">Cave 2
<br/><br/>
<input type="radio" name="cave" value="3">Cave 3
<br/><br/>
<input type="submit" value="Submit">

</form>

</body>

</html>
8 changes: 8 additions & 0 deletions last.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{% if has_won %}
<h1>You woke up!!</h1>
{% else %}
<h1>You have lost!</h1>
The Path would have been {{ win }}.
{% endif %}

<a href="./select">Play again.....</a>