PHP Code:
#include <a_samp>
public OnPlayerCommandText(playerid, cmdtext[])
{
new string[128], cmd[128], tmp[128], idx;
cmd = strtok(cmdtext, idx);
if(!strcmp(cmd, "/repair", true))
{
tmp = strtok(cmdtext, idx);
// Conditions
if(!strlen(tmp))
return SendClientMessage(playerid, -1, "[USAGE]: /repair [playerid / name]");
new targetid = ReturnUser(tmp);
if(!IsPlayerConnected(targetid))
return SendClientMessage(playerid, -1, "[ERROR]: That player is not connected !");
if(!(playerid != targetid))
return SendClientMessage(playerid, -1, "[ERROR]: You can't repair yourself !");
if(GetPlayerState(targetid) != PLAYER_STATE_DRIVER)
return SendClientMessage(playerid, -1, "[ERROR]: That player is not a driver !");
if(GetPlayerMoney(targetid) < 150)
return SendClientMessage(playerid, -1, "[ERROR]: That player doesn't have 150$ !");
new Float: vHP; GetVehicleHealth(GetPlayerVehicleID(targetid), vHP);
if(vHP > 995)
return SendClientMessage(playerid, -1, "[ERROR]: That player's vehicle it's aleardy repaired !");
// OK -> repair car
RepairVehicle(GetPlayerVehicleID(targetid));
format(string, 128, "You repaired {DE5757}%s{FFFFFF}'s car for {EB3333}150{3FAA33}$ {FFFFFF}!", returnName(targetid));
SendClientMessage(playerid, 0xFFFFFFFF, string);
format(string, 128, "Mechanic {DE5757}%s {FFFFFF}has repaired your car for {EB3333}150{3FAA33}$ {FFFFFF}!", returnName(playerid));
SendClientMessage(targetid, 0xFFFFFFFF, string);
}
return 1;
}
// =============================================================================
returnName(playerid)
{
new name[20];
GetPlayerName(playerid, name, 20);
return (name);
}
IsNumeric(const string[])
{
for (new i = 0, j = strlen(string); i < j; i++)
{
if (string[i] > '9' || string[i] < '0') return 0;
}
return 1;
}
strtok(const string[], &index)
{
new length = strlen(string);
while ((index < length) && (string[index] <= ' '))
{
index++;
}
new offset = index;
new result[128];
while ((index < length) && (string[index] > ' ') && ((index - offset) < (sizeof(result) - 1)))
{
result[index - offset] = string[index];
index++;
}
result[index - offset] = EOS;
return result;
}
ReturnUser(text[])
{
new pos = 0;
while (text[pos] < 0x21)
{
if (text[pos] == 0) return INVALID_PLAYER_ID;
pos++;
}
new userid = INVALID_PLAYER_ID;
if (IsNumeric(text[pos])) // Check whole passed string
{
userid = strval(text[pos]);
if (userid >= 0 && userid < MAX_PLAYERS)
{
if(!IsPlayerConnected(userid))
{
userid = INVALID_PLAYER_ID;
}
else
{
return userid; // A player was found
}
}
}
// They entered [part of] a name or the id search failed (check names just incase)
new len = strlen(text[pos]);
new count = 0;
new name[MAX_PLAYER_NAME];
for(new i = 0; i < MAX_PLAYERS; i++)
{
if(!IsPlayerConnected(i)) continue;
GetPlayerName(i, name, sizeof (name));
if (strcmp(name, text[pos], true, len) == 0) // Check segment of name
{
if (len == strlen(name)) // Exact match
{
return i;
}
else // Partial match
{
count++;
userid = i;
}
}
}
if (count != 1)
{
userid = INVALID_PLAYER_ID;
}
return userid; // INVALID_USER_ID for bad return
}