Skip to content

Commit

Permalink
Merge branch 'develop' into 'main'
Browse files Browse the repository at this point in the history
Refactor method to get user games, to remove 2 separated methods for played and completed

See merge request tmllull/la-viciacion!40
  • Loading branch information
tmllull committed Nov 18, 2024
2 parents 89bc259 + 49fe24b commit 9bb0517
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 81 deletions.
182 changes: 107 additions & 75 deletions api/app/crud/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -549,17 +549,33 @@ def get_games(
models.UserGame.platform.label("platform_id"),
models.Game.name.label("game_name"),
models.PlatformTag.name.label("platform_name"),
models.TimeEntry.start.label("last_played_time"),
)
.join(models.Game, models.UserGame.game_id == models.Game.id)
.join(models.PlatformTag, models.UserGame.platform == models.PlatformTag.id)
.join(
models.TimeEntry,
models.TimeEntry.project_clockify_id == models.UserGame.game_id,
)
.where(
models.UserGame.user_id == user_id,
models.UserGame.completed == completed,
extract("year", models.UserGame.started_date) == season,
)
.order_by(desc(models.TimeEntry.start))
.limit(limit)
)
return db.execute(stmt).fetchall()
# return db.execute(stmt).fetchall()
result = db.execute(stmt).fetchall()
unique_names = set()
unique_data = []
for item in result:
if item["game_name"] not in unique_names:
unique_names.add(item["game_name"])
unique_data.append(item)
if len(unique_data) == limit:
break
return unique_data

else:
stmt = (
Expand All @@ -568,16 +584,32 @@ def get_games(
models.UserGame.platform.label("platform_id"),
models.Game.name.label("game_name"),
models.PlatformTag.name.label("platform_name"),
models.TimeEntry.start.label("last_played_time"),
)
.join(models.Game, models.UserGame.game_id == models.Game.id)
.join(models.PlatformTag, models.UserGame.platform == models.PlatformTag.id)
.join(
models.TimeEntry,
models.TimeEntry.project_clockify_id == models.UserGame.game_id,
)
.where(
models.UserGame.user_id == user_id,
extract("year", models.UserGame.started_date) == season,
)
.order_by(desc(models.TimeEntry.start))
.limit(limit)
)
return db.execute(stmt).fetchall()
# return db.execute(stmt).fetchall()
result = db.execute(stmt).fetchall()
unique_names = set()
unique_data = []
for item in result:
if item["game_name"] not in unique_names:
unique_names.add(item["game_name"])
unique_data.append(item)
if len(unique_data) == limit:
break
return unique_data


