Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom quest #6

Merged
merged 8 commits into from
Nov 19, 2023
Merged
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
37 changes: 36 additions & 1 deletion nobles_and_peasants/challenges.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Functions related to the challenges table."""
from flask import session

from nobles_and_peasants.query import fetch_one
from nobles_and_peasants.query import execute, fetch_all, fetch_one


def get_random_challenge():
Expand All @@ -15,3 +15,38 @@ def get_random_challenge():
limit 1
"""
return fetch_one(query=query, args=[party_id])


def get_all_challenges():
"""Get all challenges in a given party."""
party_id = session.get("party_id")
query = """
select id, challenge
from challenges
where party_id = ?
order by id
"""
return fetch_all(query=query, args=[party_id])


def is_challenge_in_party(challenge_id):
"""Check if a challenge_id is associated with a given party."""
challenges = [c["id"] for c in get_all_challenges()]
return challenge_id in challenges


def add_challenge_to_party(challenge, commit=True):
"""Add a row to the database for a new challenge."""
party_id = session.get("party_id")
query = """
insert into challenges (party_id, challenge) values (?, ?)
"""
execute(query=query, args=[party_id, challenge], commit=commit)


def delete_challenge_from_table(challenge_id, commit=True):
"""Delete a row from the database for a challenge."""
query = """
delete from challenges where id = ?
"""
execute(query=query, args=[challenge_id], commit=commit)
71 changes: 69 additions & 2 deletions nobles_and_peasants/game.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@
from flask import Blueprint, flash, redirect, render_template, request, session, url_for

from nobles_and_peasants.auth import login_required
from nobles_and_peasants.challenges import get_random_challenge
from nobles_and_peasants.challenges import (
add_challenge_to_party,
delete_challenge_from_table,
get_all_challenges,
get_random_challenge,
is_challenge_in_party,
)
from nobles_and_peasants.constants import NOBLE, PEASANT
from nobles_and_peasants.drinks import (
add_or_update_drink_name_and_cost,
Expand Down Expand Up @@ -34,7 +40,13 @@
get_starting_coin_for_status,
update_noble_starting_coin,
)
from nobles_and_peasants.quests import get_random_quest
from nobles_and_peasants.quests import (
add_quest_to_party,
delete_quest_from_table,
get_all_quests,
get_random_quest,
is_quest_in_party,
)
from nobles_and_peasants.quest_rewards import (
get_quest_difficulty_and_reward,
get_reward_for_difficulty,
Expand All @@ -51,12 +63,16 @@ def set_up():
drinks = get_drink_name_and_cost()
starting_coin = get_status_and_starting_coin()
quest_rewards = get_quest_difficulty_and_reward()
quests = get_all_quests()
challenges = get_all_challenges()

return render_template(
"setup.html",
drinks=drinks,
starting_coin=starting_coin,
quest_rewards=quest_rewards,
quests=quests,
challenges=challenges,
party_name=session.get("party_name"),
)

Expand Down Expand Up @@ -120,6 +136,57 @@ def set_wages():
return redirect(url_for("game.set_up"))


@bp.route("/add_quest", methods=["POST"])
@login_required
def add_quest():
"""Respond to request to add a quest to the party."""
quest = request.form["quest"]
difficulty = request.form["difficulty"]

add_quest_to_party(quest=quest, difficulty=difficulty)
return redirect(url_for("game.set_up"))


@bp.route("/delete_quest", methods=["POST"])
@login_required
def delete_quest():
"""Respond to request to delete a quest from the party."""
quest_id = int(request.form["quest_id"])

if not is_quest_in_party(quest_id=quest_id):
msg = f"Unsuccessful! Quest ID: {quest_id} is not registered in your party."
flash(msg)
return redirect(url_for("game.set_up"))

delete_quest_from_table(quest_id=quest_id)
return redirect(url_for("game.set_up"))


@bp.route("/add_challenge", methods=["POST"])
@login_required
def add_challenge():
"""Respond to request to add a challenge to the party."""
challenge = request.form["challenge"]

add_challenge_to_party(challenge=challenge)
return redirect(url_for("game.set_up"))


@bp.route("/delete_challenge", methods=["POST"])
@login_required
def delete_challenge():
"""Respond to request to delete a challenge from the party."""
challenge_id = int(request.form["challenge_id"])

if not is_challenge_in_party(challenge_id=challenge_id):
msg = f"Unsuccessful! Challenge ID: {challenge_id} is not registered in your party."
flash(msg)
return redirect(url_for("game.set_up"))

delete_challenge_from_table(challenge_id=challenge_id)
return redirect(url_for("game.set_up"))


# ############################################################
# ################### Show main page #########################
# ############################################################
Expand Down
37 changes: 36 additions & 1 deletion nobles_and_peasants/quests.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Functions related to the quests table."""
from flask import session

from nobles_and_peasants.query import fetch_one
from nobles_and_peasants.query import execute, fetch_all, fetch_one


def get_random_quest(difficulty):
Expand All @@ -16,3 +16,38 @@ def get_random_quest(difficulty):
limit 1
"""
return fetch_one(query=query, args=[party_id, difficulty])


def get_all_quests():
"""Get all quests in a given party."""
party_id = session.get("party_id")
query = """
select id, quest, difficulty
from quests
where party_id = ?
order by id
"""
return fetch_all(query=query, args=[party_id])


def is_quest_in_party(quest_id):
"""Check if a quest_id is associated with a given party."""
quests = [q["id"] for q in get_all_quests()]
return quest_id in quests


def add_quest_to_party(quest, difficulty, commit=True):
"""Add a row to the database for a new quest."""
party_id = session.get("party_id")
query = """
insert into quests (party_id, quest, difficulty) values (?, ?, ?)
"""
execute(query=query, args=[party_id, quest, difficulty], commit=commit)


def delete_quest_from_table(quest_id, commit=True):
"""Delete a row from the database for a quest."""
query = """
delete from quests where id = ?
"""
execute(query=query, args=[quest_id], commit=commit)
50 changes: 44 additions & 6 deletions nobles_and_peasants/static/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,16 @@ li a:hover {
box-shadow: 0 0 10px #76323F;
}

#custom_quest_input {
width: 50%;
}

