Skip to content

Commit

Permalink
Add condition to catch no playable locations
Browse files Browse the repository at this point in the history
  • Loading branch information
ggeerraarrdd committed Oct 25, 2023
1 parent 62d0b53 commit e219854
Show file tree
Hide file tree
Showing 8 changed files with 84 additions and 54 deletions.
2 changes: 0 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ flask_session/

locations.txt

geo.db

####################################################

# Byte-compiled / optimized / DLL files
Expand Down
6 changes: 1 addition & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Apart from what was to be gained from implementing the requirements, this projec

* cs50==9.2.5
* Flask==2.2.5
* Flask-Session==0.4.1
* geographiclib==2.0
* haversine==2.8.0
* Werkzeug==3.0.0
Expand Down Expand Up @@ -83,11 +84,6 @@ password: carto
## Author(s)
* [@ggeerraarrdd](https://github.com/ggeerraarrdd/)

## Version History
* 1.0.0
* October 13, 2023
* Initial Release

## Future Work
* Add functionalities to Search History page such as reviewing submitted locations and more easily re-try locations attempted but not yet found.
* Add administration interface for data management.
Expand Down
91 changes: 49 additions & 42 deletions app.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import os

from cs50 import SQL
from flask import Flask, flash, redirect, render_template, request, session
from flask_session import Session
from werkzeug.security import check_password_hash, generate_password_hash
Expand Down Expand Up @@ -71,7 +70,7 @@ def index():
session["page"] = "index"

# Print to debug
print(f"\nprint from app.py > index():\n{session}\n")
# print(f"\nprint from app.py > index():\n{session}\n")

return render_template("index.html", page="index", userid=session["user_id"], username=session["username"], total=session["user_total_score"], map_api_key=map_api_key)

Expand All @@ -86,17 +85,17 @@ def index():
def game():
"""Start a game"""

# Printo to debug
print("GAME() function call")
# Print to debug
# print("GAME() function call")

if "try_again" not in session:
session["try_again"] = 0

# Printo to debug
print(f"Try Again: {session['try_again']}")
# print(f"Try Again: {session['try_again']}")

# Print to debug
print(f"\nPre-gane session check - BEFORE clean-up: \n{session}\n")
# print(f"\nPre-gane session check - BEFORE clean-up: \n{session}\n")

# Clean-up session
# Leave only user_id and username
Expand All @@ -113,48 +112,55 @@ def game():
session.pop("current_game_map_zoom", None)

# Print to debug
print(f"\nPre-gane session check - AFTER clean-up: \n{session}\n")
# print(f"\nPre-gane session check - AFTER clean-up: \n{session}\n")

# Get playable location
if session["try_again"] == 1:
location = queries.get_playable_location_again(db, session["current_game_loc_id"])
else:
location = queries.get_playable_location(db, session["user_id"])

# Update session with location info to be played
session["current_game_loc_id"] = location["id"]
session["current_game_lat_answer_key"] = location["loc_lat_key"]
session["current_game_long_answer_key"] = location["loc_lng_key"]

# Create new entry in games table
current_game_id, current_game_start = queries.start_game(db, session["user_id"], session["current_game_loc_id"])

# Get hour, minute and seconds from current_game_start
clock = {
"hour": current_game_start.hour,
"minute": current_game_start.minute,
"second": current_game_start.second
}

# Update session with game info to be played
session["current_game_loc_id"] = location["id"]
session["current_game_lat_default"] = location["loc_lat_game"]
session["current_game_long_default"] = location["loc_lng_game"]
session["current_game_lat_answer_key"] = location["loc_lat_key"]
session["current_game_long_answer_key"] = location["loc_lng_key"]
session["current_game_id"] = current_game_id
session["current_game_start"] = current_game_start

# Print to debug
print(f"DATA SENT TO GAME PAGE: \n{location}\n")

# Get offset latitude to position infowindow on map
loc_lat_game_offset = latitude_offset(float(location["loc_lat_game"]), float(location["loc_lng_game"]))
# Checks if query returns a playable location
if location == None:
# Print to debug
# print("\nPlayer has found all locations.\n")
return redirect("/history")
else:
# Update session with location info to be played
session["current_game_loc_id"] = location["id"]
session["current_game_lat_answer_key"] = location["loc_lat_key"]
session["current_game_long_answer_key"] = location["loc_lng_key"]

# Ensure session page is set to "game" before game.html is rendered
session["page"] = "game"
# Create new entry in games table
current_game_id, current_game_start = queries.start_game(db, session["user_id"], session["current_game_loc_id"])

# Get hour, minute and seconds from current_game_start
clock = {
"hour": current_game_start.hour,
"minute": current_game_start.minute,
"second": current_game_start.second
}

# Update session with game info to be played
session["current_game_loc_id"] = location["id"]
session["current_game_lat_default"] = location["loc_lat_game"]
session["current_game_long_default"] = location["loc_lng_game"]
session["current_game_lat_answer_key"] = location["loc_lat_key"]
session["current_game_long_answer_key"] = location["loc_lng_key"]
session["current_game_id"] = current_game_id
session["current_game_start"] = current_game_start

# Print to debug
# print(f"DATA SENT TO GAME PAGE: \n{location}\n")

# Get offset latitude to position infowindow on map
loc_lat_game_offset = latitude_offset(float(location["loc_lat_game"]), float(location["loc_lng_game"]))

# Ensure session page is set to "game" before game.html is rendered
session["page"] = "game"

# Print to debug
print(f"SESSION JUST BEFORE RENDER TEMPLATE: \n{session}\n")
# Print to debug
# print(f"SESSION JUST BEFORE RENDER TEMPLATE: \n{session}\n")

return render_template("game.html", page="game", username=session["username"], total=session["user_total_score"], location=location, loc_lat_game_offset=loc_lat_game_offset, clock=clock, map_api_key=map_api_key)

Expand Down Expand Up @@ -345,15 +351,16 @@ def history():
# Print to debug
# print("HISTORY function call")

history = queries.get_history(db, session["user_id"])
locs_playable_count, history = queries.get_history(db, session["user_id"])

# Printo to debug
# print(history[0])
# print(locs_playable_count)

# Ensure session page is set to "history" before history.html is rendered
session["page"] = "history"

return render_template("history.html", page="history", history=history, userid=session["user_id"], username=session["username"], total=session["user_total_score"], map_api_key=map_api_key)
return render_template("history.html", page="history", locs_playable_count=locs_playable_count, history=history, userid=session["user_id"], username=session["username"], total=session["user_total_score"], map_api_key=map_api_key)


####################################################################
Expand Down
Binary file modified geo.db
Binary file not shown.
22 changes: 17 additions & 5 deletions queries.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,14 @@ def get_playable_location(db, user_id):
# Get count of playable locations
locs_playable_count = len(locs_playable)

# Generate random number in range to loc_playable
row = randint(0, locs_playable_count - 1)
if locs_playable_count == 0:
location = None
else:
# Generate random number in range to loc_playable
row = randint(0, locs_playable_count - 1)

# Get row
location = dict(locs_playable[row])
# Get row
location = dict(locs_playable[row])

cursor.close()
connection.close()
Expand Down Expand Up @@ -321,6 +324,15 @@ def get_history(db, user_id):
connection.row_factory = sqlite3.Row
cursor = connection.cursor()

# Get playable locations count
query = "SELECT * "
query = query + "FROM locs "
query = query + "WHERE id NOT IN (SELECT DISTINCT loc_id FROM games WHERE user_id = ? AND game_answer_validation = 1) "
query = query + "AND loc_active = 1; "
cursor.execute(query, (user_id,))
locs_playable = cursor.fetchall()
locs_playable_count = len(locs_playable)

# Get history
query = "WITH cte AS "
query = query + "( "
Expand Down Expand Up @@ -348,7 +360,7 @@ def get_history(db, user_id):
cursor.close()
connection.close()

return history
return locs_playable_count, history



Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
cs50==9.2.5
Flask==2.2.5
Flask-Session==0.4.1
geographiclib==2.0
haversine==2.8.0
Werkzeug==3.0.0
10 changes: 10 additions & 0 deletions static/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -506,6 +506,16 @@ li {
align-items: center;
}

.container-bottom-history-title-right-found {
margin: 20px 0 0 0;
padding: 0;
font-size: 22px;
color: #ff5645;
display: flex;
justify-content: right;
align-items: center;
}

.container-bottom-history-message {
margin: 0;
padding: 0 0 20px 0;
Expand Down
6 changes: 6 additions & 0 deletions templates/history.html
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,16 @@
</div>

<div class="container-bottom-history-title-right">
{% if locs_playable_count == 0 %}
<div class="container-bottom-history-title-right-found">
Congratulations! You found all locations! More on the way...
</div>
{% else %}
<form name="submit" action="/game" method="post">
<input type="hidden" name="answer-lat" class="hidden-field" value="new-game"></input>
<button class="btn btn-primary" type="submit">Start Game</button>
</form>
{% endif %}
</div>
</div>

Expand Down

0 comments on commit e219854

Please sign in to comment.