Files
Bubberstation/code/datums/json_database.dm
Ghom 73081bcff0 Add messages (paper, photos, cash) in bottles. (#85703)
## About The Pull Request
This PR adds a new persistent feature: message inside bottles. These are
basically glass bottles with inside a piece of paper, a photo or space
cash (no holocredits, and most bills rarely go over 1000 credits anyway)
from a previous round, which can be fished at the beach, or from the
relative fishing portals.

Each piece of written paper or photo that isn't map-loaded has a roughly
a 0.2% chance to be added to the message bottles database at the end of
the round. However, you can also manually toss a glass bottle with
inside a paper/photo/bill into the ocean (or a fishing portal generator
with the ocean/beach module loaded) for guaranteed results.

The bottles are removed from the database once fished up by the by,
unless tossed back into the ocean.

I've also offset a couple bottle sprites that weren't properly aligned
(for the message overlays).

TODO:
- [x] add a couple (20 prob or less) message bottle spawners to the
beach away mission or something.
- [x] add a few sounds for adding and removing the message from the
bottle. (pickup/drop sounds already handle that)
- [x] test it properly.

## Why It's Good For The Game
I think it'd be neat to have a way to send photos, snarky "seek grass"
messages, as well as the occasional financial aid to future players, and
furthermore, another thing to tie fishing to.

## Changelog

🆑
add: You can place papers, photos and cash bills (no holochips) inside
bottles and then toss them into the ocean (or fishing portal gen with
relative settings) with right-click, for others to fish them up on
future rounds.
/🆑
2024-08-24 01:31:38 -04:00

139 lines
4.2 KiB
Plaintext

/// Represents a json file being used as a database in the data/ folder.
/// Changes made here will save back to the associated file, with recovery.
/// Will defer writes until later if multiple happen in the same tick.
/// Do not add an extra cache on top of this. This IS your cache.
/datum/json_database
VAR_PRIVATE
filepath
backup_filepath
cached_data
save_queued = FALSE
static/existing_json_database = list()
/datum/json_database/New(filepath)
if (IsAdminAdvancedProcCall())
to_chat(usr, "<span class='admin prefix'>json_database creation, linking to [html_encode(filepath)], was blocked.</span>", confidential = TRUE)
return
ASSERT(isnull(existing_json_database[filepath]), "[filepath] already has an associated json_database. You must expose it somehow and use that instead of making a new one.")
existing_json_database[filepath] = TRUE
src.filepath = filepath
backup_filepath = "[filepath].savebac"
if (fexists(filepath))
cached_data = safe_json_decode(file2text(filepath))
if (isnull(cached_data))
var/scenario = "[filepath] existed, but did not have valid JSON"
if (fexists(backup_filepath))
load_backup(scenario)
else
stack_trace("[scenario]. No backup could be found.")
cached_data = list()
else
if (fexists(backup_filepath))
load_backup("[filepath] didn't exist")
else
cached_data = list()
/datum/json_database/Destroy()
if (save_queued)
save()
existing_json_database -= filepath
return ..()
/// Returns the cached data.
/// Be careful on holding onto this data for too long, as it can mutate when other stuff changes it.
/// Do not mutate it yourself.
/datum/json_database/proc/get()
return cached_data
/// Returns the data with the given key.
/// For arrays, this is a number.
/// Be careful on holding onto this data for too long, as it can mutate when other stuff changes it.
/// Do not mutate it yourself.
/datum/json_database/proc/get_key(key)
return cached_data[key]
/// Picks the data of a random key and then removes that key from the database.
/// Since the list is no longer inside the database, you can mutate and use it as you like.
/datum/json_database/proc/pick_and_take_key()
if(!length(cached_data))
return null
var/key = pick(cached_data)
. = cached_data[key]
cached_data -= key
queue_save()
/// Sets the data at the key to the value, and queues a save.
/datum/json_database/proc/set_key(key, value)
cached_data[key] = value
queue_save()
/// Removes the data at the given item, and queues a save.
/// For dictionaries, this can be the key.
/// For arrays, this can be the value.
/datum/json_database/proc/remove(item)
UNTYPED_LIST_REMOVE(cached_data, item)
queue_save()
/// Inserts the data at the end of what is assumed to be an array, and queues a save.
/datum/json_database/proc/insert(value)
UNTYPED_LIST_ADD(cached_data, value)
queue_save()
/// Replaces the cache with the new data completely, and queues a save.
/// Do not touch the new data after passing it in.
/datum/json_database/proc/replace(list/new_data)
cached_data = new_data
queue_save()
/datum/json_database/proc/queue_save()
PRIVATE_PROC(TRUE)
if (save_queued)
return
addtimer(CALLBACK(src, PROC_REF(save)), 0)
/datum/json_database/proc/save()
PRIVATE_PROC(TRUE)
save_queued = FALSE
if (fexists(filepath))
rustg_file_write(file2text(filepath), backup_filepath)
rustg_file_write(json_encode(cached_data, JSON_PRETTY_PRINT), filepath)
ASSERT(!isnull(safe_json_decode(file2text(filepath))), "JSON written to [filepath] was not valid. Backup will be preserved.")
fdel(backup_filepath)
/datum/json_database/proc/load_backup(scenario)
PRIVATE_PROC(TRUE)
var/cached_contents = file2text(backup_filepath)
var/list/backed_up_data = safe_json_decode(cached_contents)
if (isnull(backed_up_data))
stack_trace("[scenario]. Backup existed, but also did not have valid JSON.")
cached_data = list()
else
stack_trace("[scenario]. Backup existed and was used instead. The JSON file has been updated.")
cached_data = backed_up_data
rustg_file_write(cached_contents, filepath)
/datum/json_database/vv_edit_var(var_name, var_value)
switch (var_name)
if (nameof(filepath), nameof(backup_filepath))
return FALSE
else
return ..()