Fix sent url

Fix sent url

/**
 * vim: set ts=4 :
 * =============================================================================
 * in_game_audio
 * TODO - Add your project's description
 *
 * Copyright 2013 ???
 * =============================================================================
 *
 */

#pragma semicolon 1

#include <sourcemod>
#include <steamtools>
#include <base64>
#include <smjansson>

#define PLUGIN_VERSION "0.1"

public Plugin:myinfo =
{
    name = "In Game Audio",
    author = "CrimsonTautology",
    description = "Interact with the In Game Audio web api",
    version = PLUGIN_VERSION,
    url = "https://github.com/CrimsonTautology/sm_in_game_audio"
};

#define QUERY_SONG_ROUTE "/v1/api/query_song"
#define RANDOM_SONG_ROUTE ""
#define SONGS_ROUTE "/songs"
#define DIRECTORIES_ROUTE "/directories"

#define MAX_STEAMID_LENGTH 21
#define MAX_COMMUNITYID_LENGTH 18
#define MAX_SONG_LENGTH 64

new Handle:g_Cvar_IGAApiKey = INVALID_HANDLE;
new Handle:g_Cvar_IGAUrl = INVALID_HANDLE;
new Handle:g_Cvar_IGADonatorsOnly = INVALID_HANDLE;
new Handle:g_Cvar_IGAEnabled = INVALID_HANDLE;
new Handle:g_Cvar_IGARequestCooldownTime = INVALID_HANDLE;

new bool:g_IsInCooldown[MAXPLAYERS+1];
new g_PallNextFree = 0;
public OnPluginStart()
{

    g_Cvar_IGAApiKey = CreateConVar("sm_iga_api_key", "", "API Key for your IGA webpage");
    g_Cvar_IGAUrl = CreateConVar("sm_iga_url", "", "URL to your IGA webpage");
    g_Cvar_IGADonatorsOnly = CreateConVar("sm_iga_donators_only", "1", "Whether or not only donators have access to pall");
    g_Cvar_IGAEnabled = CreateConVar("sm_iga_enabled", "1", "Whether or not pall is enabled");
    g_Cvar_IGARequestCooldownTime = CreateConVar("sm_iga_request_cooldown_time", "2.0", "How long in seconds before a client can send another http request");

    RegConsoleCmd("sm_p", Command_P, "Play a song for yourself");
    RegConsoleCmd("sm_pall", Command_Pall, "Play a song for everyone");
    RegConsoleCmd("sm_plist", Command_Plist, "Pop-up the song list");
    RegConsoleCmd("sm_stop", Command_Stop, "Stop the current song");
    RegAdminCmd("sm_fstop", Command_Fstop, ADMFLAG_VOTE, "[ADMIN] Stop the current pall for everyone");
    RegAdminCmd("sm_fpall", Command_Fpall, ADMFLAG_VOTE, "[ADMIN] Force everyone to listen to a song");
    RegConsoleCmd("sm_vol", Command_Vol, "Adjust your play volume");
    RegConsoleCmd("sm_nopall", Command_Nopall, "Turn off pall for yourself");
    RegConsoleCmd("sm_plast", Command_Plast, "Play the last played song for yourself");

    //HookEvent("map_change", Event_MapChange);
    //HookEvent("client_connect", Event_ClientConnect);
}

public Action:Command_P(client, args)
{
    if (args < 1)
    {
        ReplyToCommand(client, "[IGA] Usage: !p <song>");
        return Plugin_Handled;
    }
    if(IsClientInCooldown(client))
    {
        ReplyToCommand(client, "[IGA] User in cooldown");
        return Plugin_Handled;
    }

    if(!GetConVarBool(g_Cvar_IGAEnabled))
    {
        ReplyToCommand(client, "[IGA] IGA not enabled");
        return Plugin_Handled;
    }

    if(client && IsClientAuthorized(client)){
        decl String:path[MAX_SONG_LENGTH];
        GetCmdArgString(path, sizeof(path));
        QuerySong(client, path, false);
    }

    return Plugin_Handled;
}

public Action:Command_Pall(client, args)
{
    if (args < 1)
    {
        ReplyToCommand(client, "[IGA] Usage: !pall <song>");
        return Plugin_Handled;
    }
    if(IsClientInCooldown(client))
    {
        ReplyToCommand(client, "[IGA] User in cooldown");
        return Plugin_Handled;
    }

    if(!GetConVarBool(g_Cvar_IGAEnabled))
    {
        ReplyToCommand(client, "[IGA] IGA not enabled");
        return Plugin_Handled;
    }

    if(IsInPall())
    {
        ReplyToCommand(client, "[IGA] pall currently in use");
        return Plugin_Handled;
    }

    if(client && IsClientAuthorized(client)){
        decl String:path[MAX_SONG_LENGTH];
        GetCmdArgString(path, sizeof(path));
        QuerySong(client, path, true);
    }

    return Plugin_Handled;
}

public Action:Command_Plist(client, args)
{
    if(client && IsClientAuthorized(client)){
        SongList(client);
    }

    return Plugin_Handled;
}

public Action:Command_Stop(client, args)
{
    StopSong(client);
    return Plugin_Handled;
}

public Action:Command_Fstop(client, args)
{
    StopSongAll();
    return Plugin_Handled;
}

public Action:Command_Fpall(client, args)
{
    //TODO
}

public Action:Command_Vol(client, args)
{
    //TODO
}

public Action:Command_Nopall(client, args)
{
    //TODO
}

public Action:Command_Plast(client, args)
{
    //TODO
}

public Event_MapChange(Handle:event, const String:name[], bool:dontBroadcast)
{
    //TODO
}

public Event_ClientConnect(Handle:event, const String:name[], bool:dontBroadcast)
{
    //TODO
}

public Steam_SetHTTPRequestGetOrPostParameterInt(&HTTPRequestHandle:request, const String:param[], value)
{
    new String:tmp[64];
    IntToString(value, tmp, sizeof(tmp));
    Steam_SetHTTPRequestGetOrPostParameter(request, param, tmp);
}

public SetAccessCode(&HTTPRequestHandle:request)
{
    decl String:api_key[128];
    GetConVarString(g_Cvar_IGAApiKey, api_key, sizeof(api_key));
    Steam_SetHTTPRequestGetOrPostParameter(request, "access_token", api_key);
}

public HTTPRequestHandle:CreateIGARequest(const String:route[])
{
    decl String:base_url[256], String:url[512];
    GetConVarString(g_Cvar_IGAUrl, base_url, sizeof(base_url));
    TrimString(base_url);
    new trim_length = strlen(base_url) - 1;

    if(trim_length < 0)
    {
        //IGA Url not set
        return INVALID_HTTP_HANDLE;
    }

    //check for forward slash after base_url;
    if(base_url[trim_length] == '/')
    {
        strcopy(base_url, trim_length + 1, base_url);
    }

    Format(url, sizeof(url),
            "%s%s", base_url, route);

    new HTTPRequestHandle:request = Steam_CreateHTTPRequest(HTTPMethod_POST, url);
    SetAccessCode(request);

    return request;
}

public StartCooldown(client)
{
    //Ignore the server console
    if (client == 0)
        return;

    g_IsInCooldown[client] = true;
    CreateTimer(GetConVarFloat(g_Cvar_IGARequestCooldownTime), RemoveCooldown, client);
}

public bool:IsClientInCooldown(client)
{
    if(client == 0)
        return false;
    else
        return g_IsInCooldown[client];
}

public Action:RemoveCooldown(Handle:timer, any:client)
{
    g_IsInCooldown[client] = false;
}

public bool:IsInPall()
{
    return GetTime() < g_PallNextFree;
}

stock QuerySong(client, String:path[MAX_SONG_LENGTH], bool:pall = false, bool:force=false, client_theme = 0, String:map_theme[] ="")
{
    decl String:uid[MAX_COMMUNITYID_LENGTH];
    Steam_GetCSteamIDForClient(client, uid, sizeof(uid));

    new HTTPRequestHandle:request = CreateIGARequest(QUERY_SONG_ROUTE);

    if(request == INVALID_HTTP_HANDLE)
    {
        ReplyToCommand(client, "[IGA] sm_iga_url invalid; cannot create HTTP request");
        return;
    }

    Steam_SetHTTPRequestGetOrPostParameter(request, "uid", uid);
    Steam_SetHTTPRequestGetOrPostParameterInt(request, "pall", pall);
    Steam_SetHTTPRequestGetOrPostParameterInt(request, "force", force);
    Steam_SetHTTPRequestGetOrPostParameterInt(request, "player", GetClientUserId(client));
    Steam_SetHTTPRequestGetOrPostParameter(request, "path", path);

    if(client_theme > 0)
    {
        //Find the user's theme
        decl String:uid_theme[MAX_COMMUNITYID_LENGTH];
        Steam_GetCSteamIDForClient(client, uid_theme, sizeof(uid_theme));
        Steam_SetHTTPRequestGetOrPostParameter(request, "uid_theme", uid_theme);
    }else if(strlen(map_theme) > 0){
        //Find the map's theme
        Steam_SetHTTPRequestGetOrPostParameter(request, "map_theme", map_theme);
    }

    Steam_SendHTTPRequest(request, ReceiveQuerySong, GetClientUserId(client));

    StartCooldown(client);
}

public ReceiveQuerySong(HTTPRequestHandle:request, bool:successful, HTTPStatusCode:code, any:userid)
{
    new client = GetClientOfUserId(userid);
    if(!successful || code != HTTPStatusCode_OK)
    {
        LogError("[IGA] Error at RecivedQuerySong (HTTP Code %d; success %d)", code, successful);
        Steam_ReleaseHTTPRequest(request);
        return;
    }

    decl String:data[4096];
    Steam_GetHTTPResponseBodyData(request, data, sizeof(data));
    Steam_ReleaseHTTPRequest(request);

    new Handle:json = json_load(data);
    new bool:found = json_object_get_bool(json, "found");

    if(found)
    {
        new duration = json_object_get_int(json, "duration");
        new bool:pall = json_object_get_bool(json, "pall");
        //new bool:force = json_object_get_bool(json, "force");
        new String:song_id[64], String:full_path[64], String:description[64], String:duration_formated[64];
        json_object_get_string(json, "song_id", song_id, sizeof(song_id));
        json_object_get_string(json, "full_path", full_path, sizeof(full_path));
        json_object_get_string(json, "title", description, sizeof(description));
        json_object_get_string(json, "duration_formated", duration_formated, sizeof(duration_formated));

        if(pall)
        {
            g_PallNextFree = duration + GetTime();
            PrintToChatAll("[IGA] Started Playing \"%s\" to all", description);
            PrintToChatAll("Duration %s", duration_formated);
            PrintToChatAll("Type !stop to cancel or !nopall to mute");
            PlaySongAll(song_id);
        }else if(client > 0){
            PrintToChat(client, "[IGA] Started Playing \"%s\" to all", description);
            PrintToChat(client, "Duration %s", duration_formated);
            PrintToChat(client, "Type !stop to cancel");
            PlaySong(client, song_id);
        }
    }else{
        PrintToChat(client, "[IGA] Could not find specified sound or directory");
    }

    CloseHandle(json);
    Steam_ReleaseHTTPRequest(request);
}

public PlaySongAll(String:song[])
{
    //TODO update PALL duration
    for (new client=1; client <= MaxClients; client++)
    {
        if (ClientHasPallEnabled(client))
        {
            PlaySong(client, song);
        }
    }
}

public PlaySong(client, String:song_id[])
{
    decl String:url[256], String:base_url[128];
    GetConVarString(g_Cvar_IGAUrl, base_url, sizeof(base_url));

    TrimString(base_url);
    new trim_length = strlen(base_url) - 1;

    if(base_url[trim_length] == '/')
    {
        strcopy(base_url, trim_length + 1, base_url);
    }

    Format(url, sizeof(url),
            "%s%s/%s", base_url, SONGS_ROUTE, song_id);
    //PrintToChatAll("%s", url); //TODO

    new Handle:panel = CreateKeyValues("data");
    KvSetString(panel, "title", "In Game Audio");
    KvSetNum(panel, "type", MOTDPANEL_TYPE_URL);
    KvSetString(panel, "msg", url);

    ShowVGUIPanel(client, "info", panel, false);
    CloseHandle(panel);
    return;
}

public StopSong(client)
{
    PlaySong(client, "stop");//TODO
}
public StopSongAll()
{
    PlaySongAll("stop");//TODO
}

public bool:ClientHasPallEnabled(client)
{
    //TODO do cookie check
    return IsClientAuthorized(client);
}

