Improved steam offer #188
This commit is contained in:
@@ -0,0 +1,32 @@
|
||||
from abc import ABC, abstractmethod
|
||||
from typing import Optional
|
||||
|
||||
from cpl_query.extension import List
|
||||
|
||||
from bot_data.model.steam_special_offer import SteamSpecialOffer
|
||||
|
||||
|
||||
class SteamSpecialOfferRepositoryABC(ABC):
|
||||
@abstractmethod
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def get_steam_special_offers(self) -> List[SteamSpecialOffer]:
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def get_steam_special_offer_by_name(self, name: str) -> SteamSpecialOffer:
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def add_steam_special_offer(self, steam_special_offer: SteamSpecialOffer):
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def update_steam_special_offer(self, steam_special_offer: SteamSpecialOffer):
|
||||
pass
|
||||
|
||||
@abstractmethod
|
||||
def delete_steam_special_offer(self, steam_special_offer: SteamSpecialOffer):
|
||||
pass
|
@@ -17,6 +17,7 @@ from bot_data.abc.level_repository_abc import LevelRepositoryABC
|
||||
from bot_data.abc.server_config_repository_abc import ServerConfigRepositoryABC
|
||||
from bot_data.abc.server_repository_abc import ServerRepositoryABC
|
||||
from bot_data.abc.short_role_name_repository_abc import ShortRoleNameRepositoryABC
|
||||
from bot_data.abc.steam_special_offer_repository_abc import SteamSpecialOfferRepositoryABC
|
||||
from bot_data.abc.technician_config_repository_abc import TechnicianConfigRepositoryABC
|
||||
from bot_data.abc.user_game_ident_repository_abc import UserGameIdentRepositoryABC
|
||||
from bot_data.abc.user_joined_game_server_repository_abc import UserJoinedGameServerRepositoryABC
|
||||
@@ -43,6 +44,7 @@ from bot_data.service.server_config_repository_service import ServerConfigReposi
|
||||
from bot_data.service.server_config_seeder import ServerConfigSeeder
|
||||
from bot_data.service.server_repository_service import ServerRepositoryService
|
||||
from bot_data.service.short_role_name_repository_service import ShortRoleNameRepositoryService
|
||||
from bot_data.service.steam_special_offer_repository_service import SteamSpecialOfferRepositoryService
|
||||
from bot_data.service.technician_config_repository_service import TechnicianConfigRepositoryService
|
||||
from bot_data.service.technician_config_seeder import TechnicianConfigSeeder
|
||||
from bot_data.service.user_game_ident_repository_service import UserGameIdentRepositoryService
|
||||
@@ -92,6 +94,7 @@ class DataModule(ModuleABC):
|
||||
services.add_transient(TechnicianConfigRepositoryABC, TechnicianConfigRepositoryService)
|
||||
services.add_transient(ServerConfigRepositoryABC, ServerConfigRepositoryService)
|
||||
services.add_transient(ShortRoleNameRepositoryABC, ShortRoleNameRepositoryService)
|
||||
services.add_transient(SteamSpecialOfferRepositoryABC, SteamSpecialOfferRepositoryService)
|
||||
|
||||
services.add_transient(SeederService)
|
||||
services.add_transient(DataSeederABC, TechnicianConfigSeeder)
|
||||
|
@@ -0,0 +1,68 @@
|
||||
from bot_core.logging.database_logger import DatabaseLogger
|
||||
from bot_data.abc.migration_abc import MigrationABC
|
||||
from bot_data.db_context import DBContext
|
||||
|
||||
|
||||
class SteamSpecialOfferMigration(MigrationABC):
|
||||
name = "1.2.0_SteamSpecialOfferMigration"
|
||||
|
||||
def __init__(self, logger: DatabaseLogger, db: DBContext):
|
||||
MigrationABC.__init__(self)
|
||||
self._logger = logger
|
||||
self._db = db
|
||||
self._cursor = db.cursor
|
||||
|
||||
def upgrade(self):
|
||||
self._logger.debug(__name__, "Running upgrade")
|
||||
|
||||
self._cursor.execute(
|
||||
str(
|
||||
f"""
|
||||
CREATE TABLE IF NOT EXISTS `SteamSpecialOffers` (
|
||||
`Id` BIGINT NOT NULL AUTO_INCREMENT,
|
||||
`Game` VARCHAR(255) NOT NULL,
|
||||
`OriginalPrice` FLOAT NOT NULL,
|
||||
`DiscountPrice` FLOAT NOT NULL,
|
||||
`DiscountPct` BIGINT NOT NULL,
|
||||
`CreatedAt` DATETIME(6) NULL DEFAULT CURRENT_TIMESTAMP(6),
|
||||
`LastModifiedAt` DATETIME(6) NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6),
|
||||
PRIMARY KEY(`Id`)
|
||||
);
|
||||
"""
|
||||
)
|
||||
)
|
||||
|
||||
self._cursor.execute(
|
||||
str(
|
||||
f"""
|
||||
ALTER TABLE CFG_Server
|
||||
ADD COLUMN IF NOT EXISTS GameOfferNotificationChatId BIGINT NULL AFTER ShortRoleNameSetOnlyHighest;
|
||||
"""
|
||||
)
|
||||
)
|
||||
|
||||
self._cursor.execute(
|
||||
str(
|
||||
f"""
|
||||
ALTER TABLE CFG_ServerHistory
|
||||
ADD COLUMN IF NOT EXISTS GameOfferNotificationChatId BIGINT NULL AFTER ShortRoleNameSetOnlyHighest;
|
||||
"""
|
||||
)
|
||||
)
|
||||
|
||||
def downgrade(self):
|
||||
self._cursor.execute("DROP TABLE `SteamSpecialOffers`;")
|
||||
self._cursor.execute(
|
||||
str(
|
||||
f"""
|
||||
ALTER TABLE CFG_Server DROP COLUMN ShortRoleNameSetOnlyHighest;
|
||||
"""
|
||||
)
|
||||
)
|
||||
self._cursor.execute(
|
||||
str(
|
||||
f"""
|
||||
ALTER TABLE CFG_ServerHistory DROP COLUMN ShortRoleNameSetOnlyHighest;
|
||||
"""
|
||||
)
|
||||
)
|
@@ -31,6 +31,7 @@ class ServerConfig(TableABC, ConfigurationModelABC):
|
||||
login_message_channel_id: int,
|
||||
default_role_id: Optional[int],
|
||||
short_role_name_only_set_highest_role: bool,
|
||||
game_offer_notification_chat_id: int,
|
||||
feature_flags: dict[FeatureFlagsEnum],
|
||||
server: Server,
|
||||
afk_channel_ids: List[int],
|
||||
@@ -56,6 +57,7 @@ class ServerConfig(TableABC, ConfigurationModelABC):
|
||||
self._login_message_channel_id = login_message_channel_id
|
||||
self._default_role_id = default_role_id
|
||||
self._short_role_name_only_set_highest_role = short_role_name_only_set_highest_role
|
||||
self._game_offer_notification_chat_id = game_offer_notification_chat_id
|
||||
|
||||
self._feature_flags = feature_flags
|
||||
self._server = server
|
||||
@@ -85,6 +87,7 @@ class ServerConfig(TableABC, ConfigurationModelABC):
|
||||
guild.system_channel.id,
|
||||
None,
|
||||
False,
|
||||
guild.system_channel.id,
|
||||
{},
|
||||
server,
|
||||
List(int),
|
||||
@@ -223,6 +226,14 @@ class ServerConfig(TableABC, ConfigurationModelABC):
|
||||
def short_role_name_only_set_highest_role(self, value: bool):
|
||||
self._short_role_name_only_set_highest_role = value
|
||||
|
||||
@property
|
||||
def game_offer_notification_chat_id(self) -> int:
|
||||
return self._game_offer_notification_chat_id
|
||||
|
||||
@game_offer_notification_chat_id.setter
|
||||
def game_offer_notification_chat_id(self, value: int):
|
||||
self._game_offer_notification_chat_id = value
|
||||
|
||||
@property
|
||||
def feature_flags(self) -> dict[FeatureFlagsEnum]:
|
||||
return self._feature_flags
|
||||
@@ -298,6 +309,7 @@ class ServerConfig(TableABC, ConfigurationModelABC):
|
||||
`LoginMessageChannelId`,
|
||||
`DefaultRoleId`,
|
||||
`ShortRoleNameSetOnlyHighest`,
|
||||
`GameOfferNotificationChatId`,
|
||||
`FeatureFlags`,
|
||||
`ServerId`
|
||||
) VALUES (
|
||||
@@ -317,6 +329,7 @@ class ServerConfig(TableABC, ConfigurationModelABC):
|
||||
{self._login_message_channel_id},
|
||||
{"NULL" if self._default_role_id is None else self._default_role_id},
|
||||
{self._short_role_name_only_set_highest_role},
|
||||
{self._game_offer_notification_chat_id},
|
||||
'{json.dumps(self._feature_flags)}',
|
||||
{self._server.id}
|
||||
);
|
||||
@@ -344,6 +357,7 @@ class ServerConfig(TableABC, ConfigurationModelABC):
|
||||
`LoginMessageChannelId` = {self._login_message_channel_id},
|
||||
`DefaultRoleId` = {"NULL" if self._default_role_id is None else self._default_role_id},
|
||||
`ShortRoleNameSetOnlyHighest` = {self._short_role_name_only_set_highest_role},
|
||||
`GameOfferNotificationChatId` = {self._game_offer_notification_chat_id},
|
||||
`FeatureFlags` = '{json.dumps(self._feature_flags)}',
|
||||
`ServerId` = {self._server.id}
|
||||
WHERE `Id` = {self._id};
|
||||
|
115
kdb-bot/src/bot_data/model/steam_special_offer.py
Normal file
115
kdb-bot/src/bot_data/model/steam_special_offer.py
Normal file
@@ -0,0 +1,115 @@
|
||||
from datetime import datetime
|
||||
|
||||
from cpl_core.database import TableABC
|
||||
|
||||
|
||||
class SteamSpecialOffer(TableABC):
|
||||
def __init__(
|
||||
self,
|
||||
name: str,
|
||||
original_price: float,
|
||||
discount_price: float,
|
||||
discount_pct: int,
|
||||
created_at: datetime = None,
|
||||
modified_at: datetime = None,
|
||||
id=0,
|
||||
):
|
||||
self._id = id
|
||||
self._name = name
|
||||
self._original_price = original_price
|
||||
self._discount_price = discount_price
|
||||
self._discount_pct = discount_pct
|
||||
|
||||
TableABC.__init__(self)
|
||||
self._created_at = created_at if created_at is not None else self._created_at
|
||||
self._modified_at = modified_at if modified_at is not None else self._modified_at
|
||||
|
||||
@property
|
||||
def id(self) -> int:
|
||||
return self._id
|
||||
|
||||
@property
|
||||
def name(self) -> str:
|
||||
return self._name
|
||||
|
||||
@name.setter
|
||||
def name(self, value: str):
|
||||
self._name = value
|
||||
|
||||
@property
|
||||
def original_price(self) -> float:
|
||||
return self._original_price
|
||||
|
||||
@original_price.setter
|
||||
def original_price(self, value: float):
|
||||
self._original_price = value
|
||||
|
||||
@property
|
||||
def discount_price(self) -> float:
|
||||
return self._discount_price
|
||||
|
||||
@discount_price.setter
|
||||
def discount_price(self, value: float):
|
||||
self._discount_price = value
|
||||
|
||||
@property
|
||||
def discount_pct(self) -> int:
|
||||
return self._discount_pct
|
||||
|
||||
@discount_pct.setter
|
||||
def discount_pct(self, value: int):
|
||||
self._discount_pct = value
|
||||
|
||||
@staticmethod
|
||||
def get_select_all_string() -> str:
|
||||
return str(
|
||||
f"""
|
||||
SELECT * FROM `SteamSpecialOffers`;
|
||||
"""
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def get_select_by_name_string(name: str) -> str:
|
||||
return str(
|
||||
f"""
|
||||
SELECT * FROM `SteamSpecialOffers`
|
||||
WHERE `Game` = '{name}';
|
||||
"""
|
||||
)
|
||||
|
||||
@property
|
||||
def insert_string(self) -> str:
|
||||
return str(
|
||||
f"""
|
||||
INSERT INTO `SteamSpecialOffers` (
|
||||
`Game`, `OriginalPrice`, `DiscountPrice`, `DiscountPct`
|
||||
) VALUES (
|
||||
'{self._name}',
|
||||
{self._original_price},
|
||||
{self._discount_price},
|
||||
{self._discount_pct}
|
||||
);
|
||||
"""
|
||||
)
|
||||
|
||||
@property
|
||||
def udpate_string(self) -> str:
|
||||
return str(
|
||||
f"""
|
||||
UPDATE `SteamSpecialOffers`
|
||||
SET `Game` = '{self._name}',
|
||||
`OriginalPrice` = {self._original_price},
|
||||
`DiscountPrice` = {self._discount_price},
|
||||
`DiscountPct` = {self._discount_pct}
|
||||
WHERE `Id` = {self._id};
|
||||
"""
|
||||
)
|
||||
|
||||
@property
|
||||
def delete_string(self) -> str:
|
||||
return str(
|
||||
f"""
|
||||
DELETE FROM `SteamSpecialOffers`
|
||||
WHERE `Id` = {self._id};
|
||||
"""
|
||||
)
|
@@ -67,12 +67,13 @@ class ServerConfigRepositoryService(ServerConfigRepositoryABC):
|
||||
result[14],
|
||||
result[15],
|
||||
result[16],
|
||||
json.loads(result[17]),
|
||||
self._servers.get_server_by_id(result[18]),
|
||||
self._get_afk_channel_ids(result[18]),
|
||||
self._get_team_role_ids(result[18]),
|
||||
result[19],
|
||||
result[17],
|
||||
json.loads(result[18]),
|
||||
self._servers.get_server_by_id(result[19]),
|
||||
self._get_afk_channel_ids(result[19]),
|
||||
self._get_team_role_ids(result[19]),
|
||||
result[20],
|
||||
result[21],
|
||||
id=result[0],
|
||||
)
|
||||
|
||||
|
@@ -0,0 +1,68 @@
|
||||
from typing import Optional
|
||||
|
||||
from cpl_core.database.context import DatabaseContextABC
|
||||
from cpl_query.extension import List
|
||||
|
||||
from bot_core.logging.database_logger import DatabaseLogger
|
||||
from bot_data.abc.server_repository_abc import ServerRepositoryABC
|
||||
from bot_data.abc.steam_special_offer_repository_abc import SteamSpecialOfferRepositoryABC
|
||||
from bot_data.model.steam_special_offer import SteamSpecialOffer
|
||||
|
||||
|
||||
class SteamSpecialOfferRepositoryService(SteamSpecialOfferRepositoryABC):
|
||||
def __init__(
|
||||
self,
|
||||
logger: DatabaseLogger,
|
||||
db_context: DatabaseContextABC,
|
||||
servers: ServerRepositoryABC,
|
||||
):
|
||||
self._logger = logger
|
||||
self._context = db_context
|
||||
|
||||
self._servers = servers
|
||||
|
||||
SteamSpecialOfferRepositoryABC.__init__(self)
|
||||
|
||||
@staticmethod
|
||||
def _get_value_from_result(value: any) -> Optional[any]:
|
||||
if isinstance(value, str) and "NULL" in value:
|
||||
return None
|
||||
|
||||
return value
|
||||
|
||||
def _steam_special_offer_from_result(self, sql_result: tuple) -> SteamSpecialOffer:
|
||||
return SteamSpecialOffer(
|
||||
self._get_value_from_result(sql_result[1]), # name
|
||||
float(self._get_value_from_result(sql_result[2])), # original_price
|
||||
float(self._get_value_from_result(sql_result[3])), # discount_price
|
||||
int(self._get_value_from_result(sql_result[4])), # discount_pct
|
||||
id=self._get_value_from_result(sql_result[0]), # id
|
||||
)
|
||||
|
||||
def get_steam_special_offers(self) -> List[SteamSpecialOffer]:
|
||||
steam_special_offers = List(SteamSpecialOffer)
|
||||
self._logger.trace(__name__, f"Send SQL command: {SteamSpecialOffer.get_select_all_string()}")
|
||||
results = self._context.select(SteamSpecialOffer.get_select_all_string())
|
||||
for result in results:
|
||||
self._logger.trace(__name__, f"Get steam_special_offer with id {result[0]}")
|
||||
steam_special_offers.append(self._steam_special_offer_from_result(result))
|
||||
|
||||
return steam_special_offers
|
||||
|
||||
def get_steam_special_offer_by_name(self, name: str) -> SteamSpecialOffer:
|
||||
self._logger.trace(__name__, f"Send SQL command: {SteamSpecialOffer.get_select_by_name_string(name)}")
|
||||
result = self._context.select(SteamSpecialOffer.get_select_by_name_string(name))[0]
|
||||
|
||||
return self._steam_special_offer_from_result(result)
|
||||
|
||||
def add_steam_special_offer(self, steam_special_offer: SteamSpecialOffer):
|
||||
self._logger.trace(__name__, f"Send SQL command: {steam_special_offer.insert_string}")
|
||||
self._context.cursor.execute(steam_special_offer.insert_string)
|
||||
|
||||
def update_steam_special_offer(self, steam_special_offer: SteamSpecialOffer):
|
||||
self._logger.trace(__name__, f"Send SQL command: {steam_special_offer.udpate_string}")
|
||||
self._context.cursor.execute(steam_special_offer.udpate_string)
|
||||
|
||||
def delete_steam_special_offer(self, steam_special_offer: SteamSpecialOffer):
|
||||
self._logger.trace(__name__, f"Send SQL command: {steam_special_offer.delete_string}")
|
||||
self._context.cursor.execute(steam_special_offer.delete_string)
|
Reference in New Issue
Block a user