Completed
Push — work-fleets ( f0ff6c...5ecb7d )
by SuperNova.WS
04:50
created

Player::dbLoad()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 2
1
<?php
2
3
/**
4
 * Class Player
5
 *
6
 * @property string name
7
 * @property int    authLevel
8
 * @property int    capitalPlanetId
9
 * @property float  statPointsTotal
10
 *
11
 */
12
class Player extends UnitContainer {
13
14
  /**
15
   * Type of this location
16
   *
17
   * @var int $locationType
18
   */
19
  public static $locationType = LOC_USER;
20
21
22
  /**
23
   * Returns location's player owner ID
24
   *
25
   * @return int
26
   */
27
  public function getPlayerOwnerId() {
28
    return $this->_dbId;
29
  }
30
31
32
  public function setLocatedAt($location) {
33
    $this->locatedAt = $this;
34
  }
35
36
37
38
39
40
41
42
43
44
  // Inherited from DBRow
45
  /**
46
   * Table name in DB
47
   *
48
   * @var string
49
   */
50
  protected static $_table = 'users';
51
  /**
52
   * Name of ID field in DB
53
   *
54
   * @var string
55
   */
56
  protected static $_dbIdFieldName = 'id';
57
  /**
58
   * DB_ROW to Class translation scheme
59
   *
60
   * @var array
61
   */
62
  protected static $_properties = array(
63
    'dbId'            => array(
64
      P_DB_FIELD => 'id',
65
//      P_FUNC_INPUT => 'floatval',
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

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.

Loading history...
66
    ),
67
    'name'            => array(
68
      P_DB_FIELD => 'username',
69
    ),
70
    'authLevel'       => array(
71
      P_DB_FIELD   => 'authlevel',
72
      P_FUNC_INPUT => 'intval',
73
    ),
74
    'statPointsTotal' => array(
75
      P_DB_FIELD   => 'total_points',
76
      P_FUNC_INPUT => 'floatval',
77
    ),
78
    'capitalPlanetId' => array(
79
      P_DB_FIELD => 'id_planet',
80
//      P_FUNC_INPUT => 'floatval',
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

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.

Loading history...
81
    ),
82
  );
83
84
85
  // Innate properties
86
  /**
87
   * @var Bonus $player_bonus
88
   */
89
  public $player_bonus = null;
90
  /**
91
   * @var array
92
   */
93
  protected $db_row = array();
94
95
  protected $_name = '';
96
  protected $_authLevel = AUTH_LEVEL_REGISTERED;
97
  protected $_capitalPlanetId = 0;
98
  protected $_statPointsTotal = 0;
99
100
//  public $avatar = 0;
0 ignored issues
show
Unused Code Comprehensibility introduced by
41% of this comment could be valid code. Did you maybe forget this after debugging?

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.

Loading history...
101
//  public $vacation = 0;
102
//  public $banaday = 0;
103
//  public $dark_matter = 0;
104
//  public $dark_matter_total = 0;
105
//  public $player_rpg_explore_xp = 0;
106
//  public $player_rpg_explore_level = 0;
107
//  public $ally_id = 0;
108
//  public $ally_tag = 0;
109
//  public $ally_name = 0;
110
//  public $ally_register_time = 0;
111
//  public $ally_rank_id = 0;
112
//  public $lvl_minier = 0;
113
//  public $xpminier = 0;
114
//  public $player_rpg_tech_xp = 0;
115
//  public $player_rpg_tech_level = 0;
116
//  public $lvl_raid = 0;
117
//  public $xpraid = 0;
118
//  public $raids = 0;
119
//  public $raidsloose = 0;
120
//  public $raidswin = 0;
121
//  public $new_message = 0;
122
//  public $mnl_alliance = 0;
123
//  public $mnl_joueur = 0;
124
//  public $mnl_attaque = 0;
125
//  public $mnl_spy = 0;
126
//  public $mnl_exploit = 0;
127
//  public $mnl_transport = 0;
128
//  public $mnl_expedition = 0;
129
//  public $mnl_buildlist = 0;
130
//  public $msg_admin = 0;
131
//  public $deltime = 0;
132
//  public $news_lastread = 0;
133
//  public $total_rank = 0;
134
//  public $password = 0;
135
//  public $salt = 0;
136
//  public $email = 0;
137
//  public $email_2 = 0;
138
//  public $lang = 0;
139
//  public $sign = 0;
140
//  public $galaxy = 0;
141
//  public $system = 0;
142
//  public $planet = 0;
143
//  public $current_planet = 0;
144
//  public $user_lastip = 0;
145
//  public $user_last_proxy = 0;
146
//  public $user_last_browser_id = 0;
147
//  public $register_time = 0;
148
//  public $onlinetime = 0;
149
//  public $que_processed = 0;
150
//  public $dpath = 0;
151
//  public $design = 0;
152
//  public $noipcheck = 0;
153
//  public $options = 0;
154
//  public $user_as_ally = 0;
155
//  public $metal = 0;
156
//  public $crystal = 0;
157
//  public $deuterium = 0;
158
//  public $user_birthday = 0;
159
//  public $user_birthday_celebrated = 0;
160
//  public $player_race = 0;
161
//  public $vacation_next = 0;
162
//  public $metamatter = 0;
163
//  public $metamatter_total = 0;
164
//  public $admin_protection = 0;
165
//  public $ip_int = 0;
166
//  public $user_bot = 0;
167
//  public $gender = 0;
168
//  public $immortal = 0;
169
//  public $parent_account_id = 0;
170
//  public $server_name = 0;
171
//  public $parent_account_global = 0;
172
173
174
  /**
175
   * Player constructor.
176
   */
177
  public function __construct() {
178
    parent::__construct();
179
    $this->locatedAt = $this;
180
    $this->player_bonus = new Bonus();
181
  }
182
183
  public function isEmpty() {
184
    return false;
185
  }
186
187
  /**
188
   * Loading object from DB by primary ID
189
   *
190
   * @param int  $dbId
191
   * @param bool $lockSkip
192
   *
193
   * @return
194
   */
195
  public function dbLoad($dbId, $lockSkip = false) {
196
//    parent::dbLoad($dbId, $lockSkip); // TODO: Uncomment when the stars be right
0 ignored issues
show
Unused Code Comprehensibility introduced by
67% of this comment could be valid code. Did you maybe forget this after debugging?

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.

Loading history...
197
    $this->db_row = db_user_by_id($dbId, true);
0 ignored issues
show
Documentation Bug introduced by
It seems like db_user_by_id($dbId, true) can also be of type false. However, the property $db_row is declared as type array. Maybe add an additional type check?

Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.

For example, imagine you have a variable $accountId that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to the id property of an instance of the Account class. This class holds a proper account, so the id value must no longer be false.

Either this assignment is in error or a type check should be added for that assignment.

class Id
{
    public $id;

    public function __construct($id)
    {
        $this->id = $id;
    }

}

class Account
{
    /** @var  Id $id */
    public $id;
}

$account_id = false;

if (starsAreRight()) {
    $account_id = new Id(42);
}

$account = new Account();
if ($account instanceof Id)
{
    $account->id = $account_id;
}
Loading history...
198
    // Парсим инфу и загружаем юниты
199
    $this->dbRowParse($this->db_row);
200
  }
201
202
  /**
203
   * Меняет активную планету игрока на столицу, если активная планета равна $captured_planet_id
204
   *
205
   * @param int $captured_planet_id
206
   *
207
   * @return array|bool|mysqli_result|null
208
   */
209
  public function db_user_change_active_planet_to_capital($captured_planet_id) {
210
    $user_id = $this->_dbId;
211
212
    return doquery("UPDATE {{users}} SET `current_planet` = `id_planet` WHERE `id` = {$user_id} AND `current_planet` = {$captured_planet_id};");
213
  }
214
215
  public function calcColonyCount() {
216
    return $this->db_row[UNIT_PLAYER_COLONIES_CURRENT] = isset($this->db_row[UNIT_PLAYER_COLONIES_CURRENT]) ? $this->db_row[UNIT_PLAYER_COLONIES_CURRENT] : max(0, db_planet_count_by_type($this->db_row['id']) - 1);
217
  }
218
219
  /**
220
   * @param int $astrotech
221
   *
222
   * @return int|mixed
223
   */
224
  public function calcColonyMaxCount($astrotech = -1) {
225
    if($astrotech == -1) {
226
      if(!isset($this->db_row[UNIT_PLAYER_COLONIES_MAX])) {
227
228
        $expeditions = get_player_max_expeditons($this->db_row);
229
        $astrotech = mrc_get_level($this->db_row, null, TECH_ASTROTECH);
230
        $colonies = $astrotech - $expeditions;
231
232
        $this->db_row[UNIT_PLAYER_COLONIES_MAX] = classSupernova::$config->player_max_colonies < 0 ? $colonies : min(classSupernova::$config->player_max_colonies, $colonies);
233
      }
234
235
      return $this->db_row[UNIT_PLAYER_COLONIES_MAX];
236 View Code Duplication
    } else {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
237
      $expeditions = get_player_max_expeditons($this->db_row, $astrotech);
238
      $colonies = $astrotech - $expeditions;
239
240
      return classSupernova::$config->player_max_colonies < 0 ? $colonies : min(classSupernova::$config->player_max_colonies, $colonies);
241
    }
242
  }
243
244
245
  /**
246
   * @return array
247
   */
248
  public function getDbRow() {
249
    return $this->db_row;
250
  }
251
252
253
  /**
254
   * @return string
255
   */
256
  protected function getName() {
257
    return $this->db_row['username'];
258
//    $player_name = $this->db_row['username'];
0 ignored issues
show
Unused Code Comprehensibility introduced by
53% of this comment could be valid code. Did you maybe forget this after debugging?

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.

Loading history...
259
//
260
//    return $html_encoded ? htmlentities($player_name, ENT_COMPAT, 'UTF-8') : $player_name;
261
  }
262
263
  /**
264
   * @param string $name
265
   */
266
  protected function setName($name) {
267
    $this->db_row['username'] = $name;
268
    $this->_name = $name;
269
  }
270
271
272
  /**
273
   * Lock all fields that belongs to operation
274
   *
275
   * @param int $dbId
276
   *
277
   * @return
278
   * param DBLock $dbRow - Object that accumulates locks
279
   *
280
   */
281
  public function dbGetLockById($dbId) {
282
    // TODO: Implement dbGetLockById() method.
283
  }
284
285
  public function dbRowParse($db_row) {
286
    parent::dbRowParse($db_row); // TODO: Change the autogenerated stub
287
288
    // Высчитываем бонусы
289
    $this->player_bonus->add_unit_by_snid(MRC_ADMIRAL, mrc_get_level($this->db_row, null, MRC_ADMIRAL));
290
    $this->player_bonus->add_unit_by_snid(TECH_WEAPON, mrc_get_level($this->db_row, null, TECH_WEAPON));
291
    $this->player_bonus->add_unit_by_snid(TECH_SHIELD, mrc_get_level($this->db_row, null, TECH_SHIELD));
292
    $this->player_bonus->add_unit_by_snid(TECH_ARMOR, mrc_get_level($this->db_row, null, TECH_ARMOR));
293
  }
294
295
}
296