public SongList(client)
{
    decl String:map[PLATFORM_MAX_PATH], String:url[256], String:base_url[128];
    GetConVarString(g_Cvar_IGAUrl, base_url, sizeof(base_url));

    TrimString(base_url);
    new trim_length = strlen(base_url) - 1;

    if(base_url[trim_length] == '/')
    {
        strcopy(base_url, trim_length + 1, base_url);
    }

    Format(url, sizeof(url),
            "%s%s", base_url, DIRECTORIES_ROUTE);

    ShowMOTDPanel(client, "Song List", url, MOTDPANEL_TYPE_URL);

}

/**
 * vim: set ts=4 :
 * =============================================================================
 * in_game_audio
 * TODO - Add your project's description
 *
 * Copyright 2013 ???
 * =============================================================================
 *
 */

#pragma semicolon 1

#include <sourcemod>
#include <steamtools>
#include <base64>
#include <smjansson>

#define PLUGIN_VERSION "0.1"

public Plugin:myinfo =
{
    name = "In Game Audio",
    author = "CrimsonTautology",
    description = "Interact with the In Game Audio web api",
    version = PLUGIN_VERSION,
    url = "https://github.com/CrimsonTautology/sm_in_game_audio"
};

#define QUERY_SONG_ROUTE "/v1/api/query_song"
#define RANDOM_SONG_ROUTE ""
#define SONGS_ROUTE "/songs"
#define DIRECTORIES_ROUTE "/directories"

#define MAX_STEAMID_LENGTH 21
#define MAX_COMMUNITYID_LENGTH 18
#define MAX_SONG_LENGTH 64

new Handle:g_Cvar_IGAApiKey = INVALID_HANDLE;
new Handle:g_Cvar_IGAUrl = INVALID_HANDLE;
new Handle:g_Cvar_IGADonatorsOnly = INVALID_HANDLE;
new Handle:g_Cvar_IGAEnabled = INVALID_HANDLE;
new Handle:g_Cvar_IGARequestCooldownTime = INVALID_HANDLE;

new bool:g_IsInCooldown[MAXPLAYERS+1];
new g_PallNextFree = 0;
public OnPluginStart()
{

    g_Cvar_IGAApiKey = CreateConVar("sm_iga_api_key", "", "API Key for your IGA webpage");
    g_Cvar_IGAUrl = CreateConVar("sm_iga_url", "", "URL to your IGA webpage");
    g_Cvar_IGADonatorsOnly = CreateConVar("sm_iga_donators_only", "1", "Whether or not only donators have access to pall");
    g_Cvar_IGAEnabled = CreateConVar("sm_iga_enabled", "1", "Whether or not pall is enabled");
    g_Cvar_IGARequestCooldownTime = CreateConVar("sm_iga_request_cooldown_time", "2.0", "How long in seconds before a client can send another http request");

    RegConsoleCmd("sm_p", Command_P, "Play a song for yourself");
    RegConsoleCmd("sm_pall", Command_Pall, "Play a song for everyone");
    RegConsoleCmd("sm_plist", Command_Plist, "Pop-up the song list");
    RegConsoleCmd("sm_stop", Command_Stop, "Stop the current song");
    RegAdminCmd("sm_fstop", Command_Fstop, ADMFLAG_VOTE, "[ADMIN] Stop the current pall for everyone");
    RegAdminCmd("sm_fpall", Command_Fpall, ADMFLAG_VOTE, "[ADMIN] Force everyone to listen to a song");
    RegConsoleCmd("sm_vol", Command_Vol, "Adjust your play volume");
    RegConsoleCmd("sm_nopall", Command_Nopall, "Turn off pall for yourself");
    RegConsoleCmd("sm_plast", Command_Plast, "Play the last played song for yourself");

    //HookEvent("map_change", Event_MapChange);
    //HookEvent("client_connect", Event_ClientConnect);
}

public Action:Command_P(client, args)
{
    if (args < 1)
    {
        ReplyToCommand(client, "[IGA] Usage: !p <song>");
        return Plugin_Handled;
    }
    if(IsClientInCooldown(client))
    {
        ReplyToCommand(client, "[IGA] User in cooldown");
        return Plugin_Handled;
    }

    if(!GetConVarBool(g_Cvar_IGAEnabled))
    {
        ReplyToCommand(client, "[IGA] IGA not enabled");
        return Plugin_Handled;
    }

    if(client && IsClientAuthorized(client)){
        decl String:path[MAX_SONG_LENGTH];
        GetCmdArgString(path, sizeof(path));
        QuerySong(client, path, false);
    }

    return Plugin_Handled;
}

public Action:Command_Pall(client, args)
{
    if (args < 1)
    {
        ReplyToCommand(client, "[IGA] Usage: !pall <song>");
        return Plugin_Handled;
    }
    if(IsClientInCooldown(client))
    {
        ReplyToCommand(client, "[IGA] User in cooldown");
        return Plugin_Handled;
    }

    if(!GetConVarBool(g_Cvar_IGAEnabled))
    {
        ReplyToCommand(client, "[IGA] IGA not enabled");
        return Plugin_Handled;
    }

    if(IsInPall())
    {
        ReplyToCommand(client, "[IGA] pall currently in use");
        return Plugin_Handled;
    }

    if(client && IsClientAuthorized(client)){
        decl String:path[MAX_SONG_LENGTH];
        GetCmdArgString(path, sizeof(path));
        QuerySong(client, path, true);
    }

    return Plugin_Handled;
}

public Action:Command_Plist(client, args)
{
    if(client && IsClientAuthorized(client)){
        SongList(client);
    }

    return Plugin_Handled;
}

public Action:Command_Stop(client, args)
{
    StopSong(client);
    return Plugin_Handled;
}

public Action:Command_Fstop(client, args)
{
    StopSongAll();
    return Plugin_Handled;
}

public Action:Command_Fpall(client, args)
{
    //TODO
}

public Action:Command_Vol(client, args)
{
    //TODO
}

public Action:Command_Nopall(client, args)
{
    //TODO
}

public Action:Command_Plast(client, args)
{
    //TODO
}

public Event_MapChange(Handle:event, const String:name[], bool:dontBroadcast)
{
    //TODO
}

public Event_ClientConnect(Handle:event, const String:name[], bool:dontBroadcast)
{
    //TODO
}

public Steam_SetHTTPRequestGetOrPostParameterInt(&HTTPRequestHandle:request, const String:param[], value)
{
    new String:tmp[64];
    IntToString(value, tmp, sizeof(tmp));
    Steam_SetHTTPRequestGetOrPostParameter(request, param, tmp);
}

public SetAccessCode(&HTTPRequestHandle:request)
{
    decl String:api_key[128];
    GetConVarString(g_Cvar_IGAApiKey, api_key, sizeof(api_key));
    Steam_SetHTTPRequestGetOrPostParameter(request, "access_token", api_key);
}

public HTTPRequestHandle:CreateIGARequest(const String:route[])
{
    decl String:base_url[256], String:url[512];
    GetConVarString(g_Cvar_IGAUrl, base_url, sizeof(base_url));
    TrimString(base_url);
    new trim_length = strlen(base_url) - 1;

    if(trim_length < 0)
    {
        //IGA Url not set
        return INVALID_HTTP_HANDLE;
    }

    //check for forward slash after base_url;
    if(base_url[trim_length] == '/')
    {
        strcopy(base_url, trim_length + 1, base_url);
    }

    Format(url, sizeof(url),
            "%s%s", base_url, route);

    PrintToChatAll("hit %s", url); //TODO
    new HTTPRequestHandle:request = Steam_CreateHTTPRequest(HTTPMethod_POST, url);
    SetAccessCode(request);

    return request;
}

public StartCooldown(client)
{
    //Ignore the server console
    if (client == 0)
        return;

    g_IsInCooldown[client] = true;
    CreateTimer(GetConVarFloat(g_Cvar_IGARequestCooldownTime), RemoveCooldown, client);
}

public bool:IsClientInCooldown(client)
{
    if(client == 0)
        return false;
    else
        return g_IsInCooldown[client];
}

public Action:RemoveCooldown(Handle:timer, any:client)
{
    g_IsInCooldown[client] = false;
}

public bool:IsInPall()
{
    return GetTime() < g_PallNextFree;
}

stock QuerySong(client, String:path[MAX_SONG_LENGTH], bool:pall = false, bool:force=false, client_theme = 0, String:map_theme[] ="")
{
    decl String:uid[MAX_COMMUNITYID_LENGTH];
    Steam_GetCSteamIDForClient(client, uid, sizeof(uid));

    new HTTPRequestHandle:request = CreateIGARequest(QUERY_SONG_ROUTE);

    if(request == INVALID_HTTP_HANDLE)
    {
        ReplyToCommand(client, "[IGA] sm_iga_url invalid; cannot create HTTP request");
        return;
    }

    Steam_SetHTTPRequestGetOrPostParameter(request, "uid", uid);
    Steam_SetHTTPRequestGetOrPostParameterInt(request, "pall", pall);
    Steam_SetHTTPRequestGetOrPostParameterInt(request, "force", force);
    Steam_SetHTTPRequestGetOrPostParameterInt(request, "player", GetClientUserId(client));
    Steam_SetHTTPRequestGetOrPostParameter(request, "path", path);

    if(client_theme > 0)
    {
        //Find the user's theme
        decl String:uid_theme[MAX_COMMUNITYID_LENGTH];
        Steam_GetCSteamIDForClient(client, uid_theme, sizeof(uid_theme));
        Steam_SetHTTPRequestGetOrPostParameter(request, "uid_theme", uid_theme);
    }else if(strlen(map_theme) > 0){
        //Find the map's theme
        Steam_SetHTTPRequestGetOrPostParameter(request, "map_theme", map_theme);
    }

    PrintToChatAll("hit 1"); //TODO
    Steam_SendHTTPRequest(request, ReceiveQuerySong, GetClientUserId(client));

    StartCooldown(client);
}

public ReceiveQuerySong(HTTPRequestHandle:request, bool:successful, HTTPStatusCode:code, any:userid)
{
    new client = GetClientOfUserId(userid);
    if(!successful || code != HTTPStatusCode_OK)
    {
        LogError("[IGA] Error at RecivedQuerySong (HTTP Code %d; success %d)", code, successful);
        Steam_ReleaseHTTPRequest(request);
        return;
    }

    decl String:data[4096];
    Steam_GetHTTPResponseBodyData(request, data, sizeof(data));
    Steam_ReleaseHTTPRequest(request);

    new Handle:json = json_load(data);
    new bool:found = json_object_get_bool(json, "found");

    if(found)
    {
        new duration = json_object_get_int(json, "duration");
        new bool:pall = json_object_get_bool(json, "pall");
        //new bool:force = json_object_get_bool(json, "force");
        new String:song_id[64], String:full_path[64], String:description[64], String:duration_formated[64];
        json_object_get_string(json, "song_id", song_id, sizeof(song_id));
        json_object_get_string(json, "full_path", full_path, sizeof(full_path));
        json_object_get_string(json, "title", description, sizeof(description));
        json_object_get_string(json, "duration_formated", duration_formated, sizeof(duration_formated));

        if(pall)
        {
            g_PallNextFree = duration + GetTime();
            PrintToChatAll("[IGA] Started Playing \"%s\" to all", description);
            PrintToChatAll("Duration %s", duration_formated);
            PrintToChatAll("Type !stop to cancel or !nopall to mute");
            PlaySongAll(song_id);
        }else if(client > 0){
            PrintToChat(client, "[IGA] Started Playing \"%s\" to all", description);
            PrintToChat(client, "Duration %s", duration_formated);
            PrintToChat(client, "Type !stop to cancel");
            PlaySong(client, song_id);
        }
    }else{
        PrintToChat(client, "[IGA] Could not find specified sound or directory");
    }

    CloseHandle(json);
    Steam_ReleaseHTTPRequest(request);
}

public PlaySongAll(String:song[])
{
    //TODO update PALL duration
    for (new client=1; client <= MaxClients; client++)
    {
        if (ClientHasPallEnabled(client))
        {
            PlaySong(client, song);
        }
    }
}

public PlaySong(client, String:song[])
{
    decl String:url[256], String:base_url[128];
    GetConVarString(g_Cvar_IGAUrl, base_url, sizeof(base_url));

    TrimString(base_url);
    new trim_length = strlen(base_url) - 1;

    if(base_url[trim_length] == '/')
    {
        strcopy(base_url, trim_length + 1, base_url);
    }

    Format(url, sizeof(url),
            "%s%s/%s", base_url, SONGS_ROUTE, song);

    new Handle:panel = CreateKeyValues("data");
    KvSetString(panel, "title", "In Game Audio");
    KvSetNum(panel, "type", MOTDPANEL_TYPE_URL);
    KvSetString(panel, "msg", url);

    ShowVGUIPanel(client, "info", panel, false);
    CloseHandle(panel);
    return;
}

public StopSong(client)
{
    PlaySong(client, "stop");//TODO
}
public StopSongAll()
{
    PlaySongAll("stop");//TODO
}

