From 40230f29c1a77f5d0a6d08d3626d09e4652db74c Mon Sep 17 00:00:00 2001 From: jenz Date: Sat, 1 Apr 2023 20:48:54 +0200 Subject: [PATCH] added variable for storing result instead of always just quering again --- .../scripting/event_notifier.sp | 127 +++++++++++------- 1 file changed, 75 insertions(+), 52 deletions(-) diff --git a/event_notification/scripting/event_notifier.sp b/event_notification/scripting/event_notifier.sp index 8b718108..fc591ade 100644 --- a/event_notification/scripting/event_notifier.sp +++ b/event_notification/scripting/event_notifier.sp @@ -5,6 +5,8 @@ #include Database g_hDatabase; +char sTitle[256]; +char sBuffer[16][256]; public Plugin myinfo = { @@ -18,8 +20,13 @@ public Plugin myinfo = public void OnPluginStart() { Database.Connect(SQL_OnDatabaseConnect, "Event_notifier"); - RegConsoleCmd("sm_event", Command_Event_notifier); - RegConsoleCmd("sm_events", Command_Event_notifier); + RegConsoleCmd("sm_event", Command_Event_notifier, "Display infos about the next event"); + RegConsoleCmd("sm_events", Command_Event_notifier, "Display infos about the next event"); +} + +public void OnMapStart() +{ + Event_select_query(); } public void SQL_OnDatabaseConnect(Database db, const char[] error, any data) @@ -30,61 +37,28 @@ public void SQL_OnDatabaseConnect(Database db, const char[] error, any data) return; } g_hDatabase = db; + Event_select_query(); } public Action Command_Event_notifier(int client, int args) { - if (!g_hDatabase) - { - Database.Connect(SQL_OnDatabaseConnect, "Event_notifier"); - return Plugin_Handled; - } - //only 3 servers with events, none exist on jb - int i_port = GetConVarInt(FindConVar("hostport")); - char sQuery[512]; - Format(sQuery, sizeof(sQuery), "select event_title, event_server, event_maps, event_date, event_time, event_reward from unloze_event.event e where e.event_server like '%s%i%s'", "%", i_port, "%"); - g_hDatabase.Query(SQL_OnQueryCompleted, sQuery, GetClientSerial(client)); - return Plugin_Handled; -} - -public void SQL_OnQueryCompleted(Database db, DBResultSet results, const char[] error, int iSerial) -{ - if (!db || strlen(error)) - { - LogError("Query error 3: %s", error); - } - int client; - if ((client = GetClientFromSerial(iSerial)) == 0) - return; Panel mSayPanel = new Panel(GetMenuStyleHandle(MenuStyle_Radio)); - char sTitle[256]; - if (results.RowCount && results.FetchRow()) - { - char sBuffer[256]; - results.FetchString(0, sTitle, sizeof(sTitle)); - Format(sTitle, sizeof(sTitle), "Title: %s", sTitle); - mSayPanel.SetTitle(sTitle); - results.FetchString(1, sBuffer, sizeof(sBuffer)); - mSayPanel.DrawItem("", ITEMDRAW_SPACER); - Format(sBuffer, sizeof(sBuffer), "Server: %s", sBuffer); - mSayPanel.DrawText(sBuffer); - results.FetchString(2, sBuffer, sizeof(sBuffer)); - mSayPanel.DrawItem("", ITEMDRAW_SPACER); - Format(sBuffer, sizeof(sBuffer), "Maps: %s", sBuffer); - mSayPanel.DrawText(sBuffer); - results.FetchString(3, sBuffer, sizeof(sBuffer)); - mSayPanel.DrawItem("", ITEMDRAW_SPACER); - Format(sBuffer, sizeof(sBuffer), "Date: %s", sBuffer); - mSayPanel.DrawText(sBuffer); - results.FetchString(4, sBuffer, sizeof(sBuffer)); - mSayPanel.DrawItem("", ITEMDRAW_SPACER); - Format(sBuffer, sizeof(sBuffer), "Time: %s", sBuffer); - mSayPanel.DrawText(sBuffer); - results.FetchString(5, sBuffer, sizeof(sBuffer)); - mSayPanel.DrawItem("", ITEMDRAW_SPACER); - Format(sBuffer, sizeof(sBuffer), "Reward: %s", sBuffer); - mSayPanel.DrawText(sBuffer); - } + mSayPanel.SetTitle(sTitle); + mSayPanel.DrawItem("", ITEMDRAW_SPACER); + + mSayPanel.DrawText(sBuffer[1]); //server + mSayPanel.DrawItem("", ITEMDRAW_SPACER); + + mSayPanel.DrawText(sBuffer[2]); //maps + mSayPanel.DrawItem("", ITEMDRAW_SPACER); + + mSayPanel.DrawText(sBuffer[3]); //date + mSayPanel.DrawItem("", ITEMDRAW_SPACER); + + mSayPanel.DrawText(sBuffer[4]); //time + mSayPanel.DrawItem("", ITEMDRAW_SPACER); + + mSayPanel.DrawText(sBuffer[5]); //reward mSayPanel.DrawItem("", ITEMDRAW_SPACER); mSayPanel.DrawItem("1. Got it!", ITEMDRAW_RAWLINE); @@ -92,6 +66,55 @@ public void SQL_OnQueryCompleted(Database db, DBResultSet results, const char[] mSayPanel.SetKeys(1023); mSayPanel.Send(client, Handler_Menu, 0); delete mSayPanel; + return Plugin_Handled; +} + +public void Event_select_query() +{ + if (!g_hDatabase) + { + Database.Connect(SQL_OnDatabaseConnect, "Event_notifier"); + return; + } + //only 3 servers with events, none exist on jb or ze2 + int i_port = GetConVarInt(FindConVar("hostport")); + char sQuery[512]; + Format(sQuery, sizeof(sQuery), "select event_title, event_server, event_maps, event_date, event_time, event_reward from unloze_event.event e where e.event_server like '%s%i%s'", "%", i_port, "%"); + g_hDatabase.Query(SQL_OnQueryCompleted, sQuery); +} + +public void SQL_OnQueryCompleted(Database db, DBResultSet results, const char[] error, DataPack data) +{ + if (!db || strlen(error)) + { + LogError("Query error 3: %s", error); + delete results; + } + if (results.RowCount && results.FetchRow()) + { + results.FetchString(0, sTitle, sizeof(sTitle)); + Format(sTitle, sizeof(sTitle), "Title: %s", sTitle); + + results.FetchString(1, sBuffer[0], sizeof(sBuffer[])); + + Format(sBuffer[1], sizeof(sBuffer[]), "Server: %s", sBuffer[0]); + + results.FetchString(2, sBuffer[0], sizeof(sBuffer[])); + + Format(sBuffer[2], sizeof(sBuffer[]), "Maps: %s", sBuffer[0]); + + results.FetchString(3, sBuffer[0], sizeof(sBuffer[])); + + Format(sBuffer[3], sizeof(sBuffer[]), "Date: %s", sBuffer[0]); + + results.FetchString(4, sBuffer[0], sizeof(sBuffer[])); + + Format(sBuffer[4], sizeof(sBuffer[]), "Time: %s", sBuffer[0]); + + results.FetchString(5, sBuffer[0], sizeof(sBuffer[])); + + Format(sBuffer[5], sizeof(sBuffer[]), "Reward: %s", sBuffer[0]); + } delete results; }