2024-11-20 15:12:44 -08:00
|
|
|
#! /usr/bin/env python3
|
|
|
|
# vim:fenc=utf-8
|
|
|
|
#
|
|
|
|
# Copyright © 2024 sandvich <sandvich@archtop>
|
|
|
|
#
|
|
|
|
# Distributed under terms of the MIT license.
|
|
|
|
|
|
|
|
|
|
|
|
from datetime import datetime
|
2024-11-29 00:06:34 -08:00
|
|
|
from typing import Optional
|
2024-11-20 15:12:44 -08:00
|
|
|
|
2024-11-27 01:03:41 -08:00
|
|
|
from flask import Blueprint, abort, make_response
|
2024-11-20 15:12:44 -08:00
|
|
|
from spectree import Response
|
2024-11-29 00:06:34 -08:00
|
|
|
from sqlalchemy import Row
|
2024-11-27 01:03:41 -08:00
|
|
|
from sqlalchemy.sql import tuple_
|
2024-11-20 15:12:44 -08:00
|
|
|
from models.player import Player
|
2024-11-29 00:06:34 -08:00
|
|
|
from models.player_event import EventWithPlayerSchema, PlayerEvent, PlayerEventRolesSchema
|
2024-11-27 01:03:41 -08:00
|
|
|
from models.player_team_availability import PlayerTeamAvailability
|
|
|
|
from models.player_team_role import PlayerRoleSchema, PlayerTeamRole
|
|
|
|
from models.team import Team
|
2024-11-20 15:12:44 -08:00
|
|
|
from spec import BaseModel, spec
|
2024-11-27 01:03:41 -08:00
|
|
|
from middleware import assert_team_authority, assert_team_membership, requires_authentication, requires_team_membership
|
2024-11-20 15:12:44 -08:00
|
|
|
from models.event import Event, EventSchema
|
|
|
|
from models.player_team import PlayerTeam
|
|
|
|
from app_db import db
|
|
|
|
|
|
|
|
|
|
|
|
api_events = Blueprint("events", __name__, url_prefix="/events")
|
|
|
|
|
|
|
|
@api_events.get("/<int:event_id>")
|
|
|
|
@spec.validate(
|
|
|
|
resp=Response(
|
|
|
|
HTTP_200=EventSchema,
|
|
|
|
),
|
|
|
|
operation_id="get_event",
|
|
|
|
)
|
|
|
|
def get_event(event_id: int):
|
|
|
|
event = db.session.query(Event).filter(Event.id == event_id).one_or_none()
|
|
|
|
|
|
|
|
if not event:
|
|
|
|
abort(404)
|
|
|
|
|
|
|
|
return EventSchema.from_model(event).dict(by_alias=True)
|
|
|
|
|
|
|
|
@api_events.get("/team/id/<int:team_id>")
|
|
|
|
@spec.validate(
|
|
|
|
resp=Response(
|
2024-11-29 00:06:34 -08:00
|
|
|
HTTP_200=list[EventWithPlayerSchema],
|
2024-11-20 15:12:44 -08:00
|
|
|
),
|
|
|
|
operation_id="get_team_events",
|
|
|
|
)
|
2024-11-29 00:06:34 -08:00
|
|
|
@requires_authentication
|
|
|
|
@requires_team_membership()
|
|
|
|
def get_team_events(player_team: PlayerTeam, team_id: int, **_):
|
|
|
|
rows = db.session.query(
|
|
|
|
Event, PlayerEvent
|
|
|
|
).outerjoin(
|
|
|
|
PlayerEvent,
|
|
|
|
(PlayerEvent.event_id == Event.id) & (PlayerEvent.player_id == player_team.player_id)
|
|
|
|
).where(
|
2024-11-20 15:12:44 -08:00
|
|
|
Event.team_id == team_id
|
2024-11-24 10:47:45 -08:00
|
|
|
).order_by(
|
|
|
|
Event.start_time
|
2024-11-20 15:12:44 -08:00
|
|
|
).all()
|
|
|
|
|
2024-11-29 00:06:34 -08:00
|
|
|
def map_to_schema(row: Row[tuple[Event, PlayerEvent]]):
|
|
|
|
return EventWithPlayerSchema.from_event_player_event(
|
|
|
|
*row.tuple()
|
|
|
|
).dict(by_alias=True)
|
2024-11-20 15:12:44 -08:00
|
|
|
|
2024-11-29 00:06:34 -08:00
|
|
|
return list(map(map_to_schema, rows))
|
2024-11-20 15:12:44 -08:00
|
|
|
|
|
|
|
@api_events.get("/user/id/<int:user_id>")
|
|
|
|
def get_user_events(user_id: int):
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
|
|
|
class CreateEventJson(BaseModel):
|
|
|
|
name: str
|
2024-11-30 13:08:42 -08:00
|
|
|
description: Optional[str]
|
2024-11-20 15:12:44 -08:00
|
|
|
start_time: datetime
|
2024-11-27 01:03:41 -08:00
|
|
|
player_roles: list[PlayerRoleSchema]
|
2024-11-20 15:12:44 -08:00
|
|
|
|
2024-11-30 13:08:42 -08:00
|
|
|
class UpdateEventJson(BaseModel):
|
|
|
|
name: str
|
|
|
|
description: Optional[str]
|
|
|
|
player_roles: list[PlayerRoleSchema]
|
|
|
|
|
2024-11-20 15:12:44 -08:00
|
|
|
@api_events.post("/team/id/<int:team_id>")
|
|
|
|
@spec.validate(
|
|
|
|
resp=Response(
|
|
|
|
HTTP_200=EventSchema,
|
2024-11-27 01:03:41 -08:00
|
|
|
),
|
|
|
|
operation_id="create_event",
|
2024-11-20 15:12:44 -08:00
|
|
|
)
|
|
|
|
@requires_authentication
|
2024-11-21 18:18:43 -08:00
|
|
|
@requires_team_membership()
|
2024-11-27 01:03:41 -08:00
|
|
|
def create_event(player_team: PlayerTeam, team_id: int, json: CreateEventJson, **_):
|
2024-11-20 15:12:44 -08:00
|
|
|
event = Event()
|
|
|
|
event.team_id = player_team.team_id
|
|
|
|
event.name = json.name
|
2024-11-30 13:08:42 -08:00
|
|
|
if json.description:
|
|
|
|
event.description = json.description
|
2024-11-20 15:12:44 -08:00
|
|
|
event.start_time = json.start_time
|
|
|
|
|
2024-11-30 19:01:23 -08:00
|
|
|
assert_team_authority(player_team)
|
|
|
|
|
2024-11-20 15:12:44 -08:00
|
|
|
db.session.add(event)
|
|
|
|
db.session.flush()
|
|
|
|
db.session.refresh(event)
|
|
|
|
|
2024-11-27 01:03:41 -08:00
|
|
|
tuples = map(lambda x: (x.player.steam_id, x.role.role), json.player_roles)
|
|
|
|
|
|
|
|
results = db.session.query(
|
|
|
|
PlayerTeam, PlayerTeamRole.id, PlayerTeamAvailability.availability
|
2024-11-20 15:12:44 -08:00
|
|
|
).join(
|
2024-11-27 01:03:41 -08:00
|
|
|
PlayerTeamRole
|
|
|
|
).outerjoin(
|
|
|
|
PlayerTeamAvailability,
|
|
|
|
(PlayerTeamAvailability.player_team_id == PlayerTeam.id) &
|
|
|
|
(PlayerTeamAvailability.start_time <= event.start_time) &
|
|
|
|
(PlayerTeamAvailability.end_time > event.start_time)
|
2024-11-20 15:12:44 -08:00
|
|
|
).where(
|
2024-11-27 01:03:41 -08:00
|
|
|
PlayerTeam.team_id == team_id
|
2024-11-20 15:12:44 -08:00
|
|
|
).where(
|
2024-11-27 01:03:41 -08:00
|
|
|
# (player_id, role) in (...)
|
|
|
|
tuple_(PlayerTeam.player_id, PlayerTeamRole.role).in_(tuples)
|
2024-11-20 15:12:44 -08:00
|
|
|
).all()
|
|
|
|
|
2024-11-27 01:03:41 -08:00
|
|
|
for player_team, role_id, availability in map(lambda x: x.tuple(), results):
|
2024-11-20 15:12:44 -08:00
|
|
|
player_event = PlayerEvent()
|
2024-11-27 01:03:41 -08:00
|
|
|
player_event.player_id = player_team.player_id
|
2024-11-20 15:12:44 -08:00
|
|
|
player_event.event_id = event.id
|
2024-11-27 01:03:41 -08:00
|
|
|
player_event.player_team_role_id = role_id
|
|
|
|
|
|
|
|
# autoconfirm if availability = 2
|
|
|
|
player_event.has_confirmed = (availability == 2)
|
|
|
|
|
2024-11-20 15:12:44 -08:00
|
|
|
db.session.add(player_event)
|
|
|
|
|
|
|
|
db.session.commit()
|
|
|
|
|
2024-11-27 01:03:41 -08:00
|
|
|
event.update_discord_message()
|
|
|
|
|
2024-11-20 15:12:44 -08:00
|
|
|
return EventSchema.from_model(event).dict(by_alias=True), 200
|
|
|
|
|
2024-11-30 22:26:28 -08:00
|
|
|
class AttendanceJson(BaseModel):
|
|
|
|
confirm: bool
|
|
|
|
|
2024-11-27 01:03:41 -08:00
|
|
|
@api_events.put("/<int:event_id>/attendance")
|
|
|
|
@spec.validate(
|
|
|
|
resp=Response(
|
2024-11-29 00:06:34 -08:00
|
|
|
HTTP_200=EventWithPlayerSchema,
|
|
|
|
),
|
|
|
|
operation_id="attend_event",
|
2024-11-27 01:03:41 -08:00
|
|
|
)
|
|
|
|
@requires_authentication
|
2024-11-30 22:26:28 -08:00
|
|
|
def attend_event(player: Player, event_id: int, json: AttendanceJson, **_):
|
2024-11-29 00:06:34 -08:00
|
|
|
event = db.session.query(Event).where(Event.id == event_id).one_or_none()
|
|
|
|
|
|
|
|
if not event:
|
|
|
|
abort(404)
|
|
|
|
|
|
|
|
assert_team_membership(player, event.team)
|
|
|
|
|
2024-11-27 01:03:41 -08:00
|
|
|
player_event = db.session.query(
|
|
|
|
PlayerEvent
|
|
|
|
).where(
|
|
|
|
PlayerEvent.event_id == event_id
|
|
|
|
).where(
|
2024-11-29 00:06:34 -08:00
|
|
|
PlayerEvent.player_id == player.steam_id
|
2024-11-27 01:03:41 -08:00
|
|
|
).join(
|
|
|
|
Event
|
|
|
|
).one_or_none()
|
|
|
|
|
|
|
|
if not player_event:
|
|
|
|
player_event = PlayerEvent()
|
|
|
|
player_event.event_id = event_id
|
2024-11-29 00:06:34 -08:00
|
|
|
player_event.player_id = player.steam_id
|
2024-11-27 01:03:41 -08:00
|
|
|
db.session.add(player_event)
|
|
|
|
|
2024-11-30 22:26:28 -08:00
|
|
|
player_event.has_confirmed = json.confirm
|
2024-11-27 01:03:41 -08:00
|
|
|
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
player_event.event.update_discord_message()
|
|
|
|
|
2024-11-29 00:06:34 -08:00
|
|
|
return EventWithPlayerSchema.from_event_player_event(
|
|
|
|
player_event.event,
|
|
|
|
player_event,
|
|
|
|
).dict(by_alias=True)
|
2024-11-27 01:03:41 -08:00
|
|
|
|
|
|
|
@api_events.delete("/<int:event_id>/attendance")
|
|
|
|
@spec.validate(
|
|
|
|
resp=Response(
|
2024-11-29 00:06:34 -08:00
|
|
|
HTTP_200=EventWithPlayerSchema,
|
|
|
|
),
|
|
|
|
operation_id="unattend_event",
|
2024-11-27 01:03:41 -08:00
|
|
|
)
|
|
|
|
@requires_authentication
|
2024-11-29 00:06:34 -08:00
|
|
|
def unattend_event(player: Player, event_id: int, **_):
|
2024-11-27 01:03:41 -08:00
|
|
|
result = db.session.query(
|
|
|
|
PlayerEvent, Event
|
|
|
|
).where(
|
2024-11-29 00:06:34 -08:00
|
|
|
PlayerEvent.player_id == player.steam_id
|
2024-11-27 01:03:41 -08:00
|
|
|
).join(
|
|
|
|
Event
|
|
|
|
).where(
|
2024-11-29 00:06:34 -08:00
|
|
|
Event.id == event_id
|
2024-11-27 01:03:41 -08:00
|
|
|
).one_or_none()
|
|
|
|
|
|
|
|
if not result:
|
|
|
|
abort(404)
|
|
|
|
|
|
|
|
player_event, event = result.tuple()
|
|
|
|
|
|
|
|
db.session.delete(player_event)
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
event.update_discord_message()
|
|
|
|
|
2024-11-29 00:06:34 -08:00
|
|
|
return EventWithPlayerSchema.from_event_player_event(
|
|
|
|
event,
|
|
|
|
None,
|
|
|
|
).dict(by_alias=True)
|
2024-11-27 01:03:41 -08:00
|
|
|
|
|
|
|
class GetEventPlayersResponse(BaseModel):
|
|
|
|
players: list[PlayerEventRolesSchema]
|
|
|
|
|
|
|
|
@api_events.get("/<int:event_id>/players")
|
|
|
|
@spec.validate(
|
|
|
|
resp=Response(
|
|
|
|
HTTP_200=GetEventPlayersResponse,
|
|
|
|
),
|
|
|
|
operation_id="get_event_players",
|
|
|
|
)
|
|
|
|
@requires_authentication
|
|
|
|
def get_event_players(player: Player, event_id: int, **_):
|
|
|
|
event = db.session.query(Event).where(Event.id == event_id).one_or_none()
|
|
|
|
if not event:
|
|
|
|
abort(404)
|
|
|
|
assert_team_membership(player, event.team)
|
|
|
|
|
|
|
|
players_events = db.session.query(
|
|
|
|
PlayerEvent
|
|
|
|
).join(
|
|
|
|
Event,
|
|
|
|
Event.id == PlayerEvent.event_id
|
|
|
|
).join(
|
|
|
|
PlayerTeam,
|
|
|
|
PlayerTeam.team_id == Event.team_id & PlayerEvent.player_id == PlayerTeam.player_id
|
|
|
|
).where(
|
|
|
|
PlayerEvent.event_id == event_id
|
|
|
|
).all()
|
|
|
|
|
|
|
|
player_event_roles = [
|
|
|
|
PlayerEventRolesSchema.from_event_player_team(
|
|
|
|
player_event, player_event.player_team
|
|
|
|
)
|
|
|
|
for player_event in players_events
|
|
|
|
]
|
|
|
|
|
|
|
|
return GetEventPlayersResponse(
|
|
|
|
players=player_event_roles
|
|
|
|
).dict(by_alias=True), 200
|
|
|
|
|
2024-11-30 13:08:42 -08:00
|
|
|
@api_events.patch("/<int:event_id>")
|
|
|
|
@spec.validate(
|
|
|
|
resp=Response(
|
|
|
|
HTTP_200=EventSchema,
|
|
|
|
),
|
|
|
|
operation_id="update_event",
|
|
|
|
)
|
2024-11-20 15:12:44 -08:00
|
|
|
@requires_authentication
|
2024-11-30 13:08:42 -08:00
|
|
|
def update_event(player: Player, event_id: int, json: UpdateEventJson, **_):
|
|
|
|
event = db.session.query(Event).where(Event.id == event_id).one_or_none()
|
|
|
|
if not event:
|
|
|
|
abort(404)
|
2024-11-30 19:01:23 -08:00
|
|
|
|
|
|
|
player_team = assert_team_membership(player, event.team)
|
|
|
|
assert_team_authority(player_team)
|
2024-11-20 15:12:44 -08:00
|
|
|
|
2024-11-30 13:08:42 -08:00
|
|
|
for player_event in event.players:
|
|
|
|
player_team = player_event.player_team
|
|
|
|
roles = player_team.player_roles
|
|
|
|
# assign new roles if in json, set to None if not
|
|
|
|
new_role = next((x.role for x in json.player_roles if x.player.steam_id == str(player_event.player_id)), None)
|
|
|
|
#import sys
|
|
|
|
#print(player_event.player_id, file=sys.stderr)
|
|
|
|
# if valid role (new_role exists in roles), update player_event.role
|
|
|
|
if new_role and (role_entity := next((x for x in roles if x.role.name == new_role.role), None)):
|
|
|
|
player_event.player_team_role_id = role_entity.id
|
|
|
|
else:
|
|
|
|
player_event.role = None
|
2024-11-20 15:12:44 -08:00
|
|
|
|
2024-12-07 17:22:19 -08:00
|
|
|
event.name = json.name
|
|
|
|
event.description = json.description
|
|
|
|
|
2024-11-30 20:02:18 -08:00
|
|
|
db.session.commit()
|
|
|
|
|
2024-11-30 13:08:42 -08:00
|
|
|
event.update_discord_message()
|
|
|
|
|
|
|
|
return EventSchema.from_model(event).dict(by_alias=True), 200
|
2024-11-30 19:01:23 -08:00
|
|
|
|
|
|
|
@api_events.delete("/<int:event_id>")
|
|
|
|
@spec.validate(
|
|
|
|
resp=Response(
|
|
|
|
HTTP_204=None,
|
|
|
|
),
|
|
|
|
operation_id="delete_event",
|
|
|
|
)
|
|
|
|
@requires_authentication
|
|
|
|
def delete_event(player: Player, event_id: int, **_):
|
|
|
|
event = db.session.query(Event).where(Event.id == event_id).one_or_none()
|
|
|
|
if not event:
|
|
|
|
abort(404)
|
|
|
|
|
|
|
|
player_team = assert_team_membership(player, event.team)
|
|
|
|
assert_team_authority(player_team)
|
|
|
|
|
|
|
|
db.session.delete(event)
|
|
|
|
db.session.commit()
|
|
|
|
|
|
|
|
return make_response({ }, 204)
|