public bool:ClientHasPallEnabled(client)
{
    //TODO do cookie check
    return IsClientAuthorized(client);
}

public SongList(client)
{
    decl String:map[PLATFORM_MAX_PATH], String:url[256], String:base_url[128];
    GetConVarString(g_Cvar_IGAUrl, base_url, sizeof(base_url));

    TrimString(base_url);
    new trim_length = strlen(base_url) - 1;

    if(base_url[trim_length] == '/')
    {
        strcopy(base_url, trim_length + 1, base_url);
    }

    Format(url, sizeof(url),
            "%s%s", base_url, DIRECTORIES_ROUTE);

    ShowMOTDPanel(client, "Song List", url, MOTDPANEL_TYPE_URL);

}


Additional Lighttrails stuff

Additional Lighttrails stuff

Check it out, more stuff!

/**
* TheXeon
* ngs_autorestart.sp
*
* Files:
* addons/sourcemod/plugins/ngs_autorestart.smx
* cfg/sourcemod/autorestart.cfg
*
* Dependencies:
* sourcemod.inc, sdktools.inc, multicolors.inc, ngsutils.inc, ngsupdater.inc
*/
#pragma newdecls required
#pragma semicolon 1

#define CONTENT_URL "https://github.com/NGSNetwork/sm-plugins/raw/master/"

#include <sourcemod>
#include <sdktools>
#include <multicolors>
#include <ngsutils>
#include <ngsupdater>

public Plugin myinfo =
{
	name = "[NGS] Light Trails",
	author = "bSun Halt / TheXeon",
	description = "Gives a light trail",
	version = "1.0.0",
	url = "http://sourcemod.net"
}

static int SpriteTrail[MAXPLAYERS + 1];

public void OnPluginStart()
{
	RegAdminCmd("sm_trail", Command_Trail, ADMFLAG_GENERIC, "sm_trail <color/hex>");
}

public void OnMapStart()
{
	PrecacheModel("materials/sprites/spotlight.vmt");
}

public void OnClientPutInServer(int client)
{
	SpriteTrail[client] = INVALID_ENT_REFERENCE;
}

public void OnClientDisconnect(int client)
{
	if (IsValidEntity(SpriteTrail[client]))
	{
		AcceptEntityInput(SpriteTrail[client], "Kill");
		SpriteTrail[client] = INVALID_ENT_REFERENCE;
	}
}

public Action Command_Trail(int client, int args)
{
	if (args == 0)
	{
		CReplyToCommand(client, "{GREEN}[SM]{DEFAULT} Usage: sm_trail <color/hex>");
		return Plugin_Handled;
	}

	char arg[64];
	char TrailColor[64];
//	char ClientName[128];
	int trailcolornum;

//	Format(ClientName, sizeof(ClientName), "customname_%i", client);
//	DispatchKeyValue(client, "targetname", ClientName);
	int Trail = CreateEntityByName("env_spritetrail");
	DispatchKeyValue(Trail, "renderamt", "255");
	DispatchKeyValue(Trail, "rendermode", "1");
	DispatchKeyValue(Trail, "spritename", "materials/sprites/spotlight.vmt");
	DispatchKeyValue(Trail, "lifetime", "3.0");
	DispatchKeyValue(Trail, "startwidth", "8.0");
	DispatchKeyValue(Trail, "endwidth", "0.1");

	GetCmdArgString(arg, sizeof(arg));
	StrToLowerRemoveBlanks(arg, TrailColor, sizeof(TrailColor));

	if (IsValidEntity(SpriteTrail[client]))
	{
		AcceptEntityInput(SpriteTrail[client], "Kill");
		SpriteTrail[client] = INVALID_ENT_REFERENCE;
	}

	if (StrEqual(TrailColor, "off", false))
	{
		if (IsValidEntity(Trail))
		{
			AcceptEntityInput(Trail, "Kill");
		}
		CReplyToCommand(client, "{GREEN}[SM]{DEFAULT} Your trail has been disabled!");
		return Plugin_Handled;
	}
	else if (MC_Trie != null && MC_Trie.GetValue(TrailColor, trailcolornum))
	{
		int rgbFromHex[3];
		rgbFromHex[0] = (trailcolornum >> 16) & 255;
		rgbFromHex[1] = (trailcolornum >> 8) & 255;
		rgbFromHex[2] = trailcolornum & 255;

		char rgbString[16];

		Format(rgbString, sizeof(rgbString), "%d %d %d", rgbFromHex[0], rgbFromHex[1], rgbFromHex[2]);
		DispatchKeyValue(Trail, "rendercolor", rgbString);
	}
	else
	{
		int decimalValue = StringToHex(TrailColor);
		int rgbFromHex[3];
		rgbFromHex[0] = (decimalValue >> 16) & 255;
		rgbFromHex[1] = (decimalValue >> 8) & 255;
		rgbFromHex[2] = decimalValue & 255;

		char rgbString[16];

		Format(rgbString, 16, "%d %d %d", rgbFromHex[0], rgbFromHex[1], rgbFromHex[2]);
		DispatchKeyValue(Trail, "rendercolor", rgbString);
	}

	DispatchSpawn(Trail);
	SpriteTrail[client] = Trail;

	float CurrentOrigin[3];
	GetClientAbsOrigin(client, CurrentOrigin);
	CurrentOrigin[2] += 10.0;
	TeleportEntity(Trail, CurrentOrigin, NULL_VECTOR, NULL_VECTOR);
//	SetVariantString(ClientName);

	AcceptEntityInput(Trail, "SetParent", -1, -1);
	AcceptEntityInput(Trail, "showsprite", -1, -1);

	CReplyToCommand(client, "{GREEN}[SM]{DEFAULT} You've been given a %s trail!", arg);

	return Plugin_Handled;
}

public void OnPluginEnd()
{
	for (int i = 1; i <= MaxClients; i++)
		if (IsValidClient(i) && IsValidEntity(SpriteTrail[i]))
			AcceptEntityInput(SpriteTrail[i], "Kill");
}

// Stock converted from:
// https://github.com/GabiGrin/hex-to-rgb-string/blob/master/index.js
stock int StringToHex(char hex[64])
{
	Regex hexPattern = new Regex("/^#(?:[0-9a-f]{3}){1,2}$/i");
	if (!hex || hexPattern.Match(hex) == -1)
	{
		delete hexPattern;
		return 0;
	}
	ReplaceString(hex, sizeof(hex), "#", "");
	ReplaceString(hex, sizeof(hex), "0x", "", false);
	char splitString[6];
	if (strlen(hex) == 3)
	{
		for (int i = 0; i < 5; i += 2)
		{
			splitString[i] = hex[i/2];
			splitString[i + 1] = hex[i/2];
		}
	}
	else
		strcopy(splitString, sizeof(splitString), hex);

	delete hexPattern;
	return StringToInt(splitString, 16);
}

// Credit to berni: https://forums.alliedmods.net/showpost.php?p=1008853&postcount=4
stock int StrToLowerRemoveBlanks(const char[] str, char[] dest, int destsize) {

	int n=0, x=0;
	while (str[n] != '\0' && x < (destsize - 1)) { // Make sure we are inside bounds

		int character = str[n++]; // Caching

		if (character == ' ') { // Am I nothing ?
			// Let's do nothing !
			continue;
		}
		else if (IsCharUpper(character)) { // Am I big ?
			character = CharToLower(character); // Big becomes low
		}

		dest[x++] = character; // Write into our new string
	}

	dest[x++] = '\0'; // Finalize with the end ( = always 0 for strings)

	return x; // return number of bytes written for later proove
}

#pragma newdecls required
#pragma semicolon 1

#include <sourcemod>
#include <sdktools>
#include <multicolors>
#include <regex>

public Plugin myinfo =
{
	name = "Light Trails",
	author = "bSun Halt",
	description = "Gives a light trail",
	version = "Light Trails 1.0",
	url = "http://sourcemod.net"
}

static SpriteTrail[33];
Regex hexPattern;

public void OnPluginStart()
{
	hexPattern = new Regex("/^#(?:[0-9a-f]{3}){1,2}$/i");
	RegAdminCmd("sm_trail", Command_Trail, ADMFLAG_GENERIC, "sm_trail <color>");
}

public void OnClientPutInServer(int Client)
{
	SpriteTrail[Client] = -1;
}

public Action Command_Trail(int Client, int Args)
{
	if (Args < 1 || Args > 1)
	{
		PrintToChat(Client, "[SM] sm_trail <color>");
		return Plugin_Handled;
	}

	char arg[64];
	char TrailColor[64];
	char ClientName[128];
	int trailcolornum;

	Format(ClientName, sizeof(ClientName), "customname_%i", Client);
	DispatchKeyValue(Client, "targetname", ClientName);
	int Trail = CreateEntityByName("env_spritetrail");
	DispatchKeyValue(Trail, "renderamt", "255");
	DispatchKeyValue(Trail, "rendermode", "1");
	DispatchKeyValue(Trail, "spritename", "materials/sprites/spotlight.vmt");
	DispatchKeyValue(Trail, "lifetime", "3.0");
	DispatchKeyValue(Trail, "startwidth", "8.0");
	DispatchKeyValue(Trail, "endwidth", "0.1");

	GetCmdArgString(arg, sizeof(arg));
	StrToLowerRemoveBlanks(arg, TrailColor, sizeof(TrailColor));

	if (SpriteTrail[Client] != -1)
		AcceptEntityInput(SpriteTrail[Client], "Kill");

	if (StrEqual(TrailColor, "off", false))
	{
		if (SpriteTrail[Client] != -1)
		{
			AcceptEntityInput(Trail, "Kill");
			SpriteTrail[Client] = -1;
		}
		return Plugin_Handled;
	}
	else if (MC_Trie != null && MC_Trie.GetValue(TrailColor, trailcolornum))
	{
		int rgbFromHex[3];
		rgbFromHex[0] = (trailcolornum >> 16) & 255;
		rgbFromHex[1] = (trailcolornum >> 8) & 255;
		rgbFromHex[2] = trailcolornum & 255;

		char rgbString[16];

		Format(rgbString, 16, "%d %d %d", rgbFromHex[0], rgbFromHex[1], rgbFromHex[2]);
		DispatchKeyValue(Trail, "rendercolor", rgbString);
	}
	else
	{
		int decimalValue = HexToBase16Int(TrailColor);
		int rgbFromHex[3];
		rgbFromHex[0] = (decimalValue >> 16) & 255;
		rgbFromHex[1] = (decimalValue >> 8) & 255;
		rgbFromHex[2] = decimalValue & 255;

		char rgbString[16];

		Format(rgbString, 16, "%d %d %d", rgbFromHex[0], rgbFromHex[1], rgbFromHex[2]);
		DispatchKeyValue(Trail, "rendercolor", rgbString);
	}

	DispatchSpawn(Trail);
	SpriteTrail[Client] = Trail;

	float CurrentOrigin[3];
	GetClientAbsOrigin(Client, CurrentOrigin);
	CurrentOrigin[2] += 10.0;
	TeleportEntity(Trail, CurrentOrigin, NULL_VECTOR, NULL_VECTOR);
	SetVariantString(ClientName);

	AcceptEntityInput(Trail, "SetParent", -1, -1);
	AcceptEntityInput(Trail, "showsprite", -1, -1);

	CReplyToCommand(Client, "{GREEN}[SM]{DEFAULT} You've been given a %s trail!", arg);

	return Plugin_Handled;
}

public void OnPluginEnd()
{
	for (int i = 1; i <= MaxClients; i++)
		if (SpriteTrail[i] != -1)
			AcceptEntityInput(SpriteTrail[i], "Kill");
}

// Stock converted from:
// https://github.com/GabiGrin/hex-to-rgb-string/blob/master/index.js
stock int HexToBase16Int(char[] hex)
{
	if (!hex || hexPattern.Match(hex) == -1)
		return 0;
	ReplaceString(hex, 16, "#", "");
	char splitString[6];
	if (strlen(hex) == 3)
	{
		for (int i = 0; i < 5; i += 2)
		{
			splitString[i] = hex[i/2];
			splitString[i + 1] = hex[i/2];
		}
	}
	else
		strcopy(splitString, 6, hex);

	return StringToInt(splitString, 16);
}

// Credit to berni: https://forums.alliedmods.net/showpost.php?p=1008853&postcount=4
stock int StrToLowerRemoveBlanks(const char[] str, char[] dest, int destsize) {

    int n=0, x=0;
    while (str[n] != '\0' && x < (destsize - 1)) { // Make sure we are inside bounds

        int character = str[n++]; // Caching

        if (character == ' ') { // Am I nothing ?
            // Let's do nothing !
            continue;
        }
        else if (IsCharUpper(character)) { // Am I big ?
            character = CharToLower(character); // Big becomes low
        }

        dest[x++] = character; // Write into our new string
    }

    dest[x++] = '\0'; // Finalize with the end ( = always 0 for strings)

    return x; // return number of bytes written for later proove
}


