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