ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/ext/reseller.ext
Revision: 1.16
Committed: Thu Aug 20 18:27:21 2009 UTC (14 years, 9 months ago) by elmex
Branch: MAIN
CVS Tags: rel-2_82, rel-2_81, rel-2_80, rel-2_90, rel-2_92, rel-2_93
Changes since 1.15: +7 -0 lines
Log Message:
added small check to reseller code.

File Contents

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