initial
This commit is contained in:
23
backend/app/models/__init__.py
Normal file
23
backend/app/models/__init__.py
Normal file
@@ -0,0 +1,23 @@
|
||||
from app.models.user import User
|
||||
from app.models.marathon import Marathon, MarathonStatus
|
||||
from app.models.participant import Participant
|
||||
from app.models.game import Game
|
||||
from app.models.challenge import Challenge, ChallengeType, Difficulty, ProofType
|
||||
from app.models.assignment import Assignment, AssignmentStatus
|
||||
from app.models.activity import Activity, ActivityType
|
||||
|
||||
__all__ = [
|
||||
"User",
|
||||
"Marathon",
|
||||
"MarathonStatus",
|
||||
"Participant",
|
||||
"Game",
|
||||
"Challenge",
|
||||
"ChallengeType",
|
||||
"Difficulty",
|
||||
"ProofType",
|
||||
"Assignment",
|
||||
"AssignmentStatus",
|
||||
"Activity",
|
||||
"ActivityType",
|
||||
]
|
||||
30
backend/app/models/activity.py
Normal file
30
backend/app/models/activity.py
Normal file
@@ -0,0 +1,30 @@
|
||||
from datetime import datetime
|
||||
from enum import Enum
|
||||
from sqlalchemy import String, DateTime, ForeignKey, JSON
|
||||
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||
|
||||
from app.core.database import Base
|
||||
|
||||
|
||||
class ActivityType(str, Enum):
|
||||
JOIN = "join"
|
||||
SPIN = "spin"
|
||||
COMPLETE = "complete"
|
||||
DROP = "drop"
|
||||
START_MARATHON = "start_marathon"
|
||||
FINISH_MARATHON = "finish_marathon"
|
||||
|
||||
|
||||
class Activity(Base):
|
||||
__tablename__ = "activities"
|
||||
|
||||
id: Mapped[int] = mapped_column(primary_key=True)
|
||||
marathon_id: Mapped[int] = mapped_column(ForeignKey("marathons.id", ondelete="CASCADE"), index=True)
|
||||
user_id: Mapped[int] = mapped_column(ForeignKey("users.id", ondelete="CASCADE"))
|
||||
type: Mapped[str] = mapped_column(String(30), nullable=False)
|
||||
data: Mapped[dict | None] = mapped_column(JSON, nullable=True)
|
||||
created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow, index=True)
|
||||
|
||||
# Relationships
|
||||
marathon: Mapped["Marathon"] = relationship("Marathon", back_populates="activities")
|
||||
user: Mapped["User"] = relationship("User")
|
||||
32
backend/app/models/assignment.py
Normal file
32
backend/app/models/assignment.py
Normal file
@@ -0,0 +1,32 @@
|
||||
from datetime import datetime
|
||||
from enum import Enum
|
||||
from sqlalchemy import String, Text, DateTime, ForeignKey, Integer
|
||||
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||
|
||||
from app.core.database import Base
|
||||
|
||||
|
||||
class AssignmentStatus(str, Enum):
|
||||
ACTIVE = "active"
|
||||
COMPLETED = "completed"
|
||||
DROPPED = "dropped"
|
||||
|
||||
|
||||
class Assignment(Base):
|
||||
__tablename__ = "assignments"
|
||||
|
||||
id: Mapped[int] = mapped_column(primary_key=True)
|
||||
participant_id: Mapped[int] = mapped_column(ForeignKey("participants.id", ondelete="CASCADE"), index=True)
|
||||
challenge_id: Mapped[int] = mapped_column(ForeignKey("challenges.id", ondelete="CASCADE"))
|
||||
status: Mapped[str] = mapped_column(String(20), default=AssignmentStatus.ACTIVE.value)
|
||||
proof_path: Mapped[str | None] = mapped_column(String(500), nullable=True)
|
||||
proof_url: Mapped[str | None] = mapped_column(Text, nullable=True)
|
||||
proof_comment: Mapped[str | None] = mapped_column(Text, nullable=True)
|
||||
points_earned: Mapped[int] = mapped_column(Integer, default=0)
|
||||
streak_at_completion: Mapped[int | None] = mapped_column(Integer, nullable=True)
|
||||
started_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow)
|
||||
completed_at: Mapped[datetime | None] = mapped_column(DateTime, nullable=True)
|
||||
|
||||
# Relationships
|
||||
participant: Mapped["Participant"] = relationship("Participant", back_populates="assignments")
|
||||
challenge: Mapped["Challenge"] = relationship("Challenge", back_populates="assignments")
|
||||
53
backend/app/models/challenge.py
Normal file
53
backend/app/models/challenge.py
Normal file
@@ -0,0 +1,53 @@
|
||||
from datetime import datetime
|
||||
from enum import Enum
|
||||
from sqlalchemy import String, Text, DateTime, ForeignKey, Integer, Boolean
|
||||
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||
|
||||
from app.core.database import Base
|
||||
|
||||
|
||||
class ChallengeType(str, Enum):
|
||||
COMPLETION = "completion"
|
||||
NO_DEATH = "no_death"
|
||||
SPEEDRUN = "speedrun"
|
||||
COLLECTION = "collection"
|
||||
ACHIEVEMENT = "achievement"
|
||||
CHALLENGE_RUN = "challenge_run"
|
||||
SCORE_ATTACK = "score_attack"
|
||||
TIME_TRIAL = "time_trial"
|
||||
|
||||
|
||||
class Difficulty(str, Enum):
|
||||
EASY = "easy"
|
||||
MEDIUM = "medium"
|
||||
HARD = "hard"
|
||||
|
||||
|
||||
class ProofType(str, Enum):
|
||||
SCREENSHOT = "screenshot"
|
||||
VIDEO = "video"
|
||||
STEAM = "steam"
|
||||
|
||||
|
||||
class Challenge(Base):
|
||||
__tablename__ = "challenges"
|
||||
|
||||
id: Mapped[int] = mapped_column(primary_key=True)
|
||||
game_id: Mapped[int] = mapped_column(ForeignKey("games.id", ondelete="CASCADE"), index=True)
|
||||
title: Mapped[str] = mapped_column(String(100), nullable=False)
|
||||
description: Mapped[str] = mapped_column(Text, nullable=False)
|
||||
type: Mapped[str] = mapped_column(String(30), nullable=False)
|
||||
difficulty: Mapped[str] = mapped_column(String(10), nullable=False)
|
||||
points: Mapped[int] = mapped_column(Integer, nullable=False)
|
||||
estimated_time: Mapped[int | None] = mapped_column(Integer, nullable=True) # in minutes
|
||||
proof_type: Mapped[str] = mapped_column(String(20), nullable=False)
|
||||
proof_hint: Mapped[str | None] = mapped_column(Text, nullable=True)
|
||||
is_generated: Mapped[bool] = mapped_column(Boolean, default=True)
|
||||
created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow)
|
||||
|
||||
# Relationships
|
||||
game: Mapped["Game"] = relationship("Game", back_populates="challenges")
|
||||
assignments: Mapped[list["Assignment"]] = relationship(
|
||||
"Assignment",
|
||||
back_populates="challenge"
|
||||
)
|
||||
27
backend/app/models/game.py
Normal file
27
backend/app/models/game.py
Normal file
@@ -0,0 +1,27 @@
|
||||
from datetime import datetime
|
||||
from sqlalchemy import String, DateTime, ForeignKey, Text
|
||||
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||
|
||||
from app.core.database import Base
|
||||
|
||||
|
||||
class Game(Base):
|
||||
__tablename__ = "games"
|
||||
|
||||
id: Mapped[int] = mapped_column(primary_key=True)
|
||||
marathon_id: Mapped[int] = mapped_column(ForeignKey("marathons.id", ondelete="CASCADE"), index=True)
|
||||
title: Mapped[str] = mapped_column(String(100), nullable=False)
|
||||
cover_path: Mapped[str | None] = mapped_column(String(500), nullable=True)
|
||||
download_url: Mapped[str] = mapped_column(Text, nullable=False)
|
||||
genre: Mapped[str | None] = mapped_column(String(50), nullable=True)
|
||||
added_by_id: Mapped[int] = mapped_column(ForeignKey("users.id", ondelete="SET NULL"), nullable=True)
|
||||
created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow)
|
||||
|
||||
# Relationships
|
||||
marathon: Mapped["Marathon"] = relationship("Marathon", back_populates="games")
|
||||
added_by_user: Mapped["User"] = relationship("User", back_populates="added_games")
|
||||
challenges: Mapped[list["Challenge"]] = relationship(
|
||||
"Challenge",
|
||||
back_populates="game",
|
||||
cascade="all, delete-orphan"
|
||||
)
|
||||
48
backend/app/models/marathon.py
Normal file
48
backend/app/models/marathon.py
Normal file
@@ -0,0 +1,48 @@
|
||||
from datetime import datetime
|
||||
from enum import Enum
|
||||
from sqlalchemy import String, Text, DateTime, ForeignKey, Integer
|
||||
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||
|
||||
from app.core.database import Base
|
||||
|
||||
|
||||
class MarathonStatus(str, Enum):
|
||||
PREPARING = "preparing"
|
||||
ACTIVE = "active"
|
||||
FINISHED = "finished"
|
||||
|
||||
|
||||
class Marathon(Base):
|
||||
__tablename__ = "marathons"
|
||||
|
||||
id: Mapped[int] = mapped_column(primary_key=True)
|
||||
title: Mapped[str] = mapped_column(String(100), nullable=False)
|
||||
description: Mapped[str | None] = mapped_column(Text, nullable=True)
|
||||
organizer_id: Mapped[int] = mapped_column(ForeignKey("users.id", ondelete="CASCADE"))
|
||||
status: Mapped[str] = mapped_column(String(20), default=MarathonStatus.PREPARING.value)
|
||||
invite_code: Mapped[str] = mapped_column(String(20), unique=True, nullable=False, index=True)
|
||||
start_date: Mapped[datetime | None] = mapped_column(DateTime, nullable=True)
|
||||
end_date: Mapped[datetime | None] = mapped_column(DateTime, nullable=True)
|
||||
created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow)
|
||||
|
||||
# Relationships
|
||||
organizer: Mapped["User"] = relationship(
|
||||
"User",
|
||||
back_populates="organized_marathons",
|
||||
foreign_keys=[organizer_id]
|
||||
)
|
||||
participants: Mapped[list["Participant"]] = relationship(
|
||||
"Participant",
|
||||
back_populates="marathon",
|
||||
cascade="all, delete-orphan"
|
||||
)
|
||||
games: Mapped[list["Game"]] = relationship(
|
||||
"Game",
|
||||
back_populates="marathon",
|
||||
cascade="all, delete-orphan"
|
||||
)
|
||||
activities: Mapped[list["Activity"]] = relationship(
|
||||
"Activity",
|
||||
back_populates="marathon",
|
||||
cascade="all, delete-orphan"
|
||||
)
|
||||
29
backend/app/models/participant.py
Normal file
29
backend/app/models/participant.py
Normal file
@@ -0,0 +1,29 @@
|
||||
from datetime import datetime
|
||||
from sqlalchemy import DateTime, ForeignKey, Integer, UniqueConstraint
|
||||
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||
|
||||
from app.core.database import Base
|
||||
|
||||
|
||||
class Participant(Base):
|
||||
__tablename__ = "participants"
|
||||
__table_args__ = (
|
||||
UniqueConstraint("user_id", "marathon_id", name="unique_user_marathon"),
|
||||
)
|
||||
|
||||
id: Mapped[int] = mapped_column(primary_key=True)
|
||||
user_id: Mapped[int] = mapped_column(ForeignKey("users.id", ondelete="CASCADE"), index=True)
|
||||
marathon_id: Mapped[int] = mapped_column(ForeignKey("marathons.id", ondelete="CASCADE"), index=True)
|
||||
total_points: Mapped[int] = mapped_column(Integer, default=0)
|
||||
current_streak: Mapped[int] = mapped_column(Integer, default=0)
|
||||
drop_count: Mapped[int] = mapped_column(Integer, default=0) # For progressive penalty
|
||||
joined_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow)
|
||||
|
||||
# Relationships
|
||||
user: Mapped["User"] = relationship("User", back_populates="participations")
|
||||
marathon: Mapped["Marathon"] = relationship("Marathon", back_populates="participants")
|
||||
assignments: Mapped[list["Assignment"]] = relationship(
|
||||
"Assignment",
|
||||
back_populates="participant",
|
||||
cascade="all, delete-orphan"
|
||||
)
|
||||
33
backend/app/models/user.py
Normal file
33
backend/app/models/user.py
Normal file
@@ -0,0 +1,33 @@
|
||||
from datetime import datetime
|
||||
from sqlalchemy import String, BigInteger, DateTime
|
||||
from sqlalchemy.orm import Mapped, mapped_column, relationship
|
||||
|
||||
from app.core.database import Base
|
||||
|
||||
|
||||
class User(Base):
|
||||
__tablename__ = "users"
|
||||
|
||||
id: Mapped[int] = mapped_column(primary_key=True)
|
||||
login: Mapped[str] = mapped_column(String(50), unique=True, nullable=False, index=True)
|
||||
password_hash: Mapped[str] = mapped_column(String(255), nullable=False)
|
||||
nickname: Mapped[str] = mapped_column(String(50), nullable=False)
|
||||
avatar_path: Mapped[str | None] = mapped_column(String(500), nullable=True)
|
||||
telegram_id: Mapped[int | None] = mapped_column(BigInteger, unique=True, nullable=True)
|
||||
telegram_username: Mapped[str | None] = mapped_column(String(50), nullable=True)
|
||||
created_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.utcnow)
|
||||
|
||||
# Relationships
|
||||
organized_marathons: Mapped[list["Marathon"]] = relationship(
|
||||
"Marathon",
|
||||
back_populates="organizer",
|
||||
foreign_keys="Marathon.organizer_id"
|
||||
)
|
||||
participations: Mapped[list["Participant"]] = relationship(
|
||||
"Participant",
|
||||
back_populates="user"
|
||||
)
|
||||
added_games: Mapped[list["Game"]] = relationship(
|
||||
"Game",
|
||||
back_populates="added_by_user"
|
||||
)
|
||||
Reference in New Issue
Block a user