Veteran Member
Join Date: Sep 2012
Location: Just behind my PC screen
|
06-29-2017
, 05:17
Re: [TF2] - Test
|
#16
|
Hopefully all bugs are fixed. Also, I added a way to save test results. A bad way, but it should work.
Not tested as I don't have a PC nor a server now.
I would appreciate to know if it works.
PHP Code:
#include <sourcemod> #include <sdktools> #include <tf2> #include <tf2_stocks> #include <morecolors> #include <clientprefs>
#pragma newdecls required
#define PLUGIN_AUTHOR "Arkarr" #define PLUGIN_VERSION "1.00" #define PLUGIN_TAG "{yellow}[The Test]{default}" #define FIELD_QUESTION "question" #define FIELD_ANSWER1 "answer1" #define FIELD_ANSWER2 "answer2" #define FIELD_ANSWER3 "answer3" #define FIELD_ANSWER4 "answer4" #define FIELD_ANSWER5 "answer5"
Handle ARRAY_Questions; Handle CVAR_LockedTeam; Handle COOKIE_TestPassed;
bool REDLocked; bool BLULocked; bool HasFailed[MAXPLAYERS + 1];
char question[100]; char a1[100]; char a2[100]; char a3[100]; char a4[100]; char a5[100];
int questionNumber[MAXPLAYERS + 1];
public Plugin myinfo = { name = "[TF2] The Test", author = PLUGIN_AUTHOR, description = "Make player pass a test to unlock a team.", version = PLUGIN_VERSION, url = "http://www.sourcemod.net" };
public void OnPluginStart() { RegConsoleCmd("sm_test", CMD_TheTest, "Pop the menu with the Test."); CVAR_LockedTeam = CreateConVar("sm_thetest_lockedteam", "RED", "Set wich team are locked until a player complete the test. Valid value : RED or RED,BLU or BLU"); COOKIE_TestPassed = RegClientCookie("TestPassed", "Define if the user have passed the test or not.", CookieAccess_Private); HookEvent("player_spawn", EVENT_TeamChange, EventHookMode_Post); HookEvent("teamplay_round_win", EVENT_RoundEnd); for (int i = MaxClients; i > 0; --i) { if (!AreClientCookiesCached(i)) { continue; } OnClientCookiesCached(i); } }
public void OnClientCookiesCached(int client) { char sValue[8]; GetClientCookie(client, COOKIE_TestPassed, sValue, sizeof(sValue)); HasFailed[client] = !(sValue[0] != '\0' && StringToInt(sValue)); }
public void OnConfigsExecuted() { char lockedTeams[45]; GetConVarString(CVAR_LockedTeam, lockedTeams, sizeof(lockedTeams)); REDLocked = (StrContains(lockedTeams, "RED") == -1 ? false : true); BLULocked = (StrContains(lockedTeams, "BLU") == -1 ? false : true); for (int i = 0; i < MAXPLAYERS + 1; i++) HasFailed[i] = true; ReadConfigFile(); }
public void OnClientConnected(int client) { HasFailed[client] = true; questionNumber[client] = 0; }
public Action CMD_TheTest(int client, int args) { if (client == 0) { CPrintToChat(client, "%s In game command only !", "[SM]"); return Plugin_Handled; } if (questionNumber[client] != 0) { CPrintToChat(client, "%s Finish the Test firstly.", PLUGIN_TAG); } else { questionNumber[client] = 0; HasFailed[client] = false; DisplayTheTest(client); } return Plugin_Handled; }
public void DisplayTheTest(int client) { Handle menu = CreateMenu(MenuHandler_Question); Handle questionInfos = GetArrayCell(ARRAY_Questions, questionNumber[client]); GetTrieString(questionInfos, FIELD_QUESTION, question, sizeof(question)); SetMenuTitle(menu, question); int blank = 5; char answer[45]; char answerID[10]; int answerCount = 0; int randomAnswer = GetRandomInt(0, 4); while (answerCount != 5) { IntToString(randomAnswer + 1, answerID, sizeof(answerID)); Format(answerID, sizeof(answerID), "answer%s", answerID); GetTrieString(questionInfos, answerID, answer, sizeof(answer)); if (!StrEqual(answer, "-")) { if (randomAnswer == 0) AddMenuItem(menu, "Y", answer); else AddMenuItem(menu, "N", answer); blank--; } randomAnswer++; if (randomAnswer > 4) randomAnswer = 0; answerCount++; } while (blank > 0) { AddMenuItem(menu, "N", "-", ITEMDRAW_DISABLED); blank--; } SetMenuExitBackButton(menu, false); DisplayMenu(menu, client, MENU_TIME_FOREVER); }
public int MenuHandler_Question(Handle menu, MenuAction menuAction, int client, int menuItemID) { if (menuAction == MenuAction_Select) { char correct[10]; GetMenuItem(menu, menuItemID, correct, sizeof(correct)); if (!StrEqual(correct, "Y")) HasFailed[client] = true; questionNumber[client]++; if (questionNumber[client] < GetArraySize(ARRAY_Questions)) { DisplayTheTest(client); } else { questionNumber[client] = 0; if (!HasFailed[client]) { CPrintToChat(client, "%s Sucess ! You have passed the Test !", PLUGIN_TAG); SetClientCookie(client, COOKIE_TestPassed, "1"); } else { CPrintToChat(client, "%s Fail ! You didn't passed the Test !", PLUGIN_TAG); while(IsPlayerAlive(client)) SlapPlayer(client, 20); } } } else if (menuAction == MenuAction_End) { questionNumber[client] = 0; CloseHandle(menu); } }
public Action EVENT_RoundEnd(Handle event, const char[] name, bool dontBroadcast) { for (int i = 0; i < MAXPLAYERS + 1; i++) questionNumber[i] = true; }
public Action EVENT_TeamChange(Handle event, const char[] name, bool dontBroadcast) { int client = GetClientOfUserId(GetEventInt(event, "userid")); TFTeam team = TF2_GetClientTeam(client); if (client && HasFailed[client] && IsClientInGame(client) && !IsFakeClient(client) && (team == TFTeam_Red || team == TFTeam_Blue)) { if (team == TFTeam_Red && REDLocked) { CPrintToChat(client, "%s {green}You have to finish the test firstly ! Use {red}!test{green} !", PLUGIN_TAG); TF2_ChangeClientTeam(client, TFTeam_Blue); } else if (team == TFTeam_Blue && BLULocked) { CPrintToChat(client, "%s {green}You have to finish the test firstly ! Use {red}!test{green} !", PLUGIN_TAG); if (REDLocked) TF2_ChangeClientTeam(client, TFTeam_Spectator); else TF2_ChangeClientTeam(client, TFTeam_Red); } } }
stock bool ReadConfigFile() { ARRAY_Questions = CreateArray(); char path[100]; Handle kv = CreateKeyValues("Questions"); BuildPath(Path_SM, path, sizeof(path), "/configs/TheTest.cfg"); FileToKeyValues(kv, path); if (!KvGotoFirstSubKey(kv)) return; do { KvGetSectionName(kv, question, sizeof(question)); KvGetString(kv, FIELD_ANSWER1, a1, sizeof(a1), "-"); KvGetString(kv, FIELD_ANSWER2, a2, sizeof(a2), "-"); KvGetString(kv, FIELD_ANSWER3, a3, sizeof(a3), "-"); KvGetString(kv, FIELD_ANSWER4, a4, sizeof(a4), "-"); KvGetString(kv, FIELD_ANSWER5, a5, sizeof(a5), "-"); Handle tmpTrie = CreateTrie(); SetTrieString(tmpTrie, FIELD_QUESTION, question, false); SetTrieString(tmpTrie, FIELD_ANSWER1, a1, false); SetTrieString(tmpTrie, FIELD_ANSWER2, a2, false); SetTrieString(tmpTrie, FIELD_ANSWER3, a3, false); SetTrieString(tmpTrie, FIELD_ANSWER4, a4, false); SetTrieString(tmpTrie, FIELD_ANSWER5, a5, false); PushArrayCell(ARRAY_Questions, tmpTrie); } while (KvGotoNextKey(kv)); CloseHandle(kv); }
__________________
Last edited by Arkarr; 06-29-2017 at 05:17.
|
|