… | |
… | |
6 | platina => 50, |
6 | platina => 50, |
7 | royalty => 5000, |
7 | royalty => 5000, |
8 | ); |
8 | ); |
9 | |
9 | |
10 | my %aliases = ( |
10 | my %aliases = ( |
11 | platinum => 'platina', |
11 | platinum => 'platina', |
12 | royalties => 'royalty' |
12 | royalties => 'royalty', |
13 | ); |
13 | ); |
14 | |
14 | |
15 | sub find_rec { |
15 | sub find_rec { |
16 | my ($ob, $cb) = @_; |
16 | my ($ob, $cb) = @_; |
17 | |
17 | |
18 | my @unpaid; |
18 | my @unpaid; |
19 | for my $i ($ob->inv) { |
19 | for my $i ($ob->inv) { |
20 | push @unpaid, $i if $cb->($i); |
20 | push @unpaid, $i if $cb->($i); |
|
|
21 | |
21 | push @unpaid, find_unpaid ($i, $cb) |
22 | push @unpaid, find_unpaid ($i, $cb) |
22 | if $i->inv; |
23 | if $i->inv; |
23 | } |
24 | } |
24 | return @unpaid; |
25 | return @unpaid; |
25 | } |
26 | } |
26 | |
27 | |
27 | sub find_unpaid { |
28 | sub find_unpaid { |
28 | my ($ob) = @_; |
29 | my ($ob) = @_; |
29 | my @r = find_rec ($ob, sub { $_[0]->get_flag (cf::FLAG_UNPAID) }); |
30 | my @r = find_rec ($ob, sub { $_[0]->flag (cf::FLAG_UNPAID) }); |
30 | return @r; |
31 | return @r; |
31 | } |
32 | } |
32 | |
33 | |
33 | sub find_traded { |
34 | sub find_traded { |
34 | my ($ob) = @_; |
35 | my ($ob) = @_; |
… | |
… | |
89 | unless (@obs) { |
90 | unless (@obs) { |
90 | warn "Couldn't find shop keeper in " . $who->map . "\n"; |
91 | warn "Couldn't find shop keeper in " . $who->map . "\n"; |
91 | return cf::override; |
92 | return cf::override; |
92 | } |
93 | } |
93 | |
94 | |
94 | my $sells = cf::from_json $selfs[0]->get_ob_key_value ('ext_reseller_sales'); |
95 | my $sells = cf::from_json $obs[0]->get_ob_key_value ('ext_reseller_sales'); |
|
|
96 | |
|
|
97 | my $unpaid_items = {}; |
95 | |
98 | |
96 | for my $item (find_unpaid ($who)) { |
99 | for my $item (find_unpaid ($who)) { |
97 | if ($item->get_ob_key_value ('ext_reseller_seller') eq $who->name) { |
100 | if ($item->get_ob_key_value ('ext_reseller_seller') eq $who->name) { |
98 | $item->set_flag (cf::FLAG_UNPAID, 0); |
101 | $item->flag (cf::FLAG_UNPAID, 0); |
99 | $item->remove; |
102 | $item->remove; |
100 | $item->insert_ob_in_ob ($who); |
103 | $item->insert_ob_in_ob ($who); |
101 | next; |
104 | next; |
102 | } |
105 | } |
103 | |
106 | |
… | |
… | |
105 | |
108 | |
106 | warn "Object " . $item->name . " bought by " . $who->name . " on map " |
109 | warn "Object " . $item->name . " bought by " . $who->name . " on map " |
107 | . $who->map->path . " for $value silver has no seller set\n" |
110 | . $who->map->path . " for $value silver has no seller set\n" |
108 | if $item->get_ob_key_value ('ext_reseller_seller') eq ''; |
111 | if $item->get_ob_key_value ('ext_reseller_seller') eq ''; |
109 | |
112 | |
110 | $sells->{$item->get_ob_key_value ('ext_reseller_seller')}->{$item->name} += $value; |
113 | $unpaid_items->{$item->uuid} = $value; |
111 | } |
114 | } |
112 | |
115 | |
113 | $self->apply_shop_mat ($who); |
116 | $self->apply_shop_mat ($who); |
114 | |
117 | |
115 | for my $item (find_traded ($who)) { |
118 | for my $item (find_traded ($who)) { |
116 | next if $item->get_flag (cf::FLAG_UNPAID); |
119 | next if $item->flag (cf::FLAG_UNPAID); |
|
|
120 | if (my $value = $unpaid_items->{$item->uuid}) { |
|
|
121 | $sells->{$item->get_ob_key_value ('ext_reseller_seller')}->{$item->name} += $value; |
|
|
122 | } |
|
|
123 | |
117 | $item->set_value ($item->get_ob_key_value ('ext_reseller_orig_value')); |
124 | $item->value ($item->get_ob_key_value ('ext_reseller_orig_value')); |
118 | $item->set_ob_key_value (ext_reseller_seller => ''); |
125 | $item->set_ob_key_value (ext_reseller_seller => ''); |
119 | } |
126 | } |
120 | |
127 | |
121 | $obs[0]->set_ob_key_value (ext_reseller_sales => cf::to_json $sells); |
128 | $obs[0]->set_ob_key_value (ext_reseller_sales => cf::to_json $sells); |
|
|
129 | |
|
|
130 | cf::override; |
122 | }, |
131 | }, |
123 | ; |
132 | ; |
124 | |
133 | |
125 | cf::register "reseller_floor", |
134 | cf::register_attachment "reseller_floor", |
126 | on_drop_on => sub { |
135 | on_drop_on => sub { |
127 | my ($on, $what, $who) = @_; |
136 | my ($on, $what, $who) = @_; |
128 | my $name = $what->custom_name; |
137 | my $name = $what->custom_name; |
129 | |
138 | |
130 | return if $what->get_flag (cf::FLAG_UNPAID); |
139 | return if $what->flag (cf::FLAG_UNPAID); |
131 | |
140 | |
132 | if ($what->type == cf::MONEY) { |
141 | if ($what->type == cf::MONEY) { |
133 | $who->message ("The shopkeeper says: Sorry, you can't sell money here.", cf::NDI_BROWN); |
142 | $who->message ("The shopkeeper says: Sorry, you can't sell money here.", cf::NDI_BROWN); |
134 | $what->insert_ob_in_ob ($who); |
143 | $what->insert_ob_in_ob ($who); |
135 | return cf::override; |
144 | return cf::override; |
136 | } |
145 | } |
137 | |
146 | |
138 | if (!$what->get_flag (cf::FLAG_IDENTIFIED) && $what->need_identify) { |
147 | if (!$what->flag (cf::FLAG_IDENTIFIED) && $what->need_identify) { |
139 | $who->message ("The shopkeeper says: Sorry, you can't sell unidentified stuff here.", cf::NDI_BROWN); |
148 | $who->message ("The shopkeeper says: Sorry, you can't sell unidentified stuff here.", cf::NDI_BROWN); |
140 | $what->insert_ob_in_ob ($who); |
149 | $what->insert_ob_in_ob ($who); |
141 | return cf::override; |
150 | return cf::override; |
142 | } |
151 | } |
143 | |
152 | |
… | |
… | |
184 | "The shopkeeper says: Ok, got the fee of " . cf::cost_string_from_value ($fee) . " for the item", |
193 | "The shopkeeper says: Ok, got the fee of " . cf::cost_string_from_value ($fee) . " for the item", |
185 | cf::NDI_BROWN |
194 | cf::NDI_BROWN |
186 | ); |
195 | ); |
187 | } |
196 | } |
188 | |
197 | |
189 | $what->set_value ($value); |
198 | $what->value ($value); |
190 | my $cost = $what->query_cost ($who, cf::F_BUY | cf::F_SHOP) / ($what->nrof || 1); |
199 | my $cost = $what->query_cost ($who, cf::F_BUY | cf::F_SHOP) / ($what->nrof || 1); |
191 | |
200 | |
192 | my $fact = 0; |
201 | my $fact = 0; |
193 | if ($cost) { |
202 | if ($cost) { |
194 | $fact = $value / $cost; |
203 | $fact = $value / $cost; |
195 | $what->set_value (cf::ceil ($value * $fact)); |
204 | $what->value (cf::ceil ($value * $fact)); |
196 | } |
205 | } |
197 | |
206 | |
198 | # warn "END VALUE: $value * $fact => " . $what->value . "\n"; |
207 | # warn "END VALUE: $value * $fact => " . $what->value . "\n"; |
199 | |
208 | |
200 | # my $cost = $what->query_cost ($who, cf::F_BUY | cf::F_SHOP) / $what->nrof; |
209 | # my $cost = $what->query_cost ($who, cf::F_BUY | cf::F_SHOP) / $what->nrof; |
… | |
… | |
208 | ); |
217 | ); |
209 | |
218 | |
210 | $what->set_ob_key_value (ext_reseller_seller => $who->name); |
219 | $what->set_ob_key_value (ext_reseller_seller => $who->name); |
211 | $what->set_ob_key_value (ext_reseller_orig_value => $orig_value); |
220 | $what->set_ob_key_value (ext_reseller_orig_value => $orig_value); |
212 | # warn "SET SELLER ON " . $what->name . " + " . $what->{seller}->[0] . "\n"; |
221 | # warn "SET SELLER ON " . $what->name . " + " . $what->{seller}->[0] . "\n"; |
213 | $what->set_custom_name ( |
|
|
214 | $what->name . " (by " . $who->name . ")" |
222 | $what->custom_name ($what->name . " (by " . $who->name . ")"); |
215 | ); |
|
|
216 | $what->set_flag (cf::FLAG_UNPAID, 1); |
223 | $what->flag (cf::FLAG_UNPAID, 1); |
217 | $what->insert_ob_in_map_at ($who->map, $who, cf::INS_BELOW_ORIGINATOR, $who->x, $who->y); |
224 | $what->insert_ob_in_map_at ($who->map, $who, cf::INS_BELOW_ORIGINATOR, $who->x, $who->y); |
218 | |
225 | |
219 | cf::override; |
226 | cf::override; |
220 | }, |
227 | }, |
221 | ; |
228 | ; |