-
Notifications
You must be signed in to change notification settings - Fork 0
/
shopping.php
233 lines (158 loc) · 5.82 KB
/
shopping.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
<?php
include_once("statics.php");
include_once("class_definitions.php");
include_once("class_traits.php");
include_once("inventory.php");
include_once("adventuring.php");
class Shopping {
public $commands = [];
private function canAffordItem(&$charData, $gpCost) {
$affordable = $gpCost <= $charData->gold;
if ( !$affordable ) {
$goldMissing = $gpCost - $charData->gold;
echo "\"Sorry, looks like you'll need another $goldMissing GP for that!\"\n";
return false;
}
return true;
}
private function takeMoney($item, $shop, &$charData, $room, $inputString = null) {
$charData->gold -= $item->getCost($charData);
$output = !is_null($inputString) ? $inputString : "";
if ( !empty($shop->stock) ) {
$output .= "\"Aha! A fine purchase! Anything else for you today?\"\n";
}
else {
$output .= "\"You've cleaned me out!\", the shopkeeper says with a grin. It looks like this shop is closed.\n";
// Remove the shop.
unset($room->occupant);
StateManager::ChangeState($charData, GameStates::Adventuring);
}
echo $output;
}
public function getShopString($shop, $charData, $mapData) {
if ( empty($this->commands) ) {
$this->generateInputFragments($charData, $mapData, true);
}
$contents = "For sale: ";
foreach ( $this->commands as $fragment ) {
$token = $fragment->token;
if ( !$shop->isItemOrEquipment($token) ) {
continue;
}
list($quantity, $cost, $level) = $shop->getStockDetailsForItem($token, $charData);
// Item
if ( is_null($level) ) {
$contents .= "$quantity $fragment->displayString ($cost GP), ";
}
// Equipment
else {
$contents .= "Level $level $fragment->displayString ($cost GP), ";
}
}
$contents = rtrim($contents, ", ");
$contents .= " [You have $charData->gold GP]\n";
return $contents;
}
public function generateInputFragments(&$charData, &$mapData, $justItems = false) {
// To be in here, we need to have a shop at our current location.
$room = $mapData->map->GetRoom($mapData->playerX, $mapData->playerY);
$shop = $room->occupant;
if ( !$justItems ) {
$this->commands[] = new InputFragment("check stock", function($charData, $mapData) use ($shop) {
global $shopping;
$shopStr = $shopping->getShopString($shop, $charData, $mapData);
echo $shopStr;
});
}
if ( !$justItems ) {
$this->commands[] = new InputFragment("leave", function($charData, $mapData) {
echo "\"Maybe next time, eh?\", the shopkeeper drawls at you as you walk away.\n";
StateManager::ChangeState($charData, GameStates::Adventuring);
});
}
if ( !$justItems ) {
$this->commands[] = new InputFragment("equipment", function($charData, $mapData) {
global $adventuring;
$equipment = $adventuring->getEquippedItemsStr($charData);
echo $equipment . "\n";
});
}
foreach ( $shop->stock as $itemName => $quantity ) {
$item = findItem($itemName);
$this->commands[] = new InputFragment($itemName, function($charData, $mapData) use ($item, $itemName, $shop, $room) {
global $shopping;
if ( !$shopping->canAffordItem($charData, $item->getCost($charData)) ) {
return;
}
// Remove item from shop.
$shop->removeStockItem($itemName);
// Add item to player inventory, and deduct gold.
$inventory = lazyGetInventory($charData);
$inventory->addItem($item);
$shopping->takeMoney($item, $shop, $charData, $room);
});
}
foreach ( $shop->equipment as $equipment ) {
$this->commands[] = new InputFragment($equipment->name, function($charData, $mapData) use ($equipment, $shop, $room) {
global $shopping;
if ( !$shopping->canAffordItem($charData, $equipment->getCost($charData)) ) {
return;
}
$equipString = "You equip your new $equipment->name immediately";
$isBarbarian = strcasecmp($charData->class, "Barbarian") == 0;
if ( $equipment->type == ShopEquipment::Weapon ) {
$weapon1Better = $charData->weaponVal >= $equipment->level;
$weapon2Better = $charData->weapon2Val >= $equipment->level;
if ( ( !$isBarbarian && $weapon1Better ) || ( $isBarbarian && $weapon1Better && $weapon2Better ) ) {
echo "\"No point buying my wares if you've got better yourself!\"\n";
return;
}
// Bloody complicated Barbarians
if ( $isBarbarian ) {
// Going in the off-hand
if ( $weapon1Better ) {
$charData->weapon2 = $equipment->name;
$charData->weapon2Val = $equipment->level;
}
// Weapon 1 isn't better, so move weapon 1 to weapon 2.
else {
$equipString .= ", moving your $charData->weapon to your off-hand";
$charData->weapon2 = $charData->weapon;
$charData->weapon2Val = $charData->weaponVal;
$charData->weapon = $equipment->name;
$charData->weaponVal = $equipment->level;
}
}
else {
$charData->weapon = $equipment->name;
$charData->weaponVal = $equipment->level;
}
}
else if ( $equipment->type == ShopEquipment::Armour ) {
$armourBetter = $charData->armourVal >= $equipment->level;
if ( $isBarbarian ) {
echo "\"Not sure you'd know what to do with that!\"\n";
return;
}
else if ( $armourBetter ) {
echo "\"No point buying my wares if you've got better yourself!\"\n";
return;
}
$charData->armour = $equipment->name;
$charData->armourVal = $equipment->level;
}
$equipString .= ". ";
// Remove equipment from shop.
$shop->removeEquipment($equipment->name);
$shopping->takeMoney($equipment, $shop, $charData, $room, $equipString);
});
}
// Add unique identifiers to commands.
$allocator = new UIDAllocator($this->commands);
$allocator->Allocate();
}
}
$shopping = new Shopping();
// Add unique identifiers to commands.
$allocator = new UIDAllocator($shopping->commands);
$allocator->Allocate();