Skip to content

Commit

Permalink
re-added online players feature
Browse files Browse the repository at this point in the history
  • Loading branch information
GDcheeriosYT committed Oct 31, 2024
1 parent f307f52 commit b92baf3
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 53 deletions.
4 changes: 4 additions & 0 deletions crap/Account.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ def create(username: str, password: str, email: str, osu_id: int, about: str):

DB.do(query, params)

@staticmethod
def set_status(id: int, status: str):
DB.do("UPDATE accounts SET status = %s where id = %s", params=(status, id))

@staticmethod
def change_username(id: int, new_username: str):
DB.do(f"update accounts set username = %s where id = %s;", params=(new_username, id))
Expand Down
25 changes: 23 additions & 2 deletions crap/gentrys_quest_crap/GQManager.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import json

from GPSystem.GPmain import GPSystem
from crap.Account import Account
from crap.PSQLConnection import PSQLConnection as DB
from crap.gentrys_quest_crap.Item import Item
from crap.gentrys_quest_crap.UserRanking import UserRanking
Expand Down Expand Up @@ -173,6 +174,16 @@ def submit_classic_data(id: int, start_amount: int, money: int):

return ":thumbs_up:"

@staticmethod
def classic_check_in(id: int):
Account.set_status(id, "gqc_online")
return ""

@staticmethod
def check_out(id: int):
Account.set_status(id, "offline")
return ""

# <editor-fold desc="getters">

@staticmethod
Expand Down Expand Up @@ -223,14 +234,24 @@ def get_weighted_rating(id, object_type: str, classic: bool) -> int:
return rating

@staticmethod
def get_leaderboard(classic: bool, start: int = 0, amount: int = 50) -> list:
def get_leaderboard(classic: bool, start: int = 0, amount: int = 50, online: bool = False) -> list:
"""
grab leaderboard data
@param classic: targeting classic data
@param start: start index
@param amount: how many players to pull
@param online: targeting online players
@return: leaderboard data
"""
prefix = 'c_' if classic else '' # c_ = classic prefix
online_prefix = 'gqc_' if classic else 'gq_'
query = f"""
SELECT rankings.id, accounts.username,
rankings.{prefix + 'weighted'}, rankings.{prefix + 'rank'}, rankings.{prefix + 'tier'}
FROM rankings
INNER JOIN accounts ON rankings.id = accounts.id
WHERE accounts.status NOT IN ('restricted', 'test')
WHERE accounts.status NOT IN ('restricted', 'test') {f"AND accounts.status = '{online_prefix}online'" if online else ""}
ORDER BY {prefix + 'weighted'} desc
LIMIT %s OFFSET %s;
"""
Expand Down
39 changes: 24 additions & 15 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@

# Gentrys Quest data
GQManager.load_rankings()
gentrys_quest_classic_version = "V2.0.0"
gentrys_quest_classic_version = "V2.1.0"

# flask set up
app = Flask( # Create a flask app
Expand Down Expand Up @@ -356,9 +356,10 @@ async def gq_submit_leaderboard(leaderboard, user, score):

# <editor-fold desc="leaderboards">

@app.route("/api/gqc/get-leaderboard/<start>+<display_number>", methods=["GET"])
async def get_gq_leaderboard(start, display_number):
return GQManager.get_leaderboard(True, int(start), int(display_number))
@app.route("/api/gqc/get-leaderboard/<start>+<display_number>+<online>", methods=["GET"])
async def get_gq_leaderboard(start, display_number, online):
online = online == "true"
return GQManager.get_leaderboard(True, int(start), int(display_number), online)


# </editor-fold>
Expand All @@ -368,6 +369,11 @@ async def classic_get_version():
return gentrys_quest_classic_version


@app.route("/api/gqc/check-in/<id>", methods=['POST'])
async def classic_check_in(id):
return GQManager.classic_check_in(id)


@app.route("/api/gqc/update-data/<id>", methods=['POST'])
async def update_classic_data(id):
data = request.json
Expand Down Expand Up @@ -441,6 +447,10 @@ async def get_item(id):
return "<h1>Not found in database</h1>"


@app.route("/api/gq/check-out/<id>", methods=['POST'])
async def check_out(id):
return GQManager.check_out(id)

# </editor-fold>

# </editor-fold>
Expand Down Expand Up @@ -488,23 +498,22 @@ async def gentrys_quest_leaderboard():
"gentrys quest/leaderboard.html",
players=players,
get_color=GQManager.get_color,
version=GPSystem.version
version=GPSystem.version,
classic_header="Leaderboard"
)


@app.route("/gentrys-quest/online-players")
async def gentrys_quest_online_players():
players = GQManager.online_players

def sort_thing(player):
return player.power_level.weighted

players.sort(key=sort_thing, reverse=True)

return render_template(
"gentrys quest/online-players.html",
players=players,
version=GQManager.rater_version
"gentrys quest/leaderboard.html",
players=GQManager.get_leaderboard(
classic=True,
online=True
),
get_color=GQManager.get_color,
version=GPSystem.version,
classic_header="Online Players"
)


Expand Down
2 changes: 1 addition & 1 deletion templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<ul class="dropdown-content">
<li><a href="/gentrys-quest">Home</a></li>
<li><a href="/gentrys-quest/leaderboard">Leaderboard</a></li>
<!-- <li><a href="/gentrys-quest/online-players">Online Players</a></li>-->
<li><a href="/gentrys-quest/online-players">Online Players</a></li>
<li><a href="/gentrys-quest/ranking">Ranking Info</a></li>
</ul>
</li>
Expand Down
2 changes: 1 addition & 1 deletion templates/gentrys quest/leaderboard.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
</head>
<body style="background-color: black;">
{% block body %}
<h1>Gentry's Quest Leaderboard</h1>
<h1 style="font-size: 300%;">{{classic_header}}</h1>
<h2 style="text-align: center;"><span style="color: white;">GPSystem</span> <a href="https://github.com/GDcheeriosYT/GPSystem" target="_blank">Version {{version}}</a></h2>
{% set count = namespace(value=1) %}
{% for player in players %}
Expand Down
34 changes: 0 additions & 34 deletions templates/gentrys quest/online-players.html

This file was deleted.

0 comments on commit b92baf3

Please sign in to comment.