|
11 | 11 | return
|
12 | 12 |
|
13 | 13 | var/atom/movable/AM = holder.marked_datum
|
14 |
| - to_chat(src, json_encode(AM.serialize())) |
| 14 | + |
| 15 | + var/json_data = json_encode(AM.serialize()) |
| 16 | + |
| 17 | + var/choice = alert(usr, "Would you like to store this on your PC or server side?", "Storage Location", "PC", "Server", "Cancel") |
| 18 | + if(!choice || choice == "Cancel") |
| 19 | + return |
| 20 | + |
| 21 | + if(choice == "PC") |
| 22 | + to_chat(src, json_data) |
| 23 | + |
| 24 | + if(choice == "Server") |
| 25 | + // Right, get their slot names |
| 26 | + var/list/slots = list("--NEW--") |
| 27 | + var/datum/db_query/dbq = SSdbcore.NewQuery("SELECT slotname FROM json_datum_saves WHERE ckey=:ckey", list("ckey" = usr.ckey)) |
| 28 | + if(!dbq.warn_execute()) |
| 29 | + qdel(dbq) |
| 30 | + return |
| 31 | + |
| 32 | + while(dbq.NextRow()) |
| 33 | + slots += dbq.item[1] |
| 34 | + |
| 35 | + qdel(dbq) |
| 36 | + |
| 37 | + var/slot_choice = input(usr, "Select a slot to update, or create a new one.", "Slot Selection") as null|anything in slots |
| 38 | + |
| 39 | + if(!slot_choice) |
| 40 | + return |
| 41 | + |
| 42 | + if(slot_choice == "--NEW--") |
| 43 | + // New slot, save to DB |
| 44 | + var/chosen_slot_name = input(usr, "Name your slot", "Slot name") as null|text |
| 45 | + if(!chosen_slot_name || length(chosen_slot_name) == 0) |
| 46 | + return |
| 47 | + |
| 48 | + // Sanitize the name |
| 49 | + var/clean_name = sanitize(copytext(chosen_slot_name, 1, 32)) // 32 chars is your max |
| 50 | + |
| 51 | + // And save |
| 52 | + var/datum/db_query/dbq2 = SSdbcore.NewQuery("INSERT INTO json_datum_saves (ckey, slotname, slotjson) VALUES(:ckey, :slotname, :slotjson)", list( |
| 53 | + "ckey" = usr.ckey, |
| 54 | + "slotname" = clean_name, |
| 55 | + "slotjson" = json_data |
| 56 | + )) |
| 57 | + if(!dbq2.warn_execute()) |
| 58 | + qdel(dbq2) |
| 59 | + return |
| 60 | + |
| 61 | + qdel(dbq2) |
| 62 | + to_chat(usr, "Successfully saved <code>[clean_name]</code>. You can spawn it from <code>Debug > Spawn Saved JSON Datum</code>.") |
| 63 | + |
| 64 | + else |
| 65 | + // Existing slot, warn first |
| 66 | + var/confirmation = alert(usr, "Are you sure you want to update '[slot_choice]'? This cannot be undone!", "You sure?", "Yes", "No") |
| 67 | + if(confirmation != "Yes") |
| 68 | + return |
| 69 | + |
| 70 | + // Now update |
| 71 | + var/datum/db_query/dbq2 = SSdbcore.NewQuery("UPDATE json_datum_saves SET slotjson=:slotjson WHERE slotname=:slotname AND ckey=:ckey", list( |
| 72 | + "slotjson" = json_data, |
| 73 | + "ckey" = usr.ckey, |
| 74 | + "slotname" = slot_choice |
| 75 | + )) |
| 76 | + if(!dbq2.warn_execute()) |
| 77 | + qdel(dbq2) |
| 78 | + return |
| 79 | + |
| 80 | + qdel(dbq2) |
| 81 | + to_chat(usr, "Successfully updated <code>[slot_choice]</code>. You can spawn it from <code>Debug > Spawn Saved JSON Datum</code>.") |
| 82 | + |
15 | 83 |
|
16 | 84 | /client/proc/admin_deserialize()
|
17 | 85 | set name = "Deserialize JSON datum"
|
|
26 | 94 | json_to_object(json_text, get_turf(usr))
|
27 | 95 | message_admins("[key_name_admin(usr)] spawned an atom from a custom JSON object.")
|
28 | 96 | log_admin("[key_name(usr)] spawned an atom from a custom JSON object, JSON Text: [json_text]")
|
| 97 | + |
| 98 | + |
| 99 | +/client/proc/json_spawn_menu() |
| 100 | + set name = "Spawn Saved JSON Datum" |
| 101 | + set desc = "Spawns a JSON datums saved server side" |
| 102 | + set category = "Debug" |
| 103 | + |
| 104 | + // This needs a holder to function |
| 105 | + if(!check_rights(R_SPAWN) || !holder) |
| 106 | + return |
| 107 | + |
| 108 | + // Right, get their slot names |
| 109 | + var/list/slots = list() |
| 110 | + var/datum/db_query/dbq = SSdbcore.NewQuery("SELECT slotname, id FROM json_datum_saves WHERE ckey=:ckey", list("ckey" = usr.ckey)) |
| 111 | + if(!dbq.warn_execute()) |
| 112 | + qdel(dbq) |
| 113 | + return |
| 114 | + |
| 115 | + while(dbq.NextRow()) |
| 116 | + slots[dbq.item[1]] += dbq.item[2] |
| 117 | + qdel(dbq) |
| 118 | + |
| 119 | + |
| 120 | + var/datum/browser/popup = new(usr, "jsonspawnmenu", "JSON Spawn Menu", 400, 300) |
| 121 | + |
| 122 | + // Cache this to reduce proc jumps |
| 123 | + var/holder_uid = holder.UID() |
| 124 | + |
| 125 | + var/list/rows = list() |
| 126 | + rows += "<table><tr><th scope='col' width='90%'>Slot</th><th scope='col' width='10%'>Actions</th></tr>" |
| 127 | + for(var/slotname in slots) |
| 128 | + rows += "<tr><td>[slotname]</td><td><a href='?src=[holder_uid];spawnjsondatum=[slots[slotname]]'>Spawn</a> <a href='?src=[holder_uid];deletejsondatum=[slots[slotname]]'>Delete</a></td></tr>" |
| 129 | + |
| 130 | + rows += "</table>" |
| 131 | + |
| 132 | + popup.set_content(rows.Join("")) |
| 133 | + popup.open(FALSE) |
| 134 | + |
0 commit comments