<?php
define("INDEX", 1);
$chrono_start = microtime();
$onyx = @file_get_contents('./.onyx') or die("Configuration introuvable.");
define("_FCORE", trim($onyx));
require(_FCORE."../game/Class/JSON.php");
require_once(_FCORE."common.php");

if (isset($sess) && isset($sess->values['connected']) && $sess->values['connected'] && !empty($sess->values['id']) && !empty($sess->level) && !empty($sess->values['idPlan'])) {
    $json = new Services_JSON();
    $nbtrajet = 2;

    if (empty($config['flottes']) && $sess->level <= 1) {
        $datas = array(
            'root' => array(
                'destination' => '<input class="dest" type="text" id="amas" name="amas" maxlength="2" value="'.gpc('galaxie', 'post').'" />:<input class="dest" type="text" id="ss" name="ss" maxlength="2" value="'.gpc('ss', 'post').'" />:<input class="dest" type="text" id="plan" name="pos" maxlength="2" value="'.gpc('pos', 'post').'" />',
                'temps' => '-',
                'deblok' => '<span style="color: #FF0000;"><b>Les flottes sont désactivés pour le moment.</b></span>',
                'conso' => '-',
                'tactique' => '',
                'places' => ''
            )
        );
        header("X-JSON: ".$json->encode($datas));
        exit;
    }

    $destin = gpc('fav_dest', 'post');

    $chapeau = new bdd();
    $chapeau->connexion();
    $idPlan = $sess->values['idPlan'];
    $queryPlanete = $chapeau->unique_query("SELECT * FROM $table_planete WHERE id = '$idPlan'");
    if (!empty($destin)) {
        $chapeau->escape($destin);
        $queryPlaneteRapid = $chapeau->unique_query("SELECT nom_planete, galaxie, ss, position FROM $table_planete WHERE id = $destin;");
        if ($queryPlaneteRapid) {
            $destinRapid = '['.$queryPlaneteRapid['galaxie'].':'.$queryPlaneteRapid['ss'].':'.$queryPlaneteRapid['position'].']<input type="hidden" id="amas" name="amas" maxlength="2" value="'.$queryPlaneteRapid['galaxie'].'" /><input type="hidden" id="ss" name="ss" maxlength="2" value="'.$queryPlaneteRapid['ss'].'" /><input type="hidden" id="plan" name="pos" maxlength="2" value="'.$queryPlaneteRapid['position'].'" />';
        }
    }
    if (!isset($destinRapid)) {
        $destinRapid = '<input class="dest" type="text" id="amas" name="amas" maxlength="2" value="'.gpc('galaxie', 'post').'" />:<input class="dest" type="text" id="ss" name="ss" maxlength="2" value="'.gpc('ss', 'post').'" />:<input class="dest" type="text" id="plan" name="pos" maxlength="2" value="'.gpc('pos', 'post').'" />';
    }
    $start_galaxie = $queryPlanete['galaxie'];
    $start_ss = $queryPlanete['ss'];
    $start_position = $queryPlanete['position'];

    $met = gpc('met', 'post');
    $cri = gpc('cri', 'post');
    $hyd = gpc('hyd', 'post');
    if (!empty($sess->values['flcontenu']) && $met+$cri+$hyd >= 0 && $met >= 0 && $cri >= 0 && $hyd >= 0) {
        $places = $sess->values['flcontenu'] - ($met+$cri+$hyd);
    } else {
        $places = 'inconnu';
    }



    if (empty($sess->values['fltime']) || $sess->values['fltime'] + 600 < time() || empty($sess->values['flnbvais']) || empty($sess->values['flvitesse']) && isset($sess->values['auth_level'])) {
        $datas = array(
            'root' => array(
                'destination' => $destinRapid,
                'temps' => '-',
                'deblok' => '<b>Une erreur est survenue lors de la création de la flotte. Veuillez recommencer</b>',
                'conso' => '-',
                'tactique' => '',
                'places' => separerNombres($places)
            )
        );
    } elseif (empty($_POST['nom']) || preg_replace('@[^a-zA-Z0-9_ ]@i', '', $_POST['nom']) != $_POST['nom']) {
        $datas = array(
            'root' => array(
                'destination' => $destinRapid,
                'temps' => '-',
                'deblok' => '<b>Nom de la flotte incorrect !</b>',
                'conso' => '-',
                'tactique' => '',
                'places' => separerNombres($places)
            )
        );
    } elseif ($_POST['galaxie'] > MAX_AMAS || $_POST['ss'] > MAX_SYSTEME || $_POST['pos'] > MAX_PLANETE || $_POST['galaxie'] < 0 || $_POST['ss'] < 1 || $_POST['pos'] < 1 || ($_POST['galaxie'] < 1 && $sess->values['auth_level'] < 6)) {
        $datas = array(
            'root' => array(
                'destination' => $destinRapid,
                'temps' => '<span style="color: #FF0000;"><b>Lieu inaccessible</b></span>',
                'deblok' => '<b>Corrigez la destination !</b>',
                'conso' => '-',
                'tactique' => '',
                'places' => separerNombres($places)
            )
        );
    } else {
        $_POST['vitesse'] /= 100;
        $temps = vais_tempsDeplacement($start_galaxie, $start_ss, $start_position, $_POST['galaxie'], $_POST['ss'], $_POST['pos'], $sess->values['flvitesse'], $_POST['vitesse'], $sess->values['flpreparation'], $sess->values['flchauffe']);
        if ($temps <= 0) {
            $temps = 454;
        }
        $conso = vais_conso($temps, $sess->values['flnbvais'])*$_POST['vitesse']/45;
        $places -= ceil($conso*$nbtrajet);
        $tactique = '';
        if ($_POST['mission'] == '1') {
            $tactique = '<label for="selecttactique">Tactique d\'attaque :</label><select id="selecttactique" name="tactique">';
            //On récupère le niveau actuel de la technologie Commandement militaire
            $id_user = $sess->values['id'];
            $table = $config['db_prefix'].'user';
            $queryUser = $chapeau->unique_query("SELECT * FROM $table WHERE id = '$id_user'");
            $lvltechno = $queryUser['tactique'];
            $tactiquedef = $queryUser['combatAT_tactique'];

            if ($lvltechno < 1) { //Si on a pas le niveau, on ne peux pas envoyer la flotte
                $datas = array(
                        'root' => array(
                            'destination' => $destinRapid,
                            'temps' => affTemp(floor($temps)),
                            'deblok' => '<b>Vous ne pouvez pas attaquer sans un minimum de connaissances militaires !</b>',
                            'conso' => separerNombres(ceil($conso*$nbtrajet)),
                            'tactique' => '',
                            'places' => separerNombres($places)
                        )
                    );
                header("X-JSON: ".$json->encode($datas));
                exit;
            } else {
                for ($i=1 ; $i<=$lvltechno ; $i++) {
                    if ($tactiquedef == $i) {
                        $tactique .= '<option value="'.$i.'" selected>'.txtTactique($i).'</option>';
                    } else {
                        $tactique .= '<option value="'.$i.'">'.txtTactique($i).'</option>';
                    }
                }
            }
            $tactique .= '</select><br />';
        }
        $cds = sha1('flotte'.$conso.'ß10'.time().'|HB;'.rand(10, 99).$temps);
        $sess->values['flcds'] = $cds;
        $sess->put();
        $datas = array(
                'root' => array(
                    'destination' => $destinRapid,
                    'temps' => affTemp(floor($temps)),
                    'deblok' => '<input type="hidden" name="cds" value="'.$cds.'" /><input type="submit" value="GO" class="submit" />',
                    'conso' => separerNombres(ceil($conso*2)),
                    'tactique' => $tactique,
                    'places' => separerNombres($places)
                )
            );
    }

    $chapeau->deconnexion();
    header("X-JSON: ".$json->encode($datas));
}
