/*********************************************************
 * A slap plugin - Version 0.8                           *
 *********************************************************
 *                                                       *
 * Name: plugin_rindy_mslap                              *
 * Author: Rinde (rinde@fiatnox.de)                      *
 * Released: 15/11/02                                    *
 *                                                       *
 *                                                       *
 * Commands:                                             *
 *                                                       *
 * admin_mslap <target> [<#>]: Slaps Target. Optionally  *
 *                             include a number. 0 will  *
 *                             slap the player until he  *
 *                             dies                      *
 *                                                       *
 * Changelog:                                            *
 *                                                       *
 * Version 0.8                                           *
 *                                                       *
 *  -  Initial release                                   *
 *                                                       *
 *********************************************************/
 
/* Includes */
#include <core>
#include <console>
#include <plugin>
#include <adminlib>
 
/* Constants */
#define ACCESS_MSLAP ACCESS_KICK
 
/* Global Variables */
new g_Version[]="0.8";
new g_Timer[MAX_PLAYERS] = {0,0,...};
new g_Slaps[MAX_PLAYERS] = {0,0,...};
 
/* Function Declarations */
forward AdminMSlap(HLCommand,HLData,HLUserName,UserIndex);
forward TimerSlap(Timer,Repeat,HLUser,HLParam);
 
/* Event Handlers */
public plugin_init() {
    plugin_registerinfo("Rinde's multislap plugin","Slaps people multiple times.",g_Version);
    plugin_registercmd("admin_mslap","AdminMSlap",ACCESS_MSLAP,"admin_mslap <target> [<#>]: Slaps <target>, optionally include a number, 0 slaps until player is dead.");
    return PLUGIN_CONTINUE;
}
 
public TimerSlap(Timer,Repeat,HLUser,HLParam) {
    new sIndex[MAX_NUMBER_LENGTH];
    new Admin[MAX_NAME_LENGTH],Victim[MAX_NAME_LENGTH];
    new Text[MAX_TEXT_LENGTH];
    new iIndex;
    new iDead;
    convert_string(HLUser,Admin,MAX_NAME_LENGTH);
    convert_string(HLParam,sIndex,MAX_NUMBER_LENGTH);
    iIndex = strtonum(sIndex);
    if(playerinfo(iIndex,Victim,MAX_NAME_LENGTH,_,_,_,iDead) == 1) {
        if(iDead == 0) {
            slap(Victim);
            if(Repeat == 1) {
                snprintf(Text,MAX_TEXT_LENGTH,"Successfully slapped %s %i times.",Victim,g_Slaps[iIndex]);
                messageex(Admin,Text,print_chat);
                kill_timer(g_Timer[iIndex]);
                g_Timer[iIndex] = 0;
            }
        } else {
            snprintf(Text,MAX_TEXT_LENGTH,"Cannot slap %s anymore, player has died.",Victim);
            messageex(Admin,Text,print_chat);
            if(g_Slaps[iIndex] == 0) {
                snprintf(Text,MAX_TEXT_LENGTH,"Successfully slapped %s %i times",Victim,100000-Repeat);
            } else {
                snprintf(Text,MAX_TEXT_LENGTH,"Canceled slapping %s after %i Slaps, %i remaining.",Victim,g_Slaps[iIndex]-Repeat,Repeat);
            }
            messageex(Admin,Text,print_chat);
            kill_timer(g_Timer[iIndex]);
            g_Timer[iIndex] = 0;
        }
    } else {
        snprintf(Text,MAX_TEXT_LENGTH,"Error: Could not find player %s any more.",Victim);
        messageex(Admin,Text,print_chat);
        kill_timer(g_Timer[iIndex]);
        g_Timer[iIndex] = 0;
    }
}
 
/* Command Handlers */
public AdminMSlap(HLCommand,HLData,HLUserName,UserIndex) {
    new Command[MAX_COMMAND_LENGTH];
    new Data[MAX_DATA_LENGTH];
    new UserName[MAX_NAME_LENGTH];
    new Text[MAX_TEXT_LENGTH];
    new sSlaps[MAX_NUMBER_LENGTH];
    new sIndex[MAX_NUMBER_LENGTH];
    new iIndex,iDead;
    new iSlaps;
    convert_string(HLCommand,Command,MAX_COMMAND_LENGTH);
    convert_string(HLData,Data,MAX_DATA_LENGTH);
    convert_string(HLUserName,UserName,MAX_NAME_LENGTH);
    strstripquotes(Data);
    strbreak(Data,Data,sSlaps,MAX_DATA_LENGTH);
    if(check_user(Data) == 0) {
        selfmessage("Unrecognized player:");
        selfmessage(Data);
    } else {
        get_username(Data,Data,MAX_NAME_LENGTH);
        if(check_immunity(Data) == 1) {
            snprintf(Text,MAX_TEXT_LENGTH,"Laf. You can't slap %s, you silly bear.",Data);
            selfmessage(Text);
        } else {
            get_userindex(Data,iIndex);
            if(g_Timer[iIndex] != 0) {
                selfmessage("That player is being slapped at the moment");
            } else {
                if(playerinfo(iIndex,Data,MAX_NAME_LENGTH,_,_,_,iDead) == 0) {
                    selfmessage("Error: Could not find player any more.");
                } else {
                    if(iDead == 1) {
                        snprintf(Text,MAX_TEXT_LENGTH,"You cannot slap %s, player is dead.",Data);
                        selfmessage(Text);
                    } else {
                        if(strlen(sSlaps) == 0 || strtonum(sSlaps) < 0) {
                            selfmessage("Number of slaps missing, using default (3)");
                            iSlaps = 3;
                        } else if(streq(sSlaps,"0") == 1) {
                            selfmessage("Slapping target to death.");
                            iSlaps = 99999;
                            g_Slaps[iIndex] = 0;
                        } else {
                            iSlaps = strtonum(sSlaps) - 1;
                            g_Slaps[iIndex] = iSlaps + 1;
                        }
                        slap(Data);
                        if(iSlaps > 0) {
                            numtostr(iIndex,sIndex);
                            g_Timer[iIndex] = set_timer("TimerSlap",1,iSlaps,sIndex);
                        }
                        say_command(UserName,Command,Data);
                    }
                }
            }
        }
    }
    return PLUGIN_HANDLED;
}