Issues (1369)

classes/Alliance/Alliance.php (7 issues)

1
<?php
2
/**
3
 * Created by Gorlum 28.11.2017 6:01
4
 */
5
6
namespace Alliance;
7
8
use Core\GlobalContainer;
9
use DBAL\db_mysql;
10
use \Exception;
0 ignored issues
show
The type \Exception was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
11
use \HelperString;
0 ignored issues
show
The type \HelperString was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
12
use Player\RecordPlayer;
13
use SN;
14
15
/**
16
 * Class Alliance
17
 *
18
 * Implements Alliance entity
19
 *
20
 * @package Alliance
21
 */
22
class Alliance extends RecordAlliance {
23
  const OWNER_INDEX = -1;
24
  const DEFAULT_INDEX = 0;
25
  const RIGHTS_ALL = [
26
    0 => 'name',
27
    1 => 'mail',
28
    2 => 'online',
29
    3 => 'invite',
30
    4 => 'kick',
31
    5 => 'admin',
32
    6 => 'forum',
33
    7 => 'diplomacy'
34
  ];
35
  const RIGHT_WEIGHTS = [
36
    'mail'      => 3,
37
    'online'    => 4,
38
    'invite'    => 1,
39
    'kick'      => 10,
40
    'admin'     => 99,
41
    'forum'     => 0,
42
    'diplomacy' => 5,
43
  ];
44
45
46
  /**
47
   * @var AllianceTitleList $titles
48
   */
49
  protected $titles;
50
  /**
51
   * @var AllianceMemberList $members
52
   */
53
  protected $members;
54
55
  /**
56
   * @param array $ally
57
   *
58
   * @return array
59
   *
60
   * @deprecated
61
   */
62
  public static function ally_get_ranks(&$ally) {
63
    global $ally_rights;
64
65
    $ranks = array();
66
67
    if ($ally['ranklist']) {
68
      $str_ranks = explode(';', $ally['ranklist']);
69
      foreach ($str_ranks as $str_rank) {
70
        if (!$str_rank) {
71
          continue;
72
        }
73
74
        $tmp     = explode(',', $str_rank);
75
        $rank_id = count($ranks);
76
        foreach ($ally_rights as $key => $value) {
77
          $ranks[$rank_id][$value] = $tmp[$key];
78
        }
79
      }
80
    }
81
82
    return $ranks;
83
  }
84
85
  /**
86
   * @param array $user
87
   *
88
   * @deprecated
89
   */
90
  public static function sn_ali_fill_user_ally(&$user) {
91
    if (!$user['ally_id']) {
92
      return;
93
    }
94
95
    if (!isset($user['ally'])) {
96
      $user['ally'] = doquery("SELECT * FROM {{alliance}} WHERE `id` = {$user['ally_id']} LIMIT 1;", true);
0 ignored issues
show
Deprecated Code introduced by
The function doquery() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

96
      $user['ally'] = /** @scrutinizer ignore-deprecated */ doquery("SELECT * FROM {{alliance}} WHERE `id` = {$user['ally_id']} LIMIT 1;", true);
Loading history...
true of type true is incompatible with the type string expected by parameter $table of doquery(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

96
      $user['ally'] = doquery("SELECT * FROM {{alliance}} WHERE `id` = {$user['ally_id']} LIMIT 1;", /** @scrutinizer ignore-type */ true);
Loading history...
97
    }
98
99
    if (!isset($user['ally']['player'])) {
100
      $user['ally']['player'] = db_user_by_id($user['ally']['ally_user_id'], true, false);
0 ignored issues
show
Deprecated Code introduced by
The function db_user_by_id() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

100
      $user['ally']['player'] = /** @scrutinizer ignore-deprecated */ db_user_by_id($user['ally']['ally_user_id'], true, false);
Loading history...
101
    }
102
  }
103
104
105
  /**
106
   * Alliance constructor.
107
   *
108
   * @param GlobalContainer|null $services
109
   */
110
  public function __construct(GlobalContainer $services = null) {
111
    parent::__construct($services);
112
  }
113
114
  /**
115
   * @param array $properties
116
   */
117
  protected function fromProperties(array $properties) {
118
    parent::fromProperties($properties);
119
120
    $this->titles = new AllianceTitleList($this);
121
  }
122
123
  /**
124
   * @return AllianceMemberList
125
   */
126
  public function getMemberList() {
127
    if (!isset($this->members)) {
128
      $this->members = new AllianceMemberList(static::$db, $this);
129
    }
130
131
    return $this->members;
132
  }
133
134
  /**
135
   * List of titles
136
   *
137
   * @return AllianceTitleList
138
   */
139
  public function getTitleList() {
140
    return $this->titles;
141
  }
142
143
  /**
144
   * Pass alliance to a member
145
   *
146
   * @param AllianceMember $newOwnerMember
147
   *
148
   * @return bool
149
   * @throws Exception
150
   */
151
  public function pass(AllianceMember $newOwnerMember) {
152
    try {
153
      db_mysql::db_transaction_start();
154
155
      if ($newOwnerMember->isOwner()) {
156
        throw new Exception('{ Указанный пользователь уже является владельцем указанного Альянса }', ERR_NOTICE);
157
      }
158
159
      if (!empty($oldOwnerMember = $this->members->getOwner())) {
160
        if (!$oldOwnerMember->changeTitle($this->titles->getTitle(static::DEFAULT_INDEX))) {
161
          throw new Exception('{ Ошибка изменения ранга у старого владельца }', ERR_ERROR);
162
        }
163
      }
164
165
      if (!$newOwnerMember->changeTitle($this->titles->getTitle(static::OWNER_INDEX))) {
166
        throw new Exception('{ Ошибка изменения ранга у нового владельца }', ERR_ERROR);
167
      }
168
169
      $this->ownerId = $newOwnerMember->getPlayerId();
170
      if (!$this->update()) {
171
        throw new Exception('{ Ошибка изменения владельца Альянса }', ERR_ERROR);
172
      }
173
174
      db_mysql::db_transaction_commit();
175
    } catch (Exception $e) {
176
      db_mysql::db_transaction_rollback();
177
178
      throw $e;
179
    }
180
181
    return true;
182
  }
183
184
  /**
185
   * Get Alliance owner
186
   *
187
   * @return AllianceMember|null
188
   */
189
  public function getOwner() {
190
    $player = RecordPlayer::findById($this->ownerId);
191
    $owner  = !empty($player) ? new AllianceMember($this, $player) : null;
192
193
    return $owner;
194
  }
195
196
  /**
197
   * @return array
198
   */
199
  public function asPtl() {
200
//    $ownerName = $this->getMemberList()->getOwner() instanceof AllianceMember ? $this->getMemberList()->getOwner()->getMemberName() : '';
201
202
    $owner     = $this->getOwner();
203
    $ownerName = $owner instanceof AllianceMember ? $owner->getMemberName() : '';
204
205
    return
206
      $this->ptlArray()
207
      + [
208
        '.'                  => [
209
          'title' => $this->titles->asPtl()
210
        ],
211
        'OWNER_NAME_SAFE'    => HelperString::htmlSafe($ownerName),
212
        'CREATED_TEXT'       => date(FMT_DATE_TIME_SQL, $this->createdUnixTime),
213
        'STAT_POINTS_TEXT'   => HelperString::numberFloorAndFormat($this->statPoints),
214
        'DESCRIPTION_HTML'   => AllianceHelper::formatText($this->description),
215
        'TEXT_INTERNAL_HTML' => AllianceHelper::formatText($this->textInternal),
216
      ];
217
  }
218
219
  /**
220
   * Get list of recommended alliances for player with specified player points
221
   *
222
   * @param $points
223
   *
224
   * @return \mysqli_result|null
225
   */
226
  public static function recommend($points) {
227
    $points = floatval($points);
228
229
    $rate = 5;
230
231
    $allies = doquery(
0 ignored issues
show
Deprecated Code introduced by
The function doquery() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

231
    $allies = /** @scrutinizer ignore-deprecated */ doquery(
Loading history...
232
      "SELECT * 
233
      FROM {{alliance}} 
234
      WHERE 
235
        ally_request_notallow != 1 
236
        AND ally_members > 1 
237
        AND total_points / ally_members >= {$points} / {$rate} 
238
        AND total_points / ally_members <= {$points} * {$rate} 
239
      ORDER BY abs(total_points / ally_members - {$points}) LIMIT 10;");
240
241
    return $allies;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $allies also could return the type array|boolean which is incompatible with the documented return type mysqli_result|null.
Loading history...
242
  }
243
244
}
245