From e4b21e30d36bff2e8e0ad27263debc9f8532aef3 Mon Sep 17 00:00:00 2001 From: Jordie0608 Date: Mon, 17 Aug 2015 15:24:50 +1000 Subject: [PATCH] autoconvert notes to sql procs --- code/controllers/configuration.dm | 4 + code/modules/admin/admin.dm | 2 +- code/modules/admin/player_notes.dm | 117 ---------------------------- code/modules/admin/sql_notes.dm | 69 +++++++++++++--- code/modules/client/client procs.dm | 3 + config/config.txt | 5 +- 6 files changed, 72 insertions(+), 128 deletions(-) delete mode 100644 code/modules/admin/player_notes.dm diff --git a/code/controllers/configuration.dm b/code/controllers/configuration.dm index c8670e39a3d..7d6a0b3a608 100644 --- a/code/controllers/configuration.dm +++ b/code/controllers/configuration.dm @@ -163,6 +163,8 @@ var/reactionary_explosions = 0 //If we use reactionary explosions, explosions that react to walls and doors + var/autoconvert_notes = 0 //if all connecting player's notes should attempt to be converted to the database + /datum/configuration/New() var/list/L = typesof(/datum/game_mode) - /datum/game_mode for(var/T in L) @@ -343,6 +345,8 @@ if (world.log != newlog) world.log << "Now logging runtimes to data/logs/runtimes/runtime-[time2text(world.realtime, "YYYY-MM-DD")].log" world.log = newlog + if("autoconvert_notes") + config.autoconvert_notes = 1 else diary << "Unknown setting in configuration: '[name]'" diff --git a/code/modules/admin/admin.dm b/code/modules/admin/admin.dm index 71e35f2ff89..e40f3490f91 100644 --- a/code/modules/admin/admin.dm +++ b/code/modules/admin/admin.dm @@ -47,7 +47,7 @@ var/global/floorIsLava = 0 body += "Ban | " body += "Jobban | " body += "Identity Ban | " - body += "Notes | " + body += "Notes | " body += "Watchlist Flag " if(M.client) diff --git a/code/modules/admin/player_notes.dm b/code/modules/admin/player_notes.dm deleted file mode 100644 index 040ac99ef48..00000000000 --- a/code/modules/admin/player_notes.dm +++ /dev/null @@ -1,117 +0,0 @@ -//This stuff was originally intended to be integrated into the ban-system I was working on -//but it's safe to say that'll never be finished. So I've merged it into the current player panel. -//enjoy ~Carn - -#define NOTESFILE "data/player_notes.sav" //where the player notes are saved - -/proc/see_own_notes() - if(!config.see_own_notes) - return - var/ckey = usr.client.ckey - if(!ckey) - usr << "Error: No ckey found." - return - var/savefile/notesfile = new(NOTESFILE) - if(!notesfile) - usr << "Error: Cannot access [NOTESFILE]" - return - notesfile.cd = "/[ckey]" - var/dat = "Notes for [ckey]:
" - while(!notesfile.eof) - var/note - notesfile >> note - dat += note + "
" - var/datum/browser/popup = new(usr, "player_notes", "Player Notes", 700, 400) - popup.set_content(dat) - popup.open() - - -/datum/admins/proc/notes_show(ckey) - usr << browse("Player Notes[notes_gethtml(ckey)]","window=player_notes;size=700x400") - - -/datum/admins/proc/notes_gethtml(ckey) - var/savefile/notesfile = new(NOTESFILE) - if(!notesfile) return "Error: Cannot access [NOTESFILE]" - if(ckey) - . = "Notes for [ckey]: \[+\]
" - notesfile.cd = "/[ckey]" - var/index = 1 - while( !notesfile.eof ) - var/note - notesfile >> note - . += "[note] \[-\]
" - index++ - else - . = "All Notes: \[+\]
" - notesfile.cd = "/" - for(var/dir in notesfile.dir) - . += "[dir]
" - return - - -//handles adding notes to the end of a ckey's buffer -//originally had seperate entries such as var/by to record who left the note and when -//but the current bansystem is a heap of dung. -/proc/notes_add(ckey, note, lognote = 0) - if(!ckey) - ckey = ckey(input(usr,"Who would you like to add notes for?","Enter a ckey",null) as text|null) - if(!ckey) return - - if(!note) - note = html_encode(input(usr,"Enter your note:","Enter some text",null) as message|null) - if(!note) return - - var/savefile/notesfile = new(NOTESFILE) - if(!notesfile) return - notesfile.cd = "/[ckey]" - notesfile.eof = 1 //move to the end of the buffer - notesfile << "[time2text(world.realtime,"DD-MMM-YYYY")] | [note][(usr && usr.ckey)?" ~[usr.ckey]":""]" - - if(lognote)//don't need an admin log for the notes applied automatically during bans. - message_admins("[key_name(usr)] added note '[note]' to [ckey]") - log_admin("[key_name(usr)] added note '[note]' to [ckey]") - - return - -//handles removing entries from the buffer, or removing the entire directory if no start_index is given -/proc/notes_remove(ckey, start_index, end_index) - var/savefile/notesfile = new(NOTESFILE) - var/admin_msg - if(!notesfile) return - - if(!ckey) - notesfile.cd = "/" - ckey = ckey(input(usr,"Who would you like to remove notes for?","Enter a ckey",null) as null|anything in notesfile.dir) - if(!ckey) return - - if(start_index) - notesfile.cd = "/[ckey]" - var/list/noteslist = list() - if(!end_index) end_index = start_index - var/index = 0 - while( !notesfile.eof ) - index++ - var/temp - notesfile >> temp - if( (start_index <= index) && (index <= end_index) ) - admin_msg = temp - continue - - noteslist += temp - - notesfile.eof = -2 //Move to the start of the buffer and then erase. - - for( var/note in noteslist ) - notesfile << note - - message_admins("[key_name(usr)] removed a note '[admin_msg]' from [ckey]") - log_admin("[key_name(usr)] removed a note '[admin_msg]' from [ckey]") - - if(noteslist.len == 0) - notesfile.cd = "/" - notesfile.dir.Remove(ckey) - message_admins("[ckey] has no notes and was removed from the notes list.") - return - -#undef NOTESFILE \ No newline at end of file diff --git a/code/modules/admin/sql_notes.dm b/code/modules/admin/sql_notes.dm index fe9aeeeb2c5..54af84cb2b9 100644 --- a/code/modules/admin/sql_notes.dm +++ b/code/modules/admin/sql_notes.dm @@ -1,4 +1,4 @@ -proc/add_note(target_ckey, notetext, timestamp, adminckey, logged = 1) +/proc/add_note(target_ckey, notetext, timestamp, adminckey, logged = 1, server) if(!dbcon.IsConnected()) usr << "Failed to establish database connection." return @@ -28,10 +28,10 @@ proc/add_note(target_ckey, notetext, timestamp, adminckey, logged = 1) adminckey = usr.ckey if(!adminckey) return - var/server - if (config && config.server_name) - server = config.server_name var/admin_sql_ckey = sanitizeSQL(adminckey) + if(!server) + if (config && config.server_name) + server = config.server_name var/DBQuery/query_noteadd = dbcon.NewQuery("INSERT INTO [format_table_name("notes")] (ckey, timestamp, notetext, adminckey, server) VALUES ('[target_sql_ckey]', '[timestamp]', '[notetext]', '[admin_sql_ckey]', '[server]')") if(!query_noteadd.Execute()) var/err = query_noteadd.ErrorMsg() @@ -40,9 +40,9 @@ proc/add_note(target_ckey, notetext, timestamp, adminckey, logged = 1) if(logged) log_admin("[key_name(usr)] has added a note to [target_ckey]: [notetext]") message_admins("[key_name_admin(usr)] has added a note to [target_ckey]:
[notetext]") - show_note(target_ckey) + show_note(target_ckey) -proc/remove_note(note_id) +/proc/remove_note(note_id) var/ckey var/notetext var/adminckey @@ -69,7 +69,7 @@ proc/remove_note(note_id) message_admins("[key_name_admin(usr)] has removed a note made by [adminckey] from [ckey]:
[notetext]") show_note(ckey) -proc/edit_note(note_id) +/proc/edit_note(note_id) if(!dbcon.IsConnected()) usr << "Failed to establish database connection." return @@ -100,7 +100,7 @@ proc/edit_note(note_id) message_admins("[key_name_admin(usr)] has edited [target_ckey]'s note made by [adminckey] from
[old_note]
to
[new_note]") show_note(target_ckey) -proc/show_note(target_ckey, index, linkless = 0) +/proc/show_note(target_ckey, index, linkless = 0) var/output var/navbar var/ruler @@ -160,4 +160,55 @@ proc/show_note(target_ckey, index, linkless = 0) else output += "
\[Add Note\]
" output += ruler - usr << browse(output, "window=show_notes;size=900x500") \ No newline at end of file + usr << browse(output, "window=show_notes;size=900x500") + +/proc/regex_note_sql_extract(str, exp) + return new /datum/regex(str, exp, call(LIBREGEX_LIBRARY, "regEx_find")(str, exp)) + +#define NOTESFILE "data/player_notes.sav" +//if the AUTOCONVERT_NOTES is turned on, anytime a player connects this will be run to try and add all their notes to the databas +/proc/convert_notes_sql(ckey) + var/savefile/notesfile = new(NOTESFILE) + if(!notesfile) + log_game("Error: Cannot access [NOTESFILE]") + return + notesfile.cd = "/[ckey]" + while(!notesfile.eof) + var/notetext + notesfile >> notetext + var/server + if (config && config.server_name) + server = config.server_name + var/regex = "^(\\d{2}-\\w{3}-\\d{4}) \\| (.+) ~(\\w+)$" + var/datum/regex/results = regex_note_sql_extract(notetext, regex) + var/timestamp = results.str(2) + notetext = results.str(3) + var/adminckey = results.str(4) + var/DBQuery/query_convert_time = dbcon.NewQuery("SELECT ADDTIME(STR_TO_DATE('[timestamp]','%d-%b-%Y'), '0')") + if(!query_convert_time.Execute()) + var/err = query_convert_time.ErrorMsg() + log_game("SQL ERROR converting timestamp. Error : \[[err]\]\n") + return + if(query_convert_time.NextRow()) + timestamp = query_convert_time.item[1] + if(ckey && notetext && timestamp && adminckey && server) + add_note(ckey, notetext, timestamp, adminckey, 0, server) + else + notesfile.cd = "/" + notesfile.dir.Remove(ckey) + +/*alternatively this proc can be run once to pass through every note and attempt to convert it before deleting the file, if done then AUTOCONVERT_NOTES should be turned off +this proc can take several minutes to execute fully if converting and cause DD to hang if converting a lot of notes; it's not advised to do so while a server is live +/proc/mass_convert_notes() + world << "Beginning mass note conversion" + var/savefile/notesfile = new(NOTESFILE) + if(!notesfile) + log_game("Error: Cannot access [NOTESFILE]") + return + notesfile.cd = "/" + for(var/ckey in notesfile.dir) + convert_notes_sql(ckey) + world << "Deleting NOTESFILE" + fdel(NOTESFILE) + world << "Finished mass note conversion, remember to turn off AUTOCONVERT_NOTES"*/ +#undef NOTESFILE \ No newline at end of file diff --git a/code/modules/client/client procs.dm b/code/modules/client/client procs.dm index 15de84c0369..233fee84513 100644 --- a/code/modules/client/client procs.dm +++ b/code/modules/client/client procs.dm @@ -175,6 +175,9 @@ var/next_external_rsc = 0 else winset(src, "rpane.changelogb", "background-color=#eaeaea;font-style=bold") + if (config && config.autoconvert_notes) + convert_notes_sql(ckey) + ////////////// //DISCONNECT// ////////////// diff --git a/config/config.txt b/config/config.txt index c40c7cc783f..6f12881387b 100644 --- a/config/config.txt +++ b/config/config.txt @@ -193,4 +193,7 @@ NOTIFY_NEW_PLAYER_AGE 0 #AGGRESSIVE_CHANGELOG ## Uncomment to have the game log runtimes to the log folder. (Note: this disables normal output in dd/ds, so it should be left off for testing. -#LOG_RUNTIMES \ No newline at end of file +#LOG_RUNTIMES + +##Comment this out if you've used the mass conversion sql proc for notes or want to stop converting notes +AUTOCONVERT_NOTES \ No newline at end of file