From 6041ec5c69bd74363bd574c561c54eaf0a7ad203 Mon Sep 17 00:00:00 2001 From: "only.lurking@gmail.com" Date: Sun, 5 Sep 2010 04:29:17 +0000 Subject: [PATCH] Centralized SQL configuration data into dbconfig.txt. Server hosts should never have to edit their source to get their MySQL servers talking to SS13, now. git-svn-id: http://tgstation13.googlecode.com/svn/trunk@57 316c924e-a436-60f5-8080-3fe189b3f50e --- code/datums/configuration.dm | 51 +++++++++++++++++++++++++++ code/defines/global.dm | 14 +++++++- code/defines/procs/dbcore.dm | 7 ++-- code/defines/procs/statistics.dm | 37 ++++++++----------- code/game/cellautomata.dm | 1 + code/game/magic/library.dm | 11 +++--- code/game/spacecraft/manufacturing.dm | 4 ++- code/game/verbs/checkkarma.dm | 2 +- config/dbconfig.txt | 21 +++++++++++ 9 files changed, 117 insertions(+), 31 deletions(-) create mode 100644 config/dbconfig.txt diff --git a/code/datums/configuration.dm b/code/datums/configuration.dm index 4c6fb4fdc0b..69e99c3000b 100644 --- a/code/datums/configuration.dm +++ b/code/datums/configuration.dm @@ -173,6 +173,57 @@ else diary << "Unknown setting in configuration: '[name]'" +/datum/configuration/proc/loadsql(filename) // -- TLE + var/text = file2text(filename) + + if (!text) + diary << "No dbconfig.txt file found, retaining defaults" + world << "No dbconfig.txt file found, retaining defaults" + return + + diary << "Reading database configuration file [filename]" + + var/list/CL = dd_text2list(text, "\n") + + for (var/t in CL) + if (!t) + continue + + t = trim(t) + if (length(t) == 0) + continue + else if (copytext(t, 1, 2) == "#") + continue + + var/pos = findtext(t, " ") + var/name = null + var/value = null + + if (pos) + name = lowertext(copytext(t, 1, pos)) + value = copytext(t, pos + 1) + else + name = lowertext(t) + + if (!name) + continue + + switch (name) + if ("address") + sqladdress = value + if ("port") + sqlport = value + if ("database") + sqldb = value + if ("login") + sqllogin = value + if ("password") + sqlpass = value + if ("enable_stat_tracking") + sqllogging = 1 + else + diary << "Unknown setting in configuration: '[name]'" + /datum/configuration/proc/pick_mode(mode_name) // I wish I didn't have to instance the game modes in order to look up // their information, but it is the only way (at least that I know of). diff --git a/code/defines/global.dm b/code/defines/global.dm index f9829f4595a..d839b123f15 100644 --- a/code/defines/global.dm +++ b/code/defines/global.dm @@ -41,7 +41,7 @@ var diary = null station_name = null - game_version = "Goon Dev Station 13" + game_version = "/tg/ Station 13" datum/air_tunnel/air_tunnel1/SS13_airtunnel = null going = 1.0 @@ -163,3 +163,15 @@ var const/shuttle_time_in_station = 1800 // 3 minutes in the station const/shuttle_time_to_arrive = 6000 // 10 minutes to arrive + + + + // MySQL configuration + + sqladdress = "localhost" + sqlport = "3306" + sqldb = "tgstation" + sqllogin = "root" + sqlpass = "" + + sqllogging = 0 // Should we log deaths, population stats, etc? diff --git a/code/defines/procs/dbcore.dm b/code/defines/procs/dbcore.dm index 8a032847bfa..ff63a1d6dec 100644 --- a/code/defines/procs/dbcore.dm +++ b/code/defines/procs/dbcore.dm @@ -35,10 +35,12 @@ #define BLOB 14 // TODO: Investigate more recent type additions and see if I can handle them. - Nadrew - +// Deprecated! See global.dm for new configuration vars +/* var DB_SERVER = "" // This is the location of your MySQL server (localhost is USUALLY fine) DB_PORT = 3306 // This is the port your MySQL server is running on (3306 is the default) +*/ DBConnection New(dbi_handler,username,password_handler,cursor_handler) @@ -63,7 +65,8 @@ DBConnection ErrorMsg() return _dm_db_error_msg(_db_con) SelectDB(database_name,dbi) if(IsConnected()) Disconnect() - return Connect("[dbi?"[dbi]":"dbi:mysql:[database_name]:[DB_SERVER]:[DB_PORT]"]",user,password) + //return Connect("[dbi?"[dbi]":"dbi:mysql:[database_name]:[DB_SERVER]:[DB_PORT]"]",user,password) + return Connect("[dbi?"[dbi]":"dbi:mysql:[database_name]:[sqladdress]:[sqlport]"]",user,password) NewQuery(sql_query,cursor_handler=src.default_cursor) return new/DBQuery(sql_query,src,cursor_handler) var diff --git a/code/defines/procs/statistics.dm b/code/defines/procs/statistics.dm index 37f9cf4b362..dda6e43a00c 100644 --- a/code/defines/procs/statistics.dm +++ b/code/defines/procs/statistics.dm @@ -1,21 +1,12 @@ -#define SQL_ADDRESS "yourserver.com" -#define SQL_DB "yourdbname" -#define SQL_PORT "3306" // 3306 is default -#define SQL_LOGIN "yourlogin" -#define SQL_PASS "yourpassword" - - -#define STATLOGGING 0 // Should use #ifdef here - proc/sql_poll_players() - if(!STATLOGGING) + if(!sqllogging) return var/playercount = 0 for(var/mob/M in world) if(M.client) playercount += 1 var/DBConnection/dbcon = new() - dbcon.Connect("dbi:mysql:[SQL_DB]:[SQL_ADDRESS]:[SQL_PORT]","[SQL_LOGIN]","[SQL_PASS]") + dbcon.Connect("dbi:mysql:[sqldb]:[sqladdress]:[sqlport]","[sqllogin]","[sqlpass]") if(!dbcon.IsConnected()) log_game("SQL ERROR during player polling. Failed to connect.") else @@ -28,14 +19,14 @@ proc/sql_poll_players() proc/sql_poll_admins() - if(!STATLOGGING) + if(!sqllogging) return var/admincount = 0 for (var/mob/M in world) if(M && M.client && M.client.holder && M.client.authenticated) admincount += 1 var/DBConnection/dbcon = new() - dbcon.Connect("dbi:mysql:[SQL_DB]:[SQL_ADDRESS]:[SQL_PORT]","[SQL_LOGIN]","[SQL_PASS]") + dbcon.Connect("dbi:mysql:[sqldb]:[sqladdress]:[sqlport]","[sqllogin]","[sqlpass]") if(!dbcon.IsConnected()) log_game("SQL ERROR during admin polling. Failed to connect.") else @@ -47,14 +38,16 @@ proc/sql_poll_admins() dbcon.Disconnect() proc/sql_report_round_start() - if(!STATLOGGING) + // TODO + if(!sqllogging) return proc/sql_report_round_end() - if(!STATLOGGING) + // TODO + if(!sqllogging) return proc/sql_report_karma(var/mob/spender, var/mob/receiver, var/isnegative = 1) - if(!STATLOGGING) + if(!sqllogging) return var/sqlspendername = spender.name var/sqlspenderkey = spender.key @@ -78,7 +71,7 @@ proc/sql_report_karma(var/mob/spender, var/mob/receiver, var/isnegative = 1) sqlreceiverrole = receiver.mind.assigned_role var/DBConnection/dbcon = new() - dbcon.Connect("dbi:mysql:[SQL_DB]:[SQL_ADDRESS]:[SQL_PORT]","[SQL_LOGIN]","[SQL_PASS]") + dbcon.Connect("dbi:mysql:[sqldb]:[sqladdress]:[sqlport]","[sqllogin]","[sqlpass]") if(!dbcon.IsConnected()) log_game("SQL ERROR during karma logging. Failed to connect.") else @@ -123,7 +116,7 @@ proc/sql_report_karma(var/mob/spender, var/mob/receiver, var/isnegative = 1) proc/sql_report_death(var/mob/living/carbon/human/H) - if(!STATLOGGING) + if(!sqllogging) return if(!H) return @@ -148,7 +141,7 @@ proc/sql_report_death(var/mob/living/carbon/human/H) var/coord = "[H.x], [H.y], [H.z]" //world << "INSERT INTO death (name, byondkey, job, special, pod, tod, laname, lakey, gender, bruteloss, fireloss, brainloss, oxyloss) VALUES ('[sqlname]', '[sqlkey]', '[sqljob]', '[sqlspecial]', '[sqlpod]', '[sqltime]', '[laname]', '[lakey]', '[H.gender]', [H.bruteloss], [H.fireloss], [H.brainloss], [H.oxyloss])" var/DBConnection/dbcon = new() - dbcon.Connect("dbi:mysql:[SQL_DB]:[SQL_ADDRESS]:[SQL_PORT]","[SQL_LOGIN]","[SQL_PASS]") + dbcon.Connect("dbi:mysql:[sqldb]:[sqladdress]:[sqlport]","[sqllogin]","[sqlpass]") if(!dbcon.IsConnected()) log_game("SQL ERROR during death reporting. Failed to connect.") else @@ -160,7 +153,7 @@ proc/sql_report_death(var/mob/living/carbon/human/H) proc/sql_report_cyborg_death(var/mob/living/silicon/robot/H) - if(!STATLOGGING) + if(!sqllogging) return if(!H) return @@ -185,7 +178,7 @@ proc/sql_report_cyborg_death(var/mob/living/silicon/robot/H) var/coord = "[H.x], [H.y], [H.z]" //world << "INSERT INTO death (name, byondkey, job, special, pod, tod, laname, lakey, gender, bruteloss, fireloss, brainloss, oxyloss) VALUES ('[sqlname]', '[sqlkey]', '[sqljob]', '[sqlspecial]', '[sqlpod]', '[sqltime]', '[laname]', '[lakey]', '[H.gender]', [H.bruteloss], [H.fireloss], [H.brainloss], [H.oxyloss])" var/DBConnection/dbcon = new() - dbcon.Connect("dbi:mysql:[SQL_DB]:[SQL_ADDRESS]:[SQL_PORT]","[SQL_LOGIN]","[SQL_PASS]") + dbcon.Connect("dbi:mysql:[sqldb]:[sqladdress]:[sqlport]","[sqllogin]","[sqlpass]") if(!dbcon.IsConnected()) log_game("SQL ERROR during death reporting. Failed to connect.") else @@ -197,7 +190,7 @@ proc/sql_report_cyborg_death(var/mob/living/silicon/robot/H) proc/statistic_cycle() - if(!STATLOGGING) + if(!sqllogging) return while(1) sql_poll_players() diff --git a/code/game/cellautomata.dm b/code/game/cellautomata.dm index 9754747497e..cf2f2a9dbe5 100644 --- a/code/game/cellautomata.dm +++ b/code/game/cellautomata.dm @@ -64,6 +64,7 @@ /world/proc/load_configuration() config = new /datum/configuration() config.load("config/config.txt") + config.loadsql("config/dbconfig.txt") // apply some settings from config.. abandon_allowed = config.respawn diff --git a/code/game/magic/library.dm b/code/game/magic/library.dm index 62d3c66529b..ef0d0cf5f8f 100644 --- a/code/game/magic/library.dm +++ b/code/game/magic/library.dm @@ -4,11 +4,14 @@ // //******************************* +// Deprecated! See global.dm for new SQL config vars -- TLE +/* #define SQL_ADDRESS "" #define SQL_DB "" #define SQL_PORT "3306" #define SQL_LOGIN "" #define SQL_PASS "" +*/ //******************************* // Requires Dantom.DB library ( http://www.byond.com/developer/Dantom/DB ) @@ -285,7 +288,7 @@ datum/borrowbook // Datum used to keep track of who has borrowed what when and f dat += "\[Start Search\]
" if(1) var/DBConnection/dbcon = new() - dbcon.Connect("dbi:mysql:[SQL_DB]:[SQL_ADDRESS]:[SQL_PORT]","[SQL_LOGIN]","[SQL_PASS]") + dbcon.Connect("dbi:mysql:[sqldb]:[sqladdress]:[sqlport]","[sqllogin]","[sqlpass]") if(!dbcon.IsConnected()) dat += "ERROR: Unable to contact External Archive. Please contact your system administrator for assistance.
" else if(!SQLquery) @@ -416,7 +419,7 @@ datum/borrowbook // Datum used to keep track of who has borrowed what when and f if(4) dat += "

