TamaTown/DreamTown/cgi-bin/area/save
2022-07-31 15:21:37 +12:00

71 lines
1.6 KiB
Python

#!/usr/bin/python3
from dreamtown_config import *
import sys
import binascii
import os
import json
import sqlite3
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 TrySave():
Areas = jsonData['areas']
ActualAreaId = jsonData["actualAreaId"]
authToken = jsonData['authToken']
c = db.cursor()
c.execute('SELECT COUNT(1) from users WHERE LastSession=?',(authToken,))
rows = c.fetchone()
count = rows[0]
if count == 0:
result['status'] = USER_DOES_NOT_EXIST
return 0
#Find Username
c.execute('SELECT Name from users WHERE LastSession=?',(authToken,))
rows = c.fetchone()
username = rows[0]
for Area in Areas:
if 'areaId' in Area:
areaId = Area['areaId']
else:
areaId = None
if 'lastVisit' in Area:
lastVisit = Area['lastVisit']
else:
lastVisit = None
if 'nextRubishSpawnTime' in Area:
nextRubishSpawnTime = Area['nextRubishSpawnTime']
else:
nextRubishSpawnTime = None
c.execute('DELETE from areaList where Name=? and AreaId=?',(username,areaId))
c.execute('INSERT INTO areaList VAlUES (?,?,?,?,NULL)',(username,lastVisit,areaId,nextRubishSpawnTime))
c.execute('DELETE from areaList WHERE Name=? and ActualAreaId IS NOT NULL',(username,))
c.execute('INSERT INTO areaList VAlUES (?,NULL,NULL,NULL,?)',(username,ActualAreaId))
try:
db = DbConnect()
TrySave()
db.commit()
db.close()
except Exception as e:
print(e)
os._exit()
print(json.dumps(result))