2022-05-29 03:33:44 +00:00
|
|
|
import time
|
2022-11-15 09:19:08 +00:00
|
|
|
|
|
|
|
from sqlalchemy import Column, ForeignKey
|
|
|
|
from sqlalchemy.sql.sqltypes import *
|
|
|
|
|
|
|
|
from files.classes import Base
|
2022-12-11 23:44:34 +00:00
|
|
|
from files.helpers.config.const import *
|
2022-11-15 09:19:08 +00:00
|
|
|
from files.helpers.lazy import lazy
|
2022-05-29 03:33:44 +00:00
|
|
|
|
|
|
|
class Lottery(Base):
|
2022-06-13 18:33:25 +00:00
|
|
|
__tablename__ = "lotteries"
|
2022-05-29 03:33:44 +00:00
|
|
|
|
2022-06-13 18:33:25 +00:00
|
|
|
id = Column(Integer, primary_key=True)
|
|
|
|
is_active = Column(Boolean, default=False)
|
|
|
|
ends_at = Column(Integer)
|
|
|
|
prize = Column(Integer, default=0)
|
|
|
|
tickets_sold = Column(Integer, default=0)
|
|
|
|
winner_id = Column(Integer, ForeignKey("users.id"))
|
2022-09-19 20:40:33 +00:00
|
|
|
created_utc = Column(Integer)
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
if "created_utc" not in kwargs: kwargs["created_utc"] = int(time.time())
|
|
|
|
super().__init__(*args, **kwargs)
|
2022-05-29 03:33:44 +00:00
|
|
|
|
2022-07-02 06:48:04 +00:00
|
|
|
def __repr__(self):
|
2022-11-29 21:02:38 +00:00
|
|
|
return f"<{self.__class__.__name__}(id={self.id})>"
|
2022-07-02 06:48:04 +00:00
|
|
|
|
2022-06-13 18:33:25 +00:00
|
|
|
@property
|
|
|
|
@lazy
|
|
|
|
def timeleft(self):
|
|
|
|
if not self.is_active:
|
|
|
|
return 0
|
2022-05-29 03:33:44 +00:00
|
|
|
|
2022-06-13 18:33:25 +00:00
|
|
|
epoch_time = int(time.time())
|
|
|
|
remaining_time = self.ends_at - epoch_time
|
2022-05-29 03:33:44 +00:00
|
|
|
|
2022-06-13 18:33:25 +00:00
|
|
|
return 0 if remaining_time < 0 else remaining_time
|
2022-05-29 03:33:44 +00:00
|
|
|
|
2022-06-13 18:33:25 +00:00
|
|
|
@property
|
|
|
|
@lazy
|
|
|
|
def stats(self):
|
|
|
|
return {"active": self.is_active, "timeLeft": self.timeleft, "prize": self.prize, "ticketsSoldThisSession": self.tickets_sold,}
|