Symbol name changes

Symbol name changes

  - GiveawayType ~> Giveaway
  - [Tt]ype ~> g, giveaway, etc
  - etc

/*
 * Copyright © 2016 Alex "ZeroKnight" George
 *
 * This file is licensed under the MIT License. For details, please see the
 * LICENSE file that should be included with the source code, or
 * <https://opensource.org/licenses/MIT>
 */

#include <sourcemod>

#pragma semicolon 1
#pragma newdecls required

char PLUGIN_NAME[]    = "Multi-Giveaway";
char PLUGIN_AUTHOR[]  = "Alex \"ZeroKnight\" George";
char PLUGIN_DESC[]    = "Expansive Giveaway system with numerous types of Giveaway events and stats";
char PLUGIN_VERSION[] = "0.2.0";
char PLUGIN_URL[]     = "http:/github.com/ZeroKnight/sm-Multi-Giveaway";
char PLUGIN_TAG[]     = "[MG]";

public Plugin myinfo =
{
  name        = PLUGIN_NAME,
  author      = PLUGIN_AUTHOR,
  description = PLUGIN_DESC,
  version     = PLUGIN_VERSION,
  url         = PLUGIN_URL
};

const int MG_MAX_MESSAGE_LENGTH = 256;
const int MAX_OPTION_LENGTH = 64;

/* NOTE: (<<= 1) is actually a neat thing that SourcePawn does. It lets you
 * specify how to 'increment' each enumeration. In this case, each one will be
 * the previous value left shifted 1, which gives us our bit-field.
 */
enum Giveaway (<<= 1)
{
  GT_Invalid = -1, GT_Dice = 1, GT_Number, GT_Kill, GT_All = 7
};
const int nGiveaways = 3;

/* Global state variables/data structures */
StringMap GiveawayData;
ArrayList Dice_PlayerRolls;
ArrayList Dice_ReRolls;
ArrayList Number_PlayerGuesses;

ArrayList Current_GiveawayOpts; // NOTE: Subject to change

//File configfile = ...

// TODO: Put things like this in a personal core library?
#define CVAR(%1) ConVar cv_%1
char CVAR_PREFIX[] = "multi_giveaway";
CVAR(version);
CVAR(flags);
CVAR(enabled_giveaways);
CVAR(player_state);
CVAR(min_conn_time);
CVAR(dice_min);
CVAR(dice_max);
CVAR(dice_show_rolls);
CVAR(dice_rerolls);
CVAR(number_min);
CVAR(number_max);
CVAR(number_show_guesses);

int abs(const int i) { return (i > 0) ? i : -i; }
int min(const int i, const int k) { return (i < k) ? i : k; }
int max(const int i, const int k) { return (i > k) ? i : k; }
int clamp(const int i, const int minval, const int maxval)
{
  return max(minval, min(i, maxval));
}

void RegisterConVars()
{
  // XXX: Function overloads would be nice. Or casting that worked on strings.
  char sGT_All[32]; IntToString(view_as<int>(GT_All), sGT_All, sizeof(sGT_All));

  /* Core */
  cv_version = CreateConVar(
    "multi_giveaway_version",
    PLUGIN_VERSION,
    "Plugin version",
    FCVAR_SPONLY|FCVAR_REPLICATED|FCVAR_NOTIFY|FCVAR_DONTRECORD);
  cv_flags = CreateConVar(
    "multi_giveaway_flags",
    "b",
    "List of admin flags allowed to manage giveaways");
  cv_enabled_giveaways = CreateConVar(
    "multi_giveaway_enabled_giveaways",
    sGT_All,
    "Bit-field (sum of options) of enabled Giveaways",
    FCVAR_NONE,
    true, 1.0,
    true, float(GT_All));
  cv_player_state = CreateConVar(
    "multi_giveaway_player_state",
    "2",
    "Player states eligible for Giveaway participation; each setting includes the previous one: 0 - Alive only, 1 - Dead, 2 - Spectator",
    FCVAR_NONE,
    true, 0.0,
    true, 2.0);
  cv_min_conn_time = CreateConVar( // TODO: Allow certain giveaways to override (eg. kill objective)
    "multi_giveaway_min_conn_time",
    "300", // 5 minutes
    "Amount of time in seconds a player must be connected to participate in a Giveaway",
    FCVAR_NONE,
    true, 0.0);

  /* Dice */
  cv_dice_min = CreateConVar(
    "multi_giveaway_dice_min",
    "1",
    "Lowest possible number a player may roll",
    FCVAR_NONE,
    true, 1.0);
  cv_dice_max = CreateConVar(
    "multi_giveaway_dice_max",
    "50",
    "Highest possible number a player may roll");
  cv_dice_show_rolls = CreateConVar(
    "multi_giveaway_dice_show_rolls",
    "0",
    "Who can see other clients' dice rolls: 0 - Nobody, 1 - Admins, 2 - Everyone",
    FCVAR_NONE,
    true, 0.0,
    true, 2.0);
  cv_dice_rerolls = CreateConVar(
    "multi_giveaway_dice_rerolls",
    "0",
    "Number of 'do-overs' a player gets when rolling. Only the last is counted, for better or for worse",
    FCVAR_NONE,
    true, 0.0);

  /* Number Guess */
  cv_number_min = CreateConVar(
    "multi_giveaway_number_min",
    "1",
    "Lower bound of the range of numbers",
    FCVAR_NONE,
    true, 1.0);
  cv_number_max = CreateConVar(
    "multi_giveaway_number_max",
    "100",
    "Upper bound of the range of numbers");
  cv_number_show_guesses = CreateConVar(
    "multi_giveaway_number_show_guesses",
    "2",
    "Who can see other clients' guesses: 0 - Nobody, 1 - Admins, 2 - Everyone",
    FCVAR_NONE,
    true, 0.0,
    true, 2.0);
}

void RegisterCommands()
{
  // XXX: Is there a better way to create aliases?
  // RegConsoleCmd for client commands (ie participation)
  // RegAdminCmd for admin commands (ie (start|stop)ing giveaways methodmap may
  // be helpful
  //
  // We can use AddCommandListener() to allow for "commands" that aren't
  // actually registered commands (think 'nominate')

  RegAdminCmd("sm_mg_reload", Command_ReloadConfig, ADMFLAG_CONFIG,
    "Reload configuration file");
  RegAdminCmd("sm_multigiveaway", Command_MultiGiveaway, ADMFLAG_GENERIC,
    "Manage giveaways");
  RegAdminCmd("sm_mg", Command_MultiGiveaway, ADMFLAG_GENERIC,
    "Manage giveaways");
  RegAdminCmd("sm_dice_rig", Command_Dice_Rig, ADMFLAG_CHEATS,
              "Rigs the Dice Giveaway so the chosen client wins");

  RegConsoleCmd("sm_rolldice", Command_Dice_Roll,
                "Rolls the dice during a Dice Giveaway");
  RegConsoleCmd("sm_reroll", Command_Dice_ReRoll,
                "Rolls the dice again during a Giveaway, giving you a new result (for better or worse!");
  RegConsoleCmd("sm_guess", Command_Number_Guess,
                "Guess a number for the Number-Guess Giveaway");
}

bool LoadConfig()
{
  //if (configfile != null) // is this boilerplate necessary?
    //CloseHandle(configfile);
  // ...
  return true;
}

bool CanParticipate(const int client)
{
  bool spectating = GetClientTeam(client) == 1;

  /* Check player state */
  if (!IsPlayerAlive(client))
  {
    char phrase[32];
    switch (cv_player_state.IntValue)
    {
      case 0:
      {
        strcopy(phrase, sizeof(phrase),
                spectating ? "MG_Ineligible_Spec" : "MG_Ineligible_Alive");
      }
      case 1:
      {
        if (spectating)
          strcopy(phrase, sizeof(phrase), "MG_Ineligible_Spec");
      }
    }
    if (strlen(phrase))
    {
      ReplyToCommand(client, "%s %t", PLUGIN_TAG, phrase);
      return false;
    }
  }

  /* Check connection time */
  float time = GetClientTime(client);
  int delta = cv_min_conn_time.IntValue - RoundToCeil(time);
  if (time < cv_min_conn_time.FloatValue)
  {
    ReplyToCommand(client, "%s %t", PLUGIN_TAG, "MG_Not_Connected_Long_Enough",
                   delta);
    return false;
  }
  return true;
}

void GetGiveawayOpts(char[] str, const int sz)
{
  char opt[64];
  for (int i = 0; i < Current_GiveawayOpts.Length; ++i)
  {
    Current_GiveawayOpts.GetString(i, opt, sizeof(opt));
    StrCat(str, sz, opt);
    StrCat(str, sz, "; ");
  }
}

void ArraySetAll(ArrayList& array, any value)
{
  for (int i = 0; i < array.Length; ++i)
    array.Set(i, value);
}

void SendToAdmins(const char[] format, any ...)
{
  char buffer[MG_MAX_MESSAGE_LENGTH];
  for (int i = 1; i <= MaxClients; ++i)
  {
    if (IsClientInGame(i) && GetUserAdmin(i) != INVALID_ADMIN_ID)
    {
      SetGlobalTransTarget(i);
      VFormat(buffer, sizeof(buffer), format, 2);
      PrintToChat(i, "%s", buffer);
    }
  }
}

/***************************************************************\
 * Giveaway Functions
\***************************************************************/

void GetGiveawayName(const Giveaway g, char[] str, const int sz)
{
  switch (g)
  {
    case GT_Invalid: return;
    case GT_Dice:    strcopy(str, sz, "Dice");
    case GT_Number:  strcopy(str, sz, "Number");
    case GT_Kill:    strcopy(str, sz, "Kill");
    default:         return;
  }
}

Giveaway GetGiveaway(const char[] name)
{
  if      (StrEqual(name, "dice", false))   return GT_Dice;
  else if (StrEqual(name, "number", false)) return GT_Number;
  else if (StrEqual(name, "kill", false))   return GT_Kill;
  else return GT_Invalid;
}

bool IsGiveawayEnabled(const Giveaway g)
{
  return view_as<Giveaway>(cv_enabled_giveaways.IntValue) & g ? true : false;
}

Giveaway GetCurrentGiveaway()
{
  Giveaway cg; GiveawayData.GetValue("Current", cg);
  return cg;
}

void SetCurrentGiveaway(const Giveaway g)
{
  GiveawayData.SetValue("Current", g);
}

bool Giveaway_Start(const Giveaway g,
                    const ArrayList opts,
                    const int client,
                    const bool restarting=false)
{
  Giveaway cg = GetCurrentGiveaway();
  char gname_current[32];
  GetGiveawayName(cg, gname_current, sizeof(gname_current));
  if (cg != GT_Invalid)
  {
    ReplyToCommand(client, "%s %t", PLUGIN_TAG, "MG_Giveaway_In_Progress", gname_current);
    return false;
  }

  char gname[32], opts_str[512];
  GetGiveawayName(g, gname, sizeof(gname));
  GetGiveawayOpts(opts_str, sizeof(opts_str));
  if (g == GT_Invalid)
  {
    ReplyToCommand(client, "%s %t", PLUGIN_TAG, "MG_Type_Invalid", gname);
    return false;
  }
  else if (!IsGiveawayEnabled(g) && !restarting)
  {
    ReplyToCommand(client, "%s %t", PLUGIN_TAG, "MG_Type_Disabled", gname);
    return false;
  }
  else if (g == GT_Kill) // XXX: Temporary
  {
    ReplyToCommand(client, "%s Not yet implemented!", PLUGIN_TAG);
    return false;
  }

  SetCurrentGiveaway(g);
  GiveawayData.SetValue("Participants", 0);
  GiveawayData.SetValue("Starter", client);

  if (!restarting)
  {
    ShowActivity2(client, PLUGIN_TAG, " %t", "MG_Start", gname);
    LogAction(client, -1, "%L started Giveaway \"%s\" with options: %s", client, gname, opts_str);
  }
  switch (g)
  {
    case GT_Dice:
    {
      // TODO: Allow changing of "roll" command
      PrintToChatAll("%s %t", PLUGIN_TAG, "MG_Dice_Start", "rolldice");
      if (cv_dice_rerolls.IntValue)
        PrintToChatAll("%s %t", PLUGIN_TAG, "MG_Dice_ReRoll", "reroll");
    }
    case GT_Number:
    {
      int rand = GetRandomInt(cv_number_min.IntValue, cv_number_max.IntValue);
      GiveawayData.SetValue("Number_Target", rand);
      PrintToChatAll("%s %t", PLUGIN_TAG, "MG_Number_Start", "guess", cv_number_min.IntValue, cv_number_max.IntValue);
    }
    case GT_Kill:
    {
    }
  }
  return true;
}

