<?php

/*
PLUGIN: Pingkicker
================
Kicks players whos ping is to high.
ATTENTION: This function might not work correctly cause of a bug in the CoD
           RCON response

CONFIG:
-------
[pingkicker]
enabled = 1                         ;Enable plugin
maxping = 150                       ;Maximum ping that a player can have
checkinterval = 30                  ;Check pings every X secounds
warnstokick = 2                     ;Warns until kick
mode = "tempban"                    ;kick/ban/tempban
kickreason = "Your ping is to high" ;Reason of a kick

NAMESPACE: pingkicker

Functions:
    pingkicker_main
    pingkicker_playerInit

Vars:
    $pingkicker_lastcheck

*/

$mod->registerEvent("playerJoined", "pingkicker_playerInit");
$mod->registerEvent("everyTime", "pingkicker_main");
$mod->registerEvent("nextMap", "pingkicker_reset");  //Do not check while the clients are loading the map

$mod->setDefaultCV("pingkicker", "enabled", 0);
$mod->setDefaultCV("pingkicker", "maxping", 150);
$mod->setDefaultCV("pingkicker", "checkinterval", 30);
$mod->setDefaultCV("pingkicker", "warnstokick", 2);
$mod->setDefaultCV("pingkicker", "mode", "tempban");
$mod->setDefaultCV("pingkicker", "kickreason", "Your ping is to high");

//Init Players because playerJoined isn't called at startup
array_map("pingkicker_playerInit", array_keys($players));

$pingkicker_lastcheck = time();

function pingkicker_main() {
    global $mod;
    global $players;
    global $pingkicker_lastcheck;
    global $logging;

    if (!$mod->getCV("pingkicker", "enabled")) {
        return;
    }

    if (time() - $pingkicker_lastcheck < $mod->getCV("pingkicker", "checkinterval")) {
        return;
    }

    $pingkicker_lastcheck = time();

    $playerlist = $mod->rconPlayerList();

    foreach ($playerlist as $value) {
        if (!array_key_exists($value["guid"], $players)) {
            continue;
        }
        if ($value["ping"] <= $mod->getCV("pingkicker", "maxping")) {
            $players[$value["guid"]]->pingkicker_warns = 0;
        }
        else {
            if ($players[$value["guid"]]->isProtected()) continue;

            $players[$value["guid"]]->pingkicker_warns ++;

            if ($players[$value["guid"]]->pingkicker_warns >= $mod->getCV("pingkicker", "warnstokick")) {
                switch ($mod->getCV("pingkicker", "mode")) {
                    case "tempban":
                        $players[$value["guid"]]->tempBan($mod->getCV("pingkicker", "kickreason"));
                        break;
                    case "kick":
                        $players[$value["guid"]]->kick($mod->getCV("pingkicker", "kickreason"));
                        break;
                    case "ban":
                        $players[$value["guid"]]->ban($mod->getCV("pingkicker", "kickreason"));
                        break;
                }
            }
            else {
                 $players[$value["guid"]]->say($mod->getLngString("warningPingToHigh"));
                 $logging->write(MOD_NOTICE, "Player '".$players[$value["guid"]]->getName()."' has been warned for his high ping (".$value["ping"]."), PID: ".$players[$value["guid"]]->getPID().", GUID:".$value["guid"]);
            }
        }
    }

}

function pingkicker_playerInit($guid) {
    $GLOBALS['players'][$guid]->pingkicker_warns = 0;
}

function pingkicker_reset() {
    global $pingkicker_lastcheck;
    $pingkicker_lastcheck = time() + 30;
}
?>