Scrutinizer GitHub App not installed

We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.

Install GitHub App

Failed Conditions
Pull Request — main (#1494)
by Dan
10:59 queued 05:29
created

AllianceBank::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 0
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php declare(strict_types=1);
2
3
namespace Smr\Pages\Player\Bank;
4
5
use AbstractSmrPlayer;
6
use Menu;
7
use Smr\Database;
8
use Smr\Epoch;
9
use Smr\Page\PlayerPage;
10
use Smr\Session;
11
use Smr\Template;
12
use SmrAlliance;
13
use SmrPlayer;
14
15
class AllianceBank extends PlayerPage {
16
17
	public string $file = 'bank_alliance.php';
18
19
	public function __construct(
20
		private readonly int $allianceID,
21
	) {}
22
23
	public function build(AbstractSmrPlayer $player, Template $template): void {
24
		$session = Session::getInstance();
25
26
		// is account validated?
27
		if (!$player->getAccount()->isValidated()) {
28
			create_error('You are not validated so you cannot use banks.');
29
		}
30
31
		$allianceID = $this->allianceID;
32
33
		$alliance = SmrAlliance::getAlliance($allianceID, $player->getGameID());
34
		$template->assign('PageTopic', 'Bank');
35
36
		Menu::bank();
37
38
		$db = Database::getInstance();
39
		$dbResult = $db->read('SELECT * FROM alliance_treaties WHERE game_id = ' . $db->escapeNumber($player->getGameID()) . '
40
					AND (alliance_id_1 = ' . $db->escapeNumber($player->getAllianceID()) . ' OR alliance_id_2 = ' . $db->escapeNumber($player->getAllianceID()) . ')
41
					AND aa_access = 1 AND official = \'TRUE\'');
42
		$alliedAllianceBanks = [];
43
		foreach ($dbResult->records() as $dbRecord) {
44
			$alliedAllianceBanks[$dbRecord->getInt('alliance_id_2')] = SmrAlliance::getAlliance($dbRecord->getInt('alliance_id_2'), $alliance->getGameID());
45
			$alliedAllianceBanks[$dbRecord->getInt('alliance_id_1')] = SmrAlliance::getAlliance($dbRecord->getInt('alliance_id_1'), $alliance->getGameID());
46
		}
47
		$template->assign('AlliedAllianceBanks', $alliedAllianceBanks);
48
49
		$dbResult = $db->read('SELECT transaction, sum(amount) as total FROM alliance_bank_transactions
50
					WHERE alliance_id = ' . $db->escapeNumber($alliance->getAllianceID()) . ' AND game_id = ' . $db->escapeNumber($alliance->getGameID()) . ' AND payee_id = ' . $db->escapeNumber($player->getAccountID()) . '
51
					GROUP BY transaction');
52
		$playerTrans = ['Deposit' => 0, 'Payment' => 0];
53
		foreach ($dbResult->records() as $dbRecord) {
54
			$playerTrans[$dbRecord->getString('transaction')] = $dbRecord->getInt('total');
55
		}
56
57
		if ($alliance->getAllianceID() == $player->getAllianceID()) {
58
			$role_id = $player->getAllianceRole($alliance->getAllianceID());
59
			$query = 'role_id = ' . $db->escapeNumber($role_id);
60
		} else {
61
			$query = 'role = ' . $db->escapeString($player->getAlliance()->getAllianceName());
62
		}
63
64
		$dbResult = $db->read('SELECT * FROM alliance_has_roles WHERE alliance_id = ' . $db->escapeNumber($alliance->getAllianceID()) . ' AND game_id = ' . $db->escapeNumber($alliance->getGameID()) . ' AND ' . $query);
65
		$dbRecord = $dbResult->record();
66
		$template->assign('CanExempt', $dbRecord->getBoolean('exempt_with'));
67
		$withdrawalPerDay = $dbRecord->getInt('with_per_day');
68
69
		if ($dbRecord->getBoolean('positive_balance')) {
70
			$template->assign('PositiveWithdrawal', $withdrawalPerDay + $playerTrans['Deposit'] - $playerTrans['Payment']);
71
		} elseif ($withdrawalPerDay == ALLIANCE_BANK_UNLIMITED) {
72
			$template->assign('UnlimitedWithdrawal', true);
73
		} else {
74
			$dbResult = $db->read('SELECT IFNULL(sum(amount), 0) as total FROM alliance_bank_transactions WHERE alliance_id = ' . $db->escapeNumber($alliance->getAllianceID()) . ' AND game_id = ' . $db->escapeNumber($alliance->getGameID()) . '
75
						AND payee_id = ' . $db->escapeNumber($player->getAccountID()) . ' AND transaction = \'Payment\' AND exempt = 0 AND time > ' . $db->escapeNumber(Epoch::time() - 86400));
76
			$totalWithdrawn = $dbResult->record()->getInt('total');
77
			$template->assign('WithdrawalPerDay', $withdrawalPerDay);
78
			$template->assign('RemainingWithdrawal', $withdrawalPerDay - $totalWithdrawn);
79
			$template->assign('TotalWithdrawn', $totalWithdrawn);
80
		}
81
82
		$maxValue = $session->getRequestVarInt('maxValue', 0);
83
		$minValue = $session->getRequestVarInt('minValue', 0);
84
85
		if ($maxValue <= 0) {
86
			$dbResult = $db->read('SELECT IFNULL(MAX(transaction_id), 0) as max_transaction_id FROM alliance_bank_transactions
87
						WHERE game_id=' . $db->escapeNumber($alliance->getGameID()) . '
88
						AND alliance_id=' . $db->escapeNumber($alliance->getAllianceID()));
89
			$maxValue = $dbResult->record()->getInt('max_transaction_id');
90
		}
91
92
		if ($minValue <= 0 || $minValue > $maxValue) {
93
			$minValue = max(1, $maxValue - 5);
94
		}
95
96
		$query = 'SELECT time, transaction_id, transaction, amount, exempt, reason, payee_id
97
			FROM alliance_bank_transactions
98
			WHERE game_id=' . $db->escapeNumber($alliance->getGameID()) . '
99
			AND alliance_id=' . $db->escapeNumber($alliance->getAllianceID());
100
101
		$query .= ' AND transaction_id>=' . $db->escapeNumber($minValue) . '
102
					AND transaction_id<=' . $db->escapeNumber($maxValue) . '
103
					ORDER BY time LIMIT ' . (1 + $maxValue - $minValue);
104
105
		$dbResult = $db->read($query);
106
107
		// only if we have at least one result
108
		if ($dbResult->hasRecord()) {
109
			$bankTransactions = [];
110
			foreach ($dbResult->records() as $dbRecord) {
111
				$trans = $dbRecord->getString('transaction');
112
				$bankTransactions[$dbRecord->getInt('transaction_id')] = [
113
					'Time' => $dbRecord->getInt('time'),
114
					'Player' => SmrPlayer::getPlayer($dbRecord->getInt('payee_id'), $player->getGameID()),
115
					'Reason' => $dbRecord->getString('reason'),
116
					'TransactionType' => $trans,
117
					'Withdrawal' => $trans == 'Payment' ? number_format($dbRecord->getInt('amount')) : '',
118
					'Deposit' => $trans == 'Deposit' ? number_format($dbRecord->getInt('amount')) : '',
119
					'Exempt' => $dbRecord->getInt('exempt') == 1,
120
				];
121
			}
122
			$template->assign('BankTransactions', $bankTransactions);
123
124
			$template->assign('MinValue', $minValue);
125
			$template->assign('MaxValue', $maxValue);
126
			$container = new self($allianceID);
127
			$template->assign('FilterTransactionsFormHREF', $container->href());
128
129
			$container = new AllianceBankExemptProcessor($minValue, $maxValue);
130
			$template->assign('ExemptTransactionsFormHREF', $container->href());
131
132
			$template->assign('Alliance', $alliance);
133
		}
134
135
		$container = new AllianceBankReport($allianceID);
136
		$template->assign('BankReportHREF', $container->href());
137
138
		$container = new AllianceBankProcessor($allianceID);
139
		$template->assign('BankTransactionFormHREF', $container->href());
140
	}
141
142
}
143