void Giveaway_Stop(const int client, const bool restarting=false)
{
  Giveaway cg = GetCurrentGiveaway();
  if (cg == GT_Invalid)
  {
    ReplyToCommand(client, "%s %t", PLUGIN_TAG, "MG_No_Giveaway");
    return;
  }

  char gname[32];
  GetGiveawayName(cg, gname, sizeof(gname));
  if (!restarting)
  {
    ShowActivity2(client, PLUGIN_TAG, " %t", "MG_Stop", gname);
    LogAction(client, -1, "%L stopped Giveaway \"%s\"", client, gname);
  }

  int winner; GiveawayData.GetValue("Winner", winner);
  switch (cg)
  {
    case GT_Dice:
    {
      if (!restarting)
      {
        if (Dice_PlayerRolls.Length)
        {
          int bestroll = Dice_GetBestRoll();
          ArrayList tied = new ArrayList(1);
          int rigged; GiveawayData.GetValue("Winner", rigged);
          char winner_name[MAX_NAME_LENGTH];

          /* Determine whether more than 1 player has the best roll */
          for (int i = 1; i < Dice_PlayerRolls.Length; ++i)
            if (Dice_PlayerRolls.Get(i) == bestroll) tied.Push(i);

          if (tied.Length > 1)
          {
            PrintToChatAll("%s %t", PLUGIN_TAG, "MG_Giveaway_Tie");
            winner = tied.Get(GetRandomInt(0, tied.Length - 1));
          }
          else winner = tied.Get(0);

          /* Rig the Giveaway */
          if (rigged != -1) winner = rigged;

          GetClientName(winner, winner_name, sizeof(winner_name));
          PrintToChatAll("%s %t", PLUGIN_TAG,
                         bestroll == cv_dice_max.IntValue ?
                         "MG_Dice_Win_Perfect" : "MG_Dice_Win", winner_name, bestroll);
        }
        else
          PrintToChatAll("%s %t", PLUGIN_TAG, "MG_Giveaway_Cancelled", gname);
      }
      ArraySetAll(Dice_PlayerRolls, 0);
      ArraySetAll(Dice_ReRolls, cv_dice_rerolls.IntValue);
    }

    case GT_Number:
    {
      if (!restarting)
      {
        if (Number_PlayerGuesses.Length)
        {
          int target; GiveawayData.GetValue("Number_Target", target);
          char winner_name[MAX_NAME_LENGTH];

          if (winner == -1)
          {
            /* Nobody guessed bang on, so find the closest */
            int closest = Number_GetClosestGuess();
            ArrayList tied = new ArrayList(1);

            /* Determine whether more than 1 player has the closest guess */
            for (int i = 1; i < Number_PlayerGuesses.Length; ++i)
              if (Number_PlayerGuesses.Get(i) == closest) tied.Push(i);

            if (tied.Length > 1)
            {
              PrintToChatAll("%s %t", PLUGIN_TAG, "MG_Giveaway_Tie");
              winner = tied.Get(GetRandomInt(0, tied.Length - 1));
            }
            else winner = tied.Get(0);
            GetClientName(winner, winner_name, sizeof(winner_name));
            PrintToChatAll("%s %t", PLUGIN_TAG, "MG_Number_Win_Close", winner_name, closest, target);
          }
          else
          {
            GetClientName(winner, winner_name, sizeof(winner_name));
            PrintToChatAll("%s %t", PLUGIN_TAG, "MG_Number_Win", winner_name, target);
          }
        }
        else
          PrintToChatAll("%s %t", PLUGIN_TAG, "MG_Giveaway_Cancelled", gname);
      }
      ArraySetAll(Number_PlayerGuesses, 0);
    }

    case GT_Kill:
    {
    }
  }
  SetCurrentGiveaway(GT_Invalid);
  GiveawayData.SetValue("Participants", -1);
  GiveawayData.SetValue("Starter", -1);
  GiveawayData.SetValue("Winner", -1);
  Current_GiveawayOpts.Clear();
}

void Giveaway_Restart(const int client)
{
  Giveaway cg = GetCurrentGiveaway();
  if (cg == GT_Invalid)
  {
    ReplyToCommand(client, "%s %t", PLUGIN_TAG, "MG_No_Giveaway");
    return;
  }

  char gname[32], opts[512];
  GetGiveawayName(cg, gname, sizeof(gname));
  GetGiveawayOpts(opts, sizeof(opts));

  ShowActivity2(client, PLUGIN_TAG, " %t", "MG_Restart", gname);
  LogAction(client, -1, "%L restarted Giveaway \"%s\" with options: %s", client, gname, opts);
  Giveaway_Stop(client, true);
  Giveaway_Start(cg, Current_GiveawayOpts, client, true);
}

void Giveaway_Status(const int client)
{
  Giveaway cg = GetCurrentGiveaway();
  if (cg == GT_Invalid)
  {
    ReplyToCommand(client, "%s %t", PLUGIN_TAG, "MG_No_Giveaway");
    return;
  }

  char gname[32], opts[512];
  GetGiveawayName(cg, gname, sizeof(gname));
  GetGiveawayOpts(opts, sizeof(opts));

  ReplyToCommand(client, "%s %t", PLUGIN_TAG, "MG_Status", gname, opts);
  // do other things
}

void Dice_Roll(const int client, const bool rerolling=false)
{
  if (!rerolling && Dice_PlayerRolls.Get(client))
  {
    ReplyToCommand(client, "%s %t", PLUGIN_TAG, "MG_Dice_Rolled");
    if (cv_dice_rerolls.IntValue)
    {
      if (Dice_ReRolls.Get(client))
        ReplyToCommand(client, "%s %t", PLUGIN_TAG, "MG_Dice_ReRoll", "reroll");
      else
        ReplyToCommand(client, "%s %t", PLUGIN_TAG, "MG_Dice_ReRoll_None");
    }
    return;
  }

  int rand = GetRandomInt(cv_dice_min.IntValue, cv_dice_max.IntValue);
  int p; GiveawayData.GetValue("Participants", p);
  int rigged; GiveawayData.GetValue("Winner", rigged);

  if (!rerolling) GiveawayData.SetValue("Participants", ++p);

  // Make the rig look convincing
  if (rigged != -1 && client == rigged) rand = cv_dice_max.IntValue;

  Dice_PlayerRolls.Set(client, rand);

  char name[MAX_NAME_LENGTH];
  GetClientName(client, name, sizeof(name));
  switch (cv_dice_show_rolls.IntValue)
  {
    case 0:
    {
      ReplyToCommand(client, "%s %t", PLUGIN_TAG, "MG_Dice_Result", "You", rand);
    }
    case 1:
    {
      SendToAdmins("%s %t", PLUGIN_TAG, "MG_Dice_Result", name, rand);
      ReplyToCommand(client, "%s %t", PLUGIN_TAG, "MG_Dice_Result", name, rand);
    }
    case 2:
    {
      PrintToChatAll("%s %t", PLUGIN_TAG, "MG_Dice_Result", name, rand);
    }
  }
}

int Dice_GetBestRoll()
{
  int best = -1;
  for (int i = 1; i < Dice_PlayerRolls.Length; ++i)
  {
    int roll = Dice_PlayerRolls.Get(i);
    if (roll > best) best = roll;
  }
  return best;
}

int Number_GetClosestGuess()
{
  int closest = 0;
  int target; GiveawayData.GetValue("Number_Target", target);
  PrintToServer("target = %d", target);
  for (int i = 1; i < Number_PlayerGuesses.Length; ++i)
  {
    int guess = Number_PlayerGuesses.Get(i);
    if (guess == 0) continue; // No guess for this client
    int diff = abs(target - guess);
    int cdiff = abs(target - closest);
    if (closest == 0 || (diff < cdiff)) closest = guess;
  }
  return closest;
}

bool Number_IsGuessUnique(const int client, const int guess)
{
  for (int i = 1; i < Number_PlayerGuesses.Length; ++i)
    if (i != client && guess == Number_PlayerGuesses.Get(i)) return false;
  return true;
}

/***************************************************************\
 * SourceMod Callbacks
\***************************************************************/

public void OnPluginStart()
{
  // NOTE:
  // load our config. make use of the Kv*/KeyValue functions
  // for flag cvars, ReadFlagString(), GetUserFlagBits() and & are our friends
  // register event forwards (CreateGlobalForward())? ie kill-objectives

  RegisterCommands();
  RegisterConVars();

  /* Set up ConVar hooks */
  cv_dice_rerolls.AddChangeHook(OnConVarChange);

  char translatefile[64];
  Format(translatefile, sizeof(translatefile), "%s.phrases", PLUGIN_NAME);
  LoadTranslations(translatefile);
  LoadTranslations("common.phrases");
}

public void OnMapStart()
{
  /* Initialize global state and data structures */
  GiveawayData         = new StringMap();
  Current_GiveawayOpts     = new ArrayList(MAX_OPTION_LENGTH);
  Dice_PlayerRolls     = new ArrayList(1, MAXPLAYERS);
  Dice_ReRolls         = new ArrayList(1, MAXPLAYERS);
  Number_PlayerGuesses = new ArrayList(1, MAXPLAYERS);

  SetCurrentGiveaway(GT_Invalid);
  GiveawayData.SetValue("Participants", -1);
  GiveawayData.SetValue("Starter", -1);
  GiveawayData.SetValue("Winner", -1);

  // Dice
  ArraySetAll(Dice_PlayerRolls, 0);
  ArraySetAll(Dice_ReRolls, cv_dice_rerolls.IntValue);

  // Number Guess
  GiveawayData.SetValue("Number_Target", -1);
  ArraySetAll(Number_PlayerGuesses, 0);
}

public void OnClientDisconnect(int client)
{
  int p; GiveawayData.GetValue("Participants", p);
  int s; GiveawayData.GetValue("Starter", s);
  int w; GiveawayData.GetValue("Winner", w);
  GiveawayData.SetValue("Participants", p-1);
  if (s == client) GiveawayData.SetValue("Starter", -1);
  if (w == client) GiveawayData.SetValue("Winner", -1);

  // Dice
  Dice_PlayerRolls.Set(client, 0);

  // Number Guess
  Number_PlayerGuesses.Set(client, 0);
}

public void OnConVarChange(ConVar convar,
                           const char[] oldValue,
                           const char[] newValue)
{
  int iOld = StringToInt(oldValue);
  int iNew = StringToInt(newValue);

  if (convar == cv_dice_rerolls && iOld != iNew)
  {
    /* Change should be retroactive. Clients will get the difference added on
     * to what they have, but must be [0, new] */
    for (int i = 1; i < Dice_ReRolls.Length; ++i)
    {
      int rerolls = Dice_ReRolls.Get(i);
      int new_rerolls = max(min((iNew-iOld) + rerolls, iNew), 0);
      Dice_ReRolls.Set(i, new_rerolls);

      /* Notify players if there is a Dice Giveaway in progress */
      if ((GetCurrentGiveaway() == GT_Dice) && IsClientConnected(i))
        PrintToChat(i, "%s %t", PLUGIN_TAG, "MG_Dice_ReRoll_Changed", new_rerolls);
    }
  }
}

/***************************************************************\
 * Command Callbacks
\***************************************************************/

public Action Command_ReloadConfig(int client, int args)
{
  bool success = LoadConfig();
  LogAction(client, -1, "%s configuration reload by %L", PLUGIN_NAME, client);
  if (success)
    ShowActivity2(client, PLUGIN_TAG, " %t", "MG_Config_Reloaded");
  else // TODO: Provide error information, etc
    ShowActivity2(client, PLUGIN_TAG, " %t", "MG_Config_Error");

  return Plugin_Handled;
}

public Action Command_MultiGiveaway(int client, int args)
{
  // sm_mg [start|stop|restart|status] <dice|number|kill|...> [params]...

  if (!args)
  {
    // Interactive Mode: Menu-based interface
    ReplyToCommand(client, "%s Interactive mode not yet implemented!", PLUGIN_TAG);
    return Plugin_Handled;
  }

  char action[32], name[32];
  GetCmdArg(1, action, sizeof(action));
  GetCmdArg(2, name, sizeof(name));
  Giveaway g = GetGiveaway(name);

  if (StrEqual(action, "start", false))
  {
    if (args < 2)
    {
      /* No Giveaway specified, inform client of valid Giveaways */
      char gnames[32*nGiveaways];
      for (int i = 0; i < nGiveaways; ++i)
      {
        char gname[32];
        GetGiveawayName(view_as<Giveaway>(1<<i), gname, sizeof(gname));
        if (i != 0) StrCat(gnames, sizeof(gnames), ", ");
        StrCat(gnames, sizeof(gnames), gname);
      }
      ReplyToCommand(client, "%s %t", PLUGIN_TAG, "MG_Type_Needed", gnames);
      return Plugin_Handled;
    }

    for (int i = 3; i <= GetCmdArgs(); ++i)
    {
      char opt[MAX_OPTION_LENGTH];
      GetCmdArg(i, opt, sizeof(opt));
      Current_GiveawayOpts.PushString(opt);
    }
    Giveaway_Start(g, Current_GiveawayOpts, client);
  }
  else if (StrEqual(action, "stop", false))
    Giveaway_Stop(client);
  else if (StrEqual(action, "restart", false))
    Giveaway_Restart(client);
  else if (StrEqual(action, "status", false))
    Giveaway_Status(client);
  else
    ReplyToCommand(client, "%s %t", PLUGIN_TAG, "MG_Action_Invalid", action, "start, stop, restart, status");

  return Plugin_Handled;
}

