ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/ext/reseller.ext
Revision: 1.13
Committed: Sun Aug 31 08:54:19 2008 UTC (15 years, 8 months ago) by root
Branch: MAIN
CVS Tags: rel-2_7
Changes since 1.12: +12 -9 lines
Log Message:
improve formatting

File Contents

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