Skip to content

Commit

Permalink
Update gamecms.sma
Browse files Browse the repository at this point in the history
Added OnConfigsExecuted option so now the plugin has own config file.
  • Loading branch information
wohahobg authored Jun 1, 2024
1 parent 09b7ed9 commit 6df0aab
Showing 1 changed file with 84 additions and 94 deletions.
178 changes: 84 additions & 94 deletions gamecms.sma
Original file line number Diff line number Diff line change
Expand Up @@ -2,113 +2,91 @@
#include <easy_http>
#include <json>
#include <file>
#include <ini_file>
new PLUGIN[] = "GameCMS.ORG";
new PLUGIN[] = "[GameCMS] Core";
new AUTHOR[] = "Wohaho";
new VERSION[] = "1.00";
new VERSION[] = "1.3";
enum _:cvar_set
{
CVAR_SQL_HOST,
CVAR_SQL_USER,
CVAR_SQL_PASS,
CVAR_SQL_DB,
CVAR_SERVER_API_KEY,
CVAR_WEBSITE_API_KEY,
CVAR_ADMIN_COMMAND_SERIVCE,
CVAR_ADMIN_DELETE_EXPIRED,
CVAR_ADVANCED_BANS_PREFIX,
CVAR_SERVER_ID,
CVAR_SERVER_TIME
}
new cvar[cvar_set]
new SERVER_API_KEY[70];
new WEBSITE_API_KEY[70];
new int:ServerId = 0;
new ServerId = 0;
public plugin_precache() {
cvar[CVAR_SERVER_API_KEY] = create_cvar("server-api-key", "none", FCVAR_PROTECTED, "GameCMS Server API Key.")
cvar[CVAR_WEBSITE_API_KEY] = create_cvar("website-api-key", "none", FCVAR_PROTECTED, "GameCMS Website API Key.")
cvar[CVAR_ADMIN_COMMAND_SERIVCE] = create_cvar("admin-command-service", "gcms-service", FCVAR_PROTECTED, "Command for the admin plugin that display if the player has any access flags.")
cvar[CVAR_ADMIN_DELETE_EXPIRED] = create_cvar("admin-delete-expired-admins", "0", FCVAR_PROTECTED,
"Should the plugin delete expired admins from the database?^n\
By default is 0^n\
1 - Delete admins from database^n\
0 - Do not delete^n\");
cvar[CVAR_ADVANCED_BANS_PREFIX] = create_cvar("advanced-bans-prefix", "s1", FCVAR_PROTECTED, "GameCMS Advanced Bans table prefix.^n\
This is the table prefix for the server.^n\
If you want each server to have its own ban system, you can change this table prefix.^n\
Alternatively, you can set the ab_single_server cvar to 1 in amx.cfg.");
cvar[CVAR_SQL_HOST] = create_cvar("gcms-sql-host", "localhost", FCVAR_PROTECTED, "MySQL host")
cvar[CVAR_SQL_DB] = create_cvar("gcmss-sql-db", "amxx", FCVAR_PROTECTED, "MySQL Database Name")
cvar[CVAR_SQL_USER] = create_cvar("gcms-sql-user", "root", FCVAR_PROTECTED, "MySQL Username")
cvar[CVAR_SQL_PASS] = create_cvar("gcms-sql-password", "", FCVAR_PROTECTED, "MySQL User Password")
cvar[CVAR_SERVER_ID] = create_cvar("gamcms-server-id", "0", FCVAR_PROTECTED, "GameCMS Server ID. DO NOT CHANGE OR TOUCH THIS VALUE!!")
AutoExecConfig();
}
new const CONFIG_FILE[] = "gamecms.org"
public plugin_natives()
{
register_library("gamecms");
register_native("ReadConfigStringValue","NativeReadStringConfig");
register_native("ReadConfigIntValue","NativeReadIntConfig");
register_native("GetGameCMSServerId","getGameCMSServerId");
}
public plugin_init()
{
register_plugin(PLUGIN, VERSION, AUTHOR);
register_plugin(PLUGIN, VERSION, AUTHOR,"https://gamecms.org");
register_concmd("gcms_force_commands","forceCommands", ADMIN_RCON);
register_concmd("gcms_reload_ini","reloadIniFile", ADMIN_RCON);
register_concmd("gcms_set_server_api_key","setServerApiKey", ADMIN_RCON, "- gcms_set_server_api_key <key>");
ReadConfigStringValue("SERVER-API-KEY", "SERVER_API_KEY", "none", SERVER_API_KEY, charsmax(SERVER_API_KEY));
ReadConfigStringValue("WEBSITE-API-KEY", "WEBSITE_API_KEY", "none", WEBSITE_API_KEY, charsmax(WEBSITE_API_KEY));
setServerId();
set_task(120.0, "requestBegin", 2222, _, _, "b");
}
public NativeReadStringConfig(plugin, params)
{
new szsection[70],szKey[70],szDefaultValue[70],szOutPut[70];
get_string(1,szsection,charsmax(szsection));
get_string(2,szKey,charsmax(szKey));
get_string(3,szDefaultValue,charsmax(szDefaultValue));
ReadConfigStringValue(szsection,szKey,szDefaultValue,szOutPut,charsmax(szOutPut));
set_string(4, szOutPut,charsmax(szOutPut));
}
public ReadConfigStringValue(const section[], const key[], defaultValue[], output[], maxLength)
{
if (!ini_read_string(CONFIG_FILE, section, key, output, maxLength))
{
ini_write_string(CONFIG_FILE, section, key, defaultValue);
ini_read_string(CONFIG_FILE, section, key, output, maxLength);
}
}
public OnConfigsExecuted(){
public NativeReadIntConfig(plugin, params)
{
new szsection[70], szKey[70], szDefaultValue[70];
get_string(1, szsection, charsmax(szsection));
get_string(2, szKey, charsmax(szKey));
get_string(3, szDefaultValue, charsmax(szDefaultValue));
new intOutput;
ReadConfigIntValue(szsection, szKey, szDefaultValue, intOutput);
return intOutput;
}
public ReadConfigIntValue(const section[], const key[], defaultValue[], &output)
{
new defaultValueInt = str_to_num(defaultValue);
if (!ini_read_int(CONFIG_FILE, section, key, output))
{
ini_write_int(CONFIG_FILE, section, key, defaultValueInt);
ini_read_int(CONFIG_FILE, section, key, output);
}
get_pcvar_string(cvar[CVAR_SERVER_API_KEY], SERVER_API_KEY, charsmax(SERVER_API_KEY))
get_pcvar_string(cvar[CVAR_WEBSITE_API_KEY], WEBSITE_API_KEY, charsmax(WEBSITE_API_KEY))
setServerId();
set_task(120.0, "requestBegin", 2222, _, _, "b");
}
public reloadIniFile(){
ReadConfigStringValue("SERVER-API-KEY", "SERVER_API_KEY", "none",SERVER_API_KEY, charsmax(SERVER_API_KEY));
ReadConfigStringValue("WEBSITE-API-KEY", "WEBSITE_API_KEY","none", WEBSITE_API_KEY, charsmax(WEBSITE_API_KEY));
server_print("[GameCMS] Ini file reloaded!");
return PLUGIN_CONTINUE;
}
public setServerApiKey(id, level, cid){
if (!cmd_access (id, level, cid, 1)){
return PLUGIN_HANDLED;
}
new server_api_key[65]
read_argv(1, server_api_key, charsmax(server_api_key));
server_print("%i", strlen(server_api_key))
ini_write_string(CONFIG_FILE, "SERVER-API-KEY", "SERVER_API_KEY", server_api_key);
SERVER_API_KEY = server_api_key
server_print("[GameCMS] Server API key changed!");
return PLUGIN_CONTINUE;
}
public forceCommands()
{
requestBegin();
Expand Down Expand Up @@ -201,45 +179,57 @@ public setServerId()
public handleServerIdResponse(EzHttpRequest:request_id)
{
new error[64];
// Check for request errors
if (ezhttp_get_error_code(request_id) != EZH_OK) {
new error[64];
ezhttp_get_error_message(request_id, error, charsmax(error));
server_print("[GameCMS] Response error: %s", error);
return PLUGIN_HANDLED;
}
// Check for HTTP errors
if (ezhttp_get_http_code(request_id) != 200) {
server_print("[GameCMS] Could not connect to GameCMS API Please make sure your Server API key is set correctly.");
server_print("[GameCMS] Could not connect to GameCMS API. Please make sure your Server API key is set correctly.");
return PLUGIN_HANDLED;
}
new SERVER_ID_CONFIG_FILE[50] = "addons/amxmodx/data/gamecms_server_id.json";
ezhttp_save_data_to_file(request_id, SERVER_ID_CONFIG_FILE);
// Save the response to a file
if (!ezhttp_save_data_to_file(request_id, SERVER_ID_CONFIG_FILE)) {
server_print("[GameCMS] Failed to save response data to file.");
return PLUGIN_HANDLED;
}
// Parse JSON data from the file
new JSON:main_object = json_parse(SERVER_ID_CONFIG_FILE, true);
if (main_object == Invalid_JSON) {
server_print("[GameCMS] Invalid JSON response!");
return PLUGIN_HANDLED;
}
// Get the "id" object from the JSON data
new JSON:id_object = json_object_get_value(main_object, "id");
ServerId = json_get_number(id_object);
if (id_object == Invalid_JSON) {
server_print("[GameCMS] Failed to get 'id' from JSON response!");
json_free(main_object);
return PLUGIN_HANDLED;
}
server_print("[GameCMS] Successfully connected to GameCMS API.");
// Extract the server ID from the JSON object
ServerId = json_get_number(id_object);
if (ServerId == 0) {
server_print("[GameCMS] Invalid Server ID received from JSON response!");
} else {
server_print("[GameCMS] Successfully connected to GameCMS API with Server ID: %d.", ServerId);
set_pcvar_num(cvar[CVAR_SERVER_ID],ServerId)
}
// Clean up JSON objects and delete the temporary file
json_free(id_object);
json_free(main_object);
delete_file(SERVER_ID_CONFIG_FILE);
return PLUGIN_HANDLED;
}
public getServerApiKey()
{
return SERVER_API_KEY;
}
public getGameCMSServerId(){
return ServerId;
}

0 comments on commit 6df0aab

Please sign in to comment.