%PDF- %PDF-
| Direktori : /www/varak.net/losik.varak.net/app/ |
| Current File : /www/varak.net/losik.varak.net/app/import-process.php |
<?php
/**
* Created by PhpStorm.
* User: waritko
* Date: 22. 9. 2017
* Time: 19:31
*/
$target_dir = __DIR__ . '/../tmp/';
$target_file = $target_dir . uniqid("score", true) . ".zip";
if (!move_uploaded_file($_FILES["file"]["tmp_name"], $target_file))
{
$smarty->assign("errorMessage", "Sorry, there was an error uploading your file.");
echo "Sorry, there was an error uploading your file.";
//break;
}
else
{
header("Content-Type: text/plain");
//header("Location: index.php");
$zdata = file_get_contents("zip://" . $target_file . "#match_def.json");
$matchDef = json_decode($zdata);
$zdata = file_get_contents("zip://" . $target_file . "#match_scores.json");
$scores = json_decode($zdata);
//print_r($matchDef);
//print_r($scores);
$matchName = $matchDef->match_name;
$matchDate = $matchDef->match_date;
$matchUid = $matchDef->match_id;
$matchId = null;
dibi::begin();
$result = dibi::query("SELECT [id] FROM [matches] WHERE [guid] = %s", $matchUid);
$result = $result->fetchAll();
if(count($result) > 0)
{
$matchId = $result[0]->id;
}
else
{
$data = array(
'guid' => $matchUid,
'name' => $matchName,
'date' => $matchDate
);
dibi::query("INSERT INTO [matches]", $data);
$matchId = dibi::getInsertId();
}
$shooters = array();
foreach ($matchDef->match_shooters as $match_shooter)
{
if($match_shooter->sh_del) continue;
$shooter = new stdClass();
$shooter->id = null;
$shooter->name = $match_shooter->sh_fn . " " . $match_shooter->sh_ln;
$shooter->mail = $match_shooter->sh_eml;
$shooter->uid = $match_shooter->sh_uid;
$shooter->factor = $match_shooter->sh_pf;
$shooter->division = $match_shooter->sh_dvp;
$result = dibi::query("SELECT [id] FROM [shooters] WHERE UPPER([email]) = %s", strtoupper($shooter->mail));
$result = $result->fetchAll();
if(count($result) > 0 && $result)
{
$shooter->id = $result[0]->id;
}
else
{
$data = array(
'name' => $shooter->name,
'email' => $shooter->mail
);
dibi::query("INSERT INTO [shooters]", $data);
$shooter->id = dibi::getInsertId();
}
$shooters[$shooter->uid] = $shooter;
}
//print_r($shooters);
//dibi::query("DELETE FROM [score] WHERE [match] = %i", $matchId);
//dibi::query("DELETE FROM [score] WHERE [match] = %i", $matchId);
dibi::query("DELETE FROM [stages] WHERE [match] = %i", $matchId);
$stages = array();
foreach ($matchDef->match_stages as $match_stage)
{
$stage = new stdClass();
$stage->guid = $match_stage->stage_uuid;
$stage->order = $match_stage->stage_number;
$stage->name = $match_stage->stage_name;
$stage->deleted = $match_stage->stage_deleted;
$stage->strings = $match_stage->stage_strings;
$stages[$stage->guid] = $stage;
}
//print_r($stages);
// Process scores
foreach ($scores->match_scores as $stage)
{
if($stages[$stage->stage_uuid]->deleted) continue;
$stdata = array(
"guid" => $stage->stage_uuid,
"order" => $stage->stage_number,
"match" => $matchId,
"name" => $stages[$stage->stage_uuid]->name,
"strings" => $stages[$stage->stage_uuid]->strings
);
dibi::query("INSERT INTO [stages]", $stdata);
$stageId = dibi::getInsertId();
foreach ($stage->stage_stagescores as $score)
{
if(!isset($shooters[$score->shtr])) continue;
$noshoots = isset($score->popns) ? $score->popns : 0;
$stdata = array();
$stdata['stage'] = $stageId;
$stdata['shooter'] = $shooters[$score->shtr]->id;
//$stdata['time'] = $score->str[0];
$stdata['division'] = $shooters[$score->shtr]->division;
$stdata['proc'] = isset($score->proc) ? $score->proc : 0;
dibi::query("INSERT INTO [shooter_stage]", $stdata);
$stageNo = dibi::getInsertId();
// Proces times
$order = 1;
foreach ($score->str as $time)
{
$strings = null;
if(isset($score->meta))
{
foreach ($score->meta as $meta)
{
if(strstr($meta->k, "string" . ($order-1)))
{
$strings = array();
$times = explode(",", $meta->v);
$curr = 0.0;
foreach ($times as $t)
{
$strings[] = number_format((double)$t - $curr, 2, '.', '');
$curr = (double)$t;
}
}
}
}
$data = array(
"shooter_stage" => $stageNo,
"time" => $time,
"order" => $order++,
"string" => $strings != null ? implode(",", $strings) : null
);
dibi::query("INSERT INTO [shooter_stage_time]", $data);
}
$dataTpl = array(
"shooter_stage" => $stageNo,
"alpha" => 0,
"charlie" => 0,
"delta" => 0,
"miss" => 0,
"popper" => 0,
"missPopper" => 0
);
$order = 1;
$data = array_copy($dataTpl);
if($score->popm > 0 || $score->poph > 0)
{
$data["order"] = $order++;
$data["popper"] = $score->poph;
$data["missPopper"] = $score->popm;
dibi::query("INSERT INTO [shooter_stage_target]", $data);
$data = array_copy($dataTpl);
}
foreach ($score->ts as $t)
{
$data["order"] = $order++;
$data["alpha"] = ($t & 0xF);
$data["charlie"] = ($t & 0xF00) >> 8;
$data["delta"] = ($t & 0xF000) >> 12;
$data["miss"] = ($t & 0xF00000) >> 20;
$noshoots += ($t & 0xF0000) >> 16;
dibi::query("INSERT INTO [shooter_stage_target]", $data);
$data = array_copy($dataTpl);
}
$updateData = array(
"noshoot" => $noshoots
);
dibi::query("UPDATE [shooter_stage] SET", $updateData, "WHERE [id] = %i", $stageNo);
}
}
dibi::query("UPDATE [shooter_stage] SET [shooter]=12 WHERE [shooter]=49");
dibi::commit();
//dibi::rollback();
//die;
header("Location: index.php?action=generate-detailed-results&id=$matchId");
}