Thread: [TF2] - Test
View Single Post
Arkarr
Veteran Member
Join Date: Sep 2012
Location: Just behind my PC screen.
Old 06-29-2017 , 05:17   Re: [TF2] - Test
Reply With Quote #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_TeamChangeEventHookMode_Post);
    
HookEvent("teamplay_round_win"EVENT_RoundEnd);
    
    for (
int i MaxClients0; --i)
    {
        if (!
AreClientCookiesCached(i))
        {
            continue;
        }
        
        
OnClientCookiesCached(i);
    }
}

public 
void OnClientCookiesCached(int client)
{
    
char sValue[8];
    
GetClientCookie(clientCOOKIE_TestPassedsValuesizeof(sValue));
    
    
HasFailed[client] = !(sValue[0] != '\0' && StringToInt(sValue));
}  

public 
void OnConfigsExecuted()
{
    
char lockedTeams[45];
    
GetConVarString(CVAR_LockedTeamlockedTeamssizeof(lockedTeams));
    
    
REDLocked = (StrContains(lockedTeams"RED") == -false true);
    
BLULocked = (StrContains(lockedTeams"BLU") == -false true);
    
    for (
int i 0MAXPLAYERS 1i++)
    
HasFailed[i] = true;
    
    
ReadConfigFile();
}

public 
void OnClientConnected(int client)
{
    
HasFailed[client] = true;
    
questionNumber[client] = 0;
}

public 
Action CMD_TheTest(int clientint 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_QuestionsquestionNumber[client]);
    
    
GetTrieString(questionInfosFIELD_QUESTIONquestionsizeof(question));
    
    
SetMenuTitle(menuquestion);
    
    
int blank 5;
    
char answer[45];
    
char answerID[10];
    
int answerCount 0;
    
    
    
int randomAnswer GetRandomInt(04);
    while (
answerCount != 5)
    {
        
IntToString(randomAnswer 1answerIDsizeof(answerID));
        
        
Format(answerIDsizeof(answerID), "answer%s"answerID);
        
GetTrieString(questionInfosanswerIDanswersizeof(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(menufalse);
    
DisplayMenu(menuclientMENU_TIME_FOREVER);
}

public 
int MenuHandler_Question(Handle menuMenuAction menuActionint clientint menuItemID)
{
    if (
menuAction == MenuAction_Select)
    {
        
char correct[10];
        
GetMenuItem(menumenuItemIDcorrectsizeof(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(clientCOOKIE_TestPassed"1");
            }
            else
            {
                
CPrintToChat(client"%s Fail ! You didn't passed the Test !"PLUGIN_TAG);
                while(
IsPlayerAlive(client))
                    
SlapPlayer(client20);
            }
        }
    }
    else if (
menuAction == MenuAction_End)
    {
        
questionNumber[client] = 0;
        
CloseHandle(menu);
    }
}

public 
Action EVENT_RoundEnd(Handle event, const char[] namebool dontBroadcast)
{
    for (
int i 0MAXPLAYERS 1i++)
    
questionNumber[i] = true;
}

public 
Action EVENT_TeamChange(Handle event, const char[] namebool 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(clientTFTeam_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(clientTFTeam_Spectator);
            else
                
TF2_ChangeClientTeam(clientTFTeam_Red);
        }
    }
}

stock bool ReadConfigFile()
{
    
ARRAY_Questions CreateArray();
    
    
char path[100];
    
Handle kv CreateKeyValues("Questions");
    
BuildPath(Path_SMpathsizeof(path), "/configs/TheTest.cfg");
    
FileToKeyValues(kvpath);
    
    if (!
KvGotoFirstSubKey(kv))
        return;
    
    do
    {
        
KvGetSectionName(kvquestionsizeof(question));
        
KvGetString(kvFIELD_ANSWER1a1sizeof(a1), "-");
        
KvGetString(kvFIELD_ANSWER2a2sizeof(a2), "-");
        
KvGetString(kvFIELD_ANSWER3a3sizeof(a3), "-");
        
KvGetString(kvFIELD_ANSWER4a4sizeof(a4), "-");
        
KvGetString(kvFIELD_ANSWER5a5sizeof(a5), "-");
        
        
Handle tmpTrie CreateTrie();
        
SetTrieString(tmpTrieFIELD_QUESTIONquestionfalse);
        
SetTrieString(tmpTrieFIELD_ANSWER1a1false);
        
SetTrieString(tmpTrieFIELD_ANSWER2a2false);
        
SetTrieString(tmpTrieFIELD_ANSWER3a3false);
        
SetTrieString(tmpTrieFIELD_ANSWER4a4false);
        
SetTrieString(tmpTrieFIELD_ANSWER5a5false);
        
        
PushArrayCell(ARRAY_QuestionstmpTrie);
    } while (
KvGotoNextKey(kv));
    
    
CloseHandle(kv);

__________________
Want to check my plugins ?

Last edited by Arkarr; 06-29-2017 at 05:17.
Arkarr is offline