We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
1 | <?php declare(strict_types=1); |
||
2 | |||
3 | use Smr\Request; |
||
4 | use Smr\TransactionType; |
||
5 | |||
6 | $session = Smr\Session::getInstance(); |
||
7 | $var = $session->getCurrentVar(); |
||
8 | $player = $session->getPlayer(); |
||
9 | $ship = $player->getShip(); |
||
10 | $sector = $player->getSector(); |
||
11 | |||
12 | $amount = Request::getVarInt('amount'); |
||
13 | // no negative amounts are allowed |
||
14 | if ($amount <= 0) { |
||
15 | create_error('You must enter an amount > 0!'); |
||
16 | } |
||
17 | |||
18 | $bargain_price = Request::getVarInt('bargain_price', 0); |
||
19 | // no negative amounts are allowed |
||
20 | if ($bargain_price < 0) { |
||
21 | create_error('Negative prices are not allowed!'); |
||
22 | } |
||
23 | |||
24 | /** @var int $good_id */ |
||
25 | $good_id = $var['good_id']; |
||
26 | $good_name = Globals::getGoodName($good_id); |
||
27 | |||
28 | // do we have enough turns? |
||
29 | if ($player->getTurns() == 0) { |
||
30 | create_error('You don\'t have enough turns to trade.'); |
||
31 | } |
||
32 | |||
33 | // get rid of those bugs when we die...there is no port at the home sector |
||
34 | if (!$sector->hasPort()) { |
||
35 | create_error('I can\'t see a port in this sector. Can you?'); |
||
36 | } |
||
37 | $port = $sector->getPort(); |
||
38 | |||
39 | // check if the player has the right relations to trade at the current port |
||
40 | if ($player->getRelation($port->getRaceID()) < RELATIONS_WAR) { |
||
41 | create_error('This port refuses to trade with you because you are at <span class="big bold red">WAR!</span>'); |
||
42 | } |
||
43 | |||
44 | // does the port actually buy or sell this good? |
||
45 | if (!$port->hasGood($good_id)) { |
||
46 | create_error('I don\'t trade in that good.'); |
||
47 | } |
||
48 | |||
49 | // check if there are enough left at port |
||
50 | if ($port->getGoodAmount($good_id) < $amount) { |
||
51 | create_error('I\'m short of ' . $good_name . '. So I\'m not going to sell you ' . $amount . ' pcs.'); |
||
52 | } |
||
53 | |||
54 | $transaction = $port->getGoodTransaction($good_id); |
||
55 | |||
56 | // does we have what we are going to sell? |
||
57 | if ($transaction === TransactionType::Sell && $amount > $ship->getCargo($good_id)) { |
||
58 | create_error('Scanning your ship indicates you don\'t have ' . $amount . ' pcs. of ' . $good_name . '!'); |
||
59 | } |
||
60 | |||
61 | // check if we have enough room for the thing we are going to buy |
||
62 | if ($transaction === TransactionType::Buy && $amount > $ship->getEmptyHolds()) { |
||
63 | create_error('Scanning your ship indicates you don\'t have enough free cargo bays!'); |
||
64 | } |
||
65 | |||
66 | // check if the guy has enough money |
||
67 | if ($transaction === TransactionType::Buy && $player->getCredits() < $bargain_price) { |
||
68 | create_error('You don\'t have enough credits!'); |
||
69 | } |
||
70 | |||
71 | // get relations for us (global + personal) |
||
72 | $relations = $player->getRelation($port->getRaceID()); |
||
73 | |||
74 | if (!isset($var['ideal_price'])) { |
||
75 | $ideal_price = $port->getIdealPrice($good_id, $transaction, $amount, $relations); |
||
76 | } else { |
||
77 | /** @var int $ideal_price */ |
||
78 | $ideal_price = $var['ideal_price']; |
||
79 | } |
||
80 | |||
81 | if (!isset($var['offered_price'])) { |
||
82 | $offered_price = $port->getOfferPrice($ideal_price, $relations, $transaction); |
||
83 | } else { |
||
84 | /** @var int $offered_price */ |
||
85 | $offered_price = $var['offered_price']; |
||
86 | } |
||
87 | |||
88 | // nothing should happen here but just to avoid / by 0 |
||
89 | if ($ideal_price == 0 || $offered_price == 0) { |
||
90 | create_error('Port calculation error...buy more goods.'); |
||
91 | } |
||
92 | |||
93 | $stealing = false; |
||
94 | if (Request::getVar('action') === TransactionType::STEAL) { |
||
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
95 | $stealing = true; |
||
96 | if (!$ship->isUnderground()) { |
||
97 | throw new Exception('Player tried to steal in a non-underground ship!'); |
||
98 | } |
||
99 | if ($transaction !== TransactionType::Buy) { |
||
100 | throw new Exception('Player tried to steal a good the port does not sell!'); |
||
101 | } |
||
102 | |||
103 | // Small chance to get caught stealing |
||
104 | $catchChancePercent = $port->getMaxLevel() - $port->getLevel() + 1; |
||
105 | if (rand(1, 100) <= $catchChancePercent) { |
||
106 | $fine = $ideal_price * ($port->getLevel() + 1); |
||
107 | // Don't take the trader all the way to 0 credits |
||
108 | $newCredits = max(5000, $player->getCredits() - $fine); |
||
109 | $player->setCredits($newCredits); |
||
110 | $player->decreaseAlignment(5); |
||
111 | $player->decreaseRelationsByTrade($amount, $port->getRaceID()); |
||
112 | |||
113 | $fineMessage = '<span class="red">A Federation patrol caught you loading stolen goods onto your ship!<br />The stolen goods have been confiscated and you have been fined ' . number_format($fine) . ' credits.</span>'; |
||
114 | $container = Page::create('shop_goods.php'); |
||
115 | $container['trade_msg'] = $fineMessage; |
||
116 | $container->go(); |
||
117 | } |
||
118 | } |
||
119 | |||
120 | // can we accept the current price? |
||
121 | if ($stealing || |
||
122 | (!empty($bargain_price) && |
||
123 | (($transaction === TransactionType::Buy && $bargain_price >= $ideal_price) || |
||
124 | ($transaction === TransactionType::Sell && $bargain_price <= $ideal_price)))) { |
||
125 | |||
126 | // base xp is the amount you would get for a perfect trade. |
||
127 | // this is the absolut max. the real xp can only be smaller. |
||
128 | $base_xp = SmrPort::getBaseExperience($amount, $port->getGoodDistance($good_id)); |
||
129 | |||
130 | // if offered equals ideal we get a problem (division by zero) |
||
131 | if ($stealing) { |
||
132 | $expPercent = 1; // stealing gives full exp |
||
133 | } else { |
||
134 | $expPercent = $port->calculateExperiencePercent($ideal_price, $bargain_price, $transaction); |
||
135 | } |
||
136 | $gained_exp = IRound($expPercent * $base_xp); |
||
137 | |||
138 | $portGood = Globals::getGood($good_id); |
||
139 | if ($stealing) { |
||
140 | $msg_transaction = 'stolen'; |
||
141 | $ship->increaseCargo($good_id, $amount); |
||
142 | $player->increaseHOF($amount, ['Trade', 'Goods', 'Stolen'], HOF_ALLIANCE); |
||
143 | $player->increaseHOF($gained_exp, ['Trade', 'Experience', 'Stealing'], HOF_PUBLIC); |
||
144 | $port->stealGoods($portGood, $amount); |
||
145 | } elseif ($transaction === TransactionType::Buy) { |
||
146 | $msg_transaction = 'bought'; |
||
147 | $ship->increaseCargo($good_id, $amount); |
||
148 | $player->decreaseCredits($bargain_price); |
||
149 | $player->increaseHOF($amount, ['Trade', 'Goods', 'Bought'], HOF_ALLIANCE); |
||
150 | $player->increaseHOF($gained_exp, ['Trade', 'Experience', 'Buying'], HOF_PUBLIC); |
||
151 | $player->decreaseHOF($bargain_price, ['Trade', 'Money', 'Profit'], HOF_PUBLIC); |
||
152 | $player->increaseHOF($bargain_price, ['Trade', 'Money', 'Buying'], HOF_PUBLIC); |
||
153 | $port->buyGoods($portGood, $amount, $ideal_price, $bargain_price, $gained_exp); |
||
154 | $player->increaseRelationsByTrade($amount, $port->getRaceID()); |
||
155 | } else { // $transaction === TransactionType::Sell |
||
156 | $msg_transaction = 'sold'; |
||
157 | $ship->decreaseCargo($good_id, $amount); |
||
158 | $player->increaseCredits($bargain_price); |
||
159 | $player->increaseHOF($amount, ['Trade', 'Goods', 'Sold'], HOF_ALLIANCE); |
||
160 | $player->increaseHOF($gained_exp, ['Trade', 'Experience', 'Selling'], HOF_PUBLIC); |
||
161 | $player->increaseHOF($bargain_price, ['Trade', 'Money', 'Profit'], HOF_PUBLIC); |
||
162 | $player->increaseHOF($bargain_price, ['Trade', 'Money', 'Selling'], HOF_PUBLIC); |
||
163 | $port->sellGoods($portGood, $amount, $gained_exp); |
||
164 | $player->increaseRelationsByTrade($amount, $port->getRaceID()); |
||
165 | } |
||
166 | |||
167 | $player->increaseHOF($gained_exp, ['Trade', 'Experience', 'Total'], HOF_PUBLIC); |
||
168 | $player->increaseHOF(1, ['Trade', 'Results', 'Success'], HOF_PUBLIC); |
||
169 | |||
170 | // log action |
||
171 | $logAction = $stealing ? TransactionType::STEAL : $transaction->value; |
||
172 | $player->log(LOG_TYPE_TRADING, $logAction . 's ' . $amount . ' ' . $good_name . ' for ' . $bargain_price . ' credits and ' . $gained_exp . ' experience'); |
||
173 | |||
174 | $player->increaseExperience($gained_exp); |
||
175 | |||
176 | //will use these variables in current sector and port after successful trade |
||
177 | $tradeMessage = 'You have just ' . $msg_transaction . ' <span class="yellow">' . $amount . '</span> ' . pluralise($amount, 'unit', false) . ' of <span class="yellow">' . $good_name . '</span>'; |
||
178 | if ($bargain_price > 0) { |
||
179 | $tradeMessage .= ' for <span class="creds">' . $bargain_price . '</span> ' . pluralise($bargain_price, 'credit', false) . '.'; |
||
180 | } |
||
181 | |||
182 | if ($gained_exp > 0) { |
||
183 | if ($stealing) { |
||
184 | $qualifier = 'cunning'; |
||
185 | } elseif ($gained_exp < $base_xp * 0.25) { |
||
186 | $qualifier = 'novice'; |
||
187 | } elseif ($gained_exp < $base_xp * 0.5) { |
||
188 | $qualifier = 'mediocre'; |
||
189 | } elseif ($gained_exp < $base_xp * 0.75) { |
||
190 | $qualifier = 'respectable'; |
||
191 | } elseif ($gained_exp < IRound($base_xp)) { |
||
192 | $qualifier = 'excellent'; |
||
193 | } else { |
||
194 | $qualifier = 'peerless'; |
||
195 | } |
||
196 | $skill = $stealing ? 'thievery' : 'trading'; |
||
197 | $tradeMessage .= '<br />Your ' . $qualifier . ' ' . $skill . ' skills have earned you <span class="exp">' . $gained_exp . ' </span> ' . pluralise($gained_exp, 'experience point', false) . '!'; |
||
198 | } |
||
199 | |||
200 | |||
201 | if ($ship->getEmptyHolds() == 0) { |
||
202 | $container = Page::create('current_sector.php'); |
||
203 | } else { |
||
204 | $container = Page::create('shop_goods.php'); |
||
205 | } |
||
206 | $container['trade_msg'] = $tradeMessage; |
||
207 | |||
208 | } else { |
||
209 | // does the trader try to outsmart us? |
||
210 | $container = Page::create('shop_goods_trade.php'); |
||
211 | |||
212 | require_once(LIB . 'Default/shop_goods.inc.php'); |
||
213 | check_bargain_number($amount, $ideal_price, $offered_price, $bargain_price, $container, $player); |
||
214 | |||
215 | // transfer values to next page |
||
216 | $container->addVar('good_id'); |
||
217 | |||
218 | $container['amount'] = $amount; |
||
219 | $container['bargain_price'] = $bargain_price; |
||
220 | } |
||
221 | |||
222 | $container['ideal_price'] = $ideal_price; |
||
223 | $container['offered_price'] = $offered_price; |
||
224 | |||
225 | // only take turns if they bargained |
||
226 | if (!isset($container['number_of_bargains']) || $container['number_of_bargains'] != 1) { |
||
227 | $player->takeTurns(TURNS_PER_TRADE, TURNS_PER_TRADE); |
||
228 | } |
||
229 | |||
230 | // go to next page |
||
231 | $container->go(); |
||
232 |