def get_game_by_id(
Expand Down Expand Up @@ -911,8 +943,8 @@ def top_games(
select(
models.UserGame.user_id,
models.UserGame.game_id,
models.Game.name,
models.UserGame.played_time.label("total_played_time"),
models.Game.name.label("game_name"),
models.UserGame.played_time.label("played_time"),
)
.join(models.User, models.User.id == models.UserGame.user_id)
.join(models.Game, models.Game.id == models.UserGame.game_id)
Expand All @@ -935,77 +967,77 @@ def top_games(
raise e


def played_games(db: Session, username: str, limit: int = None):
try:
user = get_user_by_username(db, username)
stmt = (
select(
models.UserGame.user_id,
models.UserGame.game_id,
models.Game.name,
models.TimeEntry.start.label("last_played_time"),
models.UserGame.played_time,
)
.join(models.User, models.User.id == models.UserGame.user_id)
.join(models.Game, models.Game.id == models.UserGame.game_id)
.join(
models.TimeEntry,
models.TimeEntry.project_clockify_id == models.UserGame.game_id,
)
.where(
models.UserGame.user_id == user.id, models.TimeEntry.user_id == user.id
)
.group_by(
models.UserGame.user_id,
models.UserGame.game_id,
models.Game.name,
models.UserGame.played_time,
models.TimeEntry.start,
)
.order_by(desc(models.TimeEntry.start))
)
result = db.execute(stmt).fetchall()
unique_names = set()
unique_data = []
for item in result:
if item["name"] not in unique_names:
unique_names.add(item["name"])
unique_data.append(item)
if len(unique_data) == limit:
break
return unique_data
except Exception as e:
logger.error(e)
raise e


def completed_games(db: Session, username: str, limit: int = None):
try:
user = get_user_by_username(db, username)
stmt = (
select(
models.UserGame.user_id,
models.UserGame.game_id,
models.Game.name,
models.UserGame.completed_date.label("completed_date"),
)
.join(models.User, models.User.id == models.UserGame.user_id)
.join(models.Game, models.Game.id == models.UserGame.game_id)
.where(models.UserGame.user_id == user.id, models.UserGame.completed == 1)
.group_by(
models.UserGame.user_id,
models.UserGame.game_id,
models.Game.name,
# models.UserGame.played_time,
)
.order_by(desc(models.UserGame.completed_date))
.distinct()
.limit(limit)
)
return db.execute(stmt).fetchall()
except Exception as e:
logger.error(e)
raise e
# def played_games(db: Session, username: str, limit: int = None):
# try:
# user = get_user_by_username(db, username)
# stmt = (
# select(
# models.UserGame.user_id,
# models.UserGame.game_id,
# models.Game.name,
# models.TimeEntry.start.label("last_played_time"),
# models.UserGame.played_time,
# )
# .join(models.User, models.User.id == models.UserGame.user_id)
# .join(models.Game, models.Game.id == models.UserGame.game_id)
# .join(
# models.TimeEntry,
# models.TimeEntry.project_clockify_id == models.UserGame.game_id,
# )
# .where(
# models.UserGame.user_id == user.id, models.TimeEntry.user_id == user.id
# )
# .group_by(
# models.UserGame.user_id,
# models.UserGame.game_id,
# models.Game.name,
# models.UserGame.played_time,
# models.TimeEntry.start,
# )
# .order_by(desc(models.TimeEntry.start))
# )
# result = db.execute(stmt).fetchall()
# unique_names = set()
# unique_data = []
# for item in result:
# if item["name"] not in unique_names:
# unique_names.add(item["name"])
# unique_data.append(item)
# if len(unique_data) == limit:
# break
# return unique_data
# except Exception as e:
# logger.error(e)
# raise e


# def completed_games(db: Session, username: str, limit: int = None):
# try:
# user = get_user_by_username(db, username)
# stmt = (
# select(
# models.UserGame.user_id,
# models.UserGame.game_id,
# models.Game.name,
# models.UserGame.completed_date.label("completed_date"),
# )
# .join(models.User, models.User.id == models.UserGame.user_id)
# .join(models.Game, models.Game.id == models.UserGame.game_id)
# .where(models.UserGame.user_id == user.id, models.UserGame.completed == 1)
# .group_by(
# models.UserGame.user_id,
# models.UserGame.game_id,
# models.Game.name,
# # models.UserGame.played_time,
# )
# .order_by(desc(models.UserGame.completed_date))
# .distinct()
# .limit(limit)
# )
# return db.execute(stmt).fetchall()
# except Exception as e:
# logger.error(e)
# raise e


def get_achievements(db: Session, username: str, season: int = config.CURRENT_SEASON):
Expand Down
7 changes: 5 additions & 2 deletions api/app/routers/statistics.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,12 @@ def get_user_statistics(
for ranking_type in rankings_list:
content = {}
if ranking_type == UserStatisticsTypes.played_games:
data = users.played_games(db, username)
user = users.get_user_by_username(db, username)
data = users.get_games(db, user.id)
# data = users.played_games(db=db, username=username)
elif ranking_type == UserStatisticsTypes.completed_games:
data = users.completed_games(db, username)
user = users.get_user_by_username(db, username)
data = users.get_games(db=db, user_id=user.id, completed=True)
elif ranking_type == UserStatisticsTypes.top_games:
data = users.top_games(db, username)
elif ranking_type == UserStatisticsTypes.achievements:
Expand Down
8 changes: 4 additions & 4 deletions bot/app/routes/my_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ async def my_games(self, update: Update, context: ContextTypes.DEFAULT_TYPE) ->
msg
+ str(i + 1)
+ ". "
+ str(elem["name"])
+ str(elem["game_name"])
+ " ("
+ str(utils.convert_time_to_hours(elem["played_time"]))
+ ")"
Expand All @@ -72,12 +72,12 @@ async def my_top_games(
ranking = utils.load_json_response(ranking[0])
msg = "Este es tu top de juegos:\n"
for i, elem in enumerate(ranking["data"]):
played_time = utils.convert_time_to_hours(elem["total_played_time"])
played_time = utils.convert_time_to_hours(elem["played_time"])
msg = (
msg
+ str(i + 1)
+ ". "
+ str(elem["name"])
+ str(elem["game_name"])
+ " - "
+ str(played_time)
+ "\n"
Expand All @@ -102,7 +102,7 @@ async def my_completed_games(
ranking = utils.load_json_response(ranking[0])
msg = "Estos son tus últimos juegos completados:\n"
for i, elem in enumerate(ranking["data"]):
msg = msg + str(i + 1) + ". " + str(elem["name"]) + "\n"
msg = msg + str(i + 1) + ". " + str(elem["game_name"]) + "\n"

await utils.response_conversation(update, context, msg)

Expand Down

0 comments on commit 9bb0517

Please sign in to comment.