External Archive

" var/DBConnection/dbcon = new() - dbcon.Connect("dbi:mysql:[SQL_DB]:[SQL_ADDRESS]:[SQL_PORT]","[SQL_LOGIN]","[SQL_PASS]") + dbcon.Connect("dbi:mysql:[sqldb]:[sqladdress]:[sqlport]","[sqllogin]","[sqlpass]") if(!dbcon.IsConnected()) dat += "ERROR: Unable to contact External Archive. Please contact your system administrator for assistance." else @@ -522,7 +525,7 @@ datum/borrowbook // Datum used to keep track of who has borrowed what when and f var/choice = input("Are you certain you wish to upload this title to the Archive?") in list("Confirm", "Abort") if(choice == "Confirm") var/DBConnection/dbcon = new() - dbcon.Connect("dbi:mysql:[SQL_DB]:[SQL_ADDRESS]:[SQL_PORT]","[SQL_LOGIN]","[SQL_PASS]") + dbcon.Connect("dbi:mysql:[sqldb]:[sqladdress]:[sqlport]","[sqllogin]","[sqlpass]") if(!dbcon.IsConnected()) alert("Connection to Archive has been severed. Aborting.") else @@ -546,7 +549,7 @@ datum/borrowbook // Datum used to keep track of who has borrowed what when and f if(href_list["targetid"]) var/sqlid = href_list["targetid"] var/DBConnection/dbcon = new() - dbcon.Connect("dbi:mysql:[SQL_DB]:[SQL_ADDRESS]:[SQL_PORT]","[SQL_LOGIN]","[SQL_PASS]") + dbcon.Connect("dbi:mysql:[sqldb]:[sqladdress]:[sqlport]","[sqllogin]","[sqlpass]") if(!dbcon.IsConnected()) alert("Connection to Archive has been severed. Aborting.") else diff --git a/code/game/spacecraft/manufacturing.dm b/code/game/spacecraft/manufacturing.dm index 668edc15c01..172ce5f519e 100644 --- a/code/game/spacecraft/manufacturing.dm +++ b/code/game/spacecraft/manufacturing.dm @@ -241,7 +241,9 @@ obj/machinery/smelter/Topic(href, href_list) var gameticker + gameworld New() ..() - gameticker = ticker \ No newline at end of file + gameticker = ticker + gameworld = world \ No newline at end of file diff --git a/code/game/verbs/checkkarma.dm b/code/game/verbs/checkkarma.dm index a7090e93b16..72ac4117b54 100644 --- a/code/game/verbs/checkkarma.dm +++ b/code/game/verbs/checkkarma.dm @@ -4,7 +4,7 @@ mob/verb/check_karma() set desc = "Reports how much karma you have accrued" var/DBConnection/dbcon = new() - dbcon.Connect("dbi:mysql:[SQL_DB]:[SQL_ADDRESS]:[SQL_PORT]","[SQL_LOGIN]","[SQL_PASS]") + dbcon.Connect("dbi:mysql:[sqldb]:[sqladdress]:[sqlport]","[sqllogin]","[sqlpass]") if(!dbcon.IsConnected()) usr << "\red Unable to connect to karma database. This error can occur if your host has failed to set up an SQL database or improperly configured its login credentials.
" return diff --git a/config/dbconfig.txt b/config/dbconfig.txt new file mode 100644 index 00000000000..92508a18d1c --- /dev/null +++ b/config/dbconfig.txt @@ -0,0 +1,21 @@ +# MySQL Connection Configuration + +# Server the MySQL database can be found at +# Examples: localhost, 200.135.5.43, www.mysqldb.com, etc. +ADDRESS localhost + +# MySQL server port (default is 3306) +PORT 3306 + +# Database the population, death, karma, etc. tables may be found in +DATABASE tgstation + +# Username/Login used to access the database +LOGIN mylogin + +# Password used to access the database +PASSWORD mypassword + +# Track population and death statistics +# Comment this out to disable +ENABLE_STAT_TRACKING \ No newline at end of file