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->get_ob_key_value ('ext_reseller_seller'), |
… | |
… | |
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]->get_ob_key_value ('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->get_ob_key_value ('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; |
… | |
… | |
72 | 0 |
60 | 0 |
73 | }; |
61 | }; |
74 | |
62 | |
75 | cf::register_script_function "reseller::pay_player" => sub { |
63 | cf::register_script_function "reseller::pay_player" => sub { |
76 | my ($who, $msg, $npc) = @_; |
64 | my ($who, $msg, $npc) = @_; |
77 | my $sells = cf::from_json $npc->get_ob_key_value ('ext_reseller_sales'); |
65 | my $ext_re_sales = $npc->get_ob_key_value ('ext_reseller_sales'); |
|
|
66 | my $sells = $ext_re_sales && cf::decode_json $ext_re_sales; |
78 | my $hissells = $sells->{$who->name}; |
67 | my $hissells = $sells->{$who->name}; |
79 | |
68 | |
80 | unless (keys %{$hissells || {}}) { |
69 | unless (keys %{$hissells || {}}) { |
81 | $who->reply ($npc, "I'm sorry, but you sold nothing.\n"); |
70 | $who->reply ($npc, "I'm sorry, but you sold nothing.\n"); |
82 | return 0; |
71 | return 0; |
… | |
… | |
90 | |
79 | |
91 | audit_log ($who, 'collects', "$sum silver"); |
80 | audit_log ($who, 'collects', "$sum silver"); |
92 | |
81 | |
93 | $sells->{$who->name} = {}; |
82 | $sells->{$who->name} = {}; |
94 | |
83 | |
95 | $npc->set_ob_key_value (ext_reseller_sales => cf::to_json $sells); |
84 | $npc->set_ob_key_value (ext_reseller_sales => cf::encode_json $sells) |
|
|
85 | if $sells; |
96 | |
86 | |
97 | 0 |
87 | 0 |
98 | }; |
88 | }; |
99 | |
89 | |
100 | cf::register_attachment "reseller_shopmat", |
90 | cf::object::attachment "reseller_shopmat", |
101 | on_move_trigger => sub { |
91 | on_move_trigger => sub { |
102 | my ($self, $who_caused, $who) = @_; |
92 | my ($self, $who_caused, $who) = @_; |
103 | |
93 | |
104 | my @obs = grep { $_->name eq $self->{reseller_shopmat}{npc_name} } |
94 | my @obs = grep { $_->name eq $self->{reseller_shopmat}{npc_name} } |
105 | $who->map->at ($self->{reseller_shopmat}{npc_x}, $self->{reseller_shopmat}{npc_y}); |
95 | $who->map->at ($self->{reseller_shopmat}{npc_x}, $self->{reseller_shopmat}{npc_y}); |
… | |
… | |
107 | unless (@obs) { |
97 | unless (@obs) { |
108 | warn "Couldn't find shop keeper in " . $who->map->path . "\n"; |
98 | warn "Couldn't find shop keeper in " . $who->map->path . "\n"; |
109 | return cf::override; |
99 | return cf::override; |
110 | } |
100 | } |
111 | |
101 | |
112 | my $sells = cf::from_json $obs[0]->get_ob_key_value ('ext_reseller_sales'); |
102 | my $ext_re_sales = $obs[0]->get_ob_key_value ('ext_reseller_sales'); |
|
|
103 | my $sells = $ext_re_sales && cf::decode_json $ext_re_sales; |
113 | |
104 | |
114 | my $unpaid_items = {}; |
105 | my $unpaid_items = {}; |
115 | |
106 | |
116 | for my $item (find_unpaid ($who)) { |
107 | for my $item (find_unpaid ($who)) { |
117 | if ($item->get_ob_key_value ('ext_reseller_seller') eq $who->name) { |
108 | if ($item->get_ob_key_value ('ext_reseller_seller') eq $who->name) { |
118 | audit_log ($who, 'removes', ob2info ($item)); |
109 | audit_log ($who, 'removes', ob2info ($item)); |
119 | $item->flag (cf::FLAG_UNPAID, 0); |
110 | $item->flag (cf::FLAG_UNPAID, 0); |
120 | $item->remove; |
111 | $item->remove; |
121 | $item->insert_ob_in_ob ($who); |
112 | give_back ($who, $item); |
122 | next; |
113 | next; |
123 | } |
114 | } |
124 | |
115 | |
125 | my $value = $item->query_cost ($who, cf::F_BUY | cf::F_SHOP); |
116 | my $value = $item->query_cost ($who, cf::F_BUY | cf::F_SHOP); |
126 | |
117 | |
… | |
… | |
152 | } |
143 | } |
153 | |
144 | |
154 | audit_log ($who, 'removed', (join ",", @seller_noted)) |
145 | audit_log ($who, 'removed', (join ",", @seller_noted)) |
155 | if @seller_noted; |
146 | if @seller_noted; |
156 | |
147 | |
157 | $obs[0]->set_ob_key_value (ext_reseller_sales => cf::to_json $sells); |
148 | $obs[0]->set_ob_key_value (ext_reseller_sales => cf::encode_json $sells) |
|
|
149 | if $sells; |
158 | |
150 | |
159 | cf::override; |
151 | cf::override; |
160 | }, |
152 | }, |
161 | ; |
153 | ; |
162 | |
154 | |
|
|
155 | sub give_back { |
|
|
156 | my ($who, $what) = @_; |
|
|
157 | $who->insert ($what); |
|
|
158 | $who->esrv_send_item ($what); |
|
|
159 | } |
|
|
160 | |
|
|
161 | sub give_back_with_message { |
|
|
162 | my ($who, $what, $msg) = @_; |
|
|
163 | $who->message ($msg, cf::NDI_BROWN); |
|
|
164 | give_back ($who, $what); |
|
|
165 | } |
|
|
166 | |
163 | cf::register_attachment "reseller_floor", |
167 | cf::object::attachment "reseller_floor", |
164 | on_drop_on => sub { |
168 | on_drop_on => sub { |
165 | my ($on, $what, $who) = @_; |
169 | my ($on, $what, $who) = @_; |
166 | my $name = $what->custom_name; |
170 | my $name = $what->custom_name; |
167 | |
171 | |
168 | return if $what->flag (cf::FLAG_UNPAID); |
172 | return if $what->flag (cf::FLAG_UNPAID); |
169 | |
173 | |
170 | if ($what->type == cf::MONEY) { |
174 | if ($what->type == cf::MONEY) { |
|
|
175 | give_back_with_message ($who, $what, |
171 | $who->message ("The shopkeeper says: Sorry, you can't sell money here.", cf::NDI_BROWN); |
176 | "The shopkeeper says: Sorry, you can't sell money here."); |
172 | $what->insert_ob_in_ob ($who); |
|
|
173 | return cf::override; |
177 | return cf::override; |
174 | } |
178 | } |
175 | |
179 | |
176 | if (!$what->flag (cf::FLAG_IDENTIFIED) && $what->need_identify) { |
180 | if (!$what->flag (cf::FLAG_IDENTIFIED) && $what->need_identify) { |
|
|
181 | give_back_with_message ($who, $what, |
177 | $who->message ("The shopkeeper says: Sorry, you can't sell unidentified stuff here.", cf::NDI_BROWN); |
182 | "The shopkeeper says: Sorry, you can't sell unidentified stuff here."); |
178 | $what->insert_ob_in_ob ($who); |
|
|
179 | return cf::override; |
183 | return cf::override; |
180 | } |
184 | } |
181 | |
185 | |
182 | my $orig_value = $what->value; |
186 | my $orig_value = $what->value; |
183 | my $value = 0; |
187 | my $value = 0; |
184 | |
188 | |
185 | if ($name =~ m/\S/) { |
189 | if ($name =~ m/\S/) { |
186 | unless ($name =~ m/\d+\s*\S+/) { |
190 | 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); |
191 | give_back_with_message ($who, $what, |
188 | $what->insert_ob_in_ob ($who); |
192 | "The shopkeeper says: Sorry, I don't recognize '$name' as currency. " |
|
|
193 | . "Please name your item like '17 platinum' or '10 gold 8 silver'"); |
189 | return cf::override; |
194 | return cf::override; |
190 | } |
195 | } |
191 | |
196 | |
192 | while ($name =~ s/^\s*(\d+)\s*(\S+)//) { |
197 | while ($name =~ s/^\s*(\d+)\s*(\S+)//) { |
193 | if ($aliases{lc $2} or $unit{lc $2}) { |
198 | my ($v, $c) = ($1, $2); |
194 | $value += $1 * ($unit{lc $2} ? $unit{lc $2} : $unit{$aliases{lc $2}}); |
199 | if (my $coin = cf::coin_from_name $c) { |
|
|
200 | $value += $v * $coin->value; |
195 | } else { |
201 | } else { |
196 | $what->insert_ob_in_ob ($who); |
202 | give_back_with_message ($who, $what, |
197 | $who->message ("The shopkeeper says: I don't know the currency '$2'", cf::NDI_BROWN); |
203 | "The shopkeeper says: I don't know the currency '$c', you can use one of these currencies: " |
|
|
204 | . (join ", ", cf::coin_names) |
|
|
205 | ); |
198 | return cf::override; |
206 | return cf::override; |
199 | } |
207 | } |
200 | } |
208 | } |
201 | } else { |
209 | } else { |
|
|
210 | # commented out the following line because too many just use the |
|
|
211 | # reseller shop as dumpyard: |
202 | $value = $what->query_cost ($who, cf::F_SELL | cf::F_SHOP) / ($what->nrof || 1); |
212 | # $value = $what->query_cost ($who, cf::F_SELL | cf::F_SHOP) / ($what->nrof || 1); |
|
|
213 | give_back_with_message ($who, $what, |
|
|
214 | "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' " |
|
|
216 | . "and drop it again. (To rename the item use the 'rename' " |
|
|
217 | . "context menu item in the inventory)." |
|
|
218 | ); |
|
|
219 | return cf::override; |
203 | } |
220 | } |
204 | |
221 | |
205 | if ($value < 0) { |
222 | if ($value < 0) { |
206 | $what->insert_ob_in_ob ($who); |
223 | 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); |
224 | "The shopkeeper says: You can't sell something for a negative value: $value"); |
208 | return cf::override; |
225 | return cf::override; |
209 | } |
226 | } |
210 | |
227 | |
211 | my $fee = $value / 100; # 1% selling fee |
228 | my $fee = $value / 100; # 1% selling fee |
212 | |
229 | |
213 | unless ($who->pay_amount ($fee)) { |
230 | unless ($who->pay_amount ($fee)) { |
214 | $who->message ( |
231 | 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", |
232 | "The shopkeeper says: You need " . cf::cost_string_from_value ($fee) |
216 | cf::NDI_BROWN |
233 | . " to pay the 1% fee for this item"); |
217 | ); |
|
|
218 | $what->insert_ob_in_ob ($who); |
|
|
219 | return cf::override; |
234 | return cf::override; |
220 | } else { |
235 | } else { |
221 | $who->message ( |
236 | $who->message ( |
222 | "The shopkeeper says: Ok, got the fee of " . cf::cost_string_from_value ($fee) . " for the item", |
237 | "The shopkeeper says: Ok, got the fee of " . cf::cost_string_from_value ($fee) |
|
|
238 | . " for the item", |
223 | cf::NDI_BROWN |
239 | cf::NDI_BROWN |
224 | ); |
240 | ); |
225 | } |
241 | } |
226 | |
242 | |
227 | $what->value ($value); |
243 | $what->value ($value); |