62 lines
1.4 KiB
Python
62 lines
1.4 KiB
Python
#!/usr/bin/python3
|
|
from dreamtown_config import *
|
|
import sys
|
|
import binascii
|
|
import os
|
|
import json
|
|
import sqlite3
|
|
import time
|
|
import hashlib
|
|
|
|
print("Content-Type: application/json")
|
|
print("")
|
|
method = os.environ["REQUEST_METHOD"]
|
|
if method != "POST":
|
|
print("Expected POST")
|
|
os._exit()
|
|
|
|
|
|
content_len = int(os.environ["CONTENT_LENGTH"])
|
|
post = sys.stdin.read(content_len)
|
|
jsonData = json.loads(post)
|
|
result = {"status":SUCCESS}
|
|
|
|
def TryAdd():
|
|
CurrencyId = jsonData['currencyId']
|
|
Quantity = jsonData['quantity']
|
|
authToken = jsonData['authToken']
|
|
|
|
c = db.cursor()
|
|
cur = c.execute('SELECT COUNT(1) from users WHERE LastSession=?',(authToken,))
|
|
rows = cur.fetchone()
|
|
count = rows[0]
|
|
if count == 0:
|
|
result['status'] = USER_DOES_NOT_EXIST
|
|
return 0
|
|
|
|
#Find Username
|
|
cur = c.execute('SELECT Name from users WHERE LastSession=?',(authToken,))
|
|
rows = cur.fetchone()
|
|
username = rows[0]
|
|
|
|
c = db.cursor()
|
|
cur = c.execute('SELECT COUNT(1) from currencyList WHERE Name=? and CurrencyId=?',(username,CurrencyId))
|
|
rows = cur.fetchone()
|
|
count = rows[0]
|
|
if count == 0:
|
|
c.execute('INSERT INTO currencyList VAlUES (?,?,?)',(username,CurrencyId,Quantity))
|
|
else:
|
|
c.execute('UPDATE currencyList SET Quantity=Quantity+? WHERE Name=? and CurrencyId=?',(Quantity,username,CurrencyId))
|
|
|
|
|
|
|
|
try:
|
|
db = sqlite3.connect(SQLLITE_DB_PATH)
|
|
TryAdd()
|
|
db.commit()
|
|
db.close()
|
|
except Exception as e:
|
|
print(e)
|
|
os._exit()
|
|
|
|
print(json.dumps(result)) |