|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @version 2015-04-11 11:47:49 39b14.2 |
|
5
|
|
|
* @copyright 2008-2015 Gorlum for Project "SuperNova.WS" |
|
6
|
|
|
*/ |
|
7
|
|
|
|
|
8
|
|
|
defined('INSIDE') || die(); |
|
9
|
|
|
|
|
10
|
|
|
require_once('db/db_queries.php'); |
|
11
|
|
|
|
|
12
|
|
|
function db_change_units_perform($query, $tablename, $object_id) { |
|
13
|
|
|
$query = implode(',', $query); |
|
14
|
|
|
if($query && $object_id) { |
|
15
|
|
|
return classSupernova::db_upd_record_by_id($tablename == 'users' ? LOC_USER : LOC_PLANET, $object_id, $query); |
|
16
|
|
|
// return doquery("UPDATE {{{$tablename}}} SET {$query} WHERE `id` = '{$object_id}' LIMIT 1;"); |
|
|
|
|
|
|
17
|
|
|
} |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
// TODO: THIS FUNCTION IS OBSOLETE AND SHOULD BE REPLACED! |
|
21
|
|
|
// TODO - ТОЛЬКО ДЛЯ РЕСУРСОВ |
|
22
|
|
|
// $unit_list should have unique entrances! Recompress non-uniq entrances before pass param! |
|
23
|
|
|
function db_change_units(&$user, &$planet, $unit_list = array(), $query = null) { |
|
24
|
|
|
$query = is_array($query) ? $query : array( |
|
25
|
|
|
LOC_USER => array(), |
|
26
|
|
|
LOC_PLANET => array(), |
|
27
|
|
|
); |
|
28
|
|
|
|
|
29
|
|
|
$group = sn_get_groups('resources_loot'); |
|
30
|
|
|
|
|
31
|
|
|
foreach($unit_list as $unit_id => $unit_amount) { |
|
32
|
|
|
if(!in_array($unit_id, $group)) { |
|
33
|
|
|
// TODO - remove later |
|
34
|
|
|
print('<h1>СООБЩИТЕ ЭТО АДМИНУ: db_change_units() вызван для не-ресурсов!</h1>'); |
|
35
|
|
|
pdump(debug_backtrace()); |
|
36
|
|
|
die('db_change_units() вызван для не-ресурсов!'); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
if(!$unit_amount) { |
|
40
|
|
|
continue; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
$unit_db_name = pname_resource_name($unit_id); |
|
44
|
|
|
|
|
45
|
|
|
$unit_location = sys_get_unit_location($user, $planet, $unit_id); |
|
46
|
|
|
|
|
47
|
|
|
// Changing value in object |
|
48
|
|
|
switch($unit_location) { |
|
49
|
|
|
case LOC_USER: |
|
50
|
|
|
$user[$unit_db_name] += $unit_amount; |
|
51
|
|
|
break; |
|
52
|
|
|
case LOC_PLANET: |
|
53
|
|
|
$planet[$unit_db_name] += $unit_amount; |
|
54
|
|
|
break; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
$unit_amount = $unit_amount < 0 ? $unit_amount : "+{$unit_amount}"; // Converting positive unit_amount to string '+unit_amount' |
|
58
|
|
|
$query[$unit_location][$unit_id] = "`{$unit_db_name}`=`{$unit_db_name}`{$unit_amount}"; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
db_change_units_perform($query[LOC_USER], 'users', $user['id']); |
|
62
|
|
|
db_change_units_perform($query[LOC_PLANET], 'planets', $planet['id']); |
|
63
|
|
|
} |
|
64
|
|
|
function sn_db_perform($table, $values, $type = 'insert', $options = false) { |
|
65
|
|
|
$mass_perform = false; |
|
|
|
|
|
|
66
|
|
|
|
|
67
|
|
|
$field_set = ''; |
|
68
|
|
|
$value_set = ''; |
|
|
|
|
|
|
69
|
|
|
|
|
70
|
|
|
switch($type) { |
|
71
|
|
|
case 'delete': |
|
72
|
|
|
$query = 'DELETE FROM'; |
|
73
|
|
|
break; |
|
74
|
|
|
|
|
75
|
|
|
case 'insert': |
|
76
|
|
|
$query = 'INSERT INTO'; |
|
77
|
|
|
if(isset($options['__multi'])) { |
|
78
|
|
|
// Here we generate mass-insert set |
|
79
|
|
|
break; |
|
80
|
|
|
} |
|
81
|
|
|
case 'update': |
|
82
|
|
|
if(!$query) { |
|
|
|
|
|
|
83
|
|
|
$query = 'UPDATE'; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
foreach($values as $field => &$value) { |
|
87
|
|
|
$value_type = gettype($value); |
|
88
|
|
|
if ($value_type == 'string') { |
|
89
|
|
|
$value = "'" . db_escape($value) . "'"; |
|
90
|
|
|
} |
|
91
|
|
|
$value = "`{$field}` = {$value}"; |
|
92
|
|
|
} |
|
93
|
|
|
$field_set = 'SET ' . implode(', ', $values); |
|
94
|
|
|
break; |
|
95
|
|
|
|
|
96
|
|
|
}; |
|
97
|
|
|
|
|
98
|
|
|
$query .= " {$table} {$field_set}"; |
|
99
|
|
|
return doquery($query); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
|
|
103
|
|
|
|
|
104
|
|
|
function sn_db_field_set_is_safe(&$field_set) { |
|
105
|
|
|
return !empty($field_set['__IS_SAFE']); |
|
106
|
|
|
} |
|
107
|
|
|
function sn_db_field_set_safe_flag_clear(&$field_set) { |
|
108
|
|
|
unset($field_set['__IS_SAFE']); |
|
109
|
|
|
} |
|
110
|
|
|
function sn_db_field_set_safe_flag_set(&$field_set) { |
|
111
|
|
|
$field_set['__IS_SAFE'] = true; |
|
112
|
|
|
} |
|
113
|
|
|
function sn_db_field_set_make_safe($field_set, $serialize = false) { |
|
114
|
|
|
if(!is_array($field_set)) { |
|
115
|
|
|
die('$field_set is not an array!'); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
$result = array(); |
|
119
|
|
|
foreach($field_set as $field => $value) { |
|
120
|
|
|
$field = db_escape(trim($field)); |
|
121
|
|
|
switch (true) { |
|
122
|
|
|
case is_int($value): |
|
123
|
|
|
case is_double($value): |
|
124
|
|
|
break; |
|
125
|
|
|
|
|
126
|
|
|
case is_bool($value): |
|
127
|
|
|
$value = intval($value); |
|
128
|
|
|
break; |
|
129
|
|
|
|
|
130
|
|
|
case is_array($value): |
|
131
|
|
|
case is_object($value): |
|
132
|
|
|
$serialize ? $value = serialize($value) : die('$value is object or array with no $serialize'); |
|
133
|
|
|
|
|
134
|
|
|
case is_string($value): |
|
135
|
|
|
$value = '"' . db_escape($value) . '"'; |
|
136
|
|
|
break; |
|
137
|
|
|
|
|
138
|
|
|
case is_null($value): |
|
139
|
|
|
$value = 'NULL'; |
|
140
|
|
|
break; |
|
141
|
|
|
|
|
142
|
|
|
default: |
|
143
|
|
|
die('unsupported operand type'); |
|
144
|
|
|
} |
|
145
|
|
|
$result[$field] = $value; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
sn_db_field_set_safe_flag_set($field_set); |
|
149
|
|
|
|
|
150
|
|
|
return $result; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
|
|
154
|
|
|
function sn_db_unit_changeset_prepare($unit_id, $unit_value, $user, $planet_id = null) { |
|
155
|
|
|
return classSupernova::db_changeset_prepare_unit($unit_id, $unit_value, $user, $planet_id); |
|
156
|
|
|
} |
|
157
|
|
|
/** |
|
158
|
|
|
* Функция проверяет статус транзакции |
|
159
|
|
|
* |
|
160
|
|
|
* @param null|true|false $status Должна ли быть запущена транзакция в момент проверки |
|
|
|
|
|
|
161
|
|
|
* <p>null - транзакция НЕ должна быть запущена</p> |
|
162
|
|
|
* <p>true - транзакция должна быть запущена - для совместимости с $for_update</p> |
|
163
|
|
|
* <p>false - всё равно - для совместимости с $for_update</p> |
|
164
|
|
|
* @return bool Текущий статус транзакции |
|
165
|
|
|
*/ |
|
166
|
|
|
function sn_db_transaction_check($transaction_should_be_started = null) { |
|
167
|
|
|
return classSupernova::db_transaction_check($transaction_should_be_started); |
|
168
|
|
|
} |
|
169
|
|
|
function sn_db_transaction_start($level = '') { |
|
170
|
|
|
return classSupernova::db_transaction_start($level); |
|
171
|
|
|
} |
|
172
|
|
|
function sn_db_transaction_commit() { |
|
173
|
|
|
return classSupernova::db_transaction_commit(); |
|
174
|
|
|
} |
|
175
|
|
|
function sn_db_transaction_rollback() { |
|
176
|
|
|
return classSupernova::db_transaction_rollback(); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
|
|
180
|
|
|
|
|
181
|
|
|
|
|
182
|
|
|
function doquery($query, $table = '', $fetch = false, $skip_query_check = false) { |
|
183
|
|
|
return classSupernova::$db->doquery($query, $table, $fetch, $skip_query_check); |
|
184
|
|
|
} |
|
185
|
|
|
function db_fetch(&$query) { |
|
186
|
|
|
return classSupernova::$db->db_fetch($query); |
|
187
|
|
|
} |
|
188
|
|
|
function db_escape($unescaped_string) { |
|
189
|
|
|
return classSupernova::$db->db_escape($unescaped_string); |
|
190
|
|
|
} |
|
191
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.