From 37f2e97a3ccdf902c253670050676e3c0b807307 Mon Sep 17 00:00:00 2001 From: nevimer <77420409+nevimer@users.noreply.github.com> Date: Fri, 4 Oct 2024 07:16:32 -0400 Subject: [PATCH] Vetted System conversion to SQL (#2075) ## About The Pull Request This PR will convert the vetted system to SQL. It will populate the with entries from the old config file, and then that file can be safely removed after it is imported. Also adds three verbs, one to force a conversion, one to add, and one to remove a ckey from the database. The admin who adds a user is record in the DB. ## Why It's Good For The Game ## Proof Of Testing ![heidisql_086ihiIsfK](https://github.com/user-attachments/assets/b3f75490-b709-4c7f-883c-13c9c59ac9b1) ## Changelog :cl: refactor: Vetted system is now using SQL and improved. /:cl: --------- Co-authored-by: The Sharkening <95130227+StrangeWeirdKitten@users.noreply.github.com> --- SQL/bubber_schema.sql | 11 +++ SQL/database_changelog.md | 4 +- code/__DEFINES/subsystems.dm | 2 +- modular_zubbers/code/modules/vetted/vetted.dm | 87 +++++++++++++++---- 4 files changed, 86 insertions(+), 18 deletions(-) diff --git a/SQL/bubber_schema.sql b/SQL/bubber_schema.sql index 76e7c91742d..cfa5e7b6b3d 100644 --- a/SQL/bubber_schema.sql +++ b/SQL/bubber_schema.sql @@ -22,3 +22,14 @@ CREATE TABLE `jobexempt` ( PRIMARY KEY (`ckey`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci; /*!40101 SET character_set_client = @saved_cs_client */; + + +DROP TABLE IF EXISTS `vetted_list`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `vetted_list` ( + `ckey` varchar(32) NOT NULL, + `admin_who_added` VARCHAR(32) DEFAULT NULL, + PRIMARY KEY (`ckey`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci; +/*!40101 SET character_set_client = @saved_cs_client */; diff --git a/SQL/database_changelog.md b/SQL/database_changelog.md index bad8912ce93..eb4b8f84099 100644 --- a/SQL/database_changelog.md +++ b/SQL/database_changelog.md @@ -2,10 +2,10 @@ Any time you make a change to the schema files, remember to increment the databa Make sure to also update `DB_MAJOR_VERSION` and `DB_MINOR_VERSION`, which can be found in `code/__DEFINES/subsystem.dm`. -The latest database version is 5.30 (5.27 for /tg/); The query to update the schema revision table is: +The latest database version is 5.31 (for bubberstation, 5.30 for skyrat) (5.27 for /tg/); The query to update the schema revision table is: ```sql -INSERT INTO `schema_revision` (`major`, `minor`) VALUES (5, 30); +INSERT INTO `schema_revision` (`major`, `minor`) VALUES (5, 31); ``` or diff --git a/code/__DEFINES/subsystems.dm b/code/__DEFINES/subsystems.dm index 73241b9d34b..b3b377fc029 100644 --- a/code/__DEFINES/subsystems.dm +++ b/code/__DEFINES/subsystems.dm @@ -20,7 +20,7 @@ * * make sure you add an update to the schema_version stable in the db changelog */ -#define DB_MINOR_VERSION 30 +#define DB_MINOR_VERSION 31 // BUBBER EDIT //! ## Timing subsystem diff --git a/modular_zubbers/code/modules/vetted/vetted.dm b/modular_zubbers/code/modules/vetted/vetted.dm index cfe18bf1563..7f9d537619a 100644 --- a/modular_zubbers/code/modules/vetted/vetted.dm +++ b/modular_zubbers/code/modules/vetted/vetted.dm @@ -1,11 +1,14 @@ +GLOBAL_LIST_EMPTY(vetted_list_legacy) +GLOBAL_PROTECT(vetted_list_legacy) GLOBAL_LIST_EMPTY(vetted_list) GLOBAL_PROTECT(vetted_list) - /datum/controller/subsystem/player_ranks + var/loaded_vetted_sql = FALSE /datum/player_rank_controller/vetted rank_title = "vetted user" var/file_path_vetted + /datum/controller/subsystem/player_ranks/proc/is_vetted(client/user, admin_bypass = TRUE) if(!istype(user)) CRASH("Invalid user type provided to is_vetted(), expected 'client' and obtained '[user ? user.type : "null"]'.") @@ -24,33 +27,87 @@ GLOBAL_PROTECT(vetted_list) /datum/controller/subsystem/player_ranks/proc/load_vetted_ckeys() PROTECTED_PROC(TRUE) - + if(loaded_vetted_sql) + return if(IsAdminAdvancedProcCall()) return vetted_controller = new vetted_controller.file_path_vetted = "[global.config.directory]/bubbers/vetted_players.txt" - for(var/line in world.file2list(vetted_controller.file_path_vetted)) - if(!line) - continue + ASYNC + for(var/line in world.file2list(vetted_controller.file_path_vetted)) + if(!line) + continue - if(findtextEx(line, "#", 1, 2)) - continue + if(findtextEx(line, "#", 1, 2)) + continue - vetted_controller.add_player(line) + vetted_controller.add_player(line, legacy = TRUE) + world.log << "Added [line] to vetted list." + var/datum/db_query/query_load_player_rank = SSdbcore.NewQuery("SELECT * FROM vetted_list") + if(!query_load_player_rank.warn_execute()) + return + while(query_load_player_rank.NextRow()) + var/ckey = ckey(query_load_player_rank.item[1]) + vetted_controller.add_player(ckey) + world.log << "Added [ckey] to vetted list." + loaded_vetted_sql = TRUE return TRUE -/datum/player_rank_controller/vetted/add_player(ckey) - if(IsAdminAdvancedProcCall()) - return +/datum/player_rank_controller/vetted/proc/convert_all_to_sql() + if(!SSdbcore.Connect()) + return message_admins("Failed to connect to database. Unable to complete flat file to SQL conversion.") + for(var/ckey_ in GLOB.vetted_list_legacy) + add_player_to_sql(ckey_) +/datum/player_rank_controller/vetted/proc/add_player_to_sql(ckey, admin_mob) + var/ckey_admin = "Conversion Script" + var/mob/admin_who_added_client = admin_mob + if(istype(admin_who_added_client, /mob) && admin_who_added_client.client) + ckey_admin = admin_who_added_client?.client?.ckey + + var/datum/db_query/query_add_player_rank = SSdbcore.NewQuery( + "INSERT INTO vetted_list (ckey, admin_who_added) VALUES(:ckey, :admin_who_added) \ + ON DUPLICATE KEY UPDATE admin_who_added = :admin_who_added", + list("ckey" = ckey, "admin_who_added" = ckey_admin), + ) + + if(!query_add_player_rank.warn_execute()) + return FALSE + +/datum/player_rank_controller/vetted/add_player(ckey, legacy, admin) ckey = ckey(ckey) - + if(legacy) + GLOB.vetted_list_legacy[ckey] = TRUE GLOB.vetted_list[ckey] = TRUE + add_player_to_sql(ckey, admin) /datum/player_rank_controller/vetted/remove_player(ckey) - if(IsAdminAdvancedProcCall()) - return - GLOB.vetted_list -= ckey + remove_player_from_sql(ckey) + +/datum/player_rank_controller/vetted/proc/remove_player_from_sql(ckey) + var/datum/db_query/query_remove_player_vetted = SSdbcore.NewQuery( + "DELETE FROM vetted_list WHERE ckey = :ckey", + list("ckey" = ckey), + ) + if(!query_remove_player_vetted.warn_execute()) + return FALSE + +ADMIN_VERB(convert_flatfile_vettedlist_to_sql, R_DEBUG, "Convert Vetted list to SQL", "Warning! Might be slow!", ADMIN_CATEGORY_DEBUG) + var/consent = tgui_input_list(usr, "Do you want to convert the vetted list to SQL?", "UH OH", list("Yes", "No"), "No") + if(consent == "Yes") + SSplayer_ranks.vetted_controller.convert_all_to_sql() + message_admins("[usr] has forcefully converted the vetted list file to SQL.") +ADMIN_VERB(add_vetted, R_ADMIN, "Add user to Vetted", "Adds a user to the vetted list", ADMIN_CATEGORY_MAIN) + var/user_adding = tgui_input_text(usr, "Whom is being added?", "Vetted List") + if(length(user_adding)) + SSplayer_ranks.vetted_controller.add_player(ckey = user_adding, admin = usr) + message_admins("[usr] has added [user_adding] to the vetted database.") + +ADMIN_VERB(remove_vetted, R_ADMIN, "Remove user from Vetted", "Removes a user from the vetted list", ADMIN_CATEGORY_MAIN) + var/user_del = tgui_input_text(usr, "Whom is being Removed?", "Vetted List") + if(length(user_del)) + SSplayer_ranks.vetted_controller.remove_player(ckey = user_del) + message_admins("[usr] has removed [user_del] from the vetted databse.")