#cogs/money.py
# 使用try......except
try:
# 使用count確認位置
count = 0
# 預設為0先認定它不存在,如存在則改為1
exist = 0
for cargo in users[str(user.id)]["bag"]:
item = cargo["item"]
if item == itemName:
oldAmount = cargo["amount"]
newAmount = oldAmount + amount
users[str(user.id)]["bag"][count]["amount"] = newAmount
exist = 1
break
count += 1
if exist == 0:
beginexist = {"item":itemName, "amount":amount}
users[str(user.id)]["bag"].append(beginexist)
except:
beginexist = {"item" : itemName, "amount" : amount}
users[str(user.id)]["bag"] = [beginexist]
with open("bank.json", "w") as f:
json.dump(users, f)
await update_bank(user, cost*-1, "wallet")
return [True, "worked"]
{
"682809473085079625":
{
"wallet": 1054,
"bank": 0,
"bag":
[
{
"item": "watch", "amount": 10
}
]
}
}
第一是,容易解析,又可以有結構,純文字又方便傳輸,現在幾乎所有後端甚至JavaScript都支援Json格式
第二是,因為資料庫連接較慢,我每呼叫一次函式就會發起一次請求,一點點還好,量多就......望向線上版資料庫,個人沒試過MySQL,或許會比較快...?(本人使用MongoDB)