Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
CSAL - csal.php

csal.php

Caricato da: ZioCrocifisso
Scarica il programma completo

  1. <?php
  2.  
  3. $fp = fopen('db', 'r+');
  4. $db = json_decode(stream_get_contents($fp), true);
  5. $ipcount = array();
  6.  
  7. foreach ($db['active'] as $id => $act) {
  8.         if (array_key_exists($act['ip'], $ipcount)) {
  9.                 $ipcount[$act['ip']]++;
  10.         } else {
  11.                 $ipcount[$act['ip']] = 1;
  12.         }
  13.  
  14.         if (time() - $act['start'] > 60 * 60 * 24 || $ipcount[$act['ip']] > 2) {
  15.                 unset($db['active'][$id]);
  16.         }
  17. }
  18.  
  19. if ($_POST['action'] === 'start') {
  20.         $id = rand();
  21.  
  22.         if ($_POST['hmax'] < 8 || $_POST['hmax'] % 2 != 0 || $_POST['vmax'] < 5
  23.                 || $_POST['hmax'] > 100 || $_POST['vmax'] > 60) {
  24.                 die('Configurazione non valida.');
  25.         }
  26.  
  27.         if (strlen($_POST['nick']) < 2 || strlen($_POST['nick']) > 16) {
  28.                 die('Nickname non valido.');
  29.         }
  30.  
  31.         $config = gen_config($_POST['hmax'], $_POST['vmax']);
  32.         $db['active'][$id]['start'] = time();
  33.         $db['active'][$id]['config'] = $config;
  34.         $db['active'][$id]['ip'] = $_SERVER['REMOTE_ADDR'];
  35.         $db['active'][$id]['nick'] = htmlspecialchars($_POST['nick']);
  36.         echo json_encode(array('config' => $config, 'id' => $id));
  37. } else if ($_POST['action'] == 'end') {
  38.         $id = $_POST['id'];
  39.         $moves = json_decode($_POST['moves'], true);
  40.         $revs = 0;
  41.         $rots = 0;
  42.  
  43.         if (array_key_exists($id, $db['active'])) {
  44.                 $start = $db['active'][$id]['start'];
  45.                 $config = $db['active'][$id]['config'];
  46.                 $nick = $db['active'][$id]['nick'];
  47.  
  48.                 foreach ($moves as $move) {
  49.                         if ($move['type'] == 'rot') {
  50.                                 $config = rotate($config, $move['v'], $move['a']);
  51.                                 $rots++;
  52.                         } else {
  53.                                 $config = reverse($config, $move['h']);
  54.                                 $revs++;
  55.                         }
  56.                 }
  57.  
  58.                 foreach ($config as $v) {
  59.                         foreach ($v as $val) {
  60.                                 if ($val == 0) {
  61.                                 $invalid = true;
  62.                                 }
  63.                         }
  64.                 }
  65.  
  66.                 if ($invalid) {
  67.                         echo 'Patita chiusa.';
  68.                 } else {
  69.                         $score = round(1000000 / (
  70.                                                         ($rots * 1.5 + $revs * 2.5) /
  71.                                                         (pow(count($config), 1.5) +
  72.                                                         pow(count($config[0]), 2))
  73.                         ));
  74.  
  75.                         array_push($db['high'], array('nick' => $nick, 'score' => $score, 'ip' => $db['active'][$id]['ip']));
  76.                         echo "Punteggio: $score.";
  77.                 }
  78.  
  79.                 unset($db['active'][$id]);
  80.         } else {
  81.                 echo "ID non valido: $id";
  82.         }
  83. } else if ($_POST['action'] == 'high') {
  84.         echo json_encode($db['high']);
  85. }
  86.  
  87. fseek($fp, 0);
  88. $jdb = json_encode($db);
  89. fwrite($fp, $jdb);
  90. ftruncate($fp, strlen($jdb));
  91. fclose($fp);
  92.  
  93. function gen_config($hmax, $vmax) {
  94.         $config = array();
  95.  
  96.         for ($v = 0; $v < $vmax; $v++) {
  97.                 $row = array();
  98.  
  99.                 for ($h = 0; $h < $hmax; $h++) {
  100.                         if ($v == 0 && $h % 2 == 0 && $hmax % 2 == 0) {
  101.                                 array_push($row, 2);
  102.                                 $h++;
  103.                         }
  104.  
  105.                         array_push($row, 1);
  106.                 }
  107.  
  108.                 array_push($config, $row);
  109.         }
  110.  
  111.         for ($i = 0; $i < 500; $i++) {
  112.                 if (rand(0, 10) > 5 || $i < 20) {
  113.                         $config = reverse($config, rand(0, $hmax - 1));
  114.                 } else {
  115.                         $config = rotate($config, rand(1, $vmax - 1), rand(1, $hmax - 1));
  116.                 }
  117.         }
  118.  
  119.         return $config;
  120. }
  121.  
  122. function rotate($config, $v, $amount) {
  123.         $len = count($config[$v]);
  124.  
  125.         if ($amount < 0)
  126.                 $amount += $hmax;
  127.  
  128.         if ($config[$v][0] == 2 && $amount % 2 != 0)
  129.                 return $config;
  130.  
  131.         $config[$v] = array_merge(
  132.                         array_slice($config[$v], $len - $amount, $amount),
  133.                         array_slice($config[$v], 0, $len - $amount)
  134.                         );
  135.  
  136.         return $config;
  137. }
  138.  
  139. function reverse($config, $h) {
  140.         $ch = $h;
  141.  
  142.         if ($config[0][$h] == 2) {
  143.                 $ch = ($h + 1) % count($config[0]);
  144.                 $config = reverse_single($config, $ch);
  145.         } else if ($h >= 1) {
  146.                 if ($config[0][$h - 1] == 2) {
  147.                         $config = reverse_single($config, $h - 1);
  148.                 }
  149.         }
  150.  
  151.         if ($config[0][$ch] == 0) {
  152.                 $config[0][$ch] = 1;
  153.         } else {
  154.                 $config[0][$ch] = 0;
  155.         }
  156.  
  157.         return reverse_single($config, $h);
  158. }
  159.  
  160. function reverse_single($config, $h) {
  161.         for ($v = 0; $v < count($config); $v++) {
  162.                         if ($config[$v][$h] == 2)
  163.                                 $ch = ($h + 1);
  164.                         else
  165.                                 $ch = $h;
  166.  
  167.                         if ($config[$v][$ch] == 1)
  168.                                 $config[$v][$ch] = 0;
  169.                         else
  170.                                 $config[$v][$ch] = 1;
  171.         }
  172.  
  173.         return $config;
  174. }
  175.  
  176. if ($_POST['test'] == 'test') {
  177.         $config = array(array(2, 1, 2, 0), array(2, 0, 2, 1));
  178.         $config = rotate($config, 1, 3);
  179.         $config = reverse($config, 1);
  180.         echo json_encode($config);
  181. }
  182.  
  183. ?>