… | |
… | |
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 | |
|
|
15 | sub find_rec { |
|
|
16 | my ($ob, $cb) = @_; |
|
|
17 | |
|
|
18 | my @unpaid; |
|
|
19 | for my $i ($ob->inv) { |
|
|
20 | push @unpaid, $i if $cb->($i); |
|
|
21 | |
|
|
22 | push @unpaid, find_unpaid ($i, $cb) |
|
|
23 | if $i->inv; |
|
|
24 | } |
|
|
25 | return @unpaid; |
|
|
26 | } |
14 | |
27 | |
15 | sub find_unpaid { |
28 | sub find_unpaid { |
16 | my ($ob) = @_; |
29 | my ($ob) = @_; |
17 | |
30 | my @r = find_rec ($ob, sub { $_[0]->flag (cf::FLAG_UNPAID) }); |
18 | my @unpaid; |
|
|
19 | for ($ob->inv) { |
|
|
20 | push @unpaid, $_ |
|
|
21 | if $_->get_flag (cf::FLAG_UNPAID); |
|
|
22 | |
|
|
23 | if ($_->inv) { |
|
|
24 | push @unpaid, find_unpaid ($_->inv); |
|
|
25 | } |
|
|
26 | } |
|
|
27 | return @unpaid; |
31 | return @r; |
|
|
32 | } |
|
|
33 | |
|
|
34 | sub find_traded { |
|
|
35 | my ($ob) = @_; |
|
|
36 | my @r = find_rec ($ob, sub { $_[0]->get_ob_key_value ('ext_reseller_seller') ne '' }); |
|
|
37 | return @r; |
28 | } |
38 | } |
29 | |
39 | |
30 | cf::register_script_function "reseller::list_sells" => sub { |
40 | cf::register_script_function "reseller::list_sells" => sub { |
31 | my ($who, $msg, $npc) = @_; |
41 | my ($who, $msg, $npc) = @_; |
32 | my $sells = $npc->{sells}->{$who->name}; |
42 | my $sells = cf::from_json $npc->get_ob_key_value ('ext_reseller_sales'); |
|
|
43 | my $hissells = $sells->{$who->name}; |
33 | |
44 | |
34 | unless (keys %{$sells || {}}) { |
45 | unless (keys %{$hissells || {}}) { |
35 | $who->reply ($npc, "I'm sorry, but you sold nothing.\n"); |
46 | $who->reply ($npc, "I'm sorry, but you sold nothing.\n"); |
36 | return 0; |
47 | return 0; |
37 | } |
48 | } |
38 | |
49 | |
39 | $who->message ($npc->name . " says: You sold:\n", cf::NDI_BROWN); |
50 | $who->reply ($npc, "You sold:\n", cf::NDI_BROWN); |
40 | for (keys %$sells) { |
51 | for (keys %$hissells) { |
41 | my $n = $_; |
52 | my $n = $_; |
42 | $n =~ s/\s*\(unpaid\)//g; |
53 | $n =~ s/\s*\(unpaid\)//g; |
43 | $who->message ("$n for " . cf::cost_string_from_value ($sells->{$_}), cf::NDI_BROWN); |
54 | $who->reply ($npc, "$n for " . cf::cost_string_from_value ($hissells->{$_}), cf::NDI_BROWN); |
44 | } |
55 | } |
45 | |
56 | |
46 | 0 |
57 | 0 |
47 | }; |
58 | }; |
48 | |
59 | |
49 | sub on_trigger { |
60 | cf::register_script_function "reseller::pay_player" => sub { |
|
|
61 | my ($who, $msg, $npc) = @_; |
|
|
62 | my $sells = cf::from_json $npc->get_ob_key_value ('ext_reseller_sales'); |
|
|
63 | my $hissells = $sells->{$who->name}; |
|
|
64 | |
|
|
65 | unless (keys %{$hissells || {}}) { |
|
|
66 | $who->reply ($npc, "I'm sorry, but you sold nothing.\n"); |
|
|
67 | return 0; |
|
|
68 | } |
|
|
69 | |
|
|
70 | my $sum = 0; |
|
|
71 | $sum += $_ for values %$hissells; |
|
|
72 | |
|
|
73 | $who->pay_player ($sum); |
|
|
74 | $who->reply ($npc, "Here are the " . cf::cost_string_from_value ($sum) . " for your sales"); |
|
|
75 | |
|
|
76 | $sells->{$who->name} = {}; |
|
|
77 | |
|
|
78 | $npc->set_ob_key_value (ext_reseller_sales => cf::to_json $sells); |
|
|
79 | |
|
|
80 | 0 |
|
|
81 | }; |
|
|
82 | |
|
|
83 | cf::register_attachment "reseller_shopmat", |
|
|
84 | on_move_trigger => sub { |
50 | my ($ev, $ob, $who_caused, $who) = @_; |
85 | my ($self, $who_caused, $who) = @_; |
51 | my $opt = $ev->options; |
86 | |
52 | warn "OPT $opt\n"; |
87 | my @obs = grep { $_->name eq $self->{reseller_shopmat}{npc_name} } |
53 | return 1 unless $opt =~ m/(\S+),(\d+),(\d+)/; |
88 | $who->map->at ($self->{reseller_shopmat}{npc_x}, $self->{reseller_shopmat}{npc_y}); |
54 | my @obs = grep { $_->name eq $1 } $who->map->at ($2, $3); |
89 | |
55 | unless (@obs) { |
90 | unless (@obs) { |
56 | warn "Couldn't find shop keeper in " . $who->map . "\n"; |
91 | warn "Couldn't find shop keeper in " . $who->map . "\n"; |
57 | return 1; |
92 | return cf::override; |
58 | } |
93 | } |
59 | warn "FOOOOGEOG ".$obs[0]->name." <<\n"; |
|
|
60 | |
94 | |
|
|
95 | my $sells = cf::from_json $obs[0]->get_ob_key_value ('ext_reseller_sales'); |
|
|
96 | |
|
|
97 | my $unpaid_items = {}; |
|
|
98 | |
61 | for my $item (find_unpaid ($who)) { |
99 | for my $item (find_unpaid ($who)) { |
|
|
100 | if ($item->get_ob_key_value ('ext_reseller_seller') eq $who->name) { |
|
|
101 | $item->flag (cf::FLAG_UNPAID, 0); |
|
|
102 | $item->remove; |
|
|
103 | $item->insert_ob_in_ob ($who); |
|
|
104 | next; |
|
|
105 | } |
|
|
106 | |
62 | my $value = $item->query_cost ($who, cf::F_BUY | cf::F_SHOP); |
107 | my $value = $item->query_cost ($who, cf::F_BUY | cf::F_SHOP); |
63 | |
108 | |
64 | warn "Object " . $item->name . " bought by " . $who->name . " on map " |
|
|
65 | . $who->map->path . " for $value silver has no seller set\n" |
|
|
66 | unless $item->{seller}; |
|
|
67 | |
|
|
68 | $obs[0]->{sells}->{$item->{seller} || ''}->{$item->name} += $value; |
|
|
69 | |
|
|
70 | 1 and |
|
|
71 | warn "Object " . $item->name . " bought by " . $who->name . " on map " |
109 | warn "Object " . $item->name . " bought by " . $who->name . " on map " |
72 | . $who->map->path . " for $value silver sold by " . $item->{seller} . "\n"; |
110 | . $who->map->path . " for $value silver has no seller set\n" |
|
|
111 | if $item->get_ob_key_value ('ext_reseller_seller') eq ''; |
|
|
112 | |
|
|
113 | $unpaid_items->{$item} = [$value, $item]; |
|
|
114 | } |
|
|
115 | |
|
|
116 | $self->apply_shop_mat ($who); |
|
|
117 | |
|
|
118 | for my $item (find_traded ($who)) { |
|
|
119 | next if $item->flag (cf::FLAG_UNPAID); |
|
|
120 | if (my $value = $unpaid_items->{$item}[0]) { |
|
|
121 | $sells->{$item->get_ob_key_value ('ext_reseller_seller')}->{$item->name} += $value; |
|
|
122 | } |
|
|
123 | |
|
|
124 | $item->value ($item->get_ob_key_value ('ext_reseller_orig_value')); |
|
|
125 | $item->set_ob_key_value (ext_reseller_seller => ''); |
|
|
126 | } |
|
|
127 | |
|
|
128 | $obs[0]->set_ob_key_value (ext_reseller_sales => cf::to_json $sells); |
|
|
129 | |
|
|
130 | cf::override; |
73 | } |
131 | }, |
74 | use Data::Dumper; |
132 | ; |
75 | warn "DO[" .Data::Dumper::Dumper ([$obs[0]]) . "]\n"; |
|
|
76 | return 0; |
|
|
77 | } |
|
|
78 | |
133 | |
79 | sub on_drop_on { |
134 | cf::register_attachment "reseller_floor", |
80 | my ($ev, $on, $who, $what) = @_; |
135 | on_drop_on => sub { |
|
|
136 | my ($on, $what, $who) = @_; |
81 | my $name = $what->custom_name; |
137 | my $name = $what->custom_name; |
82 | |
138 | |
83 | if ($what->get_flag (cf::FLAG_UNPAID)) { |
139 | return if $what->flag (cf::FLAG_UNPAID); |
84 | return 0; |
|
|
85 | } |
|
|
86 | |
140 | |
87 | if (!$what->get_flag (cf::FLAG_IDENTIFIED)) { |
141 | if ($what->type == cf::MONEY) { |
88 | $who->message ("The shopkeeper says: Sorry, i don't accept unidentified stuff."); |
142 | $who->message ("The shopkeeper says: Sorry, you can't sell money here.", cf::NDI_BROWN); |
89 | $what->insert_ob_in_ob ($who); |
143 | $what->insert_ob_in_ob ($who); |
90 | return 1; |
144 | return cf::override; |
91 | } |
145 | } |
92 | |
146 | |
|
|
147 | if (!$what->flag (cf::FLAG_IDENTIFIED) && $what->need_identify) { |
|
|
148 | $who->message ("The shopkeeper says: Sorry, you can't sell unidentified stuff here.", cf::NDI_BROWN); |
|
|
149 | $what->insert_ob_in_ob ($who); |
|
|
150 | return cf::override; |
|
|
151 | } |
|
|
152 | |
|
|
153 | my $orig_value = $what->value; |
|
|
154 | my $value = 0; |
|
|
155 | |
|
|
156 | if ($name =~ m/\S/) { |
93 | unless ($name =~ m/\d+\s*\S+/) { |
157 | unless ($name =~ m/\d+\s*\S+/) { |
94 | $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); |
158 | $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); |
95 | $what->insert_ob_in_ob ($who); |
159 | $what->insert_ob_in_ob ($who); |
96 | return 1; |
160 | return cf::override; |
97 | } |
161 | } |
98 | |
162 | |
99 | my $value = 0; |
|
|
100 | while ($name =~ s/^\s*(\d+)\s*(\S+)//) { |
163 | while ($name =~ s/^\s*(\d+)\s*(\S+)//) { |
101 | if ($aliases{lc $2} or $unit{lc $2}) { |
164 | if ($aliases{lc $2} or $unit{lc $2}) { |
102 | $value += $1 * ($unit{lc $2} ? $unit{lc $2} : $unit{$aliases{lc $2}}); |
165 | $value += $1 * ($unit{lc $2} ? $unit{lc $2} : $unit{$aliases{lc $2}}); |
|
|
166 | } else { |
|
|
167 | $what->insert_ob_in_ob ($who); |
|
|
168 | $who->message ("The shopkeeper says: I don't know the currency '$2'", cf::NDI_BROWN); |
|
|
169 | return cf::override; |
|
|
170 | } |
|
|
171 | } |
103 | } else { |
172 | } else { |
104 | $what->insert_ob_in_ob ($who); |
173 | $value = $what->query_cost ($who, cf::F_SELL | cf::F_SHOP) / ($what->nrof || 1); |
105 | $who->message ("The shopkeeper says: I don't know the currency '$2'", cf::NDI_BROWN); |
|
|
106 | return 1; |
|
|
107 | } |
174 | } |
108 | } |
|
|
109 | |
175 | |
|
|
176 | if ($value < 0) { |
|
|
177 | $what->insert_ob_in_ob ($who); |
|
|
178 | $who->message ("The shopkeeper says: You can't sell something for a negative value: $value", cf::NDI_BROWN); |
|
|
179 | return cf::override; |
|
|
180 | } |
110 | |
181 | |
111 | my $nrof = $what->nrof; |
182 | my $fee = $value / 100; # 1% selling fee |
112 | $what->set_nrof (1); |
183 | |
|
|
184 | unless ($who->pay_amount ($fee)) { |
|
|
185 | $who->message ( |
|
|
186 | "The shopkeeper says: You need " . cf::cost_string_from_value ($fee) . " to pay the 1% fee for this item", |
|
|
187 | cf::NDI_BROWN |
|
|
188 | ); |
|
|
189 | $what->insert_ob_in_ob ($who); |
|
|
190 | return cf::override; |
|
|
191 | } else { |
|
|
192 | $who->message ( |
|
|
193 | "The shopkeeper says: Ok, got the fee of " . cf::cost_string_from_value ($fee) . " for the item", |
|
|
194 | cf::NDI_BROWN |
|
|
195 | ); |
|
|
196 | } |
|
|
197 | |
113 | $what->set_value ($value); |
198 | $what->value ($value); |
114 | my $cost = $what->query_cost ($who, cf::F_BUY | cf::F_SHOP); |
199 | my $cost = $what->query_cost ($who, cf::F_BUY | cf::F_SHOP) / ($what->nrof || 1); |
115 | |
200 | |
|
|
201 | my $fact = 0; |
116 | if ($cost) { |
202 | if ($cost) { |
117 | my $fact = $value / $cost; |
203 | $fact = $value / $cost; |
118 | $what->set_value (cf::ceil ($value * $fact)); |
204 | $what->value (cf::ceil ($value * $fact)); |
119 | } |
205 | } |
120 | |
206 | |
121 | $what->set_nrof ($nrof); |
207 | # warn "END VALUE: $value * $fact => " . $what->value . "\n"; |
122 | $who->message ("The shopkeeper says: Ok, I marked " . $what->nrof . " " . $what->name . " to sell for " . cf::cost_string_from_value ($value) . " a piece", cf::NDI_BROWN); |
208 | |
|
|
209 | # my $cost = $what->query_cost ($who, cf::F_BUY | cf::F_SHOP) / $what->nrof; |
|
|
210 | # warn "COSTS NOW: $cost\n"; |
|
|
211 | |
|
|
212 | $who->message ( |
|
|
213 | "The shopkeeper says: Ok, I marked " |
|
|
214 | . ($what->nrof || 1) . " " . $what->name . " to be sold for at least " |
|
|
215 | . cf::cost_string_from_value ($value) |
|
|
216 | . ($what->nrof > 1 ? " each" : ""), cf::NDI_BROWN |
|
|
217 | ); |
|
|
218 | |
|
|
219 | $what->set_ob_key_value (ext_reseller_seller => $who->name); |
|
|
220 | $what->set_ob_key_value (ext_reseller_orig_value => $orig_value); |
|
|
221 | # warn "SET SELLER ON " . $what->name . " + " . $what->{seller}->[0] . "\n"; |
|
|
222 | $what->custom_name ($what->name . " (by " . $who->name . ")"); |
123 | $what->set_flag (cf::FLAG_UNPAID, 1); |
223 | $what->flag (cf::FLAG_UNPAID, 1); |
124 | $what->{seller} = $who->name; |
|
|
125 | $what->set_custom_name (undef); |
|
|
126 | $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); |
127 | 1; |
225 | |
128 | } |
226 | cf::override; |
|
|
227 | }, |
|
|
228 | ; |