#include <amxmodx>
#include <amxmisc>
#include <cstrike>
#include <colorchat>
#include <nvault>
#define PLUGIN "Trivia Bot"
#define VERSION "1.1"
#define AUTHOR "THE _GAme"
#define CharsMax(%1) sizeof %1 - 1
#define MAX_LINES 512
#define MAX_LEN 256
#define QUESTION_INTERVAL 50.0
#define QUESTION_INTERVAL_NUM 50
new List[2][MAX_LINES][MAX_LEN];
new Amount;
new Timer = QUESTION_INTERVAL_NUM;
new CurrentAnswer[128];
new bool:TriviaOnOff[33];
new TriviaPoints[33];
new const PluginTag[] = "[[RO]DR-BuLLet Trivia]";
public plugin_init() {
register_plugin(PLUGIN, VERSION, AUTHOR);
register_clcmd("say", "HookSay");
register_clcmd("say !top10", "ShowTriviaTop");
register_clcmd("say !triviatop10", "ShowTriviaTop");
register_clcmd("say !triviatop", "ShowTriviaTop");
register_clcmd("say !triviapoints", "ShowTriviaPoints");
register_clcmd("say !points", "ShowTriviaPoints");
set_task(1.0, "TimeRemainingCounter", 1111, "", 0, "b", 0);
TeamInfo = get_user_msgid("TeamInfo");
SayText = get_user_msgid("SayText");
}
public plugin_cfg()
{
new ConfigsDir[64], File[64];
new TriviaDir[64];
get_configsdir(ConfigsDir, CharsMax(ConfigsDir));
formatex(TriviaDir, CharsMax(TriviaDir), "%s/Trivia Bot", ConfigsDir);
formatex(File, CharsMax(File), "%s/trivia_list.ini", TriviaDir);
if(!dir_exists(TriviaDir))
mkdir(TriviaDir);
if(!file_exists(File))
write_file(File, "", -1);
new FilePointer = fopen(File, "rt");
new Buffer[512];
while(!feof(FilePointer))
{
fgets(FilePointer, Buffer, CharsMax(Buffer));
if(!Buffer[0] || Buffer[0] == ';' || strlen(Buffer) < 3)
continue;
trim(Buffer);
strtok(Buffer, List[0][Amount], sizeof List[][] - 1, List[1][Amount], sizeof List[][] - 1, ';', 0);
Amount++;
}
fclose(FilePointer);
server_print("%s Successfuly loaded %d lines from %s.", PluginTag, Amount, File);
ShowQuestion();
}
public ShowQuestion()
{
if(task_exists(2222))
remove_task(2222);
new RndNum = random(Amount);
formatex(CurrentAnswer, CharsMax(CurrentAnswer), List[1][RndNum]);
static Players[32], Num, Player;
get_players(Players, Num, "bh");
for(new i = 0 ; i < Num ; i++)
{
Player = Players;
if(!is_user_connected(Player))
continue;
if(!TriviaOnOff[Player])
continue;
ColorChat(Player, RED, "%s^x01%s", PluginTag, List[0][RndNum]);
}
Timer = QUESTION_INTERVAL_NUM - 1;
set_task(QUESTION_INTERVAL, "ShowQuestion", 2222, "", 0, "", 0);
}
public TimeRemainingCounter()
{
if(Timer < 0)
return;
static Players[32], Num, Player;
get_players(Players, Num, "bh");
for(new i = 0 ; i < Num ; i++)
{
Player = Players;
if(!is_user_connected(Player))
continue;
if(!TriviaOnOff[Player])
continue;
set_hudmessage(255, 255, 255, -1.0, 0.7, 0, 1.0, 0.8, 1.0, 1.0, 4);
show_hudmessage(Player, "%s Timp ramas pana la urmatoarea intrebare : %d", PluginTag, Timer);
}
Timer--;
}
public HookSay(id)
{
new Args[256];
read_args(Args, CharsMax(Args));
if(containi(Args, CurrentAnswer) != -1 && TriviaOnOff[id])
{
new RndNum = random(500);
ColorChat(id, RED, "%s^x01 Raspunsul tau a fost corect. Felicitari, ai castigat %d$", PluginTag, RndNum);
TriviaPoints[id] += 1;
if(is_user_connected(id))
cs_set_user_money(id, cs_get_user_money(id) + RndNum, 0);
ShowQuestion();
return 1;
}
else if(equali(Args, "^"/trivia^"", 9))
{
if(!TriviaOnOff[id])
{
ColorChat(id, RED, "%s^x04 Te-ai alaturat la Trivia, il poti juca cat timp esti mort!", PluginTag);
TriviaOnOff[id] = true;
}
else
{
ColorChat(id, RED, "%s^x04 Ai parasit Trivia, nu mai poti juca!", PluginTag);
TriviaOnOff[id] = false;
}
}
return 0;
}
public client_disconnect(id)
{
TriviaOnOff[id] = false;
if(TriviaPoints[id] != 1)
SavePoints(id);
}
public client_putinserver(id)
{
set_task(15.0, "ShowAdvMessage", id + 3333);
LoadPoints(id);
}
public ShowAdvMessage(index)
{
index -= 3333;
ColorChat(index, RED, "%s^x01 In timp ce esti mort poti juca Trivia si poti castiga bani! Tasteaza ^x04/trivia ^x01 in chat pentru activare.", PluginTag);
}
public SavePoints(index)
{
new Vault = nvault_open("trivia_points");
new Key[64], Value[64], Name[32];
get_user_name(index, Name, sizeof Name - 1);
formatex(Key, sizeof Key - 1, "%s-trivia_points:", Name);
formatex(Value, sizeof Value - 1, "%d", TriviaPoints[index]);
nvault_set(Vault, Key, Value);
nvault_close(Vault);
}
public LoadPoints(index)
{
new Vault = nvault_open("trivia_points");
new Key[64], Value[64], Name[32];
get_user_name(index, Name, sizeof Name - 1);
formatex(Key, sizeof Key - 1, "%s-trivia_points:", Name);
nvault_get(Vault, Key, Value, sizeof Value - 1);
nvault_close(Vault);
TriviaPoints[index] = str_to_num(Value);
}
public ShowTriviaTop(id)
{
static Sort[33][2];
new Count;
new Players[32], Num, Player;
get_players(Players, Num);
for(new i = 0 ; i < Num ; i++)
{
Player = Players;
Sort[Count][0] = Player;
Sort[Count][1] = TriviaPoints[Player];
Count++;
}
SortCustom2D(Sort, Count, "points_compare");
new Motd[1024], Len;
Len = format(Motd, sizeof Motd - 1,"<body bgcolor=#000000><font color=#98f5ff><pre>");
Len += format(Motd[Len], (sizeof Motd - 1) - Len,"%s %-22.22s %3s^n", "#", "Name", "Trivia Points");
new b = clamp(Count, 0, 10);
new Name[32], User;
for(new a = 0; a < b; a++)
{
User = Sort[a][0];
get_user_name(User, Name, sizeof Name - 1);
Len += format(Motd[Len], (sizeof Motd - 1) - Len,"%d %-22.22s %d^n", a + 1, Name, Sort[a][1]);
}
Len += format(Motd[Len], (sizeof Motd - 1) - Len,"</body></font></pre>");
show_motd(id, Motd, "Trivia Top 10");
}
public points_compare(elem1[], elem2[])
{
if(elem1[1] > elem2[1])
return -1;
else if(elem1[1] < elem2[1])
return 1;
return 0;
}
public ShowTriviaPoints(id)
ColorChat(id, RED, "%s^x04 Aveti %d puncte trivia%s.", PluginTag, TriviaPoints[id], TriviaPoints[id] > 1 ? "s" : "");