1 |
root |
1.7 |
#!perl # mandatory |
2 |
root |
1.1 |
|
3 |
elmex |
1.2 |
sub ob2info { |
4 |
|
|
my ($item, $rval) = @_; |
5 |
|
|
sprintf "[%s from %s (%d:%d%s) nrof: %d uuid: %s]", |
6 |
root |
1.12 |
$item->name, $item->kv_get ('ext_reseller_seller'), |
7 |
|
|
$item->kv_get ('ext_reseller_orig_value'), $item->value, |
8 |
elmex |
1.2 |
(defined $rval ? ":$rval" : ""), $item->nrof, $item->uuid; |
9 |
|
|
} |
10 |
|
|
|
11 |
|
|
sub audit_log { |
12 |
|
|
my ($who, $action, $info) = @_; |
13 |
|
|
warn |
14 |
|
|
sprintf "RESELLER_AUDIT(%s) %s %s: %s\n", |
15 |
|
|
$who->map->path, $who->name, $action, $info; |
16 |
|
|
} |
17 |
|
|
|
18 |
elmex |
1.5 |
sub find_rec; |
19 |
|
|
|
20 |
root |
1.1 |
sub find_rec { |
21 |
|
|
my ($ob, $cb) = @_; |
22 |
|
|
|
23 |
elmex |
1.5 |
my @found; |
24 |
root |
1.1 |
for my $i ($ob->inv) { |
25 |
elmex |
1.5 |
push @found, $i if $cb->($i); |
26 |
|
|
push @found, find_rec $i, $cb if $i->inv; |
27 |
|
|
} |
28 |
root |
1.1 |
|
29 |
elmex |
1.5 |
return @found; |
30 |
root |
1.1 |
} |
31 |
|
|
|
32 |
|
|
sub find_unpaid { |
33 |
|
|
my ($ob) = @_; |
34 |
elmex |
1.5 |
find_rec $ob, sub { $_[0]->flag (cf::FLAG_UNPAID) }; |
35 |
root |
1.1 |
} |
36 |
|
|
|
37 |
|
|
sub find_traded { |
38 |
|
|
my ($ob) = @_; |
39 |
root |
1.12 |
find_rec $ob, sub { $_[0]->kv_get ('ext_reseller_seller') ne '' }; |
40 |
root |
1.1 |
} |
41 |
|
|
|
42 |
|
|
cf::register_script_function "reseller::list_sells" => sub { |
43 |
|
|
my ($who, $msg, $npc) = @_; |
44 |
root |
1.12 |
my $ext_re_sales = $npc->kv_get ('ext_reseller_sales'); |
45 |
root |
1.9 |
my $sells = $ext_re_sales && cf::decode_json $ext_re_sales; |
46 |
root |
1.1 |
my $hissells = $sells->{$who->name}; |
47 |
|
|
|
48 |
|
|
unless (keys %{$hissells || {}}) { |
49 |
|
|
$who->reply ($npc, "I'm sorry, but you sold nothing.\n"); |
50 |
|
|
return 0; |
51 |
|
|
} |
52 |
|
|
|
53 |
root |
1.14 |
my $reply = "T<You sold:>\n\n"; |
54 |
root |
1.13 |
|
55 |
root |
1.1 |
for (keys %$hissells) { |
56 |
|
|
my $n = $_; |
57 |
|
|
$n =~ s/\s*\(unpaid\)//g; |
58 |
root |
1.13 |
$reply .= " B<$n> (for " . cf::cost_string_from_value ($hissells->{$_}) . ")\n"; |
59 |
root |
1.1 |
} |
60 |
|
|
|
61 |
root |
1.13 |
$who->reply ($npc, $reply); |
62 |
|
|
|
63 |
root |
1.1 |
0 |
64 |
|
|
}; |
65 |
|
|
|
66 |
|
|
cf::register_script_function "reseller::pay_player" => sub { |
67 |
|
|
my ($who, $msg, $npc) = @_; |
68 |
root |
1.12 |
my $ext_re_sales = $npc->kv_get ('ext_reseller_sales'); |
69 |
root |
1.9 |
my $sells = $ext_re_sales && cf::decode_json $ext_re_sales; |
70 |
root |
1.1 |
my $hissells = $sells->{$who->name}; |
71 |
|
|
|
72 |
|
|
unless (keys %{$hissells || {}}) { |
73 |
|
|
$who->reply ($npc, "I'm sorry, but you sold nothing.\n"); |
74 |
|
|
return 0; |
75 |
|
|
} |
76 |
|
|
|
77 |
|
|
my $sum = 0; |
78 |
|
|
$sum += $_ for values %$hissells; |
79 |
|
|
|
80 |
|
|
$who->pay_player ($sum); |
81 |
root |
1.13 |
$who->reply ($npc, "Here are the " . cf::cost_string_from_value ($sum) . " for your sales."); |
82 |
root |
1.1 |
|
83 |
elmex |
1.2 |
audit_log ($who, 'collects', "$sum silver"); |
84 |
|
|
|
85 |
root |
1.1 |
$sells->{$who->name} = {}; |
86 |
|
|
|
87 |
root |
1.12 |
$npc->kv_set (ext_reseller_sales => cf::encode_json $sells) |
88 |
elmex |
1.6 |
if $sells; |
89 |
root |
1.1 |
|
90 |
|
|
0 |
91 |
|
|
}; |
92 |
|
|
|
93 |
root |
1.3 |
cf::object::attachment "reseller_shopmat", |
94 |
root |
1.1 |
on_move_trigger => sub { |
95 |
|
|
my ($self, $who_caused, $who) = @_; |
96 |
|
|
|
97 |
|
|
my @obs = grep { $_->name eq $self->{reseller_shopmat}{npc_name} } |
98 |
|
|
$who->map->at ($self->{reseller_shopmat}{npc_x}, $self->{reseller_shopmat}{npc_y}); |
99 |
|
|
|
100 |
|
|
unless (@obs) { |
101 |
elmex |
1.2 |
warn "Couldn't find shop keeper in " . $who->map->path . "\n"; |
102 |
root |
1.1 |
return cf::override; |
103 |
|
|
} |
104 |
|
|
|
105 |
root |
1.12 |
my $ext_re_sales = $obs[0]->kv_get ('ext_reseller_sales'); |
106 |
root |
1.9 |
my $sells = $ext_re_sales && cf::decode_json $ext_re_sales; |
107 |
root |
1.1 |
|
108 |
|
|
my $unpaid_items = {}; |
109 |
|
|
|
110 |
|
|
for my $item (find_unpaid ($who)) { |
111 |
root |
1.12 |
if ($item->kv_get ('ext_reseller_seller') eq $who->name) { |
112 |
elmex |
1.2 |
audit_log ($who, 'removes', ob2info ($item)); |
113 |
root |
1.1 |
$item->flag (cf::FLAG_UNPAID, 0); |
114 |
|
|
$item->remove; |
115 |
elmex |
1.10 |
give_back ($who, $item); |
116 |
root |
1.1 |
next; |
117 |
|
|
} |
118 |
|
|
|
119 |
|
|
my $value = $item->query_cost ($who, cf::F_BUY | cf::F_SHOP); |
120 |
|
|
|
121 |
|
|
warn "Object " . $item->name . " bought by " . $who->name . " on map " |
122 |
|
|
. $who->map->path . " for $value silver has no seller set\n" |
123 |
root |
1.12 |
if $item->kv_get ('ext_reseller_seller') eq ''; |
124 |
root |
1.1 |
|
125 |
|
|
$unpaid_items->{$item} = [$value, $item]; |
126 |
|
|
} |
127 |
|
|
|
128 |
elmex |
1.2 |
audit_log ($who, 'wants', (join ",", map { ob2info ($_->[1], $_->[0]) } values %$unpaid_items)) |
129 |
|
|
if %$unpaid_items; |
130 |
|
|
|
131 |
root |
1.1 |
$self->apply_shop_mat ($who); |
132 |
|
|
|
133 |
elmex |
1.2 |
my @seller_noted; |
134 |
|
|
|
135 |
root |
1.1 |
for my $item (find_traded ($who)) { |
136 |
|
|
next if $item->flag (cf::FLAG_UNPAID); |
137 |
|
|
if (my $value = $unpaid_items->{$item}[0]) { |
138 |
elmex |
1.2 |
push @seller_noted, ob2info ($item, $value)."P"; |
139 |
root |
1.12 |
$sells->{$item->kv_get ('ext_reseller_seller')}->{$item->name} += $value; |
140 |
elmex |
1.2 |
} else { |
141 |
|
|
push @seller_noted, ob2info ($item)."T"; |
142 |
root |
1.1 |
} |
143 |
|
|
|
144 |
root |
1.12 |
$item->value ($item->kv_get ('ext_reseller_orig_value')); |
145 |
|
|
$item->kv_del ("ext_reseller_seller"); |
146 |
root |
1.1 |
} |
147 |
|
|
|
148 |
elmex |
1.2 |
audit_log ($who, 'removed', (join ",", @seller_noted)) |
149 |
|
|
if @seller_noted; |
150 |
|
|
|
151 |
root |
1.12 |
$obs[0]->kv_set (ext_reseller_sales => cf::encode_json $sells) |
152 |
elmex |
1.6 |
if $sells; |
153 |
root |
1.1 |
|
154 |
|
|
cf::override; |
155 |
|
|
}, |
156 |
|
|
; |
157 |
|
|
|
158 |
elmex |
1.10 |
sub give_back { |
159 |
|
|
my ($who, $what) = @_; |
160 |
|
|
$who->insert ($what); |
161 |
|
|
} |
162 |
|
|
|
163 |
|
|
sub give_back_with_message { |
164 |
|
|
my ($who, $what, $msg) = @_; |
165 |
|
|
$who->message ($msg, cf::NDI_BROWN); |
166 |
|
|
give_back ($who, $what); |
167 |
|
|
} |
168 |
|
|
|
169 |
root |
1.3 |
cf::object::attachment "reseller_floor", |
170 |
root |
1.1 |
on_drop_on => sub { |
171 |
|
|
my ($on, $what, $who) = @_; |
172 |
|
|
my $name = $what->custom_name; |
173 |
|
|
|
174 |
|
|
return if $what->flag (cf::FLAG_UNPAID); |
175 |
|
|
|
176 |
|
|
if ($what->type == cf::MONEY) { |
177 |
elmex |
1.10 |
give_back_with_message ($who, $what, |
178 |
|
|
"The shopkeeper says: Sorry, you can't sell money here."); |
179 |
root |
1.1 |
return cf::override; |
180 |
|
|
} |
181 |
|
|
|
182 |
|
|
if (!$what->flag (cf::FLAG_IDENTIFIED) && $what->need_identify) { |
183 |
elmex |
1.10 |
give_back_with_message ($who, $what, |
184 |
|
|
"The shopkeeper says: Sorry, you can't sell unidentified stuff here."); |
185 |
root |
1.1 |
return cf::override; |
186 |
|
|
} |
187 |
|
|
|
188 |
|
|
my $orig_value = $what->value; |
189 |
|
|
my $value = 0; |
190 |
|
|
|
191 |
|
|
if ($name =~ m/\S/) { |
192 |
|
|
unless ($name =~ m/\d+\s*\S+/) { |
193 |
elmex |
1.10 |
give_back_with_message ($who, $what, |
194 |
|
|
"The shopkeeper says: Sorry, I don't recognize '$name' as currency. " |
195 |
root |
1.13 |
. "Please name your item like '17 platinum' or '10 gold 8 silver.'"); |
196 |
root |
1.1 |
return cf::override; |
197 |
|
|
} |
198 |
|
|
|
199 |
|
|
while ($name =~ s/^\s*(\d+)\s*(\S+)//) { |
200 |
root |
1.11 |
my ($v, $c) = ($1, $2); |
201 |
|
|
if (my $coin = cf::coin_from_name $c) { |
202 |
|
|
$value += $v * $coin->value; |
203 |
root |
1.1 |
} else { |
204 |
elmex |
1.10 |
give_back_with_message ($who, $what, |
205 |
root |
1.11 |
"The shopkeeper says: I don't know the currency '$c', you can use one of these currencies: " |
206 |
|
|
. (join ", ", cf::coin_names) |
207 |
|
|
); |
208 |
root |
1.1 |
return cf::override; |
209 |
|
|
} |
210 |
|
|
} |
211 |
|
|
} else { |
212 |
elmex |
1.10 |
# commented out the following line because too many just use the |
213 |
|
|
# reseller shop as dumpyard: |
214 |
|
|
# $value = $what->query_cost ($who, cf::F_SELL | cf::F_SHOP) / ($what->nrof || 1); |
215 |
|
|
give_back_with_message ($who, $what, |
216 |
|
|
"Sorry, you can't just sell stuff without assigning a price to it! " |
217 |
root |
1.11 |
. "Please name your item like '17 platinum' or '10 gold 8 silver' " |
218 |
root |
1.13 |
. "and drop it again. (To rename the item use the B<rename> " |
219 |
|
|
. "entry in the inventory item popup menu)." |
220 |
root |
1.11 |
); |
221 |
elmex |
1.10 |
return cf::override; |
222 |
root |
1.1 |
} |
223 |
|
|
|
224 |
elmex |
1.16 |
if ($value > 100 ** 4) { # also for overflow prevention |
225 |
|
|
give_back_with_message ($who, $what, |
226 |
|
|
"The shopkeeper says: You can't sell something for such a high " |
227 |
|
|
. "value. Please keep it below 100 royalty."); |
228 |
|
|
return cf::override; |
229 |
|
|
} |
230 |
|
|
|
231 |
root |
1.1 |
if ($value < 0) { |
232 |
elmex |
1.10 |
give_back_with_message ($who, $what, |
233 |
root |
1.13 |
"The shopkeeper says: You can't sell something for a negative value: $value."); |
234 |
root |
1.1 |
return cf::override; |
235 |
|
|
} |
236 |
|
|
|
237 |
|
|
my $fee = $value / 100; # 1% selling fee |
238 |
|
|
|
239 |
|
|
unless ($who->pay_amount ($fee)) { |
240 |
elmex |
1.10 |
give_back_with_message ($who, $what, |
241 |
|
|
"The shopkeeper says: You need " . cf::cost_string_from_value ($fee) |
242 |
root |
1.13 |
. " to pay the 1% fee for this item."); |
243 |
root |
1.1 |
return cf::override; |
244 |
|
|
} else { |
245 |
|
|
$who->message ( |
246 |
elmex |
1.10 |
"The shopkeeper says: Ok, got the fee of " . cf::cost_string_from_value ($fee) |
247 |
root |
1.13 |
. " for the item.", |
248 |
root |
1.1 |
cf::NDI_BROWN |
249 |
|
|
); |
250 |
|
|
} |
251 |
|
|
|
252 |
|
|
$what->value ($value); |
253 |
|
|
my $cost = $what->query_cost ($who, cf::F_BUY | cf::F_SHOP) / ($what->nrof || 1); |
254 |
|
|
|
255 |
|
|
my $fact = 0; |
256 |
|
|
if ($cost) { |
257 |
|
|
$fact = $value / $cost; |
258 |
|
|
$what->value (cf::ceil ($value * $fact)); |
259 |
|
|
} |
260 |
|
|
|
261 |
|
|
# warn "END VALUE: $value * $fact => " . $what->value . "\n"; |
262 |
|
|
|
263 |
|
|
# my $cost = $what->query_cost ($who, cf::F_BUY | cf::F_SHOP) / $what->nrof; |
264 |
|
|
# warn "COSTS NOW: $cost\n"; |
265 |
|
|
|
266 |
|
|
$who->message ( |
267 |
|
|
"The shopkeeper says: Ok, I marked " |
268 |
|
|
. ($what->nrof || 1) . " " . $what->name . " to be sold for at least " |
269 |
|
|
. cf::cost_string_from_value ($value) |
270 |
|
|
. ($what->nrof > 1 ? " each" : ""), cf::NDI_BROWN |
271 |
|
|
); |
272 |
|
|
|
273 |
root |
1.12 |
$what->kv_set (ext_reseller_seller => $who->name); |
274 |
|
|
$what->kv_set (ext_reseller_orig_value => $orig_value); |
275 |
root |
1.1 |
# warn "SET SELLER ON " . $what->name . " + " . $what->{seller}->[0] . "\n"; |
276 |
|
|
$what->custom_name ($what->name . " (by " . $who->name . ")"); |
277 |
|
|
$what->flag (cf::FLAG_UNPAID, 1); |
278 |
|
|
$what->insert_ob_in_map_at ($who->map, $who, cf::INS_BELOW_ORIGINATOR, $who->x, $who->y); |
279 |
|
|
|
280 |
elmex |
1.2 |
audit_log ($who, 'sells', ob2info ($what)); |
281 |
|
|
|
282 |
root |
1.1 |
cf::override; |
283 |
|
|
}, |
284 |
|
|
; |