ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/maps/perl/reseller.ext
Revision: 1.10
Committed: Sun Aug 27 15:23:59 2006 UTC (17 years, 8 months ago) by root
Branch: MAIN
Changes since 1.9: +1 -1 lines
Log Message:
further conversion

File Contents

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