php ob_start db new PDO mysql host localhost dbname sword root _items

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
<?php
ob_start();
$db = new PDO('mysql:host=localhost;dbname=sword', 'root', '');
$_items = $db -> prepare('SELECT * FROM `items`');
$_items -> execute();
$items = $_items -> fetchAll();
function check($currentX, $currentY, $x2, $y2){
$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;
}
/*
$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]
];
$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['image'])){
$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=2&rotation=0" target="" />';
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();
$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:/?image');
exit;
}
if($x2 > ($size[0] - 1) or $y2 > ($size[1] - 1)){
header('Location:/?image');
exit;
}
else{
$__table = $db->prepare('INSERT INTO `table` SET `user` = ?, `item` = ?, `x` = ?, `y` = ?, `x2` = ?, `y2` = ?, `rotation` = ?');
$__table->execute([1,(int)$_GET['item'],(int)$_GET['x'],(int)$_GET['y'],$x2,$y2,($_GET['rotation'] ? (int)$_GET['rotation'] : 0)]);
}
}
header('Location:/?image');
}
echo '
<br />
<map name="inventory">
';
foreach($inv as $slot){
$_item = $db -> prepare('SELECT * FROM `items` where `id` = ?');
$_item -> execute([$slot['item']]);
$item = $_item -> fetch();
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="?'.$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="?'.$slot['id'].'" target="" />
';
}
foreach($url as $slot){
echo $slot.'
';
}
echo '
</map>
<img usemap="inventory" src="?image" alt="inventory" />
';
?>