gold_readbag and russian

This commit is contained in:
hibiki
2021-06-15 10:57:08 +08:00
parent 95209093de
commit ea620fdae9
125 changed files with 2106 additions and 1244 deletions
+4 -7
View File
@@ -2,7 +2,7 @@
from services.db_context import db
class UserBag(db.Model):
class BagUser(db.Model):
__tablename__ = 'bag_users'
id = db.Column(db.Integer(), primary_key=True)
@@ -63,7 +63,7 @@ class UserBag(db.Model):
return ''
@classmethod
async def add_glod(cls, user_qq: int, belonging_group: int, num: int) -> bool:
async def add_gold(cls, user_qq: int, belonging_group: int, num: int) -> bool:
query = cls.query.where(
(cls.user_qq == user_qq) & (cls.belonging_group == belonging_group)
)
@@ -89,7 +89,7 @@ class UserBag(db.Model):
return False
@classmethod
async def spend_glod(cls, user_qq: int, belonging_group: int, num: int) -> bool:
async def spend_gold(cls, user_qq: int, belonging_group: int, num: int) -> bool:
query = cls.query.where(
(cls.user_qq == user_qq) & (cls.belonging_group == belonging_group)
)
@@ -170,16 +170,13 @@ class UserBag(db.Model):
@classmethod
async def get_user_all(cls, group_id: int = None) -> list:
user_list = []
if not group_id:
query = await cls.query.gino.all()
else:
query = await cls.query.where(
(cls.belonging_group == group_id)
).gino.all()
for user in query:
user_list.append(user)
return user_list
return query
+5 -7
View File
@@ -42,16 +42,15 @@ class BanUser(db.Model):
if await cls.check_ban_time(user_qq):
return True
else:
await cls.unban(user_qq, True)
await cls.unban(user_qq)
return False
@classmethod
async def ban(cls, user_qq: int, ban_level: int, duration: int, for_update: bool = False) -> 'bool':
async def ban(cls, user_qq: int, ban_level: int, duration: int) -> 'bool':
query = cls.query.where(
(cls.user_qq == user_qq)
)
if for_update:
query = await query.with_for_update()
query = query.with_for_update()
user = await query.gino.first()
if not await cls.check_ban_time(user_qq):
await cls.unban(user_qq)
@@ -68,12 +67,11 @@ class BanUser(db.Model):
return False
@classmethod
async def unban(cls, user_qq: int, for_update: bool = False) -> 'bool':
async def unban(cls, user_qq: int) -> 'bool':
query = cls.query.where(
(cls.user_qq == user_qq)
)
if for_update:
query = query.with_for_update()
query = query.with_for_update()
user = await query.gino.first()
if user is None:
return False
+1
View File
@@ -1,4 +1,5 @@
from datetime import datetime
from datetime import datetime
from services.db_context import db
+1 -1
View File
@@ -60,7 +60,7 @@ class FriendUser(db.Model):
query = cls.query.where(
cls.user_id == user_id
)
user = await query.with_for_update().gino.first()
user = await query.gino.first()
if user:
if user.nickname:
return user.nickname
+92
View File
@@ -0,0 +1,92 @@
from services.db_context import db
class GoodsInfo(db.Model):
__tablename__ = 'goods_info'
id = db.Column(db.Integer(), primary_key=True)
goods_name = db.Column(db.TEXT(), nullable=False) # 名称
goods_price = db.Column(db.Integer(), nullable=False) # 价格
goods_description = db.Column(db.TEXT(), nullable=False) # 商品描述
goods_discount = db.Column(db.Numeric(scale=3, asdecimal=False), default=1) # 打折
goods_limit_time = db.Column(db.BigInteger(), default=0) # 限时
_idx1 = db.Index('goods_group_users_idx1', 'goods_name', unique=True)
@classmethod
async def add_goods(cls, goods_name: str, goods_price: int,
goods_description: str, goods_discount: float = 1, goods_limit_time: int = 0) -> bool:
# try:
await cls.create(
goods_name=goods_name,
goods_price=goods_price,
goods_description=goods_description,
goods_discount=goods_discount,
goods_limit_time=goods_limit_time
)
return True
# except Exception:
# return False
@classmethod
async def del_goods(cls, goods_name: str) -> bool:
query = await cls.query.where(
cls.goods_name == goods_name
).with_for_update().gino.first()
if not query:
return False
await query.delete()
return True
@classmethod
async def update_goods(cls, goods_name: str, goods_price: int = None,
goods_description: str = None, goods_discount: float = None,
goods_limit_time: int = None) -> bool:
try:
query = await cls.query.where(
cls.goods_name == goods_name
).with_for_update().gino.first()
if not query:
return False
if goods_price:
await query.update(
goods_price=goods_price
).apply()
if goods_description:
await query.update(
goods_description=goods_description
).apply()
if goods_discount:
await query.update(
goods_discount=goods_discount
).apply()
if goods_limit_time:
await query.update(
goods_limit_time=goods_limit_time
).apply()
return True
except Exception:
return False
@classmethod
async def get_goods_info(cls, goods_name: str) -> 'GoodsInfo':
query = await cls.query.where(
cls.goods_name == goods_name
).gino.first()
return query
@classmethod
async def get_all_goods(cls) -> list:
query = await cls.query.gino.all()
return query
+1 -1
View File
@@ -73,7 +73,7 @@ class GroupInfoUser(db.Model):
query = cls.query.where(
(cls.user_qq == user_qq) & (cls.belonging_group == belonging_group)
)
user = await query.gino.first()
user = await query.with_for_update().gino.first()
if user:
await user.update(
nickname=nickname
+1 -1
View File
@@ -48,7 +48,7 @@ class GroupRemind(db.Model):
try:
group = await cls.query.where(
(cls.group_id == group_id)
).gino.first()
).with_for_update().gino.first()
if not group:
group = await cls.create(
group_id=group_id,
+2 -3
View File
@@ -43,12 +43,11 @@ class LevelUser(db.Model):
return False
@classmethod
async def delete_level(cls, user_qq: int, group_id: int, for_update: bool = False) -> 'bool':
async def delete_level(cls, user_qq: int, group_id: int) -> 'bool':
query = cls.query.where(
(cls.user_qq == user_qq) & (cls.group_id == group_id)
)
if for_update:
query = query.with_for_update()
query = query.with_for_update()
user = await query.gino.first()
if user is None:
return False
+65
View File
@@ -0,0 +1,65 @@
from services.db_context import db
class RedbagUser(db.Model):
__tablename__ = 'redbag_users'
id = db.Column(db.Integer(), primary_key=True)
user_qq = db.Column(db.BigInteger(), nullable=False)
group_id = db.Column(db.BigInteger(), nullable=False)
send_redbag_count = db.Column(db.Integer(), default=0)
get_redbag_count = db.Column(db.Integer(), default=0)
spend_gold = db.Column(db.Integer(), default=0)
get_gold = db.Column(db.Integer(), default=0)
_idx1 = db.Index('redbag_group_users_idx1', 'user_qq', 'group_id', unique=True)
@classmethod
async def add_redbag_data(cls, user_qq: int, group_id: int, itype: str, money: int):
query = cls.query.where(
(cls.user_qq == user_qq) & (cls.group_id == group_id)
)
user = await query.with_for_update().gino.first() or await cls.create(
user_qq=user_qq,
group_id=group_id,
)
if itype == 'get':
await user.update(
get_redbag_count=user.get_redbag_count + 1,
get_gold=user.get_gold + money,
).apply()
else:
await user.update(
send_redbag_count=user.send_redbag_count + 1,
spend_gold=user.spend_gold + money,
).apply()
@classmethod
async def ensure(cls, user_qq: int, group_id: int) -> bool:
query = cls.query.where(
(cls.user_qq == user_qq) & (cls.group_id == group_id)
)
user = await query.gino.first() or await cls.create(
user_qq=user_qq,
group_id=group_id,
)
return user
@classmethod
async def get_user_all(cls, group_id: int = None) -> list:
if not group_id:
query = await cls.query.gino.all()
else:
query = await cls.query.where(
(cls.group_id == group_id)
).gino.all()
return query
+80
View File
@@ -0,0 +1,80 @@
from services.db_context import db
from typing import List
class RussianUser(db.Model):
__tablename__ = 'russian_users'
id = db.Column(db.Integer(), primary_key=True)
user_qq = db.Column(db.BigInteger(), nullable=False)
group_id = db.Column(db.BigInteger(), nullable=False)
win_count = db.Column(db.Integer(), default=0)
fail_count = db.Column(db.Integer(), default=0)
make_money = db.Column(db.Integer(), default=0)
lose_money = db.Column(db.Integer(), default=0)
_idx1 = db.Index('russian_group_users_idx1', 'user_qq', 'group_id', unique=True)
@classmethod
async def ensure(cls, user_qq: int, group_id: int) -> 'RussianUser':
user = await cls.query.where(
(cls.user_qq == user_qq) & (cls.group_id == group_id)
).with_for_update().gino.first()
return user or await cls.create(
user_qq=user_qq,
group_id=group_id
)
@classmethod
async def add_count(cls, user_qq: int, group_id: int, itype: str) -> bool:
try:
user = await cls.query.where(
(cls.user_qq == user_qq) & (cls.group_id == group_id)
).with_for_update().gino.first()
if not user:
user = await cls.create(
user_qq=user_qq,
group_id=group_id
)
if itype == 'win':
await user.update(
win_count=user.win_count + 1,
).apply()
elif itype == 'lose':
await user.update(
fail_count=user.fail_count + 1,
).apply()
return True
except Exception:
return False
@classmethod
async def money(cls, user_qq: int, group_id: int, itype: str, count: int) -> bool:
try:
user = await cls.query.where(
(cls.user_qq == user_qq) & (cls.group_id == group_id)
).gino.first()
if not user:
user = await cls.create(
user_qq=user_qq,
group_id=group_id
)
if itype == 'win':
await user.update(
make_money=user.make_money + count,
).apply()
elif itype == 'lose':
await user.update(
lose_money=user.lose_money + count,
).with_for_update().apply()
return True
except Exception:
return False
@classmethod
async def all_user(cls, group_id: int) -> List['RussianUser']:
users = await cls.query.where(
(cls.group_id == group_id)
).gino.all()
return users