From ef103951d29742b252396f14af59162b399ef678 Mon Sep 17 00:00:00 2001 From: jenzur Date: Tue, 14 Apr 2020 00:18:16 +0200 Subject: [PATCH] assuming different servers work fine separated this should be first release, --- .../scripting/unloze_ingame_poll_css_redux.sp | 217 ++++++++++++------ 1 file changed, 145 insertions(+), 72 deletions(-) diff --git a/unloze_poll_votes/scripting/unloze_ingame_poll_css_redux.sp b/unloze_poll_votes/scripting/unloze_ingame_poll_css_redux.sp index f49f9221..512adea8 100644 --- a/unloze_poll_votes/scripting/unloze_ingame_poll_css_redux.sp +++ b/unloze_poll_votes/scripting/unloze_ingame_poll_css_redux.sp @@ -33,7 +33,7 @@ public void OnPluginStart() RegConsoleCmd("say", cmd_say); //adm cmds - RegAdminCmd("sm_poll_commands", cmd_poll_commands, ADMFLAG_GENERIC); + RegAdminCmd("sm_exec_pollcommands", cmd_poll_commands, ADMFLAG_GENERIC); //mysql con sql_start_connection(); OnMapStart(); @@ -42,6 +42,11 @@ public void OnPluginStart() public void OnMapStart() { server_port = GetConVarInt(FindConVar("hostport")); + CreateTimer(1.0, poll_commands, INVALID_HANDLE); +} + +public Action poll_commands(Handle timer) +{ mysql_find_poll_commands(); } @@ -58,12 +63,11 @@ public int MenuHandler1(Menu menu, MenuAction action, int client_, int selection else { admin_editing[client_] = 5; - mysql_get_polls("vote", client_); + mysql_get_polls("vote", client_, 0); } } case MenuAction_End: { - PrintToChatAll("menu action end 1"); if (menu != INVALID_HANDLE) delete menu; } @@ -81,7 +85,13 @@ public int MenuHandler2(Menu menu, MenuAction action, int iclient2, int selectio { admin_editing[iclient2] = selection; //PrintToChat(iclient2, "client: %N \nclient[admin_editing]: %i", iclient2, admin_editing[iclient2]); - if (selection == 1) + if (!selection) + { + mysql_find_poll_commands(); + PrintToChat(iclient2, "Executed Poll commands"); + polladminmenu(iclient2); + } + else if (selection == 1) { PrintToChat(iclient2, "Write poll question into the chat, type exit to abort"); } @@ -89,13 +99,12 @@ public int MenuHandler2(Menu menu, MenuAction action, int iclient2, int selectio { char pollchoice[16]; menu.GetItem(selection, pollchoice, sizeof(pollchoice)); - mysql_get_polls(pollchoice, iclient2); + mysql_get_polls(pollchoice, iclient2, 1); } } case MenuAction_End: { admin_editing[iclient2] = 0; - PrintToChatAll("menu action end 2"); if (menu != INVALID_HANDLE) delete menu; } @@ -126,7 +135,8 @@ public int menu_vote_poll_options(Menu menu, MenuAction action, int iclient, int { char info[32]; menu.GetItem(selection, info, sizeof(info)); - mysql_insert_poll_vote(iclient, info, admin_table_edit[iclient]); + int coloumn_index = StringToInt(info); + mysql_insert_poll_vote(iclient, coloumn_index, admin_table_edit[iclient]); } case MenuAction_End: { @@ -148,7 +158,7 @@ public int MenuHandler3(Menu menu, MenuAction action, int iclient3, int selectio { char info[32]; menu.GetItem(selection, info, sizeof(info)); - PrintToChat(iclient3, "admin_editing[iclient3]: %i \ninfo: %s", admin_editing[iclient3], info); + //PrintToChat(iclient3, "admin_editing[iclient3]: %i \ninfo: %s", admin_editing[iclient3], info); switch(admin_editing[iclient3]) { case 2: @@ -176,7 +186,6 @@ public int MenuHandler3(Menu menu, MenuAction action, int iclient3, int selectio case MenuAction_End: { admin_editing[iclient3] = 0; - PrintToChatAll("menu action end 3"); if (menu != INVALID_HANDLE) delete menu; } @@ -213,7 +222,6 @@ public int menu_edit_poll_options(Menu menu, MenuAction action, int iclient4, in } case MenuAction_End: { - PrintToChatAll("menu menu_edit_poll_options"); if (menu != INVALID_HANDLE) delete menu; } @@ -254,7 +262,6 @@ public int editpollmenu(Menu menu, MenuAction action, int iclient5, int selectio case MenuAction_End: { admin_editing[iclient5] = 0; - PrintToChatAll("menu editpollmenu"); if (menu != INVALID_HANDLE) delete menu; } @@ -285,7 +292,7 @@ public Action cmd_polls(int iclient, int args) else { admin_editing[iclient] = 5; - mysql_get_polls("vote", iclient); + mysql_get_polls("vote", iclient, 0); } } return Plugin_Handled; @@ -295,7 +302,7 @@ public void polladminmenu(int client_admin) { Menu menu = new Menu(MenuHandler2); menu.SetTitle("Poll admin menu", "", client_admin); - menu.AddItem("Disabled", "", ITEMDRAW_DISABLED); + menu.AddItem("", "Execute poll commands"); menu.AddItem("", "Create new Poll"); menu.AddItem("Edit", "Edit Poll"); menu.AddItem("Enable/Disable", "Enable/Disable Poll"); @@ -419,19 +426,35 @@ public void mysql_delete_poll(int iclient7, char []table_name) char query_delete[generic_length]; Format(query_delete, sizeof(query_delete), "DROP TABLE unloze_css_polls.`%s`", table_name); mysql_exec_prepared_statement(query_delete); + Format(query_delete, sizeof(query_delete), "DELETE FROM unloze_css_polls.poll_status WHERE poll_table = '%s'", table_name); + mysql_exec_prepared_statement(query_delete); PrintToChat(iclient7, "removed poll %s", table_name); - editpoll(iclient7, table_name); + polladminmenu(iclient7); } -public void mysql_insert_poll_vote(int client, char []coloumn, char table_name) +public void mysql_insert_poll_vote(int client, int coloumn_index, char []table_name) { - char sSID[24]; + DBResultSet rs_vote; + char sSID[64]; GetClientAuthId(client, AuthId_Steam2, sSID, sizeof(sSID)); + Format(sSID, sizeof(sSID), "0%s", sSID[7]); char query_insert[generic_length]; - Format(query_insert, sizeof(query_insert), "DELETE FROM unloze_css_polls.%s WHERE steam_auth = '%s'", table_name, sSID); + Format(query_insert, sizeof(query_insert), "DELETE FROM unloze_css_polls.`%s` WHERE steam_id = '%s'", table_name, sSID); + //PrintToChat(client, "query_insert 0: %s", query_insert); mysql_exec_prepared_statement(query_insert); - Format(query_insert, sizeof(query_insert), "INSERT INTO unloze_css_polls.%s (%s) VALUES (1) WHERE steam_auth = '%s'", table_name, coloumn, sSID); + Format(query_insert, sizeof(query_insert), "INSERT INTO unloze_css_polls.`%s` (`steam_id`, `%i`) VALUES ('%s', 1)", table_name, coloumn_index, sSID); + //PrintToChat(client, "query_insert 1: %s", query_insert); mysql_exec_prepared_statement(query_insert); + Format(query_insert, sizeof(query_insert), "SELECT pt.poll_option_text FROM unloze_css_polls.`%s` ht INNER JOIN unloze_css_polls.poll_text pt WHERE pt.poll_id = %i AND ht.`%i` = 1", table_name, coloumn_index, coloumn_index); + //PrintToChat(client, "query_insert 2: %s", query_insert); + if ((rs_vote = SQL_Query(database_connection, query_insert)) != null) + { + char query_vote_result[generic_length]; + rs_vote.FetchRow(); + rs_vote.FetchString(0, query_vote_result, sizeof(query_vote_result)); + PrintToChat(client, "Voted on poll %s for option %s", table_name, query_vote_result); + } + delete rs_vote; } public void mysql_vote_poll(int client, char []table_name) @@ -440,21 +463,6 @@ public void mysql_vote_poll(int client, char []table_name) char query_vote[generic_length]; char sSID[24]; GetClientAuthId(client, AuthId_Steam2, sSID, sizeof(sSID)); - Format(query_vote, sizeof(query_vote), "SELECT enabled from unloze_css_polls.poll_status WHERE poll_table = '%s'", table_name); - if ((rs_vote = SQL_Query(database_connection, query_vote)) == null) - { - delete rs_vote; - return; - } - rs_vote.FetchRow(); - int status = rs_vote.FetchInt(0); - if (!status) - { - delete rs_vote; - PrintToChat(client, "Poll %s was disabled by an admin to vote on", table_name); - editpoll(client, table_name); - return; - } Format(query_vote, sizeof(query_vote), "SELECT PLAYTIME FROM unloze_css_polls.poll_status WHERE poll_table = '%s'", table_name); if ((rs_vote = SQL_Query(database_connection, query_vote)) == null) { @@ -463,6 +471,7 @@ public void mysql_vote_poll(int client, char []table_name) } rs_vote.FetchRow(); int playtime = rs_vote.FetchInt(0); + //PrintToChatAll("playtime: %i query_vote: %s", playtime, query_vote); GetClientAuthId(client, AuthId_Steam3, sSID, sizeof(sSID)); if (playtime != 0) { @@ -490,18 +499,16 @@ public void mysql_vote_poll(int client, char []table_name) Format(server, sizeof(server), "css-ze"); } } - ReplaceString(sSID, sizeof(sSID), "[U:", ""); ReplaceString(sSID, sizeof(sSID), "]", ""); Format(sSID, sizeof(sSID), "0%s", sSID[1]); - Format(query_vote, sizeof(query_vote), "SELECT connection_time from unloze_stats.hlstats_Players hp INNER JOIN unloze_stats.hlstats_PlayerUniqueIds pui on hp.playerId = pui.playerId AND pui.uniqueId = '%s' AND pui.game = '%s'", sSID, server); + //PrintToChatAll("query_vote steam: %s", query_vote); if ((rs_vote = SQL_Query(database_hlstats_connection, query_vote)) == null) { delete rs_vote; return; } - rs_vote.FetchRow(); if (!rs_vote.RowCount) { Format(sSID, sizeof(sSID), "1%s", sSID[1]); @@ -511,12 +518,12 @@ public void mysql_vote_poll(int client, char []table_name) delete rs_vote; return; } - rs_vote.FetchRow(); + //PrintToChatAll("query_vote steam 1: %s", query_vote); } if (!rs_vote.RowCount) { GetClientAuthId(client, AuthId_Steam2, sSID, sizeof(sSID)); - ReplaceString(sSID, sizeof(sSID), "STEAM_1:", ""); + Format(sSID, sizeof(sSID), "%s", sSID[8]); Format(sSID, sizeof(sSID), "0%s", sSID[1]); Format(query_vote, sizeof(query_vote), "SELECT connection_time from unloze_stats.hlstats_Players hp INNER JOIN unloze_stats.hlstats_PlayerUniqueIds pui on hp.playerId = pui.playerId AND pui.uniqueId = '%s' AND pui.game = '%s'", sSID, server); if ((rs_vote = SQL_Query(database_hlstats_connection, query_vote)) == null) @@ -524,10 +531,12 @@ public void mysql_vote_poll(int client, char []table_name) delete rs_vote; return; } - rs_vote.FetchRow(); + //PrintToChatAll("query_vote steam 2: %s", query_vote); } if (!rs_vote.RowCount) { + GetClientAuthId(client, AuthId_Steam2, sSID, sizeof(sSID)); + Format(sSID, sizeof(sSID), "%s", sSID[8]); Format(sSID, sizeof(sSID), "1%s", sSID[1]); Format(query_vote, sizeof(query_vote), "SELECT connection_time from unloze_stats.hlstats_Players hp INNER JOIN unloze_stats.hlstats_PlayerUniqueIds pui on hp.playerId = pui.playerId AND pui.uniqueId = '%s' AND pui.game = '%s'", sSID, server); if ((rs_vote = SQL_Query(database_hlstats_connection, query_vote)) == null) @@ -535,15 +544,15 @@ public void mysql_vote_poll(int client, char []table_name) delete rs_vote; return; } - rs_vote.FetchRow(); + //PrintToChatAll("query_vote steam 3: %s", query_vote); } - //TODO if (!rs_vote.RowCount) { PrintToChat(client, "you do not have enough time on this server to participate in this vote"); delete rs_vote; return; } + rs_vote.FetchRow(); float connection_time_seconds = rs_vote.FetchFloat(0); float hours_pre = connection_time_seconds / 60; int hours = RoundToFloor(hours_pre / 60); @@ -561,27 +570,41 @@ public void mysql_vote_poll(int client, char []table_name) delete rs_vote; return; } - Format(query_vote, sizeof(query_vote), "SELECT COLUMN_NAME FROM information_schema.columns WHERE TABLE_NAME = '%s'", table_name); if ((rs_vote = SQL_Query(database_connection, query_vote)) == null) { delete rs_vote; return; } - char table_names[generic_length]; if (rs_vote.RowCount > 0) { - int index; + Format(admin_table_edit[client], sizeof(admin_table_edit), table_name); + Menu menu2 = new Menu(menu_vote_poll_options); + menu2.SetTitle("Select which option to vote on for poll: %s", table_name); + for (int i = 0; i < 5; i++) + menu2.AddItem("", "", ITEMDRAW_DISABLED); while (rs_vote.FetchRow()) { - rs_vote.FetchString(0, table_names[index], sizeof(table_names)); - index++; + DBResultSet rs_vote1; + int poll_id = rs_vote.FetchInt(0); + Format(query_vote, sizeof(query_vote), "SELECT COUNT(*), pt.poll_option_text FROM unloze_css_polls.`%s` ta INNER JOIN unloze_css_polls.poll_text pt WHERE pt.poll_id = %i AND ta.`%i` = 1", table_name, poll_id, poll_id); + if ((rs_vote1 = SQL_Query(database_connection, query_vote)) != null) + { + if (rs_vote1.RowCount > 0) + { + rs_vote1.FetchRow(); + int poll_vote_count = rs_vote1.FetchInt(0); + char poll_option[generic_length]; + char poll_option_vote[generic_length]; + rs_vote1.FetchString(1, poll_option, sizeof(poll_option)); + Format(poll_option_vote, sizeof(poll_option_vote), "%s Votes: %i", poll_option, poll_vote_count); + char poll_char_id[24]; + IntToString(poll_id, poll_char_id, sizeof(poll_char_id)); + menu2.AddItem(poll_char_id, poll_option_vote); + } + } + delete rs_vote1; } - Format(admin_table_edit[client], sizeof(admin_table_edit), table_name); - Menu menu2 = new Menu(menu_vote_poll_options, MENU_ACTIONS_ALL); - menu2.SetTitle("Select which option to vote on for: %s", table_name); - for (int i = sizeof(table_names); i > 0; i--) - menu2.AddItem(table_names[i], table_names[i]); menu2.ExitButton = true; menu2.Display(client, MENU_TIME_FOREVER); } @@ -667,7 +690,7 @@ public void mysql_playtime_requirement(int client, char []info) int playtime; rs.FetchRow(); playtime = rs.FetchInt(0); - PrintToChat(client, "playtime: %i", playtime); + PrintToChat(client, "playtime required: %i", playtime); admin_editing[client] = 7; Menu menu = new Menu(editpollmenu, MENU_ACTIONS_ALL); menu.SetTitle("Poll %s has playtime requirement: %i", "", info, playtime); @@ -742,7 +765,7 @@ public void mysql_edit_poll(int client, char []info, int selection) delete rs; } -public void mysql_get_polls(char []pollchoice, int client) +public void mysql_get_polls(char []pollchoice, int client, int admin_enable) { DBResultSet rs_polls; char query_get_polls[generic_length]; @@ -753,21 +776,29 @@ public void mysql_get_polls(char []pollchoice, int client) return; } char table_names[generic_length]; - int index = 0; if (rs_polls.RowCount > 0) { + Menu menu3 = new Menu(MenuHandler3); + menu3.SetTitle("Select which poll to %s", pollchoice); while (rs_polls.FetchRow()) { - rs_polls.FetchString(0, table_names[index], sizeof(table_names)); - index++; + DBResultSet rs_vote; + rs_polls.FetchString(0, table_names, sizeof(table_names)); + Format(query_get_polls, sizeof(query_get_polls), "SELECT enabled from unloze_css_polls.poll_status WHERE poll_table = '%s'", table_names); + if ((rs_vote = SQL_Query(database_connection, query_get_polls)) != null) + { + rs_vote.FetchRow(); + int status = rs_vote.FetchInt(0); + if (!status && !admin_enable) + menu3.AddItem(table_names, table_names, ITEMDRAW_DISABLED); + else + menu3.AddItem(table_names, table_names); + } + delete rs_vote; } + menu3.ExitButton = true; + menu3.Display(client, MENU_TIME_FOREVER); } - Menu menu3 = new Menu(MenuHandler3); - menu3.SetTitle("Select which poll to %s", pollchoice); - for (int i = 0; i < index; i++) - menu3.AddItem(table_names[i], table_names[i]); - menu3.ExitButton = true; - menu3.Display(client, MENU_TIME_FOREVER); delete rs_polls; } @@ -775,8 +806,8 @@ public void mysql_find_poll_commands() { DBResultSet rs; char query[generic_length]; - Format(query, sizeof(query), "SELECT command, polloption FROM unloze_css_polls.poll_reff WHERE server_port = %i", server_port); - //PrintToChatAll("query: %s size: %i", query, sizeof(query)); + char query2[generic_length]; + Format(query, sizeof(query), "SELECT poll_table FROM unloze_css_polls.poll_status ps WHERE ps.server_port = %i", server_port); if ((rs = SQL_Query(database_connection, query)) == null) { delete rs; @@ -786,14 +817,56 @@ public void mysql_find_poll_commands() { while (rs.FetchRow()) { - char table_commands[generic_length]; - char table_polloptions[generic_length]; - char sPart[2][generic_length]; - rs.FetchString(0, table_commands, sizeof(table_commands)); - rs.FetchString(1, table_polloptions, sizeof(table_polloptions)); - ExplodeString(table_polloptions, ".", sPart, sizeof(sPart), sizeof(sPart[])); - Format(query, sizeof(query), "SELECT COUNT(*) FROM unloze_css_polls.%s", sPart[0]); - //TODO + char table[generic_length]; + DBResultSet rs1; + rs.FetchString(0, table, sizeof(table)); + Format(query2, sizeof(query2), "SELECT `COLUMN_NAME` FROM `INFORMATION_SCHEMA`.`COLUMNS` WHERE `TABLE_SCHEMA`='unloze_css_polls' AND `TABLE_NAME`='%s'", table); + if ((rs1 = SQL_Query(database_connection, query2)) != null) + { + if (rs1.RowCount > 0) + { + int highest_vote_option = 0; + char highest_poll_option[generic_length]; + //skipping steam_ID + rs1.FetchRow(); + while (rs1.FetchRow()) + { + DBResultSet rs2; + int poll_id = rs1.FetchInt(0); + char column[generic_length]; + Format(query, sizeof(query), "SELECT COUNT(*), pt.poll_option_text FROM unloze_css_polls.`%s` ta INNER JOIN unloze_css_polls.poll_text pt WHERE pt.poll_id = %i AND ta.`%i` = 1", table, poll_id, poll_id); + if ((rs2 = SQL_Query(database_connection, query)) != null) + { + rs2.FetchRow(); + int vote_count = rs2.FetchInt(0); + rs2.FetchString(1, column, sizeof(column)); + if (vote_count > highest_vote_option) + { + highest_vote_option = vote_count; + Format(highest_poll_option, sizeof(highest_poll_option), column); + } + } + delete rs2; + } + if (highest_vote_option > 0) + { + DBResultSet rs2; + Format(query2, sizeof(query2), "SELECT command FROM unloze_css_polls.poll_reff WHERE polloption = '%s.%s'", table, highest_poll_option); + if ((rs2 = SQL_Query(database_connection, query2)) != null) + { + if (!!rs2.RowCount) + { + char command[generic_length]; + rs2.FetchRow(); + rs2.FetchString(0, command, sizeof(command)); + ServerCommand(command); + } + } + delete rs2; + } + } + } + delete rs1; } } delete rs;