Skip to content

Commit

Permalink
Revert "cache data.js (#153)" (#157)
Browse files Browse the repository at this point in the history
This reverts commit 2cd97ec.
  • Loading branch information
Telokis authored Oct 1, 2024
1 parent f061811 commit f46540f
Showing 1 changed file with 44 additions and 72 deletions.
116 changes: 44 additions & 72 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
from crons import *
from tests import *
from tasks import *
import hashlib

@ndb.toplevel
@app.route('/comm')
Expand Down Expand Up @@ -158,80 +157,53 @@ def serve_codejs():
else: request.response.set_data("game_log('load_code: Code not found',colors.code_error)")
return request.response

# Cache when data is prepared
cached_data = None
cached_data_hash = None

@app.route('/data.js',methods=['GET','POST'])
@ndb.toplevel
def serve_datajs():
global cached_data
global cached_data_hash

init_request(request)

additional=""

if request.values.get("reload"):
additional="add_log('Game data reloaded','#32A3B0');\napply_backup()\n"
elif cached_data_hash != None and request.headers.get('If-None-Match') == cached_data_hash:
# The ETag matches
request.response.status_code = 304
return request.response

if cached_data == None or cached_data_hash == None:
geometry={}

for name in maps:
key=maps[name]["key"]
if maps[name].get("ignore"): continue
#if name=="test" and not is_sdk: key="test"
#logging.info(key)
geometry[name]=get_by_iid("map|%s"%key).info.data

data={
"version":game_version,
"achievements":achievements,
"animations":animations,
"monsters":monsters,
"sprites":sprites,
"maps":maps,
"geometry":geometry,
"npcs":npcs,
"tilesets":tilesets,
"imagesets":imagesets,
"items":items,
"sets":sets,
"craft":craft,
"titles":titles,
"tokens":tokens,
"dismantle":dismantle,
"conditions":conditions,
"cosmetics":cosmetics,
"emotions":emotions,
"projectiles":projectiles,
"classes":classes,
"dimensions":dimensions,
"images":precomputed.images,
"levels":levels,
"positions":positions,
"skills":skills,
"events":events,
"games":games,
"multipliers":multipliers,
#"codes":codes,
"docs":docs,
"drops":drops,
}

cached_data = json.dumps(data)
cached_data_hash = hashlib.md5(cached_data).hexdigest()

# The browser should cache this
request.response.headers['ETag'] = cached_data_hash
request.response.headers['Cache-Control'] = "max-age=31536000" # 1 Year

request.response.set_data("var G=%s;\n%s"%(cached_data,additional))
domain=gdi(request); additional=""
geometry={}
for name in maps:
key=maps[name]["key"]
if maps[name].get("ignore"): continue
#if name=="test" and not is_sdk: key="test"
#logging.info(key)
geometry[name]=get_by_iid("map|%s"%key).info.data
data={
"version":game_version,
"achievements":achievements,
"animations":animations,
"monsters":monsters,
"sprites":sprites,
"maps":maps,
"geometry":geometry,
"npcs":npcs,
"tilesets":tilesets,
"imagesets":imagesets,
"items":items,
"sets":sets,
"craft":craft,
"titles":titles,
"tokens":tokens,
"dismantle":dismantle,
"conditions":conditions,
"cosmetics":cosmetics,
"emotions":emotions,
"projectiles":projectiles,
"classes":classes,
"dimensions":dimensions,
"images":precomputed.images,
"levels":levels,
"positions":positions,
"skills":skills,
"events":events,
"games":games,
"multipliers":multipliers,
#"codes":codes,
"docs":docs,
"drops":drops,
}
if request.values.get("reload"): additional="add_log('Game data reloaded','#32A3B0');\napply_backup()\n"
request.response.set_data("var G=%s;\n%s"%(json.dumps(data),additional))
return request.response


Expand Down

0 comments on commit f46540f

Please sign in to comment.