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