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