public Action Command_Dice_Roll(int client, int args)
{
  if (!CanParticipate(client)) return Plugin_Handled;

  Giveaway cg = GetCurrentGiveaway();
  if (cg != GT_Dice)
  {
    char gname[32];
    GetGiveawayName(GT_Dice, gname, sizeof(gname));
    ReplyToCommand(client, "%s %t", PLUGIN_TAG, "MG_No_Giveaway_Type", gname);
    return Plugin_Handled;
  }

  Dice_Roll(client);

  return Plugin_Handled;
}

public Action Command_Dice_ReRoll(int client, int args)
{
  if (!CanParticipate(client)) return Plugin_Handled;

  Giveaway cg = GetCurrentGiveaway();
  if (cg != GT_Dice)
  {
    char gname[32];
    GetGiveawayName(GT_Dice, gname, sizeof(gname));
    ReplyToCommand(client, "%s %t", PLUGIN_TAG, "MG_No_Giveaway_Type", gname);
    return Plugin_Handled;
  }

  int rerolls = cv_dice_rerolls.IntValue;
  if (!rerolls)
  {
    ReplyToCommand(client, "%s %t", PLUGIN_TAG, "MG_Dice_ReRoll_Disabled");
    return Plugin_Handled;
  }
  if (!Dice_PlayerRolls.Get(client))
  {
    ReplyToCommand(client, "%s %t", PLUGIN_TAG, "MG_Dice_ReRoll_Roll_Needed");
    return Plugin_Handled;
  }

  int rolls_left = Dice_ReRolls.Get(client);
  if (rolls_left)
  {
    int last_roll = Dice_PlayerRolls.Get(client);
    Dice_Roll(client, true);
    ReplyToCommand(client, "%s %t", PLUGIN_TAG, "MG_Dice_ReRolled", last_roll, --rolls_left);
    Dice_ReRolls.Set(client, rolls_left);
  }
  else
    ReplyToCommand(client, "%s %t", PLUGIN_TAG, "MG_Dice_ReRoll_None");

  return Plugin_Handled;
}

public Action Command_Dice_Rig(int client, int args)
{
  Giveaway cg = GetCurrentGiveaway();
  if (cg != GT_Dice)
  {
    char name[32];
    GetGiveawayName(GT_Dice, name, sizeof(name));
    ReplyToCommand(client, "%s %t", PLUGIN_TAG, "MG_No_Giveaway_Type", name);
    return Plugin_Handled;
  }
  if (args < 1)
  {
    ReplyToCommand(client, "%s %t", PLUGIN_TAG, "No matching client");
    return Plugin_Handled;
  }

  char arg[MAX_NAME_LENGTH], target_name[MAX_NAME_LENGTH];
  int target[1];
  bool tnml;
  GetCmdArg(1, arg, sizeof(arg));
  int rv = ProcessTargetString(arg, client, target, 1, COMMAND_FILTER_CONNECTED,
                               target_name, sizeof(target_name), tnml);
  if (rv)
  {
    ShowActivity2(client, PLUGIN_TAG, " %t", "MG_Dice_Rigged", target_name);
    GiveawayData.SetValue("Winner", target[0]);
  }
  else ReplyToTargetError(client, rv);

  return Plugin_Handled;
}

public Action Command_Number_Guess(int client, int args)
{
  if (!CanParticipate(client)) return Plugin_Handled;

  Giveaway cg = GetCurrentGiveaway();
  if (cg != GT_Number)
  {
    char gname[32];
    GetGiveawayName(GT_Dice, gname, sizeof(gname));
    ReplyToCommand(client, "%s %t", PLUGIN_TAG, "MG_No_Giveaway_Type", gname);
    return Plugin_Handled;
  }
  if (args < 1)
  {
    ReplyToCommand(client, "%s Usage: sm_guess <number>", PLUGIN_TAG);
    return Plugin_Handled;
  }
  else if (Number_PlayerGuesses.Get(client))
  {
    ReplyToCommand(client, "%s %t", PLUGIN_TAG, "MG_Number_Guessed");
    return Plugin_Handled;
  }

  int guess;
  char arg[8];
  GetCmdArg(1, arg, sizeof(arg));

  if (!StringToIntEx(arg, guess))
  {
    ReplyToCommand(client, "%s Usage: sm_guess <number>", PLUGIN_TAG);
    return Plugin_Handled;
  }
  else if (guess >= cv_number_min.IntValue && guess <= cv_number_max.IntValue)
  {
    int p; GiveawayData.GetValue("Participants", p);
    int target; GiveawayData.GetValue("Number_Target", target);

    GiveawayData.SetValue("Participants", ++p);
    Number_PlayerGuesses.Set(client, guess);
    if (guess == target)
    {
      GiveawayData.SetValue("Winner", client);
      Giveaway_Stop(0);
    }
    else if (!Number_IsGuessUnique(client, guess))
    {
      ReplyToCommand(client, "%s %t", PLUGIN_TAG, "MG_Number_Not_Unique");
      return Plugin_Handled;
    }
    else
    {
      char player[MAX_NAME_LENGTH];
      GetClientName(client, player, sizeof(player));
      switch (cv_number_show_guesses.IntValue)
      {
        case 0:
        {
          ReplyToCommand(client, "%s %t", PLUGIN_TAG, "MG_Number_Guess", "You", guess);
        }
        case 1:
        {
          SendToAdmins("%s %T", PLUGIN_TAG, "MG_Number_Guess", player, guess);
          ReplyToCommand(client, "%s %t", PLUGIN_TAG, "MG_Number_Guess", player, guess);
        }
        case 2:
        {
          PrintToChatAll("%s %t", PLUGIN_TAG, "MG_Number_Guess", player, guess);
        }
      }
    }
  }
  else
    ReplyToCommand(client, "%s %t", PLUGIN_TAG, "MG_Number_Bad_Range", cv_number_min.IntValue, cv_number_max.IntValue);

  return Plugin_Handled;
}

// vim: et sts=2 sw=2

/*
 * Copyright © 2016 Alex "ZeroKnight" George
 *
 * This file is licensed under the MIT License. For details, please see the
 * LICENSE file that should be included with the source code, or
 * <https://opensource.org/licenses/MIT>
 */

#include <sourcemod>

#pragma semicolon 1
#pragma newdecls required

char PLUGIN_NAME[]    = "Multi-Giveaway";
char PLUGIN_AUTHOR[]  = "Alex \"ZeroKnight\" George";
char PLUGIN_DESC[]    = "Expansive Giveaway system with numerous types of Giveaway events and stats";
char PLUGIN_VERSION[] = "0.2.0";
char PLUGIN_URL[]     = "http:/github.com/ZeroKnight/sm-Multi-Giveaway";
char PLUGIN_TAG[]     = "[MG]";

public Plugin myinfo =
{
  name        = PLUGIN_NAME,
  author      = PLUGIN_AUTHOR,
  description = PLUGIN_DESC,
  version     = PLUGIN_VERSION,
  url         = PLUGIN_URL
};

const int MG_MAX_MESSAGE_LENGTH = 256;
const int TYPEOPT_MAX_LEN = 64;

/* NOTE: (<<= 1) is actually a neat thing that SourcePawn does. It lets you
 * specify how to 'increment' each enumeration. In this case, each one will be
 * the previous value left shifted 1, which gives us our bit-field.
 */
enum GiveawayType (<<= 1)
{
  GT_Invalid = -1, GT_Dice = 1, GT_Number, GT_Kill, GT_All = 7
};
const int nGTypes = 3;

/* Global state variables/data structures */
StringMap GiveawayData;
ArrayList Dice_PlayerRolls;
ArrayList Dice_ReRolls;
ArrayList Number_PlayerGuesses;

ArrayList Current_TypeOpts; // NOTE: Subject to change

//File configfile = ...

// TODO: Put things like this in a personal core library?
#define CVAR(%1) ConVar cv_%1
char CVAR_PREFIX[] = "multi_giveaway";
CVAR(version);
CVAR(flags);
CVAR(enabled_types);
CVAR(player_state);
CVAR(min_conn_time);
CVAR(dice_min);
CVAR(dice_max);
CVAR(dice_show_rolls);
CVAR(dice_rerolls);
CVAR(number_min);
CVAR(number_max);
CVAR(number_show_guesses);

int abs(const int i) { return (i > 0) ? i : -i; }
int min(const int i, const int k) { return (i < k) ? i : k; }
int max(const int i, const int k) { return (i > k) ? i : k; }
int clamp(const int i, const int minval, const int maxval)
{
  return max(minval, min(i, maxval));
}

void RegisterConVars()
{
  // XXX: Function overloads would be nice. Or casting that worked on strings.
  char sGT_All[32]; IntToString(view_as<int>(GT_All), sGT_All, sizeof(sGT_All));

  /* Core */
  cv_version = CreateConVar(
    "multi_giveaway_version",
    PLUGIN_VERSION,
    "Plugin version",
    FCVAR_SPONLY|FCVAR_REPLICATED|FCVAR_NOTIFY|FCVAR_DONTRECORD);
  cv_flags = CreateConVar(
    "multi_giveaway_flags",
    "b",
    "List of admin flags allowed to manage giveaways");
  cv_enabled_types = CreateConVar(
    "multi_giveaway_enabled_types",
    sGT_All,
    "Bit-field (sum of options) of enabled Giveaway types",
    FCVAR_NONE,
    true, 1.0,
    true, float(GT_All));
  cv_player_state = CreateConVar(
    "multi_giveaway_player_state",
    "2",
    "Player states eligible for Giveaway participation; each setting includes the previous one: 0 - Alive only, 1 - Dead, 2 - Spectator",
    FCVAR_NONE,
    true, 0.0,
    true, 2.0);
  cv_min_conn_time = CreateConVar( // TODO: Allow certain giveaways to override (eg. kill objective)
    "multi_giveaway_min_conn_time",
    "300", // 5 minutes
    "Amount of time in seconds a player must be connected to participate in a Giveaway",
    FCVAR_NONE,
    true, 0.0);

  /* Dice */
  cv_dice_min = CreateConVar(
    "multi_giveaway_dice_min",
    "1",
    "Lowest possible number a player may roll",
    FCVAR_NONE,
    true, 1.0);
  cv_dice_max = CreateConVar(
    "multi_giveaway_dice_max",
    "50",
    "Highest possible number a player may roll");
  cv_dice_show_rolls = CreateConVar(
    "multi_giveaway_dice_show_rolls",
    "0",
    "Who can see other clients' dice rolls: 0 - Nobody, 1 - Admins, 2 - Everyone",
    FCVAR_NONE,
    true, 0.0,
    true, 2.0);
  cv_dice_rerolls = CreateConVar(
    "multi_giveaway_dice_rerolls",
    "0",
    "Number of 'do-overs' a player gets when rolling. Only the last is counted, for better or for worse",
    FCVAR_NONE,
    true, 0.0);

  /* Number Guess */
  cv_number_min = CreateConVar(
    "multi_giveaway_number_min",
    "1",
    "Lower bound of the range of numbers",
    FCVAR_NONE,
    true, 1.0);
  cv_number_max = CreateConVar(
    "multi_giveaway_number_max",
    "100",
    "Upper bound of the range of numbers");
  cv_number_show_guesses = CreateConVar(
    "multi_giveaway_number_show_guesses",
    "2",
    "Who can see other clients' guesses: 0 - Nobody, 1 - Admins, 2 - Everyone",
    FCVAR_NONE,
    true, 0.0,
    true, 2.0);
}

