Skip to content

Commit

Permalink
cache paintings (#953)
Browse files Browse the repository at this point in the history
  • Loading branch information
Kapu1178 authored May 24, 2024
1 parent 2f8b605 commit b9d3504
Showing 1 changed file with 39 additions and 17 deletions.
56 changes: 39 additions & 17 deletions code/controllers/subsystem/persistent_paintings.dm
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,12 @@ SUBSYSTEM_DEF(persistent_paintings)
"supermatter" = PATRONAGE_LEGENDARY_FRAME
)


/// A list of paintings' data for paintings that are currently stored in the library.
var/list/cached_painting_data = list()
/// A list of paintings' data for paintings that are currently stored in the library, with admin metadata
var/list/admin_painting_data = list()

/datum/controller/subsystem/persistent_paintings/Initialize(start_timeofday)
var/json_file = file("data/paintings.json")
if(fexists(json_file))
Expand All @@ -142,8 +148,28 @@ SUBSYSTEM_DEF(persistent_paintings)
for(var/obj/structure/sign/painting/painting_frame as anything in painting_frames)
painting_frame.load_persistent()

cache_paintings()
return ..()

/datum/controller/subsystem/persistent_paintings/proc/cache_paintings()
cached_painting_data = list()
admin_painting_data = list()

for(var/datum/painting/painting as anything in paintings)
cached_painting_data += list(list(
"title" = painting.title,
"creator" = painting.creator_name,
"md5" = painting.md5,
"ref" = REF(painting),
"width" = painting.width,
"height" = painting.height,
"ratio" = painting.width/painting.height,
))

var/list/pdata = painting.to_json()
pdata["ref"] = REF(painting)
admin_painting_data += pdata

/**
* Generates painting data ready to be consumed by ui.
* Args:
Expand All @@ -152,31 +178,27 @@ SUBSYSTEM_DEF(persistent_paintings)
* * search_text : text to search for if the PAINTINGS_FILTER_SEARCH_TITLE or PAINTINGS_FILTER_SEARCH_CREATOR filters are enabled.
*/
/datum/controller/subsystem/persistent_paintings/proc/painting_ui_data(filter=NONE, admin=FALSE, search_text)
. = list()
var/searching = filter & (PAINTINGS_FILTER_SEARCH_TITLE|PAINTINGS_FILTER_SEARCH_CREATOR) && search_text
for(var/datum/painting/painting as anything in paintings)
if(filter & PAINTINGS_FILTER_AI_PORTRAIT && ((painting.width != 24 && painting.width != 23) || (painting.height != 24 && painting.height != 23)))

if(!searching)
return admin ? admin_painting_data : cached_painting_data

var/list/filtered_paintings = list()
var/list/searched_paintings = admin ? admin_painting_data : cached_painting_data

for(var/painting as anything in searched_paintings)
if(filter & PAINTINGS_FILTER_AI_PORTRAIT && ((painting["width"] != 24 && painting["width"] != 23) || (painting["height"] != 24 && painting["height"] != 23)))
continue
if(searching)
var/haystack_text = ""
if(filter & PAINTINGS_FILTER_SEARCH_TITLE)
haystack_text = painting.title
haystack_text = painting["title"]
else if(filter & PAINTINGS_FILTER_SEARCH_CREATOR)
haystack_text = painting.creator_name
haystack_text = painting["creator"]
if(!findtext(haystack_text, search_text))
continue
if(admin)
var/list/pdata = painting.to_json()
pdata["ref"] = REF(painting)
. += list(pdata)
else
. += list(list(
"title" = painting.title,
"creator" = painting.creator_name,
"md5" = painting.md5,
"ref" = REF(painting),
"ratio" = painting.width/painting.height,
))
filtered_paintings += painting
return filtered_paintings

/// Returns paintings with given tag.
/datum/controller/subsystem/persistent_paintings/proc/get_paintings_with_tag(tag_name)
Expand Down

0 comments on commit b9d3504

Please sign in to comment.