Completed
Push — trunk ( 062c60...ebaef8 )
by SuperNova.WS
11:27 queued 05:24
created

Alliance::getOwner()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 5
ccs 0
cts 4
cp 0
crap 6
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created by Gorlum 28.11.2017 6:01
4
 */
5
6
namespace Alliance;
7
8
use Core\GlobalContainer;
9
use \Exception;
10
use \HelperString;
11
use Player\RecordPlayer;
12
13
/**
14
 * Class Alliance
15
 *
16
 * Implements Alliance entity
17
 *
18
 * @package Alliance
19
 */
20
class Alliance extends RecordAlliance {
21
  const OWNER_INDEX = -1;
22
  const DEFAULT_INDEX = 0;
23
  const RIGHTS_ALL = [
24
    0 => 'name',
25
    1 => 'mail',
26
    2 => 'online',
27
    3 => 'invite',
28
    4 => 'kick',
29
    5 => 'admin',
30
    6 => 'forum',
31
    7 => 'diplomacy'
32
  ];
33
  const RIGHT_WEIGHTS = [
34
    'mail'      => 3,
35
    'online'    => 4,
36
    'invite'    => 1,
37
    'kick'      => 10,
38
    'admin'     => 99,
39
    'forum'     => 0,
40
    'diplomacy' => 5,
41
  ];
42
43
44
  /**
45
   * @var AllianceTitleList $titles
46
   */
47
  protected $titles;
48
  /**
49
   * @var AllianceMemberList $members
50
   */
51
  protected $members;
52
53
  /**
54
   * @param array $ally
55
   *
56
   * @return array
57
   *
58
   * @deprecated
59
   */
60
  public static function ally_get_ranks(&$ally) {
61
    global $ally_rights;
62
63
    $ranks = array();
64
65
    if ($ally['ranklist']) {
66
      $str_ranks = explode(';', $ally['ranklist']);
67
      foreach ($str_ranks as $str_rank) {
68
        if (!$str_rank) {
69
          continue;
70
        }
71
72
        $tmp     = explode(',', $str_rank);
73
        $rank_id = count($ranks);
74
        foreach ($ally_rights as $key => $value) {
75
          $ranks[$rank_id][$value] = $tmp[$key];
76
        }
77
      }
78
    }
79
80
    return $ranks;
81
  }
82
83
  /**
84
   * @param array $user
85
   *
86
   * @deprecated
87
   */
88
  public static function sn_ali_fill_user_ally(&$user) {
89
    if (!$user['ally_id']) {
90
      return;
91
    }
92
93
    if (!isset($user['ally'])) {
94
      $user['ally'] = doquery("SELECT * FROM {{alliance}} WHERE `id` = {$user['ally_id']} LIMIT 1;", true);
95
    }
96
97
    if (!isset($user['ally']['player'])) {
98
      $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

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