void RegisterCommands()
{
  // XXX: Is there a better way to create aliases?
  // RegConsoleCmd for client commands (ie participation)
  // RegAdminCmd for admin commands (ie (start|stop)ing giveaways methodmap may
  // be helpful
  //
  // We can use AddCommandListener() to allow for "commands" that aren't
  // actually registered commands (think 'nominate')

  RegAdminCmd("sm_mg_reload", Command_ReloadConfig, ADMFLAG_CONFIG,
    "Reload configuration file");
  RegAdminCmd("sm_multigiveaway", Command_MultiGiveaway, ADMFLAG_GENERIC,
    "Manage giveaways");
  RegAdminCmd("sm_mg", Command_MultiGiveaway, ADMFLAG_GENERIC,
    "Manage giveaways");
  RegAdminCmd("sm_dice_rig", Command_Dice_Rig, ADMFLAG_CHEATS,
              "Rigs the Dice Giveaway so the chosen client wins");

  RegConsoleCmd("sm_rolldice", Command_Dice_Roll,
                "Rolls the dice during a Dice Giveaway");
  RegConsoleCmd("sm_reroll", Command_Dice_ReRoll,
                "Rolls the dice again during a Giveaway, giving you a new result (for better or worse!");
  RegConsoleCmd("sm_guess", Command_Number_Guess,
                "Guess a number for the Number-Guess Giveaway");
}

bool LoadConfig()
{
  //if (configfile != null) // is this boilerplate necessary?
    //CloseHandle(configfile);
  // ...
  return true;
}

bool CanParticipate(const int client)
{
  bool spectating = GetClientTeam(client) == 1;

  /* Check player state */
  if (!IsPlayerAlive(client))
  {
    char phrase[32];
    switch (cv_player_state.IntValue)
    {
      case 0:
      {
        strcopy(phrase, sizeof(phrase),
                spectating ? "MG_Ineligible_Spec" : "MG_Ineligible_Alive");
      }
      case 1:
      {
        if (spectating)
          strcopy(phrase, sizeof(phrase), "MG_Ineligible_Spec");
      }
    }
    if (strlen(phrase))
    {
      ReplyToCommand(client, "%s %t", PLUGIN_TAG, phrase);
      return false;
    }
  }

  /* Check connection time */
  float time = GetClientTime(client);
  int delta = cv_min_conn_time.IntValue - RoundToCeil(time);
  if (time < cv_min_conn_time.FloatValue)
  {
    ReplyToCommand(client, "%s %t", PLUGIN_TAG, "MG_Not_Connected_Long_Enough",
                   delta);
    return false;
  }
  return true;
}

void GetTypeOpts(char[] str, const int sz)
{
  char opt[64];
  for (int i = 0; i < Current_TypeOpts.Length; ++i)
  {
    Current_TypeOpts.GetString(i, opt, sizeof(opt));
    StrCat(str, sz, opt);
    StrCat(str, sz, "; ");
  }
}

void ArraySetAll(ArrayList& array, any value)
{
  for (int i = 0; i < array.Length; ++i)
    array.Set(i, value);
}

void SendToAdmins(const char[] format, any ...)
{
  char buffer[MG_MAX_MESSAGE_LENGTH];
  for (int i = 1; i <= MaxClients; ++i)
  {
    if (IsClientInGame(i) && GetUserAdmin(i) != INVALID_ADMIN_ID)
    {
      SetGlobalTransTarget(i);
      VFormat(buffer, sizeof(buffer), format, 2);
      PrintToChat(i, "%s", buffer);
    }
  }
}

/***************************************************************\
 * Giveaway Functions
\***************************************************************/

void GetTypeName(const GiveawayType type, char[] str, const int sz)
{
  switch (type)
  {
    case GT_Invalid: return;
    case GT_Dice:    strcopy(str, sz, "Dice");
    case GT_Number:  strcopy(str, sz, "Number");
    case GT_Kill:    strcopy(str, sz, "Kill");
    default:         return;
  }
}

GiveawayType GetType(const char[] typename)
{
  if      (StrEqual(typename, "dice", false))   return GT_Dice;
  else if (StrEqual(typename, "number", false)) return GT_Number;
  else if (StrEqual(typename, "kill", false))   return GT_Kill;
  else return GT_Invalid;
}

bool IsTypeEnabled(const GiveawayType type)
{
  return view_as<GiveawayType>(cv_enabled_types.IntValue) & type ? true : false;
}

GiveawayType GetCurrentGiveaway()
{
  GiveawayType cg; GiveawayData.GetValue("Current", cg);
  return cg;
}

void SetCurrentGiveaway(const GiveawayType type)
{
  GiveawayData.SetValue("Current", type);
}

bool Giveaway_Start(const GiveawayType type,
                    const ArrayList opts,
                    const int client,
                    const bool restarting=false)
{
  GiveawayType cg = GetCurrentGiveaway();
  char typename_current[32];
  GetTypeName(cg, typename_current, sizeof(typename_current));
  if (cg != GT_Invalid)
  {
    ReplyToCommand(client, "%s %t", PLUGIN_TAG, "MG_Giveaway_In_Progress", typename_current);
    return false;
  }

  char typename[32], typeopts[512];
  GetTypeName(type, typename, sizeof(typename));
  GetTypeOpts(typeopts, sizeof(typeopts));
  if (type == GT_Invalid)
  {
    ReplyToCommand(client, "%s %t", PLUGIN_TAG, "MG_Type_Invalid", typename);
    return false;
  }
  else if (!IsTypeEnabled(type) && !restarting)
  {
    ReplyToCommand(client, "%s %t", PLUGIN_TAG, "MG_Type_Disabled", typename);
    return false;
  }
  else if (type == GT_Kill) // XXX: Temporary
  {
    ReplyToCommand(client, "%s Not yet implemented!", PLUGIN_TAG);
    return false;
  }

  SetCurrentGiveaway(type);
  GiveawayData.SetValue("Participants", 0);
  GiveawayData.SetValue("Starter", client);

  if (!restarting)
  {
    ShowActivity2(client, PLUGIN_TAG, " %t", "MG_Start", typename);
    LogAction(client, -1, "%L started Giveaway \"%s\" with options: %s", client, typename, typeopts);
  }
  switch (type)
  {
    case GT_Dice:
    {
      // TODO: Allow changing of "roll" command
      PrintToChatAll("%s %t", PLUGIN_TAG, "MG_Dice_Start", "rolldice");
      if (cv_dice_rerolls.IntValue)
        PrintToChatAll("%s %t", PLUGIN_TAG, "MG_Dice_ReRoll", "reroll");
    }
    case GT_Number:
    {
      int rand = GetRandomInt(cv_number_min.IntValue, cv_number_max.IntValue);
      GiveawayData.SetValue("Number_Target", rand);
      PrintToChatAll("%s %t", PLUGIN_TAG, "MG_Number_Start", "guess", cv_number_min.IntValue, cv_number_max.IntValue);
    }
    case GT_Kill:
    {
    }
  }
  return true;
}

void Giveaway_Stop(const int client, const bool restarting=false)
{
  GiveawayType cg = GetCurrentGiveaway();
  if (cg == GT_Invalid)
  {
    ReplyToCommand(client, "%s %t", PLUGIN_TAG, "MG_No_Giveaway");
    return;
  }

  char typename[32];
  GetTypeName(cg, typename, sizeof(typename));
  if (!restarting)
  {
    ShowActivity2(client, PLUGIN_TAG, " %t", "MG_Stop", typename);
    LogAction(client, -1, "%L stopped Giveaway \"%s\"", client, typename);
  }

  int winner; GiveawayData.GetValue("Winner", winner);
  switch (cg)
  {
    case GT_Dice:
    {
      if (!restarting)
      {
        if (Dice_PlayerRolls.Length)
        {
          int bestroll = Dice_GetBestRoll();
          ArrayList tied = new ArrayList(1);
          int rigged; GiveawayData.GetValue("Winner", rigged);
          char name[MAX_NAME_LENGTH];

          /* Determine whether more than 1 player has the best roll */
          for (int i = 1; i < Dice_PlayerRolls.Length; ++i)
            if (Dice_PlayerRolls.Get(i) == bestroll) tied.Push(i);

          if (tied.Length > 1)
          {
            PrintToChatAll("%s %t", PLUGIN_TAG, "MG_Giveaway_Tie");
            winner = tied.Get(GetRandomInt(0, tied.Length - 1));
          }
          else winner = tied.Get(0);

          /* Rig the Giveaway */
          if (rigged != -1) winner = rigged;

          GetClientName(winner, name, sizeof(name));
          PrintToChatAll("%s %t", PLUGIN_TAG,
                         bestroll == cv_dice_max.IntValue ?
                         "MG_Dice_Win_Perfect" : "MG_Dice_Win", name, bestroll);
        }
        else
          PrintToChatAll("%s %t", PLUGIN_TAG, "MG_Giveaway_Cancelled", typename);
      }
      ArraySetAll(Dice_PlayerRolls, 0);
      ArraySetAll(Dice_ReRolls, cv_dice_rerolls.IntValue);
    }

    case GT_Number:
    {
      if (!restarting)
      {
        if (Number_PlayerGuesses.Length)
        {
          int target; GiveawayData.GetValue("Number_Target", target);
          char name[MAX_NAME_LENGTH];

          if (winner == -1)
          {
            /* Nobody guessed bang on, so find the closest */
            int closest = Number_GetClosestGuess();
            ArrayList tied = new ArrayList(1);

            /* Determine whether more than 1 player has the closest guess */
            for (int i = 1; i < Number_PlayerGuesses.Length; ++i)
              if (Number_PlayerGuesses.Get(i) == closest) tied.Push(i);

            if (tied.Length > 1)
            {
              PrintToChatAll("%s %t", PLUGIN_TAG, "MG_Giveaway_Tie");
              winner = tied.Get(GetRandomInt(0, tied.Length - 1));
            }
            else winner = tied.Get(0);
            GetClientName(winner, name, sizeof(name));
            PrintToChatAll("%s %t", PLUGIN_TAG, "MG_Number_Win_Close", name, closest, target);
          }
          else
          {
            GetClientName(winner, name, sizeof(name));
            PrintToChatAll("%s %t", PLUGIN_TAG, "MG_Number_Win", name, target);
          }
        }
        else
          PrintToChatAll("%s %t", PLUGIN_TAG, "MG_Giveaway_Cancelled", typename);
      }
      ArraySetAll(Number_PlayerGuesses, 0);
    }

    case GT_Kill:
    {
    }
  }
  SetCurrentGiveaway(GT_Invalid);
  GiveawayData.SetValue("Participants", -1);
  GiveawayData.SetValue("Starter", -1);
  GiveawayData.SetValue("Winner", -1);
  Current_TypeOpts.Clear();
}

void Giveaway_Restart(const int client)
{
  GiveawayType cg = GetCurrentGiveaway();
  if (cg == GT_Invalid)
  {
    ReplyToCommand(client, "%s %t", PLUGIN_TAG, "MG_No_Giveaway");
    return;
  }

  char typename[32], typeopts[512];
  GetTypeName(cg, typename, sizeof(typename));
  GetTypeOpts(typeopts, sizeof(typeopts));

  ShowActivity2(client, PLUGIN_TAG, " %t", "MG_Restart", typename);
  LogAction(client, -1, "%L restarted Giveaway \"%s\" with options: %s", client, typename, typeopts);
  Giveaway_Stop(client, true);
  Giveaway_Start(cg, Current_TypeOpts, client, true);
}

void Giveaway_Status(const int client)
{
  GiveawayType cg = GetCurrentGiveaway();
  if (cg == GT_Invalid)
  {
    ReplyToCommand(client, "%s %t", PLUGIN_TAG, "MG_No_Giveaway");
    return;
  }

  char typename[32], typeopts[512];
  GetTypeName(cg, typename, sizeof(typename));
  GetTypeOpts(typeopts, sizeof(typeopts));

  ReplyToCommand(client, "%s %t", PLUGIN_TAG, "MG_Status", typename, typeopts);
  // do other things
}

void Dice_Roll(const int client, const bool rerolling=false)
{
  if (!rerolling && Dice_PlayerRolls.Get(client))
  {
    ReplyToCommand(client, "%s %t", PLUGIN_TAG, "MG_Dice_Rolled");
    if (cv_dice_rerolls.IntValue)
    {
      if (Dice_ReRolls.Get(client))
        ReplyToCommand(client, "%s %t", PLUGIN_TAG, "MG_Dice_ReRoll", "reroll");
      else
        ReplyToCommand(client, "%s %t", PLUGIN_TAG, "MG_Dice_ReRoll_None");
    }
    return;
  }

  int rand = GetRandomInt(cv_dice_min.IntValue, cv_dice_max.IntValue);
  int p; GiveawayData.GetValue("Participants", p);
  int rigged; GiveawayData.GetValue("Winner", rigged);

  if (!rerolling) GiveawayData.SetValue("Participants", ++p);

  // Make the rig look convincing
  if (rigged != -1 && client == rigged) rand = cv_dice_max.IntValue;

  Dice_PlayerRolls.Set(client, rand);

  char name[MAX_NAME_LENGTH];
  GetClientName(client, name, sizeof(name));
  switch (cv_dice_show_rolls.IntValue)
  {
    case 0:
    {
      ReplyToCommand(client, "%s %t", PLUGIN_TAG, "MG_Dice_Result", "You", rand);
    }
    case 1:
    {
      SendToAdmins("%s %t", PLUGIN_TAG, "MG_Dice_Result", name, rand);
      ReplyToCommand(client, "%s %t", PLUGIN_TAG, "MG_Dice_Result", name, rand);
    }
    case 2:
    {
      PrintToChatAll("%s %t", PLUGIN_TAG, "MG_Dice_Result", name, rand);
    }
  }
}

