/**
  * @return cartographie
  */
 public static function getinstance()
 {
     if (!self::$instance) {
         self::$instance = new self();
     }
     return self::$instance;
 }
 /**
  * lance le spool de requète sql
  */
 public static function sql_do_spool()
 {
     $spoolactive = false;
     if (count(self::$conf_save) > 0) {
         foreach (self::$conf_save as $key => $value) {
             self::sql_spool('UPDATE `SQL_PREFIX_Config` SET `value` =\'' . sqlesc(serialize($value)) . '\' WHERE `key`=\'' . $key . '\' LIMIT 1');
         }
         self::$conf_save = array();
     }
     if (count(self::$sql_spool) > 0) {
         if (IN_DEV) {
             self::$sqls[] = array(0, 'Spooler...');
             $spoolactive = true;
         }
         foreach (self::$sql_spool as $sql) {
             $time = microtime(true);
             $sql = str_replace('SQL_PREFIX_', SQL_PREFIX_, $sql);
             mysql_unbuffered_query($sql);
             $time = round((microtime(true) - $time) * 1000, 3);
             if (IN_DEV) {
                 self::$sqls[] = array($time, $sql);
             }
         }
         self::$sql_spool = array();
     }
     if (class_exists('cartographie')) {
         cartographie::getinstance()->do_spooler();
     }
     if (IN_DEV && $spoolactive) {
         self::$sqls[] = array(0, '...Spooler');
     }
 }
/**
 * @author Alex10336
 * Dernière modification: $Id$
 * @license GNU Public License 3.0 ( http://www.gnu.org/licenses/gpl-3.0.txt )
 * @license Creative Commons 3.0 BY-SA ( http://creativecommons.org/licenses/by-sa/3.0/deed.fr )
 *
 **/
require_once './init.php';
require_once INCLUDE_PATH . 'Script.php';
require_once CLASS_PATH . 'parser.class.php';
require_once CLASS_PATH . 'cartographie.class.php';
require_once CLASS_PATH . 'map.class.php';
if (!Members::CheckPermsOrDie('CARTOGRAPHIE')) {
}
$map = map::getinstance();
$carto = cartographie::getinstance();
$lng = language::getinstance()->GetLngBlock('cartographie');
if (isset($_POST['massedit'])) {
    foreach ($_POST['item'] as $k => $arr) {
        if ($arr['delete']) {
            $carto->Delete_Entry($k, $arr['type']);
        } else {
            if ($arr['edit']) {
                unset($arr['edit']);
                if (isset($arr['TROOP'])) {
                    $arr['TROOP'] = DataEngine::strip_number($arr['TROOP']);
                }
                $carto->Edit_Entry($k, $arr);
            }
        }
    }
    function AddPillage_log($mode, $idate, $msg)
    {
        // check si existant
        $sql = sprintf('SELECT `pid` FROM SQL_PREFIX_troops_pillage WHERE Player=\'%s\' AND date=%d', sqlesc($_SESSION['_login']), $idate);
        $result = DataEngine::sql($sql);
        if (mysql_numrows($result) > 0) {
            return $this->lng['log_allreadyexists'];
        }
        // Type du log/bataille
        // Puis recherche de la bataille (coords+participation)
        if ($mode == 'defender') {
            preg_match($this->lng['defender_regex'], $msg, $info);
            $ident = $this->lng['defender_ident'];
            if (!($planets = ownuniverse::getinstance()->get_coordswithname())) {
                return $this->lng['battle_error_ownuniverse'];
            }
            foreach ($planets as $v) {
                if ($v['Name'] == $info[$this->lng[$mode . '_regex_planetid']]) {
                    cartographie::getinstance()->FormatId($v['Coord'], $idsys, $iddet, 'troops::AddPillage_log(def,1)');
                    break;
                }
            }
            $sql = sprintf('SELECT `ID` FROM `SQL_PREFIX_troops_attack` WHERE `type`=\'%s\' AND ' . '`coords_ss`=\'%s\' AND `coords_3p`=\'%s\' AND `when`<=%d AND `when`>=%d' . ' AND `players_defender` LIKE \'%%"%s"%%\' AND `players_attack` LIKE \'%%"%s"%%\'' . 'ORDER BY `when` DESC LIMIT 1', sqlesc($mode), $idsys, $iddet, $idate, $idate - 604800, sqlesc($_SESSION['_login']), sqlesc($info[$this->lng[$mode . '_regex_userid']]));
            $result = DataEngine::sql($sql);
            if (mysql_numrows($result) < 1) {
                return $this->lng['log_battlenofound'];
            }
            $line = mysql_fetch_assoc($result);
            $mid = $line['ID'];
        } else {
            preg_match($this->lng['attacker_regex'], $msg, $info);
            $ident = $this->lng['attacker_ident'];
            $sql = <<<sql
SELECT `POSIN`, `COORDET` FROM `SQL_PREFIX_Coordonnee`
LEFT JOIN `SQL_PREFIX_Coordonnee_Joueurs` on `ID`=`jID`
WHERE `TYPE` in (0,3,5) AND `USER`='%s' AND `INFOS`='%s'
sql;
            $sql = sprintf($sql, $info[$this->lng[$mode . '_regex_userid']], $info[$this->lng[$mode . '_regex_planetid']]);
            $result = DataEngine::sql($sql);
            if (mysql_numrows($result) < 1) {
                return $this->lng['log_coordsnotfound'];
            }
            if (mysql_numrows($result) > 1) {
                return $this->lng['log_multiplecoords'];
            }
            $line = mysql_fetch_assoc($result);
            $idsys = $line['POSIN'];
            $iddet = $line['COORDET'];
            $sql = sprintf('SELECT `ID` FROM `SQL_PREFIX_troops_attack` WHERE `type`=\'%s\' AND ' . '`coords_ss`=\'%s\' AND `coords_3p`=\'%s\' AND `when`<%d AND `when`>%d AND ' . '`players_attack` LIKE \'%%"%s"%%\' AND `players_defender` LIKE \'%%"%s"%%\'', sqlesc($mode), $idsys, $iddet, $idate, $idate - 604800, sqlesc($_SESSION['_login']), sqlesc($info[$this->lng[$mode . '_regex_userid']]));
            $result = DataEngine::sql($sql);
            if (mysql_numrows($result) < 1) {
                return $this->lng['log_battlenofound'];
            }
            if (mysql_numrows($result) > 1) {
                return 'Error battle result > 1 (omfg)';
            }
            $line = mysql_fetch_assoc($result);
            $mid = $line['ID'];
        }
        // Info à ajouter
        $amsg = explode("\n", trim(p_substr($msg, p_stripos($msg, $ident, 0) + p_strlen($ident), -1)));
        $ares = DataEngine::a_ressources();
        $fields = array();
        $sets = array();
        $fields[] = 'date';
        $sets[] = $idate;
        $fields[] = 'mid';
        $sets[] = $mid;
        $fields[] = 'Player';
        $sets[] = '\'' . sqlesc($_SESSION['_login']) . '\'';
        foreach ($ares as $k => $v) {
            foreach ($amsg as $line) {
                list($key, $value) = explode(':', $line);
                if ($v['Nom'] == trim($key)) {
                    $fields[] = 'ress' . $k;
                    $sets[] = DataEngine::strip_number($value);
                    break;
                }
            }
        }
        $fields = implode(',', $fields);
        $sets = implode(',', $sets);
        $sql = 'INSERT INTO `SQL_PREFIX_troops_pillage` (' . $fields . ') VALUES (' . $sets . ')';
        $result = DataEngine::sql($sql);
        return $this->lng['log_added'];
    }