BlindLaby/save.php

54 lines
1.4 KiB
PHP
Raw Normal View History

<?php
2020-08-04 12:22:09 +02:00
session_start();
$startingPoint = $_POST['startingPoint'];
$endingPoint = $_POST['endingPoint'];
$largeur = (int)$_POST['largeur'];
$hauteur = (int)$_POST['hauteur'];
$cases = [];
foreach ($_POST as $n => $v) {
if (!in_array($n, ['startingPoint', 'endingPoint', 'largeur', 'hauteur'])) {
$coord = getCoordForCase($n);
$cases[$coord[0]][$coord[1]] = 1;
}
2020-08-04 12:22:09 +02:00
}
function getCoordForPt($t)
{
if (preg_match('/^check(\d{1,2})-(\d{1,2})$/', $t, $m) !== 0) {
2020-08-04 12:22:09 +02:00
return '[' . $m[2] . ',' . $m[1] . ']';
}
return null;
2020-08-04 12:22:09 +02:00
}
function getCoordForCase($t)
{
if (preg_match('/^check(\d{1,2})-(\d{1,2})$/', $t, $m) !== 0) {
2020-08-04 12:22:09 +02:00
return [$m[1], $m[2]];
}
return null;
}
$level = '[';
for ($i = 0; $i < $hauteur; $i++) {
$level .= '[';
for ($j = 0; $j < $largeur; $j++) {
if (!empty($cases[$i][$j])) {
2020-08-04 12:22:09 +02:00
$level .= '1,';
} else {
$level .= '0,';
}
}
$level = substr($level, 0, -1) . '],';
2020-08-04 12:22:09 +02:00
}
$level = substr($level, 0, -1) . '];';
2020-08-04 12:22:09 +02:00
$file = '';
$file .= 'currentPosition = ' . getCoordForPt($startingPoint) . ';';
$file .= 'finalPosition =' . getCoordForPt($endingPoint) . ';';
$file .= 'level = ' . $level;
2020-08-04 12:22:09 +02:00
$tod = gettimeofday();
$finalFilename = $tod['sec'] . mt_rand(0, 50) . '.js';
file_put_contents(__DIR__ . '/custom/' . $finalFilename, $file);
2020-08-04 12:22:09 +02:00
$_SESSION['file'] = $finalFilename;
header('Location: list/');