1 | #!perl # MANDATORY |
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, |
… | |
… | |
51 | 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 | } |
52 | } |
53 | |
53 | |
54 | cf::register_script_function "reseller::list_sells" => sub { |
54 | cf::register_script_function "reseller::list_sells" => sub { |
55 | my ($who, $msg, $npc) = @_; |
55 | my ($who, $msg, $npc) = @_; |
56 | 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; |
57 | my $hissells = $sells->{$who->name}; |
58 | my $hissells = $sells->{$who->name}; |
58 | |
59 | |
59 | unless (keys %{$hissells || {}}) { |
60 | unless (keys %{$hissells || {}}) { |
60 | $who->reply ($npc, "I'm sorry, but you sold nothing.\n"); |
61 | $who->reply ($npc, "I'm sorry, but you sold nothing.\n"); |
61 | return 0; |
62 | return 0; |
… | |
… | |
71 | 0 |
72 | 0 |
72 | }; |
73 | }; |
73 | |
74 | |
74 | cf::register_script_function "reseller::pay_player" => sub { |
75 | cf::register_script_function "reseller::pay_player" => sub { |
75 | my ($who, $msg, $npc) = @_; |
76 | my ($who, $msg, $npc) = @_; |
76 | 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; |
77 | my $hissells = $sells->{$who->name}; |
79 | my $hissells = $sells->{$who->name}; |
78 | |
80 | |
79 | unless (keys %{$hissells || {}}) { |
81 | unless (keys %{$hissells || {}}) { |
80 | $who->reply ($npc, "I'm sorry, but you sold nothing.\n"); |
82 | $who->reply ($npc, "I'm sorry, but you sold nothing.\n"); |
81 | return 0; |
83 | return 0; |
… | |
… | |
89 | |
91 | |
90 | audit_log ($who, 'collects', "$sum silver"); |
92 | audit_log ($who, 'collects', "$sum silver"); |
91 | |
93 | |
92 | $sells->{$who->name} = {}; |
94 | $sells->{$who->name} = {}; |
93 | |
95 | |
94 | $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; |
95 | |
98 | |
96 | 0 |
99 | 0 |
97 | }; |
100 | }; |
98 | |
101 | |
99 | cf::object::attachment "reseller_shopmat", |
102 | cf::object::attachment "reseller_shopmat", |
… | |
… | |
106 | unless (@obs) { |
109 | unless (@obs) { |
107 | warn "Couldn't find shop keeper in " . $who->map->path . "\n"; |
110 | warn "Couldn't find shop keeper in " . $who->map->path . "\n"; |
108 | return cf::override; |
111 | return cf::override; |
109 | } |
112 | } |
110 | |
113 | |
111 | 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; |
112 | |
116 | |
113 | my $unpaid_items = {}; |
117 | my $unpaid_items = {}; |
114 | |
118 | |
115 | for my $item (find_unpaid ($who)) { |
119 | for my $item (find_unpaid ($who)) { |
116 | 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) { |
117 | audit_log ($who, 'removes', ob2info ($item)); |
121 | audit_log ($who, 'removes', ob2info ($item)); |
118 | $item->flag (cf::FLAG_UNPAID, 0); |
122 | $item->flag (cf::FLAG_UNPAID, 0); |
119 | $item->remove; |
123 | $item->remove; |
120 | $item->insert_ob_in_ob ($who); |
124 | $who->insert ($item); |
121 | next; |
125 | next; |
122 | } |
126 | } |
123 | |
127 | |
124 | 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); |
125 | |
129 | |
… | |
… | |
151 | } |
155 | } |
152 | |
156 | |
153 | audit_log ($who, 'removed', (join ",", @seller_noted)) |
157 | audit_log ($who, 'removed', (join ",", @seller_noted)) |
154 | if @seller_noted; |
158 | if @seller_noted; |
155 | |
159 | |
156 | $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; |
157 | |
162 | |
158 | cf::override; |
163 | cf::override; |
159 | }, |
164 | }, |
160 | ; |
165 | ; |
161 | |
166 | |
… | |
… | |
166 | |
171 | |
167 | return if $what->flag (cf::FLAG_UNPAID); |
172 | return if $what->flag (cf::FLAG_UNPAID); |
168 | |
173 | |
169 | if ($what->type == cf::MONEY) { |
174 | if ($what->type == cf::MONEY) { |
170 | $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); |
171 | $what->insert_ob_in_ob ($who); |
176 | $who->insert ($what); |
172 | return cf::override; |
177 | return cf::override; |
173 | } |
178 | } |
174 | |
179 | |
175 | if (!$what->flag (cf::FLAG_IDENTIFIED) && $what->need_identify) { |
180 | 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); |
181 | $who->message ("The shopkeeper says: Sorry, you can't sell unidentified stuff here.", cf::NDI_BROWN); |
177 | $what->insert_ob_in_ob ($who); |
182 | $who->insert ($what); |
178 | return cf::override; |
183 | return cf::override; |
179 | } |
184 | } |
180 | |
185 | |
181 | my $orig_value = $what->value; |
186 | my $orig_value = $what->value; |
182 | my $value = 0; |
187 | my $value = 0; |
183 | |
188 | |
184 | if ($name =~ m/\S/) { |
189 | if ($name =~ m/\S/) { |
185 | unless ($name =~ m/\d+\s*\S+/) { |
190 | 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); |
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); |
187 | $what->insert_ob_in_ob ($who); |
192 | $who->insert ($what); |
188 | return cf::override; |
193 | return cf::override; |
189 | } |
194 | } |
190 | |
195 | |
191 | while ($name =~ s/^\s*(\d+)\s*(\S+)//) { |
196 | while ($name =~ s/^\s*(\d+)\s*(\S+)//) { |
192 | if ($aliases{lc $2} or $unit{lc $2}) { |
197 | if ($aliases{lc $2} or $unit{lc $2}) { |
193 | $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}}); |
194 | } else { |
199 | } else { |
195 | $what->insert_ob_in_ob ($who); |
200 | $who->insert ($what); |
196 | $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); |
197 | return cf::override; |
202 | return cf::override; |
198 | } |
203 | } |
199 | } |
204 | } |
200 | } else { |
205 | } else { |
201 | $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); |
202 | } |
207 | } |
203 | |
208 | |
204 | if ($value < 0) { |
209 | if ($value < 0) { |
205 | $what->insert_ob_in_ob ($who); |
210 | $who->insert ($what); |
206 | $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); |
207 | return cf::override; |
212 | return cf::override; |
208 | } |
213 | } |
209 | |
214 | |
210 | my $fee = $value / 100; # 1% selling fee |
215 | my $fee = $value / 100; # 1% selling fee |
… | |
… | |
212 | unless ($who->pay_amount ($fee)) { |
217 | unless ($who->pay_amount ($fee)) { |
213 | $who->message ( |
218 | $who->message ( |
214 | "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", |
215 | cf::NDI_BROWN |
220 | cf::NDI_BROWN |
216 | ); |
221 | ); |
217 | $what->insert_ob_in_ob ($who); |
222 | $who->insert ($what); |
218 | return cf::override; |
223 | return cf::override; |
219 | } else { |
224 | } else { |
220 | $who->message ( |
225 | $who->message ( |
221 | "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", |
222 | cf::NDI_BROWN |
227 | cf::NDI_BROWN |