Файл: games/puzzle/index.php
Строк: 92
<?php
require '../../lang_inc.php';
require '../../sid.php';
require '../../config.php';
$link = connect_db();
list($user, $id, $ps) = check_login($link);
whorm(0, 'games');
include '../../head.php';
echo $div_title . ''.$lang['Пазлы'].'' . $div_end . $div_left;
    $act = (isset($_GET['act'])) ? $_GET['act'] : NULL;
    switch ($act) {
        case hod:
            $ms = mysql_fetch_array(mysql_query("SELECT * FROM `game` WHERE type = 'Puzzle' AND users = '$user[id]' LIMIT 1"));
            if (!$ms['id']) {
                header("Location: index.php?lg=$lg");
            }
            $motion = (isset($_GET['motion'])) ? trim($_GET['motion']) : NULL;
            switch ($motion) {
                case up:
                    if ($ms['cursor'] != 0 and $ms['cursor'] != 1 and $ms['cursor'] != 2) {
                        $dump = unserialize($ms['dump']);
                        $cursor = $dump[$ms['cursor']];
                        $dump[$ms['cursor']] = $dump[$ms['cursor'] - 3];
                        $dump[$ms['cursor'] - 3] = $cursor;
                        mysql_query("UPDATE `game` SET
                                    `cursor` = '" . ($ms['cursor'] - 3) . "',
                                       `dump` = '" . serialize($dump) . "',
                                       `motion` = `motion` + '1'
                                      WHERE type = 'Puzzle' AND id = '" . $ms['id'] . "' LIMIT 1") or die('Error');
                    }
                    break;
                case down:
                    if ($ms['cursor'] != 6 and $ms['cursor'] != 7 and $ms['cursor'] != 8) {
                        $dump = unserialize($ms['dump']);
                        $cursor = $dump[$ms['cursor']];
                        $dump[$ms['cursor']] = $dump[$ms['cursor'] + 3];
                        $dump[$ms['cursor'] + 3] = $cursor;
                        mysql_query("UPDATE `game` SET
                                    `cursor` = '" . ($ms['cursor'] + 3) . "',
                                    `dump` = '" . serialize($dump) . "',
                                    `motion` = `motion` + '1'
                                    WHERE type = 'Puzzle' and id = '" . $ms['id'] . "' LIMIT 1") or die('Error');
                    }
                    break;
                case left:
                    if ($ms['cursor'] != 0 and $ms['cursor'] != 3 and $ms['cursor'] != 6) {
                        $dump = unserialize($ms['dump']);
                        $cursor = $dump[$ms['cursor']];
                        $dump[$ms['cursor']] = $dump[$ms['cursor'] - 1];
                        $dump[$ms['cursor'] - 1] = $cursor;
                        mysql_query("UPDATE `game` SET
                                    `cursor` = '" . ($ms['cursor'] - 1) . "',
                                       `dump` = '" . serialize($dump) . "',
                                      `motion` = `motion` + '1'
                                        WHERE type = 'Puzzle' AND id='" . $ms['id'] . "' LIMIT 1") or die('Error');
                    }
                    break;
                case right:
                    if ($ms['cursor'] != 2 and $ms['cursor'] != 5 and $ms['cursor'] != 8) {
                        $dump = unserialize($ms['dump']);
                        $cursor = $dump[$ms['cursor']];
                        $dump[$ms['cursor']] = $dump[$ms['cursor'] + 1];
                        $dump[$ms['cursor'] + 1] = $cursor;
                        mysql_query("UPDATE `game` SET
                                       `cursor` = '" . ($ms['cursor'] + 1) . "',
                                       `dump` = '" . serialize($dump) . "',
                                      `motion` = `motion` + '1'
                                      WHERE type='Puzzle' AND id = '" . $ms['id'] . "' LIMIT 1") or die('Error');
                    }
                    break;
            }
            header('Location: index.php?act=game&lg='.$lg.'');
            break;
        case game:
            $ms = mysql_fetch_array(mysql_query("SELECT *
            FROM `game` 
            WHERE type = 'Puzzle' and users = '$user[id]' LIMIT 1"));
            if (!$ms['id']) {
                header('Location: index.php?lg='.$lg.'&' . $ref);
            }
            if ($ms['motion'] == 0) {
                echo '<img src="temp/' . $ms['sum'] . '/img.jpg" alt="+" border="1"/> ';
            }
            $dump = unserialize($ms['dump']);
            echo '<img src="img.php?name=' . $ms['sum'] . '&1=' . $dump[0] . '&2=' .
                $dump[1] . '&3=' . $dump[2] . '&4=' . $dump[3] . '&5=' . $dump[4] .
                '&6=' . $dump[5] . '&7=' . $dump[6] . '&8=' . $dump[7] . '&9=' .
                $dump[8] . '" alt="+" border="1"/><br/>';
            echo $div_menu . '<a href="index.php?act=hod&motion=up&lg='.$lg.'">'.$lang['Вверх'].'</a>|
                 <a href="index.php?act=hod&motion=down&lg='.$lg.'">'.$lang['Вниз'].'</a>
                 <br/><a href="index.php?act=hod&motion=left&lg='.$lg.'">'.$lang['Влево'].'</a>|
                 <a href="index.php?act=hod&motion=right&lg='.$lg.'">'.$lang['Вправо'].'</a>' . $div_end;
            $time_game = time() - $ms['time'];
            if ($dump['0'] == 1 and $dump['1'] == 2 and $dump['2'] == 3 and $dump['3'] == 4 and
                $dump['4'] == 5 and $dump['5'] == 6 and $dump['6'] == 7 and $dump['7'] == 8) {
                echo '<span class="green">'.$lang['Вы выиграли'].'!</span>';
            }
            echo "".$lang['Время игры'].": " . date('H:i:s', $time_game) . "<br/>
                  Ходов: " . $ms['motion'] . "<br /></div>";
            echo $div_razdel . '<a href="index.php?lg='.$lg.'&'. $ref . '">'.$lang['Заново'].'</a>';
            break;
        default:
            $old = time() - (3 * 24 * 3600);
            mysql_query("DELETE FROM `game` WHERE type = 'Puzzle' AND (`users` = '$user[id]' OR `time` >= '" . $old . "')");
            mysql_query("OPTIMIZE TABLE `game`");
            $dropen = opendir("temp");
            while (($file1 = readdir($dropen))) {
                if ($file1 != "." && $file1 != ".." && $file1 != "index.php?lg=$lg") {
                    if (is_dir("temp/$file1")) {
                        $drt[] = $file1;
                    }
                }
            }
            shuffle($drt);
            $name = $drt[0];
            $arr = array('1', '2', '3', '4', '5', '6', '7', '8');
            shuffle($arr);
            $arr[8] = 0;
            mysql_query("INSERT INTO `game` SET
                        `time` = '" . time() . "',
                        `sum` = '" . $name . "',
                        `users` = '$user[id]',
                        `cursor` = '8',
                        `type` = 'Puzzle',
                        `dump` = '" . serialize($arr) . "'") or die('Error');
            echo ''.$lang['Головоломка'].'
                 ' . $block . $div_razdel . '
                  '.$lang['Цель игры: Собрать картинку из кусочков'].'.
                  ' . $div_end . '
                  <a href="index.php?act=game&lg='.$lg.'">'.$lang['Начать игру'].'</a><br/>';
            break;
    }
echo $div_end;
include '../../foot.php';
?>