int Dice_GetBestRoll()
{
  int best = -1;
  for (int i = 1; i < Dice_PlayerRolls.Length; ++i)
  {
    int roll = Dice_PlayerRolls.Get(i);
    if (roll > best) best = roll;
  }
  return best;
}

int Number_GetClosestGuess()
{
  int closest = 0;
  int target; GiveawayData.GetValue("Number_Target", target);
  PrintToServer("target = %d", target);
  for (int i = 1; i < Number_PlayerGuesses.Length; ++i)
  {
    int guess = Number_PlayerGuesses.Get(i);
    if (guess == 0) continue; // No guess for this client
    int diff = abs(target - guess);
    int cdiff = abs(target - closest);
    if (closest == 0 || (diff < cdiff)) closest = guess;
  }
  return closest;
}

bool Number_IsGuessUnique(const int client, const int guess)
{
  for (int i = 1; i < Number_PlayerGuesses.Length; ++i)
    if (i != client && guess == Number_PlayerGuesses.Get(i)) return false;
  return true;
}

/***************************************************************\
 * SourceMod Callbacks
\***************************************************************/

public void OnPluginStart()
{
  // NOTE:
  // load our config. make use of the Kv*/KeyValue functions
  // for flag cvars, ReadFlagString(), GetUserFlagBits() and & are our friends
  // register event forwards (CreateGlobalForward())? ie kill-objectives

  RegisterCommands();
  RegisterConVars();

  /* Set up ConVar hooks */
  cv_dice_rerolls.AddChangeHook(OnConVarChange);

  char translatefile[64];
  Format(translatefile, sizeof(translatefile), "%s.phrases", PLUGIN_NAME);
  LoadTranslations(translatefile);
  LoadTranslations("common.phrases");
}

public void OnMapStart()
{
  /* Initialize global state and data structures */
  GiveawayData         = new StringMap();
  Current_TypeOpts     = new ArrayList(TYPEOPT_MAX_LEN);
  Dice_PlayerRolls     = new ArrayList(1, MAXPLAYERS);
  Dice_ReRolls         = new ArrayList(1, MAXPLAYERS);
  Number_PlayerGuesses = new ArrayList(1, MAXPLAYERS);

  SetCurrentGiveaway(GT_Invalid);
  GiveawayData.SetValue("Participants", -1);
  GiveawayData.SetValue("Starter", -1);
  GiveawayData.SetValue("Winner", -1);

  // Dice
  ArraySetAll(Dice_PlayerRolls, 0);
  ArraySetAll(Dice_ReRolls, cv_dice_rerolls.IntValue);

  // Number Guess
  GiveawayData.SetValue("Number_Target", -1);
  ArraySetAll(Number_PlayerGuesses, 0);
}

public void OnClientDisconnect(int client)
{
  int p; GiveawayData.GetValue("Participants", p);
  int s; GiveawayData.GetValue("Starter", s);
  int w; GiveawayData.GetValue("Winner", w);
  GiveawayData.SetValue("Participants", p-1);
  if (s == client) GiveawayData.SetValue("Starter", -1);
  if (w == client) GiveawayData.SetValue("Winner", -1);

  // Dice
  Dice_PlayerRolls.Set(client, 0);

  // Number Guess
  Number_PlayerGuesses.Set(client, 0);
}

public void OnConVarChange(ConVar convar,
                           const char[] oldValue,
                           const char[] newValue)
{
  int iOld = StringToInt(oldValue);
  int iNew = StringToInt(newValue);

  if (convar == cv_dice_rerolls && iOld != iNew)
  {
    /* Change should be retroactive. Clients will get the difference added on
     * to what they have, but must be [0, new] */
    for (int i = 1; i < Dice_ReRolls.Length; ++i)
    {
      int rerolls = Dice_ReRolls.Get(i);
      int new_rerolls = max(min((iNew-iOld) + rerolls, iNew), 0);
      Dice_ReRolls.Set(i, new_rerolls);

      /* Notify players if there is a Dice Giveaway in progress */
      if ((GetCurrentGiveaway() == GT_Dice) && IsClientConnected(i))
        PrintToChat(i, "%s %t", PLUGIN_TAG, "MG_Dice_ReRoll_Changed", new_rerolls);
    }
  }
}

/***************************************************************\
 * Command Callbacks
\***************************************************************/

public Action Command_ReloadConfig(int client, int args)
{
  bool success = LoadConfig();
  LogAction(client, -1, "%s configuration reload by %L", PLUGIN_NAME, client);
  if (success)
    ShowActivity2(client, PLUGIN_TAG, " %t", "MG_Config_Reloaded");
  else // TODO: Provide error information, etc
    ShowActivity2(client, PLUGIN_TAG, " %t", "MG_Config_Error");

  return Plugin_Handled;
}

public Action Command_MultiGiveaway(int client, int args)
{
  // sm_mg [start|stop|restart|status] <dice|number|kill|...> [params]...

  if (!args)
  {
    // Interactive Mode: Menu-based interface
    ReplyToCommand(client, "%s Interactive mode not yet implemented!", PLUGIN_TAG);
    return Plugin_Handled;
  }

  char action[32], typename[32];
  GetCmdArg(1, action, sizeof(action));
  GetCmdArg(2, typename, sizeof(typename));
  GiveawayType type = GetType(typename);

  if (StrEqual(action, "start", false))
  {
    if (args < 2)
    {
      /* No type specified, inform client of valid types */
      char typenames[32*nGTypes];
      for (int i = 0; i < nGTypes; ++i)
      {
        char name[32];
        GetTypeName(view_as<GiveawayType>(1<<i), name, sizeof(name));
        if (i != 0) StrCat(typenames, sizeof(typenames), ", ");
        StrCat(typenames, sizeof(typenames), name);
      }
      ReplyToCommand(client, "%s %t", PLUGIN_TAG, "MG_Type_Needed", typenames);
      return Plugin_Handled;
    }

    for (int i = 3; i <= GetCmdArgs(); ++i)
    {
      char opt[TYPEOPT_MAX_LEN];
      GetCmdArg(i, opt, sizeof(opt));
      Current_TypeOpts.PushString(opt);
    }
    Giveaway_Start(type, Current_TypeOpts, client);
  }
  else if (StrEqual(action, "stop", false))
    Giveaway_Stop(client);
  else if (StrEqual(action, "restart", false))
    Giveaway_Restart(client);
  else if (StrEqual(action, "status", false))
    Giveaway_Status(client);
  else
    ReplyToCommand(client, "%s %t", PLUGIN_TAG, "MG_Action_Invalid", action, "start, stop, restart, status");

  return Plugin_Handled;
}

public Action Command_Dice_Roll(int client, int args)
{
  if (!CanParticipate(client)) return Plugin_Handled;

  GiveawayType cg = GetCurrentGiveaway();
  if (cg != GT_Dice)
  {
    char typename[32];
    GetTypeName(GT_Dice, typename, sizeof(typename));
    ReplyToCommand(client, "%s %t", PLUGIN_TAG, "MG_No_Giveaway_Type", typename);
    return Plugin_Handled;
  }

  Dice_Roll(client);

  return Plugin_Handled;
}

public Action Command_Dice_ReRoll(int client, int args)
{
  if (!CanParticipate(client)) return Plugin_Handled;

  GiveawayType cg = GetCurrentGiveaway();
  if (cg != GT_Dice)
  {
    char typename[32];
    GetTypeName(GT_Dice, typename, sizeof(typename));
    ReplyToCommand(client, "%s %t", PLUGIN_TAG, "MG_No_Giveaway_Type", typename);
    return Plugin_Handled;
  }

  int rerolls = cv_dice_rerolls.IntValue;
  if (!rerolls)
  {
    ReplyToCommand(client, "%s %t", PLUGIN_TAG, "MG_Dice_ReRoll_Disabled");
    return Plugin_Handled;
  }
  if (!Dice_PlayerRolls.Get(client))
  {
    ReplyToCommand(client, "%s %t", PLUGIN_TAG, "MG_Dice_ReRoll_Roll_Needed");
    return Plugin_Handled;
  }

  int rolls_left = Dice_ReRolls.Get(client);
  if (rolls_left)
  {
    int last_roll = Dice_PlayerRolls.Get(client);
    Dice_Roll(client, true);
    ReplyToCommand(client, "%s %t", PLUGIN_TAG, "MG_Dice_ReRolled", last_roll, --rolls_left);
    Dice_ReRolls.Set(client, rolls_left);
  }
  else
    ReplyToCommand(client, "%s %t", PLUGIN_TAG, "MG_Dice_ReRoll_None");

  return Plugin_Handled;
}

public Action Command_Dice_Rig(int client, int args)
{
  GiveawayType cg = GetCurrentGiveaway();
  if (cg != GT_Dice)
  {
    char typename[32];
    GetTypeName(GT_Dice, typename, sizeof(typename));
    ReplyToCommand(client, "%s %t", PLUGIN_TAG, "MG_No_Giveaway_Type", typename);
    return Plugin_Handled;
  }
  if (args < 1)
  {
    ReplyToCommand(client, "%s %t", PLUGIN_TAG, "No matching client");
    return Plugin_Handled;
  }

  char arg[MAX_NAME_LENGTH], target_name[MAX_NAME_LENGTH];
  int target[1];
  bool tnml;
  GetCmdArg(1, arg, sizeof(arg));
  int rv = ProcessTargetString(arg, client, target, 1, COMMAND_FILTER_CONNECTED,
                               target_name, sizeof(target_name), tnml);
  if (rv)
  {
    ShowActivity2(client, PLUGIN_TAG, " %t", "MG_Dice_Rigged", target_name);
    GiveawayData.SetValue("Winner", target[0]);
  }
  else ReplyToTargetError(client, rv);

  return Plugin_Handled;
}

public Action Command_Number_Guess(int client, int args)
{
  if (!CanParticipate(client)) return Plugin_Handled;

  GiveawayType cg = GetCurrentGiveaway();
  if (cg != GT_Number)
  {
    char typename[32];
    GetTypeName(GT_Dice, typename, sizeof(typename));
    ReplyToCommand(client, "%s %t", PLUGIN_TAG, "MG_No_Giveaway_Type", typename);
    return Plugin_Handled;
  }
  if (args < 1)
  {
    ReplyToCommand(client, "%s Usage: sm_guess <number>", PLUGIN_TAG);
    return Plugin_Handled;
  }
  else if (Number_PlayerGuesses.Get(client))
  {
    ReplyToCommand(client, "%s %t", PLUGIN_TAG, "MG_Number_Guessed");
    return Plugin_Handled;
  }

  int guess;
  char arg[8];
  GetCmdArg(1, arg, sizeof(arg));

  if (!StringToIntEx(arg, guess))
  {
    ReplyToCommand(client, "%s Usage: sm_guess <number>", PLUGIN_TAG);
    return Plugin_Handled;
  }
  else if (guess >= cv_number_min.IntValue && guess <= cv_number_max.IntValue)
  {
    int p; GiveawayData.GetValue("Participants", p);
    int target; GiveawayData.GetValue("Number_Target", target);

    GiveawayData.SetValue("Participants", ++p);
    Number_PlayerGuesses.Set(client, guess);
    if (guess == target)
    {
      GiveawayData.SetValue("Winner", client);
      Giveaway_Stop(0);
    }
    else if (!Number_IsGuessUnique(client, guess))
    {
      ReplyToCommand(client, "%s %t", PLUGIN_TAG, "MG_Number_Not_Unique");
      return Plugin_Handled;
    }
    else
    {
      char name[MAX_NAME_LENGTH];
      GetClientName(client, name, sizeof(name));
      switch (cv_number_show_guesses.IntValue)
      {
        case 0:
        {
          ReplyToCommand(client, "%s %t", PLUGIN_TAG, "MG_Number_Guess", "You", guess);
        }
        case 1:
        {
          char str[128];
          SendToAdmins("%s %T", PLUGIN_TAG, "MG_Number_Guess", name, guess);
          ReplyToCommand(client, "%s %t", PLUGIN_TAG, "MG_Number_Guess", name, guess);
        }
        case 2:
        {
          PrintToChatAll("%s %t", PLUGIN_TAG, "MG_Number_Guess", name, guess);
        }
      }
    }
  }
  else
    ReplyToCommand(client, "%s %t", PLUGIN_TAG, "MG_Number_Bad_Range", cv_number_min.IntValue, cv_number_max.IntValue);

  return Plugin_Handled;
}

// vim: et sts=2 sw=2