mirror of
https://github.com/Lost-MSth/Arcaea-server.git
synced 2026-02-04 21:47:28 +08:00
228 lines
6.2 KiB
Python
228 lines
6.2 KiB
Python
import sqlite3
|
||
import time
|
||
import json
|
||
|
||
|
||
def int2b(x):
|
||
# int与布尔值转换
|
||
if x is None or x == 0:
|
||
return False
|
||
else:
|
||
return True
|
||
|
||
|
||
def get_item(c, type='pack'):
|
||
# 读取packs内容,返回字典列表
|
||
c.execute('''select * from item where type = :a''', {'a': type})
|
||
x = c.fetchall()
|
||
if not x:
|
||
return []
|
||
|
||
re = []
|
||
for i in x:
|
||
r = {"name": i[0],
|
||
"items": [{
|
||
"type": i[1],
|
||
"id": i[0],
|
||
"is_available": int2b(i[2])
|
||
}],
|
||
"price": i[3],
|
||
"orig_price": i[4]}
|
||
|
||
if i[5] > 0:
|
||
r['discount_from'] = i[5]
|
||
if i[6] > 0:
|
||
r['discount_to'] = i[6]
|
||
|
||
re.append(r)
|
||
|
||
return re
|
||
|
||
|
||
def get_single_purchase():
|
||
# main里面没开数据库,这里写一下代替
|
||
conn = sqlite3.connect('./database/arcaea_database.db')
|
||
c = conn.cursor()
|
||
re = get_item(c, type='single')
|
||
conn.commit()
|
||
conn.close()
|
||
return re
|
||
|
||
|
||
def buy_pack(user_id, pack_id):
|
||
# 曲包购买,返回字典
|
||
conn = sqlite3.connect('./database/arcaea_database.db')
|
||
c = conn.cursor()
|
||
c.execute('''select price from item where item_id = :a''', {'a': pack_id})
|
||
price = c.fetchone()
|
||
if price:
|
||
price = price[0]
|
||
else:
|
||
price = 0
|
||
|
||
c.execute('''select ticket from user where user_id = :a''', {'a': user_id})
|
||
ticket = c.fetchone()
|
||
if ticket:
|
||
ticket = ticket[0]
|
||
else:
|
||
ticket = 0
|
||
|
||
if ticket < price:
|
||
conn.commit()
|
||
conn.close()
|
||
return {
|
||
"success": False
|
||
}
|
||
|
||
c.execute('''update user set ticket = :b where user_id = :a''',
|
||
{'a': user_id, 'b': ticket-price})
|
||
c.execute('''insert into user_item values(:a,:b,'pack')''',
|
||
{'a': user_id, 'b': pack_id})
|
||
|
||
conn.commit()
|
||
conn.close()
|
||
return {
|
||
"success": True
|
||
}
|
||
|
||
|
||
def buy_single(user_id, single_id):
|
||
# 单曲购买,返回字典
|
||
conn = sqlite3.connect('./database/arcaea_database.db')
|
||
c = conn.cursor()
|
||
c.execute('''select price from item where item_id = :a''',
|
||
{'a': single_id})
|
||
price = c.fetchone()
|
||
if price:
|
||
price = price[0]
|
||
else:
|
||
price = 0
|
||
|
||
c.execute('''select ticket from user where user_id = :a''', {'a': user_id})
|
||
ticket = c.fetchone()
|
||
if ticket:
|
||
ticket = ticket[0]
|
||
else:
|
||
ticket = 0
|
||
|
||
if ticket < price:
|
||
conn.commit()
|
||
conn.close()
|
||
return {
|
||
"success": False
|
||
}
|
||
|
||
c.execute('''update user set ticket = :b where user_id = :a''',
|
||
{'a': user_id, 'b': ticket-price})
|
||
c.execute('''insert into user_item values(:a,:b,'single')''',
|
||
{'a': user_id, 'b': single_id})
|
||
|
||
conn.commit()
|
||
conn.close()
|
||
return {
|
||
"success": True
|
||
}
|
||
|
||
|
||
def get_user_present(c, user_id):
|
||
# 获取用户奖励,返回字典列表
|
||
c.execute(
|
||
'''select * from present where present_id in (select present_id from user_present where user_id=:a)''', {'a': user_id})
|
||
x = c.fetchall()
|
||
re = []
|
||
now = int(time.time() * 1000)
|
||
if x:
|
||
for i in x:
|
||
if now <= int(i[1]):
|
||
re.append({'expire_ts': i[1],
|
||
'description': i[3],
|
||
'present_id': i[0],
|
||
'items': json.loads(i[2])
|
||
})
|
||
|
||
return re
|
||
|
||
|
||
def claim_user_present(user_id, present_id):
|
||
# 确认并删除用户奖励,返回成功与否的布尔值
|
||
flag = False
|
||
conn = sqlite3.connect('./database/arcaea_database.db')
|
||
c = conn.cursor()
|
||
|
||
c.execute('''select exists(select * from user_present where user_id=:a and present_id=:b)''',
|
||
{'a': user_id, 'b': present_id})
|
||
if c.fetchone() == (1,):
|
||
flag = True
|
||
c.execute('''delete from user_present where user_id=:a and present_id=:b''',
|
||
{'a': user_id, 'b': present_id})
|
||
c.execute('''select * from present where present_id=:b''',
|
||
{'b': present_id})
|
||
x = c.fetchone()
|
||
now = int(time.time() * 1000)
|
||
if now <= int(x[1]):
|
||
# 处理memory
|
||
items = json.loads(x[2])
|
||
for i in items:
|
||
if i['id'] == 'memory':
|
||
c.execute('''select ticket from user where user_id=:a''', {
|
||
'a': user_id})
|
||
ticket = int(c.fetchone()[0])
|
||
ticket += int(i['amount'])
|
||
c.execute('''update user set ticket=:b where user_id=:a''', {
|
||
'a': user_id, 'b': ticket})
|
||
|
||
else:
|
||
# 过期
|
||
flag = False
|
||
|
||
conn.commit()
|
||
conn.close()
|
||
return flag
|
||
|
||
|
||
def claim_user_redeem(user_id, code):
|
||
# 处理兑换码,返回碎片数量和错误码
|
||
fragment = 0
|
||
conn = sqlite3.connect('./database/arcaea_database.db')
|
||
c = conn.cursor()
|
||
c.execute('''select * from redeem where code=:a''', {'a': code})
|
||
x = c.fetchone()
|
||
if not x:
|
||
conn.commit()
|
||
conn.close()
|
||
return 0, 504
|
||
|
||
if x[2] == 0: # 一次性
|
||
c.execute(
|
||
'''select exists(select * from user_redeem where code=:a)''', {'a': code})
|
||
if c.fetchone() == (1,):
|
||
conn.commit()
|
||
conn.close()
|
||
return 0, 505
|
||
elif x[2] == 1: # 每个玩家一次
|
||
c.execute('''select exists(select * from user_redeem where code=:a and user_id=:b)''',
|
||
{'a': code, 'b': user_id})
|
||
if c.fetchone() == (1,):
|
||
conn.commit()
|
||
conn.close()
|
||
return 0, 506
|
||
|
||
c.execute('''insert into user_redeem values(:b,:a)''',
|
||
{'a': code, 'b': user_id})
|
||
|
||
items = json.loads(x[1])
|
||
for i in items:
|
||
if i['type'] == 'fragment':
|
||
fragment = i['amount']
|
||
if i['type'] == 'memory':
|
||
c.execute('''select ticket from user where user_id=:a''', {
|
||
'a': user_id})
|
||
ticket = int(c.fetchone()[0])
|
||
ticket += int(i['amount'])
|
||
c.execute('''update user set ticket=:b where user_id=:a''', {
|
||
'a': user_id, 'b': ticket})
|
||
|
||
conn.commit()
|
||
conn.close()
|
||
return fragment, None
|