#custom_challenge_input {
width: 70%;
}

.num_box {
width: 40%;
width: 20%;
padding: 10px;
border: solid 2px #565656;
transition: border 0.3s;
Expand All @@ -92,6 +100,10 @@ li a:hover {
box-shadow: 0 0 10px #76323F;
}

#noble_coin_input {
width: 25%;
}

.data_list {
width: 50%;
padding: 10px;
Expand All @@ -105,7 +117,16 @@ li a:hover {
width: 90%;
table-layout: fixed;
border-collapse: collapse;
margin: 30px auto 10px auto;
margin: 10px auto 10px auto;
}

.q_table {
width: 90%;
margin: 10px auto auto auto;
border-collapse: collapse;
display: block;
height: 300px;
overflow: auto;
}

.nap_table th,
Expand All @@ -115,11 +136,24 @@ li a:hover {
min-width: 150px;
}

.q_table th, td {
padding: 5px;
text-align: left;
}

.nap_table thead {
background-color: #76323F;
color: white;
}

.q_table thead {
background-color: #76323F;
color: white;
position: sticky;
top: 0;
z-index: 1;
}

.nap_table thead tr {
display: block;
position: relative;
Expand Down Expand Up @@ -266,9 +300,9 @@ li a:hover {

.setup_block {
float: left;
width: 33%;
width: 30%;
min-width: 320px;
margin: auto;
margin: auto;
}

.setup_form { margin: 10px auto 10px auto;
Expand Down Expand Up @@ -317,13 +351,17 @@ li a:hover {
}

.select_box {
width: 150px;
width: 30%;
height: 40px;
font-size: 1em;
}

#custom_quest_difficulty {
width: 20%;
}

#receive_reward {
width: 70%;
width: 50%;
}

/*-------------------Quest Page------------------------*/
Expand Down
2 changes: 1 addition & 1 deletion nobles_and_peasants/templates/main.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<input class="input_box" type="text" name="player_name" placeholder="Choose a Name" required></input>
<p></p>
<select class="select_box" name="player_status" required>
<option value="randomly decide" selected>Randomly Decide</option>
<option value="randomly decide" selected>Random</option>
<option value="noble">Noble</option>
<option value="peasant">Peasant</option>
</select>
Expand Down
69 changes: 68 additions & 1 deletion nobles_and_peasants/templates/setup.html
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ <h3>Initial Coin For Players</h3>
</tbody>
</table>
<form action="{{ url_for('game.set_coin') }}" method="post" class="setup_form" id="set_coin_form">
<input class="num_box" type="number" name="noble_coin" placeholder="Noble Coin" min=1 required></input>
<input class="num_box" id="noble_coin_input" type="number" name="noble_coin" placeholder="Noble Coin" min=1 required></input>
<p></p>
<input class="submit_button" type="submit" value="Update Initial Coin"></input>
</form>
Expand Down Expand Up @@ -81,5 +81,72 @@ <h3>Rewards For Completing Quests</h3>
<input class="submit_button" type="submit" value="Set Quest Rewards"></input>
</form>
</div>
<div class="setup_block" id="add_quest">
<h3>Add A Custom Quest</h3>
<p>Dont add anything illegal</p>
<table class="q_table" id="quest_table">
<thead>
<tr>
<th>ID</th>
<th>Quest</th>
<th>Difficulty</th>
</tr>
</thead>
<tbody>
{% for q in quests %}
<tr>
<td>{{q.id}}</td>
<td>{{q.quest}}</td>
<td>{{q.difficulty}}</td>
</tr>
{% endfor %}
</tbody>
</table>
<form action="{{ url_for('game.add_quest') }}" method="post" class="setup_form" id="add_quest_form">
<input class="input_box" id="custom_quest_input" type="text" name="quest" placeholder="Quest Description" required></input>
<select class="select_box" id="custom_quest_difficulty" name="difficulty" required>
<option value="easy" selected>Easy</option>
<option value="medium">Medium</option>
<option value="hard">Hard</option>
</select>
<p></p>
<input class="submit_button" type="submit" value="Add Quest"></input>
</form>
<form action="{{ url_for('game.delete_quest') }}" method="post" class="setup_form" id="delete_quest_form">
<input class="num_box" type="number" name="quest_id" placeholder="Quest ID" required></input>
<p></p>
<input class="submit_button" type="submit" value="Delete Quest"></input>
</form>
</div>
<div class="setup_block" id="add_challenge">
<h3>Add A Custom Challenge</h3>
<p>Challenges must involve two people</p>
<table class="q_table" id="challenge_table">
<thead>
<tr>
<th>ID</th>
<th>Challenge</th>
</tr>
</thead>
<tbody>
{% for c in challenges %}
<tr>
<td>{{c.id}}</td>
<td>{{c.challenge}}</td>
</tr>
{% endfor %}
</tbody>
</table>
<form action="{{ url_for('game.add_challenge') }}" method="post" class="setup_form" id="add_challenge_form">
<input class="input_box" id="custom_challenge_input" type="text" name="challenge" placeholder="Challenge Description" required></input>
<p></p>
<input class="submit_button" type="submit" value="Add Challenge"></input>
</form>
<form action="{{ url_for('game.delete_challenge') }}" method="post" class="setup_form" id="delete_challenge_form">
<input class="num_box" type="number" name="challenge_id" placeholder="Challenge ID" required></input>
<p></p>
<input class="submit_button" type="submit" value="Delete Challenge"></input>
</form>
</div>
</div>
{% endblock %}
Loading