php ob_start class Table public db user protected dp 32 protected size

  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
175
176
177
178
179
180
181
182
183
<?php
ob_start();
class Table {
public $db, $user;
protected $dp = 32;
protected $size = [4, 4];
protected $table = [];
public function __construct($db, $user) {
$this->db = $db;
$_user = $this->db->prepare('SELECT * FROM `user` WHERE `id` = ?');
$_user -> execute([1]);
$this->user = $_user -> fetch();
}
public function fill() {
for ($x = 0; $x < $this->size[0]; ++$x) {
for ($y = 0; $y < $this->size[1]; ++$y) {
$table = '
<span style="width: %d; height: %d; background: url(%s) %dpx %dpx;">
%s
</span>
';
if ($slot = $this->find($x, $y)) {
$_item = $this->db->prepare('SELECT * FROM `items` where `id` = ? LIMIT 1');
$_item -> execute([$slot['item']]);
$item = $_item -> fetch();
if(time() > ($slot['time'] + $item['time'])){
$__table = $this->db->prepare('UPDATE `table` SET `status` = ? WHERE `id` = ? LIMIT 1');
$__table->execute(['sword', $slot['id']]);
}
if(time() > ($slot['time'] + $item['complete'])){
$__table = $this->db->prepare('UPDATE `table` SET `status` = ? WHERE `id` = ? LIMIT 1');
$__table->execute(['complete', $slot['id']]);
}
$background = '?i=preview&img='.$item['image'].'&rotation='.($slot['rotation'] ? $slot['rotation'] : 0);
$shiftX = ($x - ($slot['rotation'] == '180' ? $slot['x2'] : $slot['x'])) * $this->dp;
$shiftY = ($y - ($slot['rotation'] == '90' ? $slot['y2'] : $slot['y'])) * $this->dp;
$a = '<a style="display:block; padding:32px;" href="?i=sword&sword='.$slot['id'].'"></a>';
$this->table[$y][$x] = sprintf($table, $this->dp, $this->dp, $background, $shiftX, $shiftY, $a);
}
else{
if($_GET['i'] == 'ore' && isset($_GET['item'])) $a2 = '<a style="display:block; padding:32px;" href="?i=ore&x='.$x.'&y='.$y.'&item='.(int)$_GET['item'].'"></a>';
$this->table[$y][$x] = sprintf($table, $this->dp, $this->dp, 'images/slot.png', 0, 0, $a2);
}
}
}
}
public function show() {
$this->fill();
for ($y = 0; $y < $this->size[1]; ++$y) {
echo '<div>';
for ($x = 0; $x < $this->size[0]; ++$x) {
echo $this->table[$y][$x];
}
echo '</div>';
}
}
public function find($x, $y) {
$_check = $this->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]);
return $_check->fetch();
}
}
$Table = new Table(new PDO('mysql:host=127.0.0.1;dbname=sword;charset=UTF8', 'root', ''), 1);
$user = $Table->user;
switch($_GET['i']){
case 'sword':
$_slot = $Table->db->prepare('SELECT * FROM `table` WHERE `user` = ? AND `id` = ?');
$_slot -> execute([$user['id'], (int)$_GET['sword']]);
$slot = $_slot-> fetch();
$_item = $Table->db->prepare('SELECT * FROM `items` WHERE `id` = ?');
$_item -> execute([$slot['item']]);
$item = $_item-> fetch();
if(!$slot){
header('Location:/');
exit;
}
if($slot['status'] == 'ore'){
echo '
До стадии клинка:'.($item['time'] + ($slot['time'] - time())).' сек
До изготовления:'.($item['complete'] + ($slot['time'] - time())).' сек
<br />
Стадия: Руда
';
}
if($slot['status'] == 'sword'){
echo '
До изготовления:'.($item['complete'] + ($slot['time'] - time())).' сек
<br />
Стадия: Клинок
';
}
if($slot['status'] == 'complete'){
$_sword = $Table->db->prepare('SELECT * FROM `swords` WHERE `ore` = ? ORDER BY RAND() LIMIT 1');
$_sword -> execute([$item['ore']]);
$sword = $_sword-> fetch();
echo 'Вы выковали '.$sword['name'].'!!';
$__slot = $Table->db->prepare('INSERT INTO `inventory` SET `user` = ?, `time` = ?, `item` = ?, `type` = ?, `count` = ?, `rang` = ?');
$__slot->execute([$user['id'], time(), $sword['id'], 'sword', '1', '1']);
$__slot = $Table->db->prepare('DELETE FROM `table` WHERE `id` = ? LIMIT 1');
$__slot->execute([(int)$_GET['sword']]);
}
default:
echo'
<style>
span {
display: block;
float: left;
}
div {
overflow: hidden;
}
</style>
';
$Table->show();
break;
case 'preview':
header('Content-Type: image/png');
$dp = 4;
$image = imagecreatefrompng('images/swords/'.$_GET['img'].'.png');
list($width, $height) = getimagesize('images/swords/'.$_GET['img'].'.png');
if(in_array($_GET['rotation'], [90,270])){
$image = imagerotate($image, $_GET['rotation'], 0);
$image2 = imagecreate ($height * $dp, $width * $dp);
imagecopyresampled($image2, $image, 0, 0, 0, 0, $height * $dp, $width * $dp , $height, $width);
}
elseif($_GET['rotation'] == 180){
$image = imagerotate($image, $_GET['rotation'], 0);
$image2 = imagecreate ($width * $dp, $height * $dp);
imagecopyresampled($image2, $image, 0, 0, 0, 0, $width * $dp, $height * $dp , $width, $height);
}
else{
$image2 = imagecreate ($width * $dp, $height * $dp);
imagecopyresampled($image2, $image, 0, 0, 0, 0, $width * $dp, $height * $dp , $width, $height);
}
imagepng($image2);
exit;
case 'ore':
// ?x='.$x.'&y='.$y.'&item='.$_GET['ore'].'&rotation='.$user['last_rotation'].'
if(isset($_GET['x']) and isset($_GET['y']) and isset($_GET['item'])){
$_items = $Table->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 = $Table->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;
}
else{
$__table = $Table->db->prepare('INSERT INTO `table` SET `user` = ?, `item` = ?, `x` = ?, `y` = ?, `x2` = ?, `y2` = ?, `rotation` = ?, `time` = ?, `status` = ?');
$__table->execute([$user['id'],(int)$_GET['item'],(int)$_GET['x'],(int)$_GET['y'],$x2,$y2,($_GET['rotation'] ? (int)$_GET['rotation'] : 0), time(), 'ore']);
}
}
header('Location:/?blacksmit');
exit;
}
echo'
<style>
span {
display: block;
float: left;
}
div {
overflow: hidden;
}
</style>
';
$Table->show();
break;
}