-
Notifications
You must be signed in to change notification settings - Fork 1
/
ajax_sellinventory2.php
195 lines (156 loc) · 6.01 KB
/
ajax_sellinventory2.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
<?php
$require_petload = 'no';
$invisible = 'yes';
$AJAX = true;
// confirm the session...
require_once 'commons/dbconnect.php';
require_once 'commons/sessions.php';
require_once 'commons/rpgfunctions.php';
require_once 'commons/inventory.php';
require_once 'commons/houselib.php';
require_once 'commons/userlib.php';
require_once 'commons/itemstats.php';
require_once 'commons/sellermarketlib.php';
if(!array_key_exists('ids', $_POST))
{
echo 'message:No items were selected...';
exit();
}
$itemids = explode(',', $_POST['ids']);
$house = get_house_byuser($user['idnum']);
if($house === false)
{
echo 'message:Error loading your house. If this problem persists (especially if there\'s nothing about it in the City Hall), please report it to <a href="admincontact.php">an administrator</a>.' . "\n" .
'failure:' . implode(',', $itemids);
exit();
}
$failed_ids = array();
$moved_ids = array();
$soldback = array();
$profit = 0;
$delete_ids = array();
$refuse_ids = array();
$grocery_ids = array();
$NO_BIDS_REMAINING = array();
foreach($itemids as $id)
{
$item = get_inventory_byid($id, 'idnum,user,location,itemname,health');
if($item['user'] == $user['user'] && $item['location'] != 'seized' && $item['location'] != 'pet' && $item['location'] != 'storage/outgoing')
{
$item_details = get_item_byname($item['itemname']);
if($item_details === false)
continue;
if($item_details['cursed'] == 'yes' || $item_details['nosellback'] == 'yes' || $item_details['quest_item'] == 'yes')
$failed_ids[] = $id;
else
{
$moved_ids[] = $id;
$sellback = ceil($item_details['value'] * sellback_rate());
if($item['health'] < $item_details['durability'] || $NO_BIDS_REMAINING[$item['itemname']])
$sold_to_sellers_market = false;
else
{
$sold_to_sellers_market = sell_to_sellers_market($item, $sellback);
if($sold_to_sellers_market === false)
$NO_BIDS_REMAINING[$item['itemname']] = true;
}
if($sold_to_sellers_market !== false)
{
$profit += $sold_to_sellers_market;
$sold[$id]++;
$sold_value[$id] += $sellback;
$delete_ids[] = $id;
}
else
{
$profit += $sellback;
$sold[$id]++;
$sold_value[$id] += $sellback;
$soldback[$item['itemname']]++;
if(mt_rand(1, 2) == 1)
$delete_ids[] = $id;
else
{
if($item_details['is_grocery'] == 'yes')
$grocery_ids[] = $id;
else
$refuse_ids[] = $id;
}
}
}
}
else
$failed_ids[] = $id;
}
if(count($soldback) > 0)
{
foreach($soldback as $item=>$quantity)
record_item_disposal($item, 'sold', $quantity);
}
$moved_ids_count = count($moved_ids);
$failed_ids_count = count($failed_ids);
if($moved_ids_count > 0)
{
if(count($delete_ids) > 0)
{
$command = 'DELETE FROM monster_inventory WHERE idnum IN (' . implode(',', $delete_ids) . ') LIMIT ' . count($delete_ids);
$database->FetchNone($command, 'deleting items');
}
if(count($refuse_ids) > 0)
{
$command = 'UPDATE monster_inventory SET user=\'ihobbs\',message2=' . quote_smart('Gamesold by ' . $user['display'] . '.') . ',changed=' . $now . ' WHERE idnum IN (' . implode(',', $refuse_ids) . ') LIMIT ' . count($refuse_ids);
$database->FetchNone($command, 'recovering items for refuse store');
}
if(count($grocery_ids) > 0)
{
$command = 'UPDATE monster_inventory SET user=\'grocerystore\',message2=' . quote_smart('Gamesold by ' . $user['display'] . '.') . ',changed=' . $now . ' WHERE idnum IN (' . implode(',', $grocery_ids) . ') LIMIT ' . count($grocery_ids);
$database->FetchNone($command, 'recovering items for refuse store');
}
recount_house_bulk($user, $house);
}
if($profit > 0)
{
give_money($user, $profit, 'Game-sold ' . count($sold) . ' item' . (count($sold) == 1 ? '' : 's'));
require_once 'commons/questlib.php';
$sellback = get_quest_value($user['idnum'], 'total sellback');
$sellback_value = (int)$sellback['value'];
$sellback_value += $profit;
if($sellback === false)
add_quest_value($user['idnum'], 'total sellback', $sellback_value);
else
update_quest_value($sellback['idnum'], $sellback_value);
$badges = get_badges_byuserid($user['idnum']);
if($badges['gamesell'] == 'no' && $sellback_value >= 1000)
{
set_badge($user['idnum'], 'gamesell');
$body = 'Just wanted to let you know that you\'re doing well. Selling cheap items back to the game adds up. You\'ve made 1,000{m} by selling stuff back!<br /><br />' .
'Keep up the good work!<br /><br />' .
'{i}(You earned the Bourgeois badge!){/}';
psymail_user($user['user'], 'lpawlak', 'You game-sold over 1,000 moneys worth of items!', $body);
}
if($badges['gamesellmore'] == 'no' && $sellback_value >= 1000000)
{
set_badge($user['idnum'], 'gamesellmore');
$body = 'You\'ve probably bought and sold so much stuff you wouldn\'t believe that you\'ve sold items totalling 1,000,000{m} in value!<br /><br />' .
'It\'s true! You\'ve come a long way since you first got here.<br /><br />' .
'Anyway, see you around.<br /><br />' .
'{i}(You earned the Profiteer badge!){/}';
psymail_user($user['user'], 'lpawlak', 'You game-sold over 1,000,000 moneys worth of items!', $body);
}
}
if($moved_ids_count > 0)
{
echo 'message:<span class="success">' . $moved_ids_count . ' item' . ($moved_ids_count == 1 ? ' was' : 's were') . ' sold, for ' . $profit . '<span class="money">m</span>.';
if($failed_ids_count > 0)
echo ' ' . $failed_ids_count . ' item' . ($failed_ids_count == 1 ? '' : 's') . ' could not be sold.';
echo '</span>' . "\n" .
'domoney:' . ($user['money'] + $profit) . "\n" .
'success:' . implode(',', $moved_ids);
}
if($failed_ids_count > 0)
{
if($moved_ids_count == 0)
echo 'message:<span class="failure">The selected item' . ($failed_ids_count == 1 ? '' : 's') . ' could not be sold.</span>';
echo "\n" . 'failure:' . implode(',', $failed_ids);
}
?>