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