From c1700ec7dbd32249516f759681c0faa222d3770f Mon Sep 17 00:00:00 2001 From: David Rodenkirchen Date: Thu, 12 Feb 2026 10:05:36 +0100 Subject: [PATCH 1/6] declutter layout and and boilerplate for teams page --- src/EzggLanManager.py | 5 +++++ .../components/DesktopNavigation.py | 3 +-- src/ezgg_lan_manager/pages/BasePage.py | 12 ++++++++++-- src/ezgg_lan_manager/pages/TeamsPage.py | 17 +++++++++++++++++ src/ezgg_lan_manager/pages/__init__.py | 1 + 5 files changed, 34 insertions(+), 4 deletions(-) create mode 100644 src/ezgg_lan_manager/pages/TeamsPage.py diff --git a/src/EzggLanManager.py b/src/EzggLanManager.py index 82d5146..ce50bc8 100644 --- a/src/EzggLanManager.py +++ b/src/EzggLanManager.py @@ -172,6 +172,11 @@ if __name__ == "__main__": url_segment="tournament-rules", build=pages.TournamentRulesPage, ), + ComponentPage( + name="Teams", + url_segment="teams", + build=pages.TeamsPage, + ), ComponentPage( name="ConwaysGameOfLife", url_segment="conway", diff --git a/src/ezgg_lan_manager/components/DesktopNavigation.py b/src/ezgg_lan_manager/components/DesktopNavigation.py index 20be550..2a97964 100644 --- a/src/ezgg_lan_manager/components/DesktopNavigation.py +++ b/src/ezgg_lan_manager/components/DesktopNavigation.py @@ -51,14 +51,13 @@ class DesktopNavigation(Component): DesktopNavigationButton("Sitzplan", "./seating"), DesktopNavigationButton("Catering", "./catering"), DesktopNavigationButton("Teilnehmer", "./guests"), + DesktopNavigationButton("Teams", "./teams"), DesktopNavigationButton("Turniere", "./tournaments"), DesktopNavigationButton("FAQ", "./faq"), DesktopNavigationButton("Regeln & AGB", "./rules-gtc"), Spacer(min_height=0.7), DesktopNavigationButton("Discord", "https://discord.gg/8gTjg34yyH", open_new_tab=True), DesktopNavigationButton("Die EZ GG e.V.", "https://ezgg-ev.de/about", open_new_tab=True), - DesktopNavigationButton("Kontakt", "./contact"), - DesktopNavigationButton("Impressum & DSGVO", "./imprint"), Spacer(min_height=0.7) ] team_navigation = [ diff --git a/src/ezgg_lan_manager/pages/BasePage.py b/src/ezgg_lan_manager/pages/BasePage.py index 91c6cf6..128aa94 100644 --- a/src/ezgg_lan_manager/pages/BasePage.py +++ b/src/ezgg_lan_manager/pages/BasePage.py @@ -2,7 +2,7 @@ from __future__ import annotations from typing import * # type: ignore -from rio import Component, event, Spacer, Card, Container, Column, Row, TextStyle, Color, Text, PageView, Button +from rio import Component, event, Spacer, Card, Container, Column, Row, TextStyle, Color, Text, PageView, Button, Link from src.ezgg_lan_manager import ConfigurationService, DatabaseService from src.ezgg_lan_manager.components.DesktopNavigation import DesktopNavigation @@ -58,7 +58,15 @@ class BasePage(Component): Row( Spacer(grow_x=True, grow_y=False), Card( - content=Text(f"EZGG LAN Manager Version {self.session[ConfigurationService].APP_VERSION} © EZ GG e.V.", align_x=0.5, align_y=0.5, fill=self.session.theme.primary_color, style=TextStyle(font_size=0.5)), + content=Row( + Text(f"EZGG LAN Manager Version {self.session[ConfigurationService].APP_VERSION} © EZ GG e.V.", fill=self.session.theme.primary_color, style=TextStyle(font_size=0.5)), + Text(f"-", fill=self.session.theme.primary_color, style=TextStyle(font_size=0.5), margin_left=0.5, margin_right=0.5), + Link(content=Text(f"Impressum & DSGVO", fill=self.session.theme.primary_color, style=TextStyle(font_size=0.5)), target_url="./imprint"), + Text(f"-", fill=self.session.theme.primary_color, style=TextStyle(font_size=0.5), margin_left=0.5, margin_right=0.5), + Link(content=Text(f"Kontakt", fill=self.session.theme.primary_color, style=TextStyle(font_size=0.5)), target_url="./contact"), + align_x=0.5, + align_y=0.5 + ), color=self.session.theme.neutral_color, corner_radius=(0, 0, 0.5, 0.5), grow_x=False, diff --git a/src/ezgg_lan_manager/pages/TeamsPage.py b/src/ezgg_lan_manager/pages/TeamsPage.py new file mode 100644 index 0000000..89fd3be --- /dev/null +++ b/src/ezgg_lan_manager/pages/TeamsPage.py @@ -0,0 +1,17 @@ +from rio import Column, Component, event, Text + +from src.ezgg_lan_manager import ConfigurationService +from src.ezgg_lan_manager.components.MainViewContentBox import MainViewContentBox +from src.ezgg_lan_manager.types.News import News + + +class TeamsPage(Component): + @event.on_populate + async def on_populate(self) -> None: + await self.session.set_title(f"{self.session[ConfigurationService].get_lan_info().name} - Teams") + + def build(self) -> Component: + return Column( + MainViewContentBox(Text("Teams")), + align_y=0 + ) diff --git a/src/ezgg_lan_manager/pages/__init__.py b/src/ezgg_lan_manager/pages/__init__.py index 8bb9e24..e682fb2 100644 --- a/src/ezgg_lan_manager/pages/__init__.py +++ b/src/ezgg_lan_manager/pages/__init__.py @@ -23,3 +23,4 @@ from .OverviewPage import OverviewPage from .TournamentDetailsPage import TournamentDetailsPage from .TournamentRulesPage import TournamentRulesPage from .ConwayPage import ConwayPage +from .TeamsPage import TeamsPage -- 2.45.2 From e30359fefb57b500df57a012f8cea81538d9acdd Mon Sep 17 00:00:00 2001 From: David Rodenkirchen Date: Thu, 12 Feb 2026 10:48:18 +0100 Subject: [PATCH 2/6] add teams datamodel, service and boilerplate database extension --- .../services/DatabaseService.py | 23 +++++ src/ezgg_lan_manager/services/TeamService.py | 94 +++++++++++++++++++ src/ezgg_lan_manager/types/Team.py | 18 ++++ 3 files changed, 135 insertions(+) create mode 100644 src/ezgg_lan_manager/services/TeamService.py create mode 100644 src/ezgg_lan_manager/types/Team.py diff --git a/src/ezgg_lan_manager/services/DatabaseService.py b/src/ezgg_lan_manager/services/DatabaseService.py index ccbbfc1..fa24411 100644 --- a/src/ezgg_lan_manager/services/DatabaseService.py +++ b/src/ezgg_lan_manager/services/DatabaseService.py @@ -14,6 +14,7 @@ from src.ezgg_lan_manager.types.ConfigurationTypes import DatabaseConfiguration from src.ezgg_lan_manager.types.News import News from src.ezgg_lan_manager.types.Participant import Participant from src.ezgg_lan_manager.types.Seat import Seat +from src.ezgg_lan_manager.types.Team import TeamStatus, Team from src.ezgg_lan_manager.types.Ticket import Ticket from src.ezgg_lan_manager.types.Tournament import Tournament from src.ezgg_lan_manager.types.TournamentBase import GameTitle, TournamentFormat, TournamentStatus, ParticipantType @@ -967,3 +968,25 @@ class DatabaseService: return await self.remove_participant_from_tournament(participant, tournament) except Exception as e: logger.warning(f"Error removing participant from tournament: {e}") + + async def get_teams(self) -> list[Team]: + # ToDo: Implement + return [] + + async def get_team_by_id(self, team_id: int) -> Optional[Team]: + return + + async def get_team_for_user_by_id(self, user_id: int) -> Optional[Team]: + return + + async def create_team(self, team_name: str, team_abbr: str, join_password: str) -> Team: + return # ToDo: Raise on existing team + + async def update_team(self, team: Team) -> Team: + pass + + async def add_member_to_team(self, team: Team, user: User) -> None: + pass + + async def remove_user_from_team(self, team: Team, user: User) -> None: + pass diff --git a/src/ezgg_lan_manager/services/TeamService.py b/src/ezgg_lan_manager/services/TeamService.py new file mode 100644 index 0000000..9c88f50 --- /dev/null +++ b/src/ezgg_lan_manager/services/TeamService.py @@ -0,0 +1,94 @@ +from hashlib import sha256 +from typing import Union, Optional +from string import ascii_letters, digits + +from src.ezgg_lan_manager.services.DatabaseService import DatabaseService +from src.ezgg_lan_manager.types.User import User +from src.ezgg_lan_manager.types.Team import TeamStatus, Team + + +class NameNotAllowedError(Exception): + def __init__(self, disallowed_char: str) -> None: + self.disallowed_char = disallowed_char + +class AlreadyMemberError(Exception): + def __init__(self) -> None: + pass + +class NotMemberError(Exception): + def __init__(self) -> None: + pass + +class TeamLeadRemovalError(Exception): + def __init__(self) -> None: + pass + + +class TeamService: + ALLOWED_TEAM_NAME_SYMBOLS = ascii_letters + digits + "!#$%&*+,-./:;<=>?[]^_{|}~" + + def __init__(self, db_service: DatabaseService) -> None: + self._db_service = db_service + + async def get_all_teams(self) -> list[Team]: + return await self._db_service.get_teams() + + async def get_team_by_id(self, team_id: int) -> Optional[Team]: + return await self._db_service.get_team_by_id(team_id) + + async def get_team_for_user_by_id(self, user_id: int) -> Optional[Team]: + return await self._db_service.get_team_for_user_by_id(user_id) + + async def create_team(self, team_name: str, team_abbr: str, join_password: str) -> Team: + disallowed_char = self._check_for_disallowed_char(team_name) + if disallowed_char: + raise NameNotAllowedError(disallowed_char) + disallowed_char = self._check_for_disallowed_char(team_abbr) + if disallowed_char: + raise NameNotAllowedError(disallowed_char) + + created_team = await self._db_service.create_team(team_name, team_abbr, join_password) + return created_team + + async def update_team(self, team: Team) -> Team: + """ + Updates the team EXCLUDING adding and removing members. This is to be done via add_member_to_team and remove_member_from_team + :param team: New instance of Team that is to be updated + :return: The modified Team instance + """ + disallowed_char = self._check_for_disallowed_char(team.name) + if disallowed_char: + raise NameNotAllowedError(disallowed_char) + disallowed_char = self._check_for_disallowed_char(team.abbreviation) + if disallowed_char: + raise NameNotAllowedError(disallowed_char) + return await self._db_service.update_team(team) + + async def add_member_to_team(self, team: Team, user: User) -> Team: + if user in team.members: + raise AlreadyMemberError() + + await self._db_service.add_member_to_team(team, user) + return await self.get_team_by_id(team.id) + + async def remove_member_from_team(self, team: Team, user: User) -> Team: + if user not in team.members: + raise NotMemberError() + + if team.members[user] is TeamStatus.LEADER: + raise TeamLeadRemovalError() + + await self._db_service.remove_user_from_team(team, user) + return await self.get_team_by_id(team.id) + + async def is_join_password_valid(self, team_id: int, join_password: str) -> bool: + team = await self.get_team_by_id(team_id) + if not team: + return False + return team.join_password == join_password + + def _check_for_disallowed_char(self, name: str) -> Optional[str]: + for c in name: + if c not in self.ALLOWED_TEAM_NAME_SYMBOLS: + return c + return None diff --git a/src/ezgg_lan_manager/types/Team.py b/src/ezgg_lan_manager/types/Team.py new file mode 100644 index 0000000..b4063f8 --- /dev/null +++ b/src/ezgg_lan_manager/types/Team.py @@ -0,0 +1,18 @@ +from dataclasses import dataclass +from enum import Enum + +from src.ezgg_lan_manager.types.User import User + +class TeamStatus(Enum): + MEMBER = 0 + OFFICER = 1 + LEADER = 2 + + +@dataclass(frozen=True) +class Team: + id: int + name: str + abbreviation: str + members: dict[User, TeamStatus] + join_password: str -- 2.45.2 From b34269cdb5d32f8aa3322fe0afd5918f39de14f5 Mon Sep 17 00:00:00 2001 From: David Rodenkirchen Date: Thu, 12 Feb 2026 22:15:45 +0100 Subject: [PATCH 3/6] add basic UI for teams (end of day commit) --- src/ezgg_lan_manager/__init__.py | 6 +- .../components/TeamRevealer.py | 43 ++++++ src/ezgg_lan_manager/pages/TeamsPage.py | 122 +++++++++++++++++- .../services/DatabaseService.py | 25 +++- src/ezgg_lan_manager/services/TeamService.py | 4 +- 5 files changed, 190 insertions(+), 10 deletions(-) create mode 100644 src/ezgg_lan_manager/components/TeamRevealer.py diff --git a/src/ezgg_lan_manager/__init__.py b/src/ezgg_lan_manager/__init__.py index f781029..b52831d 100644 --- a/src/ezgg_lan_manager/__init__.py +++ b/src/ezgg_lan_manager/__init__.py @@ -12,13 +12,14 @@ from src.ezgg_lan_manager.services.MailingService import MailingService from src.ezgg_lan_manager.services.NewsService import NewsService from src.ezgg_lan_manager.services.ReceiptPrintingService import ReceiptPrintingService from src.ezgg_lan_manager.services.SeatingService import SeatingService +from src.ezgg_lan_manager.services.TeamService import TeamService from src.ezgg_lan_manager.services.TicketingService import TicketingService from src.ezgg_lan_manager.services.TournamentService import TournamentService from src.ezgg_lan_manager.services.UserService import UserService from src.ezgg_lan_manager.types import * # Inits services in the correct order -def init_services() -> tuple[AccountingService, CateringService, ConfigurationService, DatabaseService, MailingService, NewsService, SeatingService, TicketingService, UserService, LocalDataService, ReceiptPrintingService, TournamentService]: +def init_services() -> tuple[AccountingService, CateringService, ConfigurationService, DatabaseService, MailingService, NewsService, SeatingService, TicketingService, UserService, LocalDataService, ReceiptPrintingService, TournamentService, TeamService]: logging.basicConfig(level=logging.DEBUG) configuration_service = ConfigurationService(from_root("config.toml")) db_service = DatabaseService(configuration_service.get_database_configuration()) @@ -32,6 +33,7 @@ def init_services() -> tuple[AccountingService, CateringService, ConfigurationSe catering_service = CateringService(db_service, accounting_service, user_service, receipt_printing_service) local_data_service = LocalDataService() tournament_service = TournamentService(db_service, user_service) + team_service = TeamService(db_service) - return accounting_service, catering_service, configuration_service, db_service, mailing_service, news_service, seating_service, ticketing_service, user_service, local_data_service, receipt_printing_service, tournament_service + return accounting_service, catering_service, configuration_service, db_service, mailing_service, news_service, seating_service, ticketing_service, user_service, local_data_service, receipt_printing_service, tournament_service, team_service diff --git a/src/ezgg_lan_manager/components/TeamRevealer.py b/src/ezgg_lan_manager/components/TeamRevealer.py new file mode 100644 index 0000000..c2f8126 --- /dev/null +++ b/src/ezgg_lan_manager/components/TeamRevealer.py @@ -0,0 +1,43 @@ +from functools import partial +from typing import Callable, Optional + +from rio import Component, Revealer, TextStyle, Column, Row, Tooltip, Icon, Spacer, Text, Button + +from src.ezgg_lan_manager.types.Team import TeamStatus, Team +from src.ezgg_lan_manager.types.User import User + + +class TeamRevealer(Component): + user: Optional[User] + team: Team + on_join_button_pressed: Callable + + def build(self) -> Component: + return Revealer( + header=self.team.name, + header_style=TextStyle( + fill=self.session.theme.background_color, + font_size=1 + ), + content=Column( + *[Row( + Tooltip( + anchor=Icon("material/star" if self.team.members[member] == TeamStatus.LEADER else "material/stat_1", fill=self.session.theme.hud_color), + tip="Leiter" if self.team.members[member] == TeamStatus.LEADER else "Mitglied", position="top"), + Text(member.user_name, style=TextStyle(fill=self.session.theme.background_color, font_size=1), margin_left=0.5), + Spacer(grow_y=False)) + for member in self.team.members + ], + Row(Button( + content=f"{self.team.name} beitreten", + shape="rectangle", + style="major", + color="hud", + on_press=partial(self.on_join_button_pressed, self.team), + ) if self.user not in self.team.members.keys() and self.user is not None else Spacer(grow_x=False, grow_y=False), margin_top=1, margin_bottom=1), + margin_right=1, + margin_left=1 + ), + margin_left=1, + margin_right=1, + ) diff --git a/src/ezgg_lan_manager/pages/TeamsPage.py b/src/ezgg_lan_manager/pages/TeamsPage.py index 89fd3be..b3e5ae0 100644 --- a/src/ezgg_lan_manager/pages/TeamsPage.py +++ b/src/ezgg_lan_manager/pages/TeamsPage.py @@ -1,17 +1,133 @@ -from rio import Column, Component, event, Text +from typing import Optional + +from rio import Column, Component, event, Text, TextStyle, ProgressCircle, Spacer, Revealer, Row, Button, Icon, Tooltip, Rectangle, PointerEventListener, PointerEvent from src.ezgg_lan_manager import ConfigurationService from src.ezgg_lan_manager.components.MainViewContentBox import MainViewContentBox -from src.ezgg_lan_manager.types.News import News +from src.ezgg_lan_manager.components.TeamRevealer import TeamRevealer +from src.ezgg_lan_manager.services.TeamService import TeamService +from src.ezgg_lan_manager.services.UserService import UserService +from src.ezgg_lan_manager.types.SessionStorage import SessionStorage +from src.ezgg_lan_manager.types.Team import Team +from src.ezgg_lan_manager.types.User import User class TeamsPage(Component): + all_teams: Optional[list[Team]] = None + user: Optional[User] = None + user_teams: list[Team] = [] + @event.on_populate async def on_populate(self) -> None: + self.all_teams = await self.session[TeamService].get_all_teams() + #self.user = await self.session[UserService].get_user(self.session[SessionStorage].user_id) + self.user = await self.session[UserService].get_user("Typhus") # FixMe: Only debug + if self.user is not None: + self.user_teams = await self.session[TeamService].get_teams_for_user_by_id(self.user.user_id) await self.session.set_title(f"{self.session[ConfigurationService].get_lan_info().name} - Teams") + self.session[SessionStorage].subscribe_to_logged_in_or_out_event(str(self.__class__), self.on_populate) + + async def on_join_button_pressed(self, team: Team) -> None: + # ToDo: handle joining by displaying password popup + print(f"Starting join process for team {team.abbreviation}") + + async def on_leave_button_pressed(self, team: Team) -> None: + # ToDo: handle leaving + print(f"Starting leaving process for team {team.abbreviation}") + + async def on_create_button_pressed(self, _: PointerEvent) -> None: + # ToDo: Add Popup creation dialog + print(f"Starting creation process for team") def build(self) -> Component: + if self.all_teams is None: + return Column( + MainViewContentBox( + ProgressCircle( + color="secondary", + align_x=0.5, + margin_top=1, + margin_bottom=1 + ) + ), + Spacer() + ) + + team_list = [] + for team in self.all_teams: + team_list.append(TeamRevealer(user=self.user, team=team, on_join_button_pressed=self.on_join_button_pressed)) + + if team_list: + team_list[-1].margin_bottom = 1 + + own_teams_content = Spacer(grow_x=False, grow_y=False) + if self.user is not None: + user_team_list = [] + for team in self.user_teams: + # ToDo: Remove from team instead of join + user_team_list.append(TeamRevealer(user=self.user, team=team, on_join_button_pressed=self.on_join_button_pressed)) + + if not user_team_list: + user_team_list.append(Text( + text="Du bist noch in keinem Team.", + style=TextStyle( + fill=self.session.theme.background_color, + font_size=1 + ), + margin_top=1, + margin_bottom=1, + align_x=0.5 + )) + else: + user_team_list[-1].margin_bottom = 1 + own_teams_content = MainViewContentBox( + Column( + Row( + Text( + text="Deine Teams", + style=TextStyle( + fill=self.session.theme.background_color, + font_size=1.2 + ), + grow_x=True, + justify="right", + margin_right=3 + ), + Column( + PointerEventListener(Rectangle( + content=Text(text="Team erstellen", style=TextStyle(fill=self.session.theme.background_color, font_size=0.7), margin=0.1, selectable=False), + stroke_width=0.1, + stroke_color=self.session.theme.hud_color, + cursor="pointer", + hover_fill=self.session.theme.hud_color, + transition_time=0 + ), on_press=self.on_create_button_pressed), + Spacer(), + margin_right=2 + ), + margin_top=1, + margin_bottom=1 + ), + *user_team_list + ) + ) + return Column( - MainViewContentBox(Text("Teams")), + own_teams_content, + MainViewContentBox( + Column( + Text( + text="Alle Teams", + style=TextStyle( + fill=self.session.theme.background_color, + font_size=1.2 + ), + margin_top=1, + margin_bottom=1, + align_x=0.5 + ), + *team_list + ) + ), align_y=0 ) diff --git a/src/ezgg_lan_manager/services/DatabaseService.py b/src/ezgg_lan_manager/services/DatabaseService.py index fa24411..14fb294 100644 --- a/src/ezgg_lan_manager/services/DatabaseService.py +++ b/src/ezgg_lan_manager/services/DatabaseService.py @@ -971,13 +971,32 @@ class DatabaseService: async def get_teams(self) -> list[Team]: # ToDo: Implement - return [] + return [ + Team( + id=1, + name="MockTeamAlpha", + abbreviation="-=MTA=-", + members={User(0, "DemoUserA", "", "abuvbwer", None, None, None, True, False, False, datetime.now(), datetime.now()): TeamStatus.LEADER}, + join_password="abc" + ), + Team( + id=1, + name="MockTeamBeta", + abbreviation="[MTB]", + members={ + User(1, "DemoUserB", "", "abuvbwer", None, None, None, True, False, False, datetime.now(), datetime.now()): TeamStatus.LEADER, + User(2, "DemoUserC", "", "abuvbwer", None, None, None, True, False, False, datetime.now(), datetime.now()): TeamStatus.OFFICER, + User(3, "DemoUserD", "", "abuvbwer", None, None, None, True, False, False, datetime.now(), datetime.now()): TeamStatus.MEMBER + }, + join_password="abc" + ) + ] async def get_team_by_id(self, team_id: int) -> Optional[Team]: return - async def get_team_for_user_by_id(self, user_id: int) -> Optional[Team]: - return + async def get_teams_for_user_by_id(self, user_id: int) -> list[Team]: + return [] async def create_team(self, team_name: str, team_abbr: str, join_password: str) -> Team: return # ToDo: Raise on existing team diff --git a/src/ezgg_lan_manager/services/TeamService.py b/src/ezgg_lan_manager/services/TeamService.py index 9c88f50..96e0194 100644 --- a/src/ezgg_lan_manager/services/TeamService.py +++ b/src/ezgg_lan_manager/services/TeamService.py @@ -36,8 +36,8 @@ class TeamService: async def get_team_by_id(self, team_id: int) -> Optional[Team]: return await self._db_service.get_team_by_id(team_id) - async def get_team_for_user_by_id(self, user_id: int) -> Optional[Team]: - return await self._db_service.get_team_for_user_by_id(user_id) + async def get_teams_for_user_by_id(self, user_id: int) -> list[Team]: + return await self._db_service.get_teams_for_user_by_id(user_id) async def create_team(self, team_name: str, team_abbr: str, join_password: str) -> Team: disallowed_char = self._check_for_disallowed_char(team_name) -- 2.45.2 From 8a9004a9a03d439bc7e8fdb7cd714ad5f50db17e Mon Sep 17 00:00:00 2001 From: David Rodenkirchen Date: Fri, 13 Feb 2026 11:57:32 +0100 Subject: [PATCH 4/6] integrate database layer --- sql/teams_patch.sql | 63 +++++ .../components/TeamRevealer.py | 11 +- src/ezgg_lan_manager/pages/TeamsPage.py | 23 +- .../services/DatabaseService.py | 216 +++++++++++++++--- src/ezgg_lan_manager/services/TeamService.py | 44 +++- src/ezgg_lan_manager/types/Team.py | 11 + src/ezgg_lan_manager/types/User.py | 7 +- 7 files changed, 326 insertions(+), 49 deletions(-) create mode 100644 sql/teams_patch.sql diff --git a/sql/teams_patch.sql b/sql/teams_patch.sql new file mode 100644 index 0000000..16283ff --- /dev/null +++ b/sql/teams_patch.sql @@ -0,0 +1,63 @@ +-- ===================================================== +-- Teams +-- ===================================================== + +DROP TABLE IF EXISTS `team_members`; +DROP TABLE IF EXISTS `teams`; + + +-- ----------------------------------------------------- +-- Teams table +-- ----------------------------------------------------- +CREATE TABLE `teams` ( + id INT AUTO_INCREMENT PRIMARY KEY, + name VARCHAR(100) NOT NULL, + abbreviation VARCHAR(10) NOT NULL, + join_password VARCHAR(255) NOT NULL, + created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, + + UNIQUE KEY uq_team_name (name), + UNIQUE KEY uq_team_abbr (abbreviation) + +) ENGINE=InnoDB + DEFAULT CHARSET=utf8mb4 + COLLATE=utf8mb4_unicode_ci; + + +-- ----------------------------------------------------- +-- Team Members (Junction Table) +-- ----------------------------------------------------- +CREATE TABLE `team_members` ( + team_id INT NOT NULL, + user_id INT NOT NULL, + + status ENUM('MEMBER','OFFICER','LEADER') + NOT NULL DEFAULT 'MEMBER', + + joined_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, + + PRIMARY KEY (team_id, user_id), + + CONSTRAINT fk_tm_team + FOREIGN KEY (team_id) + REFERENCES teams(id) + ON DELETE CASCADE, + + CONSTRAINT fk_tm_user + FOREIGN KEY (user_id) + REFERENCES users(user_id) + ON DELETE CASCADE + +) ENGINE=InnoDB + DEFAULT CHARSET=utf8mb4 + COLLATE=utf8mb4_unicode_ci; + + +-- ----------------------------------------------------- +-- Indexes +-- ----------------------------------------------------- +CREATE INDEX idx_tm_user + ON team_members(user_id); + +CREATE INDEX idx_tm_team_status + ON team_members(team_id, status); diff --git a/src/ezgg_lan_manager/components/TeamRevealer.py b/src/ezgg_lan_manager/components/TeamRevealer.py index c2f8126..4c04701 100644 --- a/src/ezgg_lan_manager/components/TeamRevealer.py +++ b/src/ezgg_lan_manager/components/TeamRevealer.py @@ -1,5 +1,5 @@ from functools import partial -from typing import Callable, Optional +from typing import Callable, Optional, Literal from rio import Component, Revealer, TextStyle, Column, Row, Tooltip, Icon, Spacer, Text, Button @@ -10,7 +10,8 @@ from src.ezgg_lan_manager.types.User import User class TeamRevealer(Component): user: Optional[User] team: Team - on_join_button_pressed: Callable + mode: Literal["join", "leave", "display"] + on_button_pressed: Callable def build(self) -> Component: return Revealer( @@ -29,12 +30,12 @@ class TeamRevealer(Component): for member in self.team.members ], Row(Button( - content=f"{self.team.name} beitreten", + content=f"{self.team.name} beitreten" if self.mode == "join" else f"{self.team.name} verlassen", shape="rectangle", style="major", color="hud", - on_press=partial(self.on_join_button_pressed, self.team), - ) if self.user not in self.team.members.keys() and self.user is not None else Spacer(grow_x=False, grow_y=False), margin_top=1, margin_bottom=1), + on_press=partial(self.on_button_pressed, self.team), + ), margin_top=1, margin_bottom=1), margin_right=1, margin_left=1 ), diff --git a/src/ezgg_lan_manager/pages/TeamsPage.py b/src/ezgg_lan_manager/pages/TeamsPage.py index b3e5ae0..397c2d3 100644 --- a/src/ezgg_lan_manager/pages/TeamsPage.py +++ b/src/ezgg_lan_manager/pages/TeamsPage.py @@ -1,6 +1,6 @@ from typing import Optional -from rio import Column, Component, event, Text, TextStyle, ProgressCircle, Spacer, Revealer, Row, Button, Icon, Tooltip, Rectangle, PointerEventListener, PointerEvent +from rio import Column, Component, event, Text, TextStyle, ProgressCircle, Spacer, Row, Rectangle, PointerEventListener, PointerEvent from src.ezgg_lan_manager import ConfigurationService from src.ezgg_lan_manager.components.MainViewContentBox import MainViewContentBox @@ -15,15 +15,11 @@ from src.ezgg_lan_manager.types.User import User class TeamsPage(Component): all_teams: Optional[list[Team]] = None user: Optional[User] = None - user_teams: list[Team] = [] @event.on_populate async def on_populate(self) -> None: self.all_teams = await self.session[TeamService].get_all_teams() - #self.user = await self.session[UserService].get_user(self.session[SessionStorage].user_id) - self.user = await self.session[UserService].get_user("Typhus") # FixMe: Only debug - if self.user is not None: - self.user_teams = await self.session[TeamService].get_teams_for_user_by_id(self.user.user_id) + self.user = await self.session[UserService].get_user(self.session[SessionStorage].user_id) await self.session.set_title(f"{self.session[ConfigurationService].get_lan_info().name} - Teams") self.session[SessionStorage].subscribe_to_logged_in_or_out_event(str(self.__class__), self.on_populate) @@ -55,7 +51,14 @@ class TeamsPage(Component): team_list = [] for team in self.all_teams: - team_list.append(TeamRevealer(user=self.user, team=team, on_join_button_pressed=self.on_join_button_pressed)) + team_list.append( + TeamRevealer( + user=self.user, + team=team, + mode="leave" if self.user in team.members.keys() else "join", + on_button_pressed=self.on_leave_button_pressed if self.user in team.members.keys() else self.on_join_button_pressed + ) + ) if team_list: team_list[-1].margin_bottom = 1 @@ -63,9 +66,9 @@ class TeamsPage(Component): own_teams_content = Spacer(grow_x=False, grow_y=False) if self.user is not None: user_team_list = [] - for team in self.user_teams: - # ToDo: Remove from team instead of join - user_team_list.append(TeamRevealer(user=self.user, team=team, on_join_button_pressed=self.on_join_button_pressed)) + for team in self.all_teams: + if self.user in team.members.keys(): + user_team_list.append(TeamRevealer(user=self.user, team=team, mode="leave", on_button_pressed=self.on_leave_button_pressed)) if not user_team_list: user_team_list.append(Text( diff --git a/src/ezgg_lan_manager/services/DatabaseService.py b/src/ezgg_lan_manager/services/DatabaseService.py index 14fb294..3d9a319 100644 --- a/src/ezgg_lan_manager/services/DatabaseService.py +++ b/src/ezgg_lan_manager/services/DatabaseService.py @@ -970,42 +970,200 @@ class DatabaseService: logger.warning(f"Error removing participant from tournament: {e}") async def get_teams(self) -> list[Team]: - # ToDo: Implement - return [ - Team( - id=1, - name="MockTeamAlpha", - abbreviation="-=MTA=-", - members={User(0, "DemoUserA", "", "abuvbwer", None, None, None, True, False, False, datetime.now(), datetime.now()): TeamStatus.LEADER}, - join_password="abc" - ), - Team( - id=1, - name="MockTeamBeta", - abbreviation="[MTB]", - members={ - User(1, "DemoUserB", "", "abuvbwer", None, None, None, True, False, False, datetime.now(), datetime.now()): TeamStatus.LEADER, - User(2, "DemoUserC", "", "abuvbwer", None, None, None, True, False, False, datetime.now(), datetime.now()): TeamStatus.OFFICER, - User(3, "DemoUserD", "", "abuvbwer", None, None, None, True, False, False, datetime.now(), datetime.now()): TeamStatus.MEMBER - }, - join_password="abc" - ) - ] + async with self._connection_pool.acquire() as conn: + async with conn.cursor(aiomysql.Cursor) as cursor: + query = """ + SELECT + t.id AS team_id, + t.name AS team_name, + t.abbreviation AS team_abbr, + t.join_password, + t.created_at AS team_created_at, + + tm.status AS team_status, + tm.joined_at AS member_joined_at, + + u.* + + FROM teams t + + LEFT JOIN team_members tm + ON t.id = tm.team_id + + LEFT JOIN users u + ON tm.user_id = u.user_id + + ORDER BY + t.id, + CASE tm.status + WHEN 'LEADER' THEN 1 + WHEN 'OFFICER' THEN 2 + WHEN 'MEMBER' THEN 3 + ELSE 4 + END, + u.user_name; + """ + try: + await cursor.execute(query) + await conn.commit() + except aiomysql.InterfaceError: + pool_init_result = await self.init_db_pool() + if not pool_init_result: + raise NoDatabaseConnectionError + return await self.get_teams() + except Exception as e: + logger.warning(f"Error getting teams: {e}") + return [] + + current_team: Optional[Team] = None + all_teams = [] + + for row in await cursor.fetchall(): + if row[5] is None: # Teams without single member are ignored + continue + if current_team is None: + user = self._map_db_result_to_user(row[7:]) + current_team = Team(id=row[0], name=row[1], abbreviation=row[2], join_password=row[3], members={user: TeamStatus.from_str(row[5])}) + elif current_team.id == row[0]: # Still same team + current_team.members[self._map_db_result_to_user(row[7:])] = TeamStatus.from_str(row[5]) + else: + all_teams.append(current_team) + user = self._map_db_result_to_user(row[7:]) + current_team = Team(id=row[0], name=row[1], abbreviation=row[2], join_password=row[3], members={user: TeamStatus.from_str(row[5])}) + + all_teams.append(current_team) + + return all_teams async def get_team_by_id(self, team_id: int) -> Optional[Team]: - return + async with self._connection_pool.acquire() as conn: + async with conn.cursor(aiomysql.Cursor) as cursor: + query = """ + SELECT + t.id AS team_id, + t.name AS team_name, + t.abbreviation AS team_abbr, + t.join_password, + t.created_at AS team_created_at, + + tm.status AS team_status, + tm.joined_at AS member_joined_at, + + u.* + + FROM teams t + + LEFT JOIN team_members tm + ON t.id = tm.team_id + + LEFT JOIN users u + ON tm.user_id = u.user_id + + WHERE t.id = %s + + ORDER BY + t.id, + CASE tm.status + WHEN 'LEADER' THEN 1 + WHEN 'OFFICER' THEN 2 + WHEN 'MEMBER' THEN 3 + ELSE 4 + END, + u.user_name; + """ + try: + await cursor.execute(query, (team_id, )) + await conn.commit() + except aiomysql.InterfaceError: + pool_init_result = await self.init_db_pool() + if not pool_init_result: + raise NoDatabaseConnectionError + return await self.get_team_by_id(team_id) + except Exception as e: + logger.warning(f"Error getting team: {e}") + return None - async def get_teams_for_user_by_id(self, user_id: int) -> list[Team]: - return [] + team: Optional[Team] = None + + for row in await cursor.fetchall(): + if team is None: + user = self._map_db_result_to_user(row[7:]) + team = Team(id=row[0], name=row[1], abbreviation=row[2], join_password=row[3], members={user: TeamStatus.from_str(row[5])}) + elif team.id == row[0]: + team.members[self._map_db_result_to_user(row[7:])] = TeamStatus.from_str(row[5]) + + return team async def create_team(self, team_name: str, team_abbr: str, join_password: str) -> Team: - return # ToDo: Raise on existing team + async with self._connection_pool.acquire() as conn: + async with conn.cursor(aiomysql.Cursor) as cursor: + try: + await cursor.execute( + "INSERT INTO teams (name, abbreviation, join_password) " + "VALUES (%s, %s, %s)", (team_name, team_abbr, join_password) + ) + await conn.commit() + return await self.get_team_by_id(cursor.lastrowid) + + except aiomysql.InterfaceError: + pool_init_result = await self.init_db_pool() + if not pool_init_result: + raise NoDatabaseConnectionError + return await self.create_team(team_name, team_abbr, join_password) + except aiomysql.IntegrityError as e: + logger.warning(f"Aborted duplication entry: {e}") + raise DuplicationError async def update_team(self, team: Team) -> Team: - pass + async with self._connection_pool.acquire() as conn: + async with conn.cursor(aiomysql.Cursor) as cursor: + try: + await cursor.execute( + "UPDATE teams SET name = %s, abbreviation = %s, join_password = %s WHERE (id = %s)", + (team.name, team.abbreviation, team.join_password, team.id) + ) + await conn.commit() + return await self.get_team_by_id(team.id) + except aiomysql.InterfaceError: + pool_init_result = await self.init_db_pool() + if not pool_init_result: + raise NoDatabaseConnectionError + return await self.update_team(team) + except aiomysql.IntegrityError as e: + logger.warning(f"Aborted duplication entry: {e}") + raise DuplicationError + + + async def add_member_to_team(self, team: Team, user: User, status: TeamStatus = TeamStatus.MEMBER) -> None: + async with self._connection_pool.acquire() as conn: + async with conn.cursor(aiomysql.Cursor) as cursor: + try: + await cursor.execute( + "INSERT INTO team_members (team_id, user_id, status) VALUES (%s, %s, %s)", + (team.id, user.user_id, status.name) + ) + await conn.commit() + except aiomysql.InterfaceError: + pool_init_result = await self.init_db_pool() + if not pool_init_result: + raise NoDatabaseConnectionError + return await self.add_member_to_team(team, user, status) + except aiomysql.IntegrityError as e: + logger.warning(f"Failed to add member {user.user_name} to team {team.name}: {e}") + raise DuplicationError - async def add_member_to_team(self, team: Team, user: User) -> None: - pass async def remove_user_from_team(self, team: Team, user: User) -> None: - pass + async with self._connection_pool.acquire() as conn: + async with conn.cursor(aiomysql.Cursor) as cursor: + try: + await cursor.execute( + "DELETE FROM team_members WHERE team_id = %s AND user_id = %s", + (team.id, user.user_id) + ) + await conn.commit() + except aiomysql.InterfaceError: + pool_init_result = await self.init_db_pool() + if not pool_init_result: + raise NoDatabaseConnectionError + return await self.remove_user_from_team(team, user) diff --git a/src/ezgg_lan_manager/services/TeamService.py b/src/ezgg_lan_manager/services/TeamService.py index 96e0194..043eef1 100644 --- a/src/ezgg_lan_manager/services/TeamService.py +++ b/src/ezgg_lan_manager/services/TeamService.py @@ -2,7 +2,7 @@ from hashlib import sha256 from typing import Union, Optional from string import ascii_letters, digits -from src.ezgg_lan_manager.services.DatabaseService import DatabaseService +from src.ezgg_lan_manager.services.DatabaseService import DatabaseService, DuplicationError from src.ezgg_lan_manager.types.User import User from src.ezgg_lan_manager.types.Team import TeamStatus, Team @@ -23,9 +23,24 @@ class TeamLeadRemovalError(Exception): def __init__(self) -> None: pass +class TeamNameTooLongError(Exception): + def __init__(self) -> None: + pass + +class TeamNameAlreadyTaken(Exception): + def __init__(self) -> None: + pass + +class TeamAbbrInvalidError(Exception): + def __init__(self) -> None: + pass + + class TeamService: ALLOWED_TEAM_NAME_SYMBOLS = ascii_letters + digits + "!#$%&*+,-./:;<=>?[]^_{|}~" + MAX_TEAM_NAME_LENGTH = 24 + MAX_TEAM_ABBR_LENGTH = 8 def __init__(self, db_service: DatabaseService) -> None: self._db_service = db_service @@ -37,7 +52,12 @@ class TeamService: return await self._db_service.get_team_by_id(team_id) async def get_teams_for_user_by_id(self, user_id: int) -> list[Team]: - return await self._db_service.get_teams_for_user_by_id(user_id) + all_teams = await self.get_all_teams() + user_teams = [] + for team in all_teams: + if user_id in [u.user_id for u in team.members.keys()]: + user_teams.append(team) + return user_teams async def create_team(self, team_name: str, team_abbr: str, join_password: str) -> Team: disallowed_char = self._check_for_disallowed_char(team_name) @@ -47,7 +67,16 @@ class TeamService: if disallowed_char: raise NameNotAllowedError(disallowed_char) - created_team = await self._db_service.create_team(team_name, team_abbr, join_password) + if not team_name or len(team_name) > self.MAX_TEAM_NAME_LENGTH: + raise TeamNameTooLongError() + + if not team_abbr or len(team_abbr) > self.MAX_TEAM_ABBR_LENGTH: + raise TeamAbbrInvalidError() + + try: + created_team = await self._db_service.create_team(team_name, team_abbr, join_password) + except DuplicationError: + raise TeamNameAlreadyTaken return created_team async def update_team(self, team: Team) -> Team: @@ -62,6 +91,13 @@ class TeamService: disallowed_char = self._check_for_disallowed_char(team.abbreviation) if disallowed_char: raise NameNotAllowedError(disallowed_char) + + if not team.name or len(team.name) > self.MAX_TEAM_NAME_LENGTH: + raise TeamNameTooLongError() + + if not team.abbreviation or len(team.abbreviation) > self.MAX_TEAM_ABBR_LENGTH: + raise TeamAbbrInvalidError() + return await self._db_service.update_team(team) async def add_member_to_team(self, team: Team, user: User) -> Team: @@ -75,7 +111,7 @@ class TeamService: if user not in team.members: raise NotMemberError() - if team.members[user] is TeamStatus.LEADER: + if team.members[user] == TeamStatus.LEADER: raise TeamLeadRemovalError() await self._db_service.remove_user_from_team(team, user) diff --git a/src/ezgg_lan_manager/types/Team.py b/src/ezgg_lan_manager/types/Team.py index b4063f8..aed4454 100644 --- a/src/ezgg_lan_manager/types/Team.py +++ b/src/ezgg_lan_manager/types/Team.py @@ -1,5 +1,6 @@ from dataclasses import dataclass from enum import Enum +from typing import Self from src.ezgg_lan_manager.types.User import User @@ -8,6 +9,16 @@ class TeamStatus(Enum): OFFICER = 1 LEADER = 2 + @classmethod + def from_str(cls, team_status: str) -> Self: + if team_status == "MEMBER": + return TeamStatus.MEMBER + elif team_status == "OFFICER": + return TeamStatus.OFFICER + elif team_status == "LEADER": + return TeamStatus.LEADER + raise ValueError + @dataclass(frozen=True) class Team: diff --git a/src/ezgg_lan_manager/types/User.py b/src/ezgg_lan_manager/types/User.py index a397962..d91f6d5 100644 --- a/src/ezgg_lan_manager/types/User.py +++ b/src/ezgg_lan_manager/types/User.py @@ -19,4 +19,9 @@ class User: last_updated_at: datetime def __hash__(self) -> int: - return hash(f"{self.user_id}{self.user_name}{self.user_mail}") + return hash(self.user_id) + + def __eq__(self, other): + if not isinstance(other, User): + return NotImplemented + return self.user_id == other.user_id -- 2.45.2 From 6cc77f26b5a5e8f90d50a29766b0ea263a364290 Mon Sep 17 00:00:00 2001 From: David Rodenkirchen Date: Sun, 15 Feb 2026 01:02:01 +0100 Subject: [PATCH 5/6] finalize core feature --- .../components/TeamsDialogHandler.py | 222 ++++++++++++++++++ src/ezgg_lan_manager/pages/TeamsPage.py | 84 +++++-- .../services/DatabaseService.py | 10 +- src/ezgg_lan_manager/services/TeamService.py | 22 +- 4 files changed, 303 insertions(+), 35 deletions(-) create mode 100644 src/ezgg_lan_manager/components/TeamsDialogHandler.py diff --git a/src/ezgg_lan_manager/components/TeamsDialogHandler.py b/src/ezgg_lan_manager/components/TeamsDialogHandler.py new file mode 100644 index 0000000..10a9532 --- /dev/null +++ b/src/ezgg_lan_manager/components/TeamsDialogHandler.py @@ -0,0 +1,222 @@ +import logging +from typing import Optional, Callable + +from rio import Component, Text, Spacer, Rectangle, Column, TextStyle, Row, Button, TextInput, ThemeContextSwitcher + +from src.ezgg_lan_manager.services.TeamService import TeamService, NotMemberError, TeamLeadRemovalError, AlreadyMemberError, NameNotAllowedError, TeamNameTooLongError, \ + TeamAbbrInvalidError, TeamNameAlreadyTaken +from src.ezgg_lan_manager.types.Team import Team +from src.ezgg_lan_manager.types.User import User + +logger = logging.getLogger(__name__.split(".")[-1]) + + +class ErrorBox(Component): + error_message: str + cancel: Callable + + def build(self) -> Component: + return Rectangle( + content=Column( + Text(self.error_message, style=TextStyle(fill=self.session.theme.background_color), margin_bottom=1.5), + Row( + Button( + content="Ok", + shape="rectangle", + style="major", + color="hud", + on_press=self.cancel, + ) + ), + margin=1 + ), + fill=self.session.theme.primary_color + ) + + +class TeamsDialogJoinHandler(Component): + is_active: bool + cancel: Callable + user: Optional[User] = None + team: Optional[Team] = None + error_message: Optional[str] = None + password: str = "" + + async def join(self) -> None: + if self.user is None or self.team is None: + return + + if self.password != self.team.join_password: + self.error_message = "Falsches Passwort!" + return + + try: + await self.session[TeamService].add_member_to_team(self.team, self.user) + except AlreadyMemberError: + self.error_message = "Du bist bereits Mitglied dieses Teams" + else: + await self.cancel_with_reset() + + async def cancel_with_reset(self) -> None: + await self.cancel() + self.error_message = None + self.password = "" + + def build(self) -> Component: + if not self.is_active or self.user is None or self.team is None: + return Spacer() + + if self.error_message is not None: + return ErrorBox(error_message=self.error_message, cancel=self.cancel_with_reset) + + return Rectangle( + content=Column( + Text(f"Team {self.team.name} beitreten", style=TextStyle(fill=self.session.theme.background_color), margin_bottom=1, justify="center"), + ThemeContextSwitcher(content=TextInput(text=self.bind().password, label="Beitrittspasswort", margin_bottom=1), color="secondary"), + Row( + Button( + content="Abbrechen", + shape="rectangle", + style="major", + color=self.session.theme.danger_color, + on_press=self.cancel_with_reset, + ), + Button( + content="Beitreten", + shape="rectangle", + style="major", + color=self.session.theme.success_color, + on_press=self.join, + ), + spacing=1 + ), + margin=1 + ), + fill=self.session.theme.primary_color + ) + + +class TeamsDialogLeaveHandler(Component): + is_active: bool + cancel: Callable + user: Optional[User] = None + team: Optional[Team] = None + error_message: Optional[str] = None + + async def leave(self) -> None: + if self.user is not None and self.team is not None: + try: + await self.session[TeamService].remove_member_from_team(self.team, self.user) + except NotMemberError: + self.error_message = "Du bist kein Mitglied in diesem Team" + except TeamLeadRemovalError: + self.error_message = "Als Teamleiter kannst du das Team nicht verlassen" + else: + await self.cancel_with_reset() + + async def cancel_with_reset(self) -> None: + await self.cancel() + self.error_message = None + + def build(self) -> Component: + if not self.is_active or self.user is None or self.team is None: + return Spacer() + + if self.error_message is not None: + return ErrorBox(error_message=self.error_message, cancel=self.cancel_with_reset) + + return Rectangle( + content=Column( + Text(f"Team {self.team.name} wirklich verlassen?", style=TextStyle(fill=self.session.theme.background_color), margin_bottom=1.5, justify="center"), + Row( + Button( + content="Nein", + shape="rectangle", + style="major", + color=self.session.theme.danger_color, + on_press=self.cancel_with_reset, + ), + Button( + content="Ja", + shape="rectangle", + style="major", + color=self.session.theme.success_color, + on_press=self.leave, + ), + spacing=1 + ), + margin=1 + ), + fill=self.session.theme.primary_color + ) + + +class TeamsDialogCreateHandler(Component): + is_active: bool + cancel: Callable + user: Optional[User] = None + error_message: Optional[str] = None + team_name: str = "" + team_abbr: str = "" + team_join_password: str = "" + + async def cancel_with_reset(self) -> None: + await self.cancel() + self.error_message = None + self.team_name, self.team_abbr, self.team_join_password = "", "", "" + + async def create(self) -> None: + if self.user is None: + return + + if not self.team_name or not self.team_abbr or not self.team_join_password: + self.error_message = "Angaben unvollständig" + return + + try: + await self.session[TeamService].create_team(self.team_name, self.team_abbr, self.team_join_password, self.user) + except NameNotAllowedError as e: + self.error_message = f"Angaben ungültig. Darf kein '{e.disallowed_char}' enthalten." + except TeamNameTooLongError: + self.error_message = f"Name zu lang. Maximal {TeamService.MAX_TEAM_NAME_LENGTH} Zeichen." + except TeamAbbrInvalidError: + self.error_message = f"Name zu lang. Maximal {TeamService.MAX_TEAM_ABBR_LENGTH} Zeichen." + except TeamNameAlreadyTaken: + self.error_message = "Ein Team mit diesem Namen existiert bereits." + else: + await self.cancel_with_reset() + + def build(self) -> Component: + if not self.is_active or self.user is None: + return Spacer() + + if self.error_message is not None: + return ErrorBox(error_message=self.error_message, cancel=self.cancel_with_reset) + + return Rectangle( + content=Column( + Text(f"Team gründen", style=TextStyle(fill=self.session.theme.background_color), margin_bottom=1.5, justify="center"), + ThemeContextSwitcher(content=TextInput(text=self.bind().team_name, label="Team Name", margin_bottom=1), color="secondary"), + ThemeContextSwitcher(content=TextInput(text=self.bind().team_abbr, label="Team Abkürzung", margin_bottom=1), color="secondary"), + ThemeContextSwitcher(content=TextInput(text=self.bind().team_join_password, label="Beitrittspasswort", margin_bottom=1), color="secondary"), + Row( + Button( + content="Abbrechen", + shape="rectangle", + style="major", + color=self.session.theme.danger_color, + on_press=self.cancel_with_reset, + ), + Button( + content="Gründen", + shape="rectangle", + style="major", + color=self.session.theme.success_color, + on_press=self.create, + ), + spacing=1 + ), + margin=1 + ), + fill=self.session.theme.primary_color + ) diff --git a/src/ezgg_lan_manager/pages/TeamsPage.py b/src/ezgg_lan_manager/pages/TeamsPage.py index 397c2d3..b6c94a6 100644 --- a/src/ezgg_lan_manager/pages/TeamsPage.py +++ b/src/ezgg_lan_manager/pages/TeamsPage.py @@ -1,10 +1,11 @@ -from typing import Optional +from asyncio import sleep -from rio import Column, Component, event, Text, TextStyle, ProgressCircle, Spacer, Row, Rectangle, PointerEventListener, PointerEvent +from rio import event, ProgressCircle, PointerEventListener, PointerEvent, Popup, Color from src.ezgg_lan_manager import ConfigurationService from src.ezgg_lan_manager.components.MainViewContentBox import MainViewContentBox from src.ezgg_lan_manager.components.TeamRevealer import TeamRevealer +from src.ezgg_lan_manager.components.TeamsDialogHandler import * from src.ezgg_lan_manager.services.TeamService import TeamService from src.ezgg_lan_manager.services.UserService import UserService from src.ezgg_lan_manager.types.SessionStorage import SessionStorage @@ -16,6 +17,13 @@ class TeamsPage(Component): all_teams: Optional[list[Team]] = None user: Optional[User] = None + # Dialog handling + popup_open: bool = False + join_active: bool = False + leave_active: bool = True + create_active: bool = False + selected_team_for_join_or_leave: Optional[Team] = None + @event.on_populate async def on_populate(self) -> None: self.all_teams = await self.session[TeamService].get_all_teams() @@ -24,16 +32,31 @@ class TeamsPage(Component): self.session[SessionStorage].subscribe_to_logged_in_or_out_event(str(self.__class__), self.on_populate) async def on_join_button_pressed(self, team: Team) -> None: - # ToDo: handle joining by displaying password popup - print(f"Starting join process for team {team.abbreviation}") + if self.user is None: + return + self.selected_team_for_join_or_leave = team + self.join_active, self.leave_active, self.create_active = True, False, False + self.popup_open = True async def on_leave_button_pressed(self, team: Team) -> None: - # ToDo: handle leaving - print(f"Starting leaving process for team {team.abbreviation}") + if self.user is None: + return + self.selected_team_for_join_or_leave = team + self.join_active, self.leave_active, self.create_active = False, True, False + self.popup_open = True async def on_create_button_pressed(self, _: PointerEvent) -> None: - # ToDo: Add Popup creation dialog - print(f"Starting creation process for team") + if self.user is None: + return + self.join_active, self.leave_active, self.create_active = False, False, True + self.popup_open = True + + async def popup_action_cancelled(self) -> None: + self.popup_open = False + await sleep(0.2) # Waits for the animation to play before resetting its contents + self.join_active, self.leave_active, self.create_active = False, False, False + self.selected_team_for_join_or_leave = None + self.all_teams = await self.session[TeamService].get_all_teams() def build(self) -> Component: if self.all_teams is None: @@ -115,22 +138,35 @@ class TeamsPage(Component): ) ) - return Column( - own_teams_content, - MainViewContentBox( - Column( - Text( - text="Alle Teams", - style=TextStyle( - fill=self.session.theme.background_color, - font_size=1.2 + return Popup( + anchor=Column( + own_teams_content, + MainViewContentBox( + Column( + Text( + text="Alle Teams", + style=TextStyle( + fill=self.session.theme.background_color, + font_size=1.2 + ), + margin_top=1, + margin_bottom=1, + align_x=0.5 ), - margin_top=1, - margin_bottom=1, - align_x=0.5 - ), - *team_list - ) + *team_list + ) + ), + align_y=0, ), - align_y=0 + content=Column( + TeamsDialogJoinHandler(is_active=self.join_active, cancel=self.popup_action_cancelled, user=self.user, team=self.selected_team_for_join_or_leave), + TeamsDialogLeaveHandler(is_active=self.leave_active, cancel=self.popup_action_cancelled, user=self.user, team=self.selected_team_for_join_or_leave), + TeamsDialogCreateHandler(is_active=self.create_active, cancel=self.popup_action_cancelled, user=self.user) + ), + is_open=self.popup_open, + modal=False, + corner_radius=(0.5, 0.5, 0.5, 0.5), + color=Color.TRANSPARENT, + user_closable=False, + position="top" ) diff --git a/src/ezgg_lan_manager/services/DatabaseService.py b/src/ezgg_lan_manager/services/DatabaseService.py index 3d9a319..efecffe 100644 --- a/src/ezgg_lan_manager/services/DatabaseService.py +++ b/src/ezgg_lan_manager/services/DatabaseService.py @@ -1094,7 +1094,7 @@ class DatabaseService: return team - async def create_team(self, team_name: str, team_abbr: str, join_password: str) -> Team: + async def create_team(self, team_name: str, team_abbr: str, join_password: str, leader: User) -> Team: async with self._connection_pool.acquire() as conn: async with conn.cursor(aiomysql.Cursor) as cursor: try: @@ -1103,7 +1103,13 @@ class DatabaseService: "VALUES (%s, %s, %s)", (team_name, team_abbr, join_password) ) await conn.commit() - return await self.get_team_by_id(cursor.lastrowid) + team_id = cursor.lastrowid + await cursor.execute( + "INSERT INTO team_members (team_id, user_id, status) VALUES (%s, %s, %s)", + (team_id, leader.user_id, TeamStatus.LEADER.name) + ) + await conn.commit() + return await self.get_team_by_id(team_id) except aiomysql.InterfaceError: pool_init_result = await self.init_db_pool() diff --git a/src/ezgg_lan_manager/services/TeamService.py b/src/ezgg_lan_manager/services/TeamService.py index 043eef1..b9732fa 100644 --- a/src/ezgg_lan_manager/services/TeamService.py +++ b/src/ezgg_lan_manager/services/TeamService.py @@ -1,44 +1,48 @@ -from hashlib import sha256 -from typing import Union, Optional from string import ascii_letters, digits +from typing import Optional from src.ezgg_lan_manager.services.DatabaseService import DatabaseService, DuplicationError -from src.ezgg_lan_manager.types.User import User from src.ezgg_lan_manager.types.Team import TeamStatus, Team +from src.ezgg_lan_manager.types.User import User class NameNotAllowedError(Exception): def __init__(self, disallowed_char: str) -> None: self.disallowed_char = disallowed_char + class AlreadyMemberError(Exception): def __init__(self) -> None: pass + class NotMemberError(Exception): def __init__(self) -> None: pass + class TeamLeadRemovalError(Exception): def __init__(self) -> None: pass + class TeamNameTooLongError(Exception): def __init__(self) -> None: pass + class TeamNameAlreadyTaken(Exception): def __init__(self) -> None: pass + class TeamAbbrInvalidError(Exception): def __init__(self) -> None: pass - class TeamService: - ALLOWED_TEAM_NAME_SYMBOLS = ascii_letters + digits + "!#$%&*+,-./:;<=>?[]^_{|}~" + ALLOWED_TEAM_NAME_SYMBOLS = ascii_letters + digits + "!#$%&*+,-./:;<=>?[]^_{|}~ " MAX_TEAM_NAME_LENGTH = 24 MAX_TEAM_ABBR_LENGTH = 8 @@ -59,7 +63,7 @@ class TeamService: user_teams.append(team) return user_teams - async def create_team(self, team_name: str, team_abbr: str, join_password: str) -> Team: + async def create_team(self, team_name: str, team_abbr: str, join_password: str, leader: User) -> Team: disallowed_char = self._check_for_disallowed_char(team_name) if disallowed_char: raise NameNotAllowedError(disallowed_char) @@ -74,7 +78,7 @@ class TeamService: raise TeamAbbrInvalidError() try: - created_team = await self._db_service.create_team(team_name, team_abbr, join_password) + created_team = await self._db_service.create_team(team_name, team_abbr, join_password, leader) except DuplicationError: raise TeamNameAlreadyTaken return created_team @@ -100,11 +104,11 @@ class TeamService: return await self._db_service.update_team(team) - async def add_member_to_team(self, team: Team, user: User) -> Team: + async def add_member_to_team(self, team: Team, user: User, status: TeamStatus = TeamStatus.MEMBER) -> Team: if user in team.members: raise AlreadyMemberError() - await self._db_service.add_member_to_team(team, user) + await self._db_service.add_member_to_team(team, user, status) return await self.get_team_by_id(team.id) async def remove_member_from_team(self, team: Team, user: User) -> Team: -- 2.45.2 From e621597a94bf2eb46b308524c1719ab4fd72c7b9 Mon Sep 17 00:00:00 2001 From: David Rodenkirchen Date: Sun, 15 Feb 2026 01:15:35 +0100 Subject: [PATCH 6/6] cleanup --- README.md | 9 ++++++--- VERSION | 2 +- sql/{tournament_patch.sql => 01-tournament_patch.sql} | 0 sql/{teams_patch.sql => 02-teams_patch.sql} | 0 4 files changed, 7 insertions(+), 4 deletions(-) rename sql/{tournament_patch.sql => 01-tournament_patch.sql} (100%) rename sql/{teams_patch.sql => 02-teams_patch.sql} (100%) diff --git a/README.md b/README.md index 9d70fae..f2c20b9 100644 --- a/README.md +++ b/README.md @@ -8,13 +8,16 @@ This repository contains the code for the EZGG LAN Manager. ### Prerequisites -- Working Installation of MySQL 5 or latest MariaDB Server (`mariadb-server` for Debian-based Linux, `XAMPP` for Windows) +- Working Installation of MariaDB Server (version `10.6.25` or later) + + MySQL should work too, but there are no guarantees. - Python 3.9 or higher - PyCharm or similar IDE (optional) ### Step 1: Preparing Database -To prepare the database, apply the SQL file located in `sql/create_database.sql` followed by `sql/tournament_patch.sql` to your database server. This is easily accomplished with the MYSQL Workbench, but it can be also done by pipeing the file into the mariadb-server executable. +To prepare the database, apply the SQL file located in `sql/create_database.sql` to your database server. This is easily accomplished with the MYSQL Workbench, but it can be also done by piping the file into the mariadb-server executable. + +After creating the database, apply all patches found in `sql/*_patch.sql` in their numeric order. Optionally, you can now execute the script `create_demo_database_content.py`, found in `src/ezgg_lan_manager/helpers`. Be aware that it can be buggy sometimes, especially if you overwrite existing data. @@ -43,4 +46,4 @@ FLUSH PRIVILEGES; ``` 3. Make sure to **NOT** use the default passwords! 4. Apply the `create_database.sql` when starting the MariaDB container for the first time. -5. Apply the `tournament_patch.sql` when starting the MariaDB container for the first time. +5. Apply the patches (`sql/*_patch.sql`) when starting the MariaDB container for the first time. diff --git a/VERSION b/VERSION index f477849..9325c3c 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -0.2.2 \ No newline at end of file +0.3.0 \ No newline at end of file diff --git a/sql/tournament_patch.sql b/sql/01-tournament_patch.sql similarity index 100% rename from sql/tournament_patch.sql rename to sql/01-tournament_patch.sql diff --git a/sql/teams_patch.sql b/sql/02-teams_patch.sql similarity index 100% rename from sql/teams_patch.sql rename to sql/02-teams_patch.sql -- 2.45.2