2023-04-10 09:35:14 +02:00
|
|
|
from typing import Optional, Dict, List
|
|
|
|
from sqlalchemy import Table, Column, UniqueConstraint, PrimaryKeyConstraint, and_, case
|
|
|
|
from sqlalchemy.types import Integer, String, TIMESTAMP, Boolean, JSON
|
|
|
|
from sqlalchemy.schema import ForeignKey
|
|
|
|
from sqlalchemy.sql import func, select, update, delete
|
|
|
|
from sqlalchemy.engine import Row
|
|
|
|
from sqlalchemy.dialects.mysql import insert
|
|
|
|
|
|
|
|
from core.data.schema import BaseData, metadata
|
|
|
|
|
|
|
|
item = Table(
|
2023-04-24 03:04:52 +02:00
|
|
|
"pokken_item",
|
2023-04-10 09:35:14 +02:00
|
|
|
metadata,
|
2023-04-24 03:04:52 +02:00
|
|
|
Column("id", Integer, primary_key=True, nullable=False),
|
|
|
|
Column(
|
|
|
|
"user",
|
|
|
|
ForeignKey("aime_user.id", ondelete="cascade", onupdate="cascade"),
|
|
|
|
nullable=False,
|
|
|
|
unique=True,
|
|
|
|
),
|
|
|
|
Column("category", Integer),
|
|
|
|
Column("content", Integer),
|
|
|
|
Column("type", Integer),
|
|
|
|
UniqueConstraint("user", "category", "content", "type", name="pokken_item_uk"),
|
2023-04-10 09:35:14 +02:00
|
|
|
mysql_charset="utf8mb4",
|
|
|
|
)
|
|
|
|
|
2023-04-24 03:04:52 +02:00
|
|
|
|
2023-04-10 09:35:14 +02:00
|
|
|
class PokkenItemData(BaseData):
|
|
|
|
"""
|
|
|
|
Items obtained as rewards
|
|
|
|
"""
|
2023-04-24 03:04:52 +02:00
|
|
|
|
2024-01-09 20:42:17 +01:00
|
|
|
async def add_reward(self, user_id: int, category: int, content: int, item_type: int) -> Optional[int]:
|
2023-06-14 09:00:52 +02:00
|
|
|
sql = insert(item).values(
|
|
|
|
user=user_id,
|
|
|
|
category=category,
|
|
|
|
content=content,
|
|
|
|
type=item_type,
|
|
|
|
)
|
|
|
|
|
2023-07-23 18:47:37 +02:00
|
|
|
conflict = sql.on_duplicate_key_update(
|
|
|
|
content=content,
|
|
|
|
)
|
|
|
|
|
2024-01-09 20:42:17 +01:00
|
|
|
result = await self.execute(conflict)
|
2023-06-14 09:00:52 +02:00
|
|
|
if result is None:
|
2023-08-08 16:17:56 +02:00
|
|
|
self.logger.warning(f"Failed to insert reward for user {user_id}: {category}-{content}-{item_type}")
|
2023-06-14 09:00:52 +02:00
|
|
|
return None
|
|
|
|
return result.lastrowid
|