|
<?php
foreach ($db['active'] as $id => $act) {
$ipcount[$act['ip']]++;
} else {
$ipcount[$act['ip']] = 1;
}
if (time() - $act['start'] > 60 * 60 * 24 || $ipcount[$act['ip']] > 2 ) {
unset($db['active'][$id]);
}
}
if ($_POST['action'] === 'start') {
if ($_POST['hmax'] < 8 || $_POST['hmax'] % 2 != 0 || $_POST['vmax'] < 5
|| $_POST['hmax'] > 100 || $_POST['vmax'] > 60) {
die('Configurazione non valida.');
}
if (strlen($_POST['nick']) < 2 || strlen($_POST['nick']) > 16 ) {
die('Nickname non valido.');
}
$config = gen_config($_POST['hmax'], $_POST['vmax']);
$db['active'][$id]['start'] = time();
$db['active'][$id]['config'] = $config;
$db['active'][$id]['ip'] = $_SERVER['REMOTE_ADDR'];
} else if ($_POST['action'] == 'end') {
$id = $_POST['id'];
$revs = 0;
$rots = 0;
$start = $db['active'][$id]['start'];
$config = $db['active'][$id]['config'];
$nick = $db['active'][$id]['nick'];
foreach ($moves as $move) {
if ($move['type'] == 'rot') {
$config = rotate($config, $move['v'], $move['a']);
$rots++;
} else {
$config = reverse($config, $move['h']);
$revs++;
}
}
foreach ($config as $v) {
foreach ($v as $val) {
if ($val == 0) {
$invalid = true;
}
}
}
if ($invalid) {
echo 'Patita chiusa.';
} else {
$score = round(1000000 / (
($rots * 1.5 + $revs * 2.5) /
));
array_push($db['high'], array('nick' => $nick, 'score' => $score, 'ip' => $db['active'][$id]['ip']));
echo "Punteggio: $score.";
}
unset($db['active'][$id]);
} else {
echo "ID non valido: $id";
}
} else if ($_POST['action'] == 'high') {
}
function gen_config($hmax, $vmax) {
for ($v = 0; $v < $vmax; $v++) {
for ($h = 0; $h < $hmax; $h++) {
if ($v == 0 && $h % 2 == 0 && $hmax % 2 == 0) {
$h++;
}
}
}
for ($i = 0; $i < 500; $i++) {
if (rand(0 , 10 ) > 5 || $i < 20 ) {
$config = reverse ($config, rand(0 , $hmax - 1 ));
} else {
$config = rotate ($config, rand(1 , $vmax - 1 ), rand(1 , $hmax - 1 ));
}
}
return $config;
}
function rotate($config, $v, $amount) {
$len = count($config[$v]);
if ($amount < 0)
$amount += $hmax;
if ($config[$v][0] == 2 && $amount % 2 != 0)
return $config;
);
return $config;
}
function reverse($config, $h) {
$ch = $h;
if ($config[0][$h] == 2) {
$ch = ($h + 1 ) % count($config[0 ]);
$config = reverse_single($config, $ch);
} else if ($h >= 1) {
if ($config[0][$h - 1] == 2) {
$config = reverse_single($config, $h - 1);
}
}
if ($config[0][$ch] == 0) {
$config[0][$ch] = 1;
} else {
$config[0][$ch] = 0;
}
return reverse_single($config, $h);
}
function reverse_single($config, $h) {
for ($v = 0; $v < count($config); $v++) {
if ($config[$v][$h] == 2)
$ch = ($h + 1);
else
$ch = $h;
if ($config[$v][$ch] == 1)
$config[$v][$ch] = 0;
else
$config[$v][$ch] = 1;
}
return $config;
}
if ($_POST['test'] == 'test') {
$config = rotate($config, 1, 3);
$config = reverse($config, 1);
}
?>
|
|