<?php
ob_start();
$db = new PDO('mysql:host=localhost;dbname=sword;charset=UTF8', 'root', '');
$_items = $db -> prepare('SELECT * FROM `items`');
$_items -> execute();
$items = $_items -> fetchAll();
/*
$items = [
'sword' => ['width' => 1, 'height' => 2, 'image' => 'swords/sword.png'],
'rezak' => ['width' => 1, 'height' => 3, 'image' => 'swords/rezak.png'],
'double' => ['width' => 2, 'height' => 2, 'image' => 'swords/double.png']
];
$inv = [
['id' => '1', 'x' => 0, 'y' => 0, 'rotation' => 180],
['name' => 'rezak', 'x' => 3, 'y' => 1, 'rotation' => 180],
['name' => 'sword', 'x' => 0, 'y' => 2],
['name' => 'sword', 'x' = 1, 'y' => 2, 'rotation' => 180],
['name' => 'rezak', 'x' => 2, 'y' => 1],
//['name' => 'sword', 'x' => 2, 'y' => 0, 'rotation' => 90]
];
$minX = min($x1, $x2);
$minY = min($y1, $y2);
$maxX = max($x1, $x2);
$maxY = max($y1, $y2);
if ($currentX >= $minX && $currentX <= $maxX && $currentY >= $minY && $currentY <= $maxY) return true;
else return false;
$x1 = 1;
$y1 = 3;
$rotation = 270;
$width = 1;
$height = 3;
# В зависимости от угла поворота находим x2 и y2
switch ($rotation) {
case 90:
$x2 = $x1 + $height;
$y2 = $y1 + $width;
break;
case 180:
$x2 = $x1 + $width;
$y2 = $y1 - $height;
break;
case 270:
$x2 = $x1 - $height;
$y2 = $y1 + $width;
break;
default:
$x2 = $x1 + $width;
$y2 = $y1 + $height;
break;
}
*/
$_inv = $db -> prepare('SELECT * FROM `table` WHERE `user` = ?');
$_inv -> execute([1]);
$inv = $_inv-> fetchAll();
$size = [4, 4];
$dp = 32;
$url = [];
if(isset($_GET['blacksmit']) or isset($_GET['ore'])){
$basis = imagecreatetruecolor($size[0] * $dp, $size[1] * $dp);
imagecolorallocate($basis, 0, 0, 0);
$slot = imagecreatefrompng('slot.png');
list($slot_width, $slot_height) = getimagesize('slot.png');
for ($x = 0; $x < $size[0]; ++$x) {
for ($y = 0; $y < $size[1]; ++$y) {
$_check = $db->prepare('SELECT * FROM `table` WHERE ? >= LEAST(`x`, `x2`) AND ? <= GREATEST(`x`, `x2`) AND ? >= LEAST(`y`, `y2`) AND ? <= GREATEST(`y`, `y2`)');
$_check -> execute([$x,$x,$y,$y]);
$check = $_check->fetch();
if(!$check) $url[] = '<area shape="rect" alt="" title="" coords="'.($x * $dp).','.($y * $dp).','.($x * $dp + $dp).','.($y * $dp + $dp).'" href="?x='.$x.'&y='.$y.'&item=3&rotation=90" target="" />';
if(!$check) imagecopyresampled($basis, $slot, $x * $dp, $y * $dp, 0, 0, $dp, $dp, $slot_width, $slot_height);
}
}
imagedestroy($slot);
$count = count($inv);
for ($i = 0; $i < $count; ++$i) {
$slot = $inv[$i];
$_item = $db -> prepare('SELECT * FROM `items` where `id` = ?');
$_item -> execute([$slot['item']]);
$item = $_item -> fetch();
if($slot['status'] == 'ore') $item['image'] = 'ores/'.$item['ore'];
if($slot['status'] == 'complete') $item['image'] = 'completes/'.$item['complete_image'];
$image = imagecreatefrompng($item['image']);
list($width, $height) = getimagesize($item['image']);
if(in_array($slot['rotation'], [90,270])){
$image = imagerotate($image, $slot['rotation'], 0);
imagecopyresampled($basis, $image, $slot['x'] * $dp, $slot['y'] * $dp, 0, 0, $item['height'] * $dp, $item['width'] * $dp, $height, $width);
}
else{
if($slot['rotation']) $image = imagerotate($image, $slot['rotation'], 0);
imagecopyresampled($basis, $image, $slot['x'] * $dp, $slot['y'] * $dp, 0, 0, $item['width'] * $dp, $item['height'] * $dp, $width, $height);
}
imagedestroy($image);
}
imagepng($basis);
imagedestroy($basis);
}
if(isset($_GET['x']) and isset($_GET['y']) and isset($_GET['item'])){
$_items = $db -> prepare('SELECT * FROM `items` WHERE `id` = ?');
$_items -> execute([(int)$_GET['item']]);
$item = $_items -> fetch();
if($item){
$x2 = (in_array($_GET['rotation'], [90,270]) ? ((int)$_GET['x'] + $item['height'] - 1) : ((int)$_GET['x'] + $item['width'] -1));
$y2 = (in_array($_GET['rotation'], [90,270]) ? ((int)$_GET['y'] + $item['width'] - 1) : ((int)$_GET['y'] + $item['height'] -1));
$_check = $db->prepare('SELECT * FROM `table` WHERE ? >= LEAST(`x`, `x2`) AND ? <= GREATEST(`x`, `x2`) AND ? >= LEAST(`y`, `y2`) AND ? <= GREATEST(`y`, `y2`)');
$_check -> execute([$x2,$x2,$y2,$y2]);
$check = $_check->fetch();
if($check){
header('Location:/?blacksmit');
exit;
}
if($x2 > ($size[0] - 1) or $y2 > ($size[1] - 1)){
header('Location:/?blacksmit');
exit;
}
else{
$__table = $db->prepare('INSERT INTO `table` SET `user` = ?, `item` = ?, `x` = ?, `y` = ?, `x2` = ?, `y2` = ?, `rotation` = ?, `time` = ?, `status` = ?');
$__table->execute([1,(int)$_GET['item'],(int)$_GET['x'],(int)$_GET['y'],$x2,$y2,($_GET['rotation'] ? (int)$_GET['rotation'] : 0), time(), 'ore']);
}
}
header('Location:/?blacksmit');
exit;
}
if(isset($_GET['sword'])){
$_slot = $db -> prepare('SELECT * FROM `table` WHERE `user` = ? AND `id` = ?');
$_slot -> execute([1, (int)$_GET['sword']]);
$slot = $_slot-> fetch();
echo $slot['item'];
$_item = $db -> prepare('SELECT * FROM `items` WHERE `id` = ?');
$_item -> execute([$slot['item']]);
$item = $_item-> fetch();
echo $item['id'];
if(!$slot){
header('Location:/');
exit;
}
if($slot['status'] == 'ore'){
echo '
До изготовления:'.$item['time'] - ($slot['time'] - time()).'<br />
Стадия: Руда
';
}
if($slot['status'] == 'sword'){
echo '
До изготовления:'.$item['time'] - ($slot['time'] - time()).'<br />
Стадия: Клинок
';
}
if($slot['status'] == 'complete'){
$_sword = $db -> prepare('SELECT * FROM `swords` WHERE `ore` = ? ORDER BY RAND() LIMIT 1');
$_sword -> execute([$item['ore']]);
$sword = $_sword-> fetch();
echo 'Вы выковали '.$sword['name'].'!!';
$__slot = $db->prepare('INSERT INTO `inventory` SET `user` = ?, `time` = ?, `item` = ?, `type` = ?, `count` = ?, `rang` = ?');
$__slot->execute([1, time(), $sword['id'], 'sword', '1', '1']);
$__slot = $db->prepare('DELETE FROM `table` WHERE `id` = ? LIMIT 1');
$__slot->execute([(int)$_GET['sword']]);
}
}
echo '
<br />
<map name="inventory">
';
foreach($inv as $slot){
$_item = $db -> prepare('SELECT * FROM `items` WHERE `id` = ?');
$_item -> execute([$slot['item']]);
$item = $_item -> fetch();
if(time() > ($slot['time'] + $item['time'])){
$__table = $db->prepare('UPDATE `table` SET `status` = ? WHERE `id` = ? LIMIT 1');
$__table->execute(['sword', $slot['id']]);
}
if(time() > ($slot['time'] + $item['complete'])){
$__table = $db->prepare('UPDATE `table` SET `status` = ? WHERE `id` = ? LIMIT 1');
$__table->execute(['complete', $slot['id']]);
}
echo $slot['rotation'] ? '
<area shape="rect" alt="" title="" coords="'.($slot['x'] * $dp).','.($slot['y'] * $dp).','.($item['height'] * $dp + $slot['x'] * $dp ).','.($item['width'] * $dp + $slot['y'] * $dp).'" href="?sword='.$slot['id'].'" target="" />
' : '
<area shape="rect" alt="" title="" coords="'.($slot['x'] * $dp).','.($slot['y'] * $dp).','.($item['width'] * $dp + $slot['x'] * $dp ).','.($item['height'] * $dp + $slot['y'] * $dp).'" href="?sword='.$slot['id'].'" target="" />
';
}
foreach($url as $slot){
echo $slot.'
';
}
echo '
</map>
<img usemap="inventory" src="?blacksmit" alt="inventory" />
';
?>
CREATE TABLE IF NOT EXISTS `inventory` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user` int(11) NOT NULL,
`item` int(11) NOT NULL,
`time` int(20) NOT NULL,
`type` varchar(11) NOT NULL DEFAULT 'sword',
`count` int(11) NOT NULL,
`rang` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=8 ;
INSERT INTO `inventory` (`id`, `user`, `item`, `time`, `type`, `count`, `rang`) VALUES
(1, 1, 2, 1420649143, 'sword', 1, 1),
(2, 1, 2, 1420649149, 'sword', 1, 1),
(3, 1, 2, 1420649150, 'sword', 1, 1),
(4, 1, 2, 1420649151, 'sword', 1, 1),
(5, 1, 1, 1420649672, 'sword', 1, 1),
(6, 1, 1, 1420649673, 'sword', 1, 1),
(7, 1, 1, 1420649673, 'sword', 1, 1);
CREATE TABLE IF NOT EXISTS `items` (
`id` int(20) NOT NULL AUTO_INCREMENT,
`name` varchar(55) NOT NULL,
`ore` varchar(55) NOT NULL,
`type` varchar(55) NOT NULL,
`image` varchar(55) NOT NULL,
`complete_image` varchar(55) NOT NULL,
`width` int(10) NOT NULL,
`height` int(10) NOT NULL,
`time` int(55) NOT NULL,
`complete` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=4 ;
INSERT INTO `items` (`id`, `name`, `ore`, `type`, `image`, `complete_image`, `width`, `height`, `time`, `complete`) VALUES
(1, 'Меч', '1.png', 'sword', 'swords/sword.png', '1.png', 1, 2, 60, 120),
(2, 'Резак', '2.png', 'sword', 'swords/rezak.png', '2.png', 1, 3, 60, 120),
(3, 'Двойной Клинок', '3.png', 'sword', 'swords/double.png', '3.png', 2, 2, 60, 120);
CREATE TABLE IF NOT EXISTS `swords` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(55) NOT NULL,
`img` varchar(55) NOT NULL,
`ore` varchar(55) NOT NULL,
`rang` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=5 ;
INSERT INTO `swords` (`id`, `name`, `img`, `ore`, `rang`) VALUES
(1, 'Обычный Меч', 'sword.png', '1.png', 1),
(2, 'Редкий Меч', 'sword.png', '1.png', 1),
(3, 'Чёткий Меч', 'sword.png', '2.png', 1),
(4, 'Чётенький Меч', 'sword.png', '3.png', 1);
CREATE TABLE IF NOT EXISTS `table` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`user` int(11) NOT NULL,
`item` int(11) NOT NULL,
`status` varchar(55) NOT NULL,
`time` int(15) NOT NULL,
`x` int(11) NOT NULL,
`y` int(11) NOT NULL,
`x2` int(11) NOT NULL,
`y2` int(11) NOT NULL,
`rotation` int(11) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=118 ;
INSERT INTO `table` (`id`, `user`, `item`, `status`, `time`, `x`, `y`, `x2`, `y2`, `rotation`) VALUES
(112, 1, 2, 'sword', 1420650301, 0, 0, 0, 2, 0),
(113, 1, 2, 'sword', 1420650312, 1, 0, 1, 2, 0),
(114, 1, 1, 'sword', 1420650339, 0, 3, 1, 3, 90),
(115, 1, 1, 'sword', 1420650341, 2, 3, 3, 3, 90),
(116, 1, 1, 'sword', 1420650342, 2, 2, 3, 2, 90),
(117, 1, 3, 'sword', 1420650348, 2, 0, 3, 1, 90);