ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/ext/reseller.ext
Revision: 1.11
Committed: Thu Apr 10 15:35:16 2008 UTC (16 years, 1 month ago) by root
Branch: MAIN
CVS Tags: rel-2_5, rel-2_52
Changes since 1.10: +11 -19 lines
Log Message:
*** empty log message ***

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->get_ob_key_value ('ext_reseller_seller'),
7 $item->get_ob_key_value ('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]->get_ob_key_value ('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->get_ob_key_value ('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 $who->reply ($npc, "You sold:\n", cf::NDI_BROWN);
54 for (keys %$hissells) {
55 my $n = $_;
56 $n =~ s/\s*\(unpaid\)//g;
57 $who->reply ($npc, "$n for " . cf::cost_string_from_value ($hissells->{$_}), cf::NDI_BROWN);
58 }
59
60 0
61 };
62
63 cf::register_script_function "reseller::pay_player" => sub {
64 my ($who, $msg, $npc) = @_;
65 my $ext_re_sales = $npc->get_ob_key_value ('ext_reseller_sales');
66 my $sells = $ext_re_sales && cf::decode_json $ext_re_sales;
67 my $hissells = $sells->{$who->name};
68
69 unless (keys %{$hissells || {}}) {
70 $who->reply ($npc, "I'm sorry, but you sold nothing.\n");
71 return 0;
72 }
73
74 my $sum = 0;
75 $sum += $_ for values %$hissells;
76
77 $who->pay_player ($sum);
78 $who->reply ($npc, "Here are the " . cf::cost_string_from_value ($sum) . " for your sales");
79
80 audit_log ($who, 'collects', "$sum silver");
81
82 $sells->{$who->name} = {};
83
84 $npc->set_ob_key_value (ext_reseller_sales => cf::encode_json $sells)
85 if $sells;
86
87 0
88 };
89
90 cf::object::attachment "reseller_shopmat",
91 on_move_trigger => sub {
92 my ($self, $who_caused, $who) = @_;
93
94 my @obs = grep { $_->name eq $self->{reseller_shopmat}{npc_name} }
95 $who->map->at ($self->{reseller_shopmat}{npc_x}, $self->{reseller_shopmat}{npc_y});
96
97 unless (@obs) {
98 warn "Couldn't find shop keeper in " . $who->map->path . "\n";
99 return cf::override;
100 }
101
102 my $ext_re_sales = $obs[0]->get_ob_key_value ('ext_reseller_sales');
103 my $sells = $ext_re_sales && cf::decode_json $ext_re_sales;
104
105 my $unpaid_items = {};
106
107 for my $item (find_unpaid ($who)) {
108 if ($item->get_ob_key_value ('ext_reseller_seller') eq $who->name) {
109 audit_log ($who, 'removes', ob2info ($item));
110 $item->flag (cf::FLAG_UNPAID, 0);
111 $item->remove;
112 give_back ($who, $item);
113 next;
114 }
115
116 my $value = $item->query_cost ($who, cf::F_BUY | cf::F_SHOP);
117
118 warn "Object " . $item->name . " bought by " . $who->name . " on map "
119 . $who->map->path . " for $value silver has no seller set\n"
120 if $item->get_ob_key_value ('ext_reseller_seller') eq '';
121
122 $unpaid_items->{$item} = [$value, $item];
123 }
124
125 audit_log ($who, 'wants', (join ",", map { ob2info ($_->[1], $_->[0]) } values %$unpaid_items))
126 if %$unpaid_items;
127
128 $self->apply_shop_mat ($who);
129
130 my @seller_noted;
131
132 for my $item (find_traded ($who)) {
133 next if $item->flag (cf::FLAG_UNPAID);
134 if (my $value = $unpaid_items->{$item}[0]) {
135 push @seller_noted, ob2info ($item, $value)."P";
136 $sells->{$item->get_ob_key_value ('ext_reseller_seller')}->{$item->name} += $value;
137 } else {
138 push @seller_noted, ob2info ($item)."T";
139 }
140
141 $item->value ($item->get_ob_key_value ('ext_reseller_orig_value'));
142 $item->set_ob_key_value (ext_reseller_seller => '');
143 }
144
145 audit_log ($who, 'removed', (join ",", @seller_noted))
146 if @seller_noted;
147
148 $obs[0]->set_ob_key_value (ext_reseller_sales => cf::encode_json $sells)
149 if $sells;
150
151 cf::override;
152 },
153 ;
154
155 sub give_back {
156 my ($who, $what) = @_;
157 $who->insert ($what);
158 $who->esrv_send_item ($what);
159 }
160
161 sub give_back_with_message {
162 my ($who, $what, $msg) = @_;
163 $who->message ($msg, cf::NDI_BROWN);
164 give_back ($who, $what);
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 give_back_with_message ($who, $what,
176 "The shopkeeper says: Sorry, you can't sell money here.");
177 return cf::override;
178 }
179
180 if (!$what->flag (cf::FLAG_IDENTIFIED) && $what->need_identify) {
181 give_back_with_message ($who, $what,
182 "The shopkeeper says: Sorry, you can't sell unidentified stuff here.");
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 give_back_with_message ($who, $what,
192 "The shopkeeper says: Sorry, I don't recognize '$name' as currency. "
193 . "Please name your item like '17 platinum' or '10 gold 8 silver'");
194 return cf::override;
195 }
196
197 while ($name =~ s/^\s*(\d+)\s*(\S+)//) {
198 my ($v, $c) = ($1, $2);
199 if (my $coin = cf::coin_from_name $c) {
200 $value += $v * $coin->value;
201 } else {
202 give_back_with_message ($who, $what,
203 "The shopkeeper says: I don't know the currency '$c', you can use one of these currencies: "
204 . (join ", ", cf::coin_names)
205 );
206 return cf::override;
207 }
208 }
209 } else {
210 # commented out the following line because too many just use the
211 # reseller shop as dumpyard:
212 # $value = $what->query_cost ($who, cf::F_SELL | cf::F_SHOP) / ($what->nrof || 1);
213 give_back_with_message ($who, $what,
214 "Sorry, you can't just sell stuff without assigning a price to it! "
215 . "Please name your item like '17 platinum' or '10 gold 8 silver' "
216 . "and drop it again. (To rename the item use the 'rename' "
217 . "context menu item in the inventory)."
218 );
219 return cf::override;
220 }
221
222 if ($value < 0) {
223 give_back_with_message ($who, $what,
224 "The shopkeeper says: You can't sell something for a negative value: $value");
225 return cf::override;
226 }
227
228 my $fee = $value / 100; # 1% selling fee
229
230 unless ($who->pay_amount ($fee)) {
231 give_back_with_message ($who, $what,
232 "The shopkeeper says: You need " . cf::cost_string_from_value ($fee)
233 . " to pay the 1% fee for this item");
234 return cf::override;
235 } else {
236 $who->message (
237 "The shopkeeper says: Ok, got the fee of " . cf::cost_string_from_value ($fee)
238 . " for the item",
239 cf::NDI_BROWN
240 );
241 }
242
243 $what->value ($value);
244 my $cost = $what->query_cost ($who, cf::F_BUY | cf::F_SHOP) / ($what->nrof || 1);
245
246 my $fact = 0;
247 if ($cost) {
248 $fact = $value / $cost;
249 $what->value (cf::ceil ($value * $fact));
250 }
251
252 # warn "END VALUE: $value * $fact => " . $what->value . "\n";
253
254 # my $cost = $what->query_cost ($who, cf::F_BUY | cf::F_SHOP) / $what->nrof;
255 # warn "COSTS NOW: $cost\n";
256
257 $who->message (
258 "The shopkeeper says: Ok, I marked "
259 . ($what->nrof || 1) . " " . $what->name . " to be sold for at least "
260 . cf::cost_string_from_value ($value)
261 . ($what->nrof > 1 ? " each" : ""), cf::NDI_BROWN
262 );
263
264 $what->set_ob_key_value (ext_reseller_seller => $who->name);
265 $what->set_ob_key_value (ext_reseller_orig_value => $orig_value);
266 # warn "SET SELLER ON " . $what->name . " + " . $what->{seller}->[0] . "\n";
267 $what->custom_name ($what->name . " (by " . $who->name . ")");
268 $what->flag (cf::FLAG_UNPAID, 1);
269 $what->insert_ob_in_map_at ($who->map, $who, cf::INS_BELOW_ORIGINATOR, $who->x, $who->y);
270
271 audit_log ($who, 'sells', ob2info ($what));
272
273 cf::override;
274 },
275 ;