1 | #!perl |
1 | #!perl # mandatory |
2 | |
2 | |
3 | my %unit = ( |
3 | my %unit = ( |
4 | silver => 1, |
4 | silver => 1, |
5 | gold => 10, |
5 | gold => 10, |
6 | platina => 50, |
6 | platina => 50, |
… | |
… | |
25 | warn |
25 | warn |
26 | sprintf "RESELLER_AUDIT(%s) %s %s: %s\n", |
26 | sprintf "RESELLER_AUDIT(%s) %s %s: %s\n", |
27 | $who->map->path, $who->name, $action, $info; |
27 | $who->map->path, $who->name, $action, $info; |
28 | } |
28 | } |
29 | |
29 | |
|
|
30 | sub find_rec; |
|
|
31 | |
30 | sub find_rec { |
32 | sub find_rec { |
31 | my ($ob, $cb) = @_; |
33 | my ($ob, $cb) = @_; |
32 | |
34 | |
33 | my @unpaid; |
35 | my @found; |
34 | for my $i ($ob->inv) { |
36 | for my $i ($ob->inv) { |
35 | push @unpaid, $i if $cb->($i); |
37 | push @found, $i if $cb->($i); |
36 | |
38 | push @found, find_rec $i, $cb if $i->inv; |
37 | push @unpaid, find_unpaid ($i, $cb) |
|
|
38 | if $i->inv; |
|
|
39 | } |
39 | } |
|
|
40 | |
40 | return @unpaid; |
41 | return @found; |
41 | } |
42 | } |
42 | |
43 | |
43 | sub find_unpaid { |
44 | sub find_unpaid { |
44 | my ($ob) = @_; |
45 | my ($ob) = @_; |
45 | my @r = find_rec ($ob, sub { $_[0]->flag (cf::FLAG_UNPAID) }); |
46 | find_rec $ob, sub { $_[0]->flag (cf::FLAG_UNPAID) }; |
46 | return @r; |
|
|
47 | } |
47 | } |
48 | |
48 | |
49 | sub find_traded { |
49 | sub find_traded { |
50 | my ($ob) = @_; |
50 | my ($ob) = @_; |
51 | my @r = find_rec ($ob, sub { $_[0]->get_ob_key_value ('ext_reseller_seller') ne '' }); |
51 | find_rec $ob, sub { $_[0]->get_ob_key_value ('ext_reseller_seller') ne '' }; |
52 | return @r; |
|
|
53 | } |
52 | } |
54 | |
53 | |
55 | cf::register_script_function "reseller::list_sells" => sub { |
54 | cf::register_script_function "reseller::list_sells" => sub { |
56 | my ($who, $msg, $npc) = @_; |
55 | my ($who, $msg, $npc) = @_; |
57 | my $sells = cf::from_json $npc->get_ob_key_value ('ext_reseller_sales'); |
56 | my $ext_re_sales = $npc->get_ob_key_value ('ext_reseller_sales'); |
|
|
57 | my $sells = $ext_re_sales && cf::decode_json $ext_re_sales; |
58 | my $hissells = $sells->{$who->name}; |
58 | my $hissells = $sells->{$who->name}; |
59 | |
59 | |
60 | unless (keys %{$hissells || {}}) { |
60 | unless (keys %{$hissells || {}}) { |
61 | $who->reply ($npc, "I'm sorry, but you sold nothing.\n"); |
61 | $who->reply ($npc, "I'm sorry, but you sold nothing.\n"); |
62 | return 0; |
62 | return 0; |
… | |
… | |
72 | 0 |
72 | 0 |
73 | }; |
73 | }; |
74 | |
74 | |
75 | cf::register_script_function "reseller::pay_player" => sub { |
75 | cf::register_script_function "reseller::pay_player" => sub { |
76 | my ($who, $msg, $npc) = @_; |
76 | my ($who, $msg, $npc) = @_; |
77 | my $sells = cf::from_json $npc->get_ob_key_value ('ext_reseller_sales'); |
77 | my $ext_re_sales = $npc->get_ob_key_value ('ext_reseller_sales'); |
|
|
78 | my $sells = $ext_re_sales && cf::decode_json $ext_re_sales; |
78 | my $hissells = $sells->{$who->name}; |
79 | my $hissells = $sells->{$who->name}; |
79 | |
80 | |
80 | unless (keys %{$hissells || {}}) { |
81 | unless (keys %{$hissells || {}}) { |
81 | $who->reply ($npc, "I'm sorry, but you sold nothing.\n"); |
82 | $who->reply ($npc, "I'm sorry, but you sold nothing.\n"); |
82 | return 0; |
83 | return 0; |
… | |
… | |
90 | |
91 | |
91 | audit_log ($who, 'collects', "$sum silver"); |
92 | audit_log ($who, 'collects', "$sum silver"); |
92 | |
93 | |
93 | $sells->{$who->name} = {}; |
94 | $sells->{$who->name} = {}; |
94 | |
95 | |
95 | $npc->set_ob_key_value (ext_reseller_sales => cf::to_json $sells); |
96 | $npc->set_ob_key_value (ext_reseller_sales => cf::encode_json $sells) |
|
|
97 | if $sells; |
96 | |
98 | |
97 | 0 |
99 | 0 |
98 | }; |
100 | }; |
99 | |
101 | |
100 | cf::register_attachment "reseller_shopmat", |
102 | cf::object::attachment "reseller_shopmat", |
101 | on_move_trigger => sub { |
103 | on_move_trigger => sub { |
102 | my ($self, $who_caused, $who) = @_; |
104 | my ($self, $who_caused, $who) = @_; |
103 | |
105 | |
104 | my @obs = grep { $_->name eq $self->{reseller_shopmat}{npc_name} } |
106 | my @obs = grep { $_->name eq $self->{reseller_shopmat}{npc_name} } |
105 | $who->map->at ($self->{reseller_shopmat}{npc_x}, $self->{reseller_shopmat}{npc_y}); |
107 | $who->map->at ($self->{reseller_shopmat}{npc_x}, $self->{reseller_shopmat}{npc_y}); |
… | |
… | |
107 | unless (@obs) { |
109 | unless (@obs) { |
108 | warn "Couldn't find shop keeper in " . $who->map->path . "\n"; |
110 | warn "Couldn't find shop keeper in " . $who->map->path . "\n"; |
109 | return cf::override; |
111 | return cf::override; |
110 | } |
112 | } |
111 | |
113 | |
112 | my $sells = cf::from_json $obs[0]->get_ob_key_value ('ext_reseller_sales'); |
114 | my $ext_re_sales = $obs[0]->get_ob_key_value ('ext_reseller_sales'); |
|
|
115 | my $sells = $ext_re_sales && cf::decode_json $ext_re_sales; |
113 | |
116 | |
114 | my $unpaid_items = {}; |
117 | my $unpaid_items = {}; |
115 | |
118 | |
116 | for my $item (find_unpaid ($who)) { |
119 | for my $item (find_unpaid ($who)) { |
117 | if ($item->get_ob_key_value ('ext_reseller_seller') eq $who->name) { |
120 | if ($item->get_ob_key_value ('ext_reseller_seller') eq $who->name) { |
118 | audit_log ($who, 'removes', ob2info ($item)); |
121 | audit_log ($who, 'removes', ob2info ($item)); |
119 | $item->flag (cf::FLAG_UNPAID, 0); |
122 | $item->flag (cf::FLAG_UNPAID, 0); |
120 | $item->remove; |
123 | $item->remove; |
121 | $item->insert_ob_in_ob ($who); |
124 | $who->insert ($item); |
122 | next; |
125 | next; |
123 | } |
126 | } |
124 | |
127 | |
125 | my $value = $item->query_cost ($who, cf::F_BUY | cf::F_SHOP); |
128 | my $value = $item->query_cost ($who, cf::F_BUY | cf::F_SHOP); |
126 | |
129 | |
… | |
… | |
152 | } |
155 | } |
153 | |
156 | |
154 | audit_log ($who, 'removed', (join ",", @seller_noted)) |
157 | audit_log ($who, 'removed', (join ",", @seller_noted)) |
155 | if @seller_noted; |
158 | if @seller_noted; |
156 | |
159 | |
157 | $obs[0]->set_ob_key_value (ext_reseller_sales => cf::to_json $sells); |
160 | $obs[0]->set_ob_key_value (ext_reseller_sales => cf::encode_json $sells) |
|
|
161 | if $sells; |
158 | |
162 | |
159 | cf::override; |
163 | cf::override; |
160 | }, |
164 | }, |
161 | ; |
165 | ; |
162 | |
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) { |
171 | $who->message ("The shopkeeper says: Sorry, you can't sell money here.", cf::NDI_BROWN); |
175 | $who->message ("The shopkeeper says: Sorry, you can't sell money here.", cf::NDI_BROWN); |
172 | $what->insert_ob_in_ob ($who); |
176 | $who->insert ($what); |
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) { |
177 | $who->message ("The shopkeeper says: Sorry, you can't sell unidentified stuff here.", cf::NDI_BROWN); |
181 | $who->message ("The shopkeeper says: Sorry, you can't sell unidentified stuff here.", cf::NDI_BROWN); |
178 | $what->insert_ob_in_ob ($who); |
182 | $who->insert ($what); |
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 | $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); |
188 | $what->insert_ob_in_ob ($who); |
192 | $who->insert ($what); |
189 | return cf::override; |
193 | return cf::override; |
190 | } |
194 | } |
191 | |
195 | |
192 | while ($name =~ s/^\s*(\d+)\s*(\S+)//) { |
196 | while ($name =~ s/^\s*(\d+)\s*(\S+)//) { |
193 | if ($aliases{lc $2} or $unit{lc $2}) { |
197 | if ($aliases{lc $2} or $unit{lc $2}) { |
194 | $value += $1 * ($unit{lc $2} ? $unit{lc $2} : $unit{$aliases{lc $2}}); |
198 | $value += $1 * ($unit{lc $2} ? $unit{lc $2} : $unit{$aliases{lc $2}}); |
195 | } else { |
199 | } else { |
196 | $what->insert_ob_in_ob ($who); |
200 | $who->insert ($what); |
197 | $who->message ("The shopkeeper says: I don't know the currency '$2'", cf::NDI_BROWN); |
201 | $who->message ("The shopkeeper says: I don't know the currency '$2'", cf::NDI_BROWN); |
198 | return cf::override; |
202 | return cf::override; |
199 | } |
203 | } |
200 | } |
204 | } |
201 | } else { |
205 | } else { |
202 | $value = $what->query_cost ($who, cf::F_SELL | cf::F_SHOP) / ($what->nrof || 1); |
206 | $value = $what->query_cost ($who, cf::F_SELL | cf::F_SHOP) / ($what->nrof || 1); |
203 | } |
207 | } |
204 | |
208 | |
205 | if ($value < 0) { |
209 | if ($value < 0) { |
206 | $what->insert_ob_in_ob ($who); |
210 | $who->insert ($what); |
207 | $who->message ("The shopkeeper says: You can't sell something for a negative value: $value", cf::NDI_BROWN); |
211 | $who->message ("The shopkeeper says: You can't sell something for a negative value: $value", cf::NDI_BROWN); |
208 | return cf::override; |
212 | return cf::override; |
209 | } |
213 | } |
210 | |
214 | |
211 | my $fee = $value / 100; # 1% selling fee |
215 | my $fee = $value / 100; # 1% selling fee |
… | |
… | |
213 | unless ($who->pay_amount ($fee)) { |
217 | unless ($who->pay_amount ($fee)) { |
214 | $who->message ( |
218 | $who->message ( |
215 | "The shopkeeper says: You need " . cf::cost_string_from_value ($fee) . " to pay the 1% fee for this item", |
219 | "The shopkeeper says: You need " . cf::cost_string_from_value ($fee) . " to pay the 1% fee for this item", |
216 | cf::NDI_BROWN |
220 | cf::NDI_BROWN |
217 | ); |
221 | ); |
218 | $what->insert_ob_in_ob ($who); |
222 | $who->insert ($what); |
219 | return cf::override; |
223 | return cf::override; |
220 | } else { |
224 | } else { |
221 | $who->message ( |
225 | $who->message ( |
222 | "The shopkeeper says: Ok, got the fee of " . cf::cost_string_from_value ($fee) . " for the item", |
226 | "The shopkeeper says: Ok, got the fee of " . cf::cost_string_from_value ($fee) . " for the item", |
223 | cf::NDI_BROWN |
227 | cf::NDI_BROWN |