ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/ext/reseller.ext
Revision: 1.7
Committed: Mon Jun 11 21:38:13 2007 UTC (16 years, 11 months ago) by root
Branch: MAIN
CVS Tags: rel-2_2
Changes since 1.6: +1 -1 lines
Log Message:
port micropather to c++...

File Contents

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