adding nối từ game

This commit is contained in:
2025-06-23 17:01:31 +07:00
parent 300807e26a
commit 537be0c4ab
15 changed files with 752 additions and 32 deletions
+2 -1
View File
@@ -2,5 +2,6 @@ from .server import ServerRepository
from .channel import ChannelRepository
from .channel_app import ChannelAppRepository
from .home_debt import HomeDebtRepository
from .currency import CurrencyRepository
__all__ = ['ServerRepository', 'ChannelRepository', 'ChannelAppRepository', 'HomeDebtRepository']
__all__ = ['ServerRepository', 'ChannelRepository', 'ChannelAppRepository', 'HomeDebtRepository', 'CurrencyRepository']
+111
View File
@@ -0,0 +1,111 @@
from typing import List, Optional
from models.currency import DiscordCurrency
from infra.db import postgres
from datetime import datetime
class CurrencyRepository:
def __init__(self):
self.table = postgres.get_table('discord_currency')
async def get(self, user_id: int) -> Optional[DiscordCurrency]:
"""Lấy thông tin thành viên"""
try:
response = self.table.select('*').eq('user_id', user_id).execute()
if response.data:
data = response.data[0]
return DiscordCurrency(
id=data.get('id'),
user_id=data['user_id'],
balance=data['balance'],
updated_at=datetime.fromisoformat(data['updated_at'].replace('Z', '+00:00')) if data['updated_at'] else datetime.now()
)
return None
except Exception as e:
print(f"Error getting user: {e}")
return None
async def create(self, user_id: int, balance: int) -> Optional[DiscordCurrency]:
"""Tạo thông tin thành viên"""
try:
response = self.table.insert({'user_id': user_id, 'balance': balance}).execute()
if response.data:
data = response.data[0]
return DiscordCurrency(
id=data.get('id'),
user_id=data['user_id'],
balance=data['balance'],
updated_at=datetime.fromisoformat(data['updated_at'].replace('Z', '+00:00')) if data['updated_at'] else datetime.now()
)
return None
except Exception as e:
print(f"Error creating user: {e}")
return None
async def update(self, user_id: int, balance: int) -> Optional[DiscordCurrency]:
"""Cập nhật thông tin thành viên"""
try:
response = self.table.update({'balance': balance}).eq('user_id', user_id).execute()
if response.data:
data = response.data[0]
return DiscordCurrency(
id=data.get('id'),
user_id=data['user_id'],
balance=data['balance'],
updated_at=datetime.fromisoformat(data['updated_at'].replace('Z', '+00:00')) if data['updated_at'] else datetime.now()
)
return None
except Exception as e:
print(f"Error updating user: {e}")
return None
async def get_all(self) -> List[DiscordCurrency]:
"""Lấy tất cả thông tin thành viên"""
try:
response = self.table.select('*').execute()
currencies = []
for data in response.data:
currencies.append(DiscordCurrency(
id=data.get('id'),
user_id=data['user_id'],
balance=data['balance'],
updated_at=datetime.fromisoformat(data['updated_at'].replace('Z', '+00:00')) if data['updated_at'] else datetime.now()
))
return currencies
except Exception as e:
print(f"Error getting all users: {e}")
return []
async def get_all_with_balance(self) -> List[DiscordCurrency]:
"""Lấy tất cả thông tin thành viên và số dư"""
try:
response = self.table.select('*').execute()
currencies = []
for data in response.data:
currencies.append(DiscordCurrency(
id=data.get('id'),
user_id=data['user_id'],
balance=data['balance'],
updated_at=datetime.fromisoformat(data['updated_at'].replace('Z', '+00:00')) if data['updated_at'] else datetime.now()
))
return currencies
except Exception as e:
print(f"Error getting all users with balance: {e}")
return []
# Get all with sort by balance
async def get_all_with_sort_by_balance(self) -> List[DiscordCurrency]:
"""Lấy tất cả thông tin thành viên và sắp xếp theo số dư"""
try:
response = self.table.select('*').order('balance', desc=True).execute()
currencies = []
for data in response.data:
currencies.append(DiscordCurrency(
id=data.get('id'),
user_id=data['user_id'],
balance=data['balance'],
updated_at=datetime.fromisoformat(data['updated_at'].replace('Z', '+00:00')) if data['updated_at'] else datetime.now()
))
return currencies
except Exception as e:
print(f"Error getting all users with sort by balance: {e}")
return []
+86
View File
@@ -0,0 +1,86 @@
from typing import List, Optional
from models.noi_tu import DiscordNoiTu
from infra.db import postgres
from datetime import datetime
import random
class NoiTuRepository:
def __init__(self):
self.table = postgres.get_table('dictionary_vietnamese_two_words')
async def is_exist(self, word: str) -> bool:
"""Kiểm tra xem từ có tồn tại trong bảng không"""
try:
word = word.strip()
response = self.table.select('*').eq('word', word).execute()
return len(response.data) > 0
except Exception as e:
print(f"Error checking if word exists: {e}")
return False
async def add(self, word: str, meaning: Optional[str] = None) -> bool:
"""Thêm từ vào bảng"""
try:
response = self.table.insert({
'word': word,
'meaning': meaning
}).execute()
return response.data is not None
except Exception as e:
print(f"Error adding word: {e}")
return False
async def remove(self, word: str) -> bool:
"""Xóa từ khỏi bảng"""
try:
response = self.table.delete().eq('word', word).execute()
return response.data is not None
except Exception as e:
print(f"Error removing word: {e}")
return False
async def get_random_word(self) -> Optional[str]:
"""Lấy một từ ngẫu nhiên từ bảng"""
try:
response = self.table.select('word').execute()
if response.data and len(response.data) > 0:
# Lọc từ có đúng 2 từ ghép, mỗi từ chỉ gồm chữ cái
words = []
for item in response.data:
word = item['word'].strip()
parts = word.split()
if len(parts) == 2 and all(part.isalpha() for part in parts):
words.append(word)
if words:
return random.choice(words)
return None
except Exception as e:
print(f"Error getting random word: {e}")
return None
async def get_all_words(self) -> List[str]:
"""Lấy tất cả từ trong bảng"""
try:
response = self.table.select('word').execute()
if response.data:
# Lọc từ có đúng 2 từ ghép, mỗi từ chỉ gồm chữ cái
words = []
for item in response.data:
word = item['word'].strip()
parts = word.split()
if len(parts) == 2 and all(part.isalpha() for part in parts):
words.append(word)
return words
return []
except Exception as e:
print(f"Error getting all words: {e}")
return []
async def is_valid_word(self, word: str) -> bool:
"""Kiểm tra từ có hợp lệ không (2 từ ghép, mỗi từ chỉ gồm chữ cái)"""
word = word.strip()
parts = word.split()
if len(parts) != 2:
return False
return all(part.isalpha() for part in parts)