ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/maps/perl/reseller.ext
Revision: 1.15
Committed: Fri Oct 27 23:06:36 2006 UTC (17 years, 6 months ago) by root
Branch: MAIN
Changes since 1.14: +3 -2 lines
Log Message:
arena slight redesign

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
22 push @unpaid, find_unpaid ($i, $cb)
23 if $i->inv;
24 }
25 return @unpaid;
26 }
27
28 sub find_unpaid {
29 my ($ob) = @_;
30 my @r = find_rec ($ob, sub { $_[0]->flag (cf::FLAG_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;
38 }
39
40 cf::register_script_function "reseller::list_sells" => sub {
41 my ($who, $msg, $npc) = @_;
42 my $sells = cf::from_json $npc->get_ob_key_value ('ext_reseller_sales');
43 my $hissells = $sells->{$who->name};
44
45 unless (keys %{$hissells || {}}) {
46 $who->reply ($npc, "I'm sorry, but you sold nothing.\n");
47 return 0;
48 }
49
50 $who->reply ($npc, "You sold:\n", cf::NDI_BROWN);
51 for (keys %$hissells) {
52 my $n = $_;
53 $n =~ s/\s*\(unpaid\)//g;
54 $who->reply ($npc, "$n for " . cf::cost_string_from_value ($hissells->{$_}), cf::NDI_BROWN);
55 }
56
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);
79
80 0
81 };
82
83 cf::register_attachment "reseller_shopmat",
84 on_move_trigger => sub {
85 my ($self, $who_caused, $who) = @_;
86
87 my @obs = grep { $_->name eq $self->{reseller_shopmat}{npc_name} }
88 $who->map->at ($self->{reseller_shopmat}{npc_x}, $self->{reseller_shopmat}{npc_y});
89
90 unless (@obs) {
91 warn "Couldn't find shop keeper in " . $who->map . "\n";
92 return cf::override;
93 }
94
95 my $sells = cf::from_json $obs[0]->get_ob_key_value ('ext_reseller_sales');
96
97 for my $item (find_unpaid ($who)) {
98 if ($item->get_ob_key_value ('ext_reseller_seller') eq $who->name) {
99 $item->flag (cf::FLAG_UNPAID, 0);
100 $item->remove;
101 $item->insert_ob_in_ob ($who);
102 next;
103 }
104
105 my $value = $item->query_cost ($who, cf::F_BUY | cf::F_SHOP);
106
107 warn "Object " . $item->name . " bought by " . $who->name . " on map "
108 . $who->map->path . " for $value silver has no seller set\n"
109 if $item->get_ob_key_value ('ext_reseller_seller') eq '';
110
111 $sells->{$item->get_ob_key_value ('ext_reseller_seller')}->{$item->name} += $value;
112 }
113
114 $self->apply_shop_mat ($who);
115
116 for my $item (find_traded ($who)) {
117 next if $item->flag (cf::FLAG_UNPAID);
118 $item->value ($item->get_ob_key_value ('ext_reseller_orig_value'));
119 $item->set_ob_key_value (ext_reseller_seller => '');
120 }
121
122 $obs[0]->set_ob_key_value (ext_reseller_sales => cf::to_json $sells);
123
124 cf::override;
125 },
126 ;
127
128 cf::register_attachment "reseller_floor",
129 on_drop_on => sub {
130 my ($on, $what, $who) = @_;
131 my $name = $what->custom_name;
132
133 return if $what->flag (cf::FLAG_UNPAID);
134
135 if ($what->type == cf::MONEY) {
136 $who->message ("The shopkeeper says: Sorry, you can't sell money here.", cf::NDI_BROWN);
137 $what->insert_ob_in_ob ($who);
138 return cf::override;
139 }
140
141 if (!$what->flag (cf::FLAG_IDENTIFIED) && $what->need_identify) {
142 $who->message ("The shopkeeper says: Sorry, you can't sell unidentified stuff here.", cf::NDI_BROWN);
143 $what->insert_ob_in_ob ($who);
144 return cf::override;
145 }
146
147 my $orig_value = $what->value;
148 my $value = 0;
149
150 if ($name =~ m/\S/) {
151 unless ($name =~ m/\d+\s*\S+/) {
152 $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);
153 $what->insert_ob_in_ob ($who);
154 return cf::override;
155 }
156
157 while ($name =~ s/^\s*(\d+)\s*(\S+)//) {
158 if ($aliases{lc $2} or $unit{lc $2}) {
159 $value += $1 * ($unit{lc $2} ? $unit{lc $2} : $unit{$aliases{lc $2}});
160 } else {
161 $what->insert_ob_in_ob ($who);
162 $who->message ("The shopkeeper says: I don't know the currency '$2'", cf::NDI_BROWN);
163 return cf::override;
164 }
165 }
166 } else {
167 $value = $what->query_cost ($who, cf::F_SELL | cf::F_SHOP) / ($what->nrof || 1);
168 }
169
170 if ($value < 0) {
171 $what->insert_ob_in_ob ($who);
172 $who->message ("The shopkeeper says: You can't sell something for a negative value: $value", cf::NDI_BROWN);
173 return cf::override;
174 }
175
176 my $fee = $value / 100; # 1% selling fee
177
178 unless ($who->pay_amount ($fee)) {
179 $who->message (
180 "The shopkeeper says: You need " . cf::cost_string_from_value ($fee) . " to pay the 1% fee for this item",
181 cf::NDI_BROWN
182 );
183 $what->insert_ob_in_ob ($who);
184 return cf::override;
185 } else {
186 $who->message (
187 "The shopkeeper says: Ok, got the fee of " . cf::cost_string_from_value ($fee) . " for the item",
188 cf::NDI_BROWN
189 );
190 }
191
192 $what->value ($value);
193 my $cost = $what->query_cost ($who, cf::F_BUY | cf::F_SHOP) / ($what->nrof || 1);
194
195 my $fact = 0;
196 if ($cost) {
197 $fact = $value / $cost;
198 $what->value (cf::ceil ($value * $fact));
199 }
200
201 # warn "END VALUE: $value * $fact => " . $what->value . "\n";
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->custom_name ($what->name . " (by " . $who->name . ")");
217 $what->flag (cf::FLAG_UNPAID, 1);
218 $what->insert_ob_in_map_at ($who->map, $who, cf::INS_BELOW_ORIGINATOR, $who->x, $who->y);
219
220 cf::override;
221 },
222 ;