Files
Aurora.3/code/modules/client/preference_setup/other/01_incidents.dm
skull132 a3ec0cf45d Better SQL prepared statements (#2474)
The system used to be of complexity O(n^2). Essentially two for loops running per every argument. Which ended up being surprisingly slow (there were instances where I saw the argument parser as using quite a lot of CPU time).

This replaces it with a more linear algorithm. It's somewhere near O(n) where n is the length of the unparsed query. Which is more stable and faaaster. This comes with two changes, however:

Parameters inside the query now have to be delimited from both sides with : (colons). The alternative to this would be to use something like $n or just assume that space marks the end of a marker. Only the former is workable, the latter would break a few queries already.
Arguments in the argument array no longer have to be prefixed by : (colons). So, while in the query you would write :thing:, you'd initialize the array of args as: list("thing" = somevar). It could be made to work without it, but eh, I think this is fine.
Argument validation is slightly weaker. What I mean by this is that with the old system, unused keys would result in an error. This is no longer a thing. Missing keys will still result in an error, however.
One more improvement: double delimiting removes an edge case where if key A partially covers key B, depending on the order, key A would mangle key B.
Updated and tested all queries that I could find. So this should be good.
2017-05-29 21:17:41 +03:00

108 lines
4.0 KiB
Plaintext

/datum/category_item/player_setup_item/other/incidents
name = "Incidents"
sort_order = 7
/datum/category_item/player_setup_item/other/incidents/load_special(var/savefile/S)
pref.incidents = list()
//Special Aurora Snowflake to load in the ccia actions and persistant incidents
if (config.sql_saves) // Doesnt work without db
//Load in the CCIA Actions
var/DBQuery/ccia_action_query = dbcon.NewQuery({"SELECT
act.title,
act.type,
act.issuedby,
act.details,
act.url,
act.expires_at
FROM ss13_ccia_action_char act_chr
JOIN ss13_characters chr ON act_chr.char_id = chr.id
JOIN ss13_ccia_actions act ON act_chr.action_id = act.id
WHERE
act_chr.char_id = :char_id: AND
(act.expires_at IS NULL OR act.expires_at >= CURRENT_DATE()) AND
act.deleted_at IS NULL;
"})
if (!ccia_action_query.Execute(list("char_id" = pref.current_character)))
error("Error CCIA Actions for character #[pref.current_character]. SQL error message: '[ccia_action_query.ErrorMsg()]'.")
while(ccia_action_query.NextRow())
var/list/action = list(
ccia_action_query.item[1],
ccia_action_query.item[2],
ccia_action_query.item[3],
ccia_action_query.item[4],
ccia_action_query.item[5],
ccia_action_query.item[6]
)
pref.ccia_actions.Add(list(action))
//Load in the infractions
var/DBQuery/char_infraction_query = dbcon.NewQuery({"SELECT
id, char_id, UID, datetime, notes, charges, evidence, arbiters, brig_sentence, fine, felony
FROM ss13_character_incidents
WHERE
char_id = :char_id: AND deleted_at IS NULL
"})
char_infraction_query.Execute(list("char_id" = pref.current_character))
while(char_infraction_query.NextRow())
var/datum/char_infraction/infraction = new()
infraction.db_id = text2num(char_infraction_query.item[1])
infraction.char_id = text2num(char_infraction_query.item[2])
infraction.UID = char_infraction_query.item[3]
infraction.datetime = char_infraction_query.item[4]
infraction.notes = char_infraction_query.item[5]
infraction.charges = json_decode(char_infraction_query.item[6])
infraction.evidence = json_decode(char_infraction_query.item[7])
infraction.arbiters = json_decode(char_infraction_query.item[8])
infraction.brig_sentence = text2num(char_infraction_query.item[9])
infraction.fine = text2num(char_infraction_query.item[10])
infraction.felony = text2num(char_infraction_query.item[11])
pref.incidents.Add(infraction)
/datum/category_item/player_setup_item/other/incidents/content(var/mob/user)
pref.incidents = list()
. += "<b>Incident Information</b><br>"
. += "The following incidents are on file for your character<br>"
for (var/datum/char_infraction/I in pref.incidents)
. += "<hr>"
. += "UID: [I.UID]<br>"
. += "Date/Time: [I.datetime]<br>"
. += "Charges: "
for (var/L in I.charges)
. += "[L], "
if (I.fine == 0)
. += "<br>Brig Sentence: [I.getBrigSentence()] <br>"
else
. += "Fine: [I.fine] Credits<br>"
. += "Notes: <br>"
if (I.notes != "")
. += nl2br(I.notes)
else
. += "- No Summary Entered -"
. += "<br><a href='?src=\ref[src];details_sec_incident=[I.db_id]'>Show Details</a><br><a href='?src=\ref[src];del_sec_incident=[I.db_id]'>Delete Incident</a>"
/datum/category_item/player_setup_item/other/incidents/OnTopic(var/href,var/list/href_list, var/mob/user)
if(href_list["del_sec_incident"])
var/search_incident = text2num(href_list["del_sec_incident"])
var/confirm = alert(user,"Do you want to delete that incident ?","Delete Incident","Yes","No")
if(!search_incident || !CanUseTopic(user) || confirm == "No")
return TOPIC_NOACTION
for(var/datum/char_infraction/I in pref.incidents)
if(I.db_id == search_incident && I.char_id == pref.current_character)
I.deleteFromDB("user")
qdel(I)
return TOPIC_REFRESH
else if(href_list["details_sec_incident"])
if(!CanUseTopic(user))
return TOPIC_NOACTION
var/list/params = list("location" = "security_incident", "incident" = href_list["details_sec_incident"])
usr.client.process_webint_link("interface/login/sso_server", list2params(params))
return ..()