ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/ext/reseller.ext
Revision: 1.12
Committed: Sat May 3 11:14:50 2008 UTC (16 years ago) by root
Branch: MAIN
CVS Tags: rel-2_6, rel-2_54, rel-2_55, rel-2_56, rel-2_53, rel-2_61
Changes since 1.11: +15 -15 lines
Log Message:
kv rewrite

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     $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 root 1.12 my $ext_re_sales = $npc->kv_get ('ext_reseller_sales');
66 root 1.9 my $sells = $ext_re_sales && cf::decode_json $ext_re_sales;
67 root 1.1 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 elmex 1.2 audit_log ($who, 'collects', "$sum silver");
81    
82 root 1.1 $sells->{$who->name} = {};
83    
84 root 1.12 $npc->kv_set (ext_reseller_sales => cf::encode_json $sells)
85 elmex 1.6 if $sells;
86 root 1.1
87     0
88     };
89    
90 root 1.3 cf::object::attachment "reseller_shopmat",
91 root 1.1 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 elmex 1.2 warn "Couldn't find shop keeper in " . $who->map->path . "\n";
99 root 1.1 return cf::override;
100     }
101    
102 root 1.12 my $ext_re_sales = $obs[0]->kv_get ('ext_reseller_sales');
103 root 1.9 my $sells = $ext_re_sales && cf::decode_json $ext_re_sales;
104 root 1.1
105     my $unpaid_items = {};
106    
107     for my $item (find_unpaid ($who)) {
108 root 1.12 if ($item->kv_get ('ext_reseller_seller') eq $who->name) {
109 elmex 1.2 audit_log ($who, 'removes', ob2info ($item));
110 root 1.1 $item->flag (cf::FLAG_UNPAID, 0);
111     $item->remove;
112 elmex 1.10 give_back ($who, $item);
113 root 1.1 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 root 1.12 if $item->kv_get ('ext_reseller_seller') eq '';
121 root 1.1
122     $unpaid_items->{$item} = [$value, $item];
123     }
124    
125 elmex 1.2 audit_log ($who, 'wants', (join ",", map { ob2info ($_->[1], $_->[0]) } values %$unpaid_items))
126     if %$unpaid_items;
127    
128 root 1.1 $self->apply_shop_mat ($who);
129    
130 elmex 1.2 my @seller_noted;
131    
132 root 1.1 for my $item (find_traded ($who)) {
133     next if $item->flag (cf::FLAG_UNPAID);
134     if (my $value = $unpaid_items->{$item}[0]) {
135 elmex 1.2 push @seller_noted, ob2info ($item, $value)."P";
136 root 1.12 $sells->{$item->kv_get ('ext_reseller_seller')}->{$item->name} += $value;
137 elmex 1.2 } else {
138     push @seller_noted, ob2info ($item)."T";
139 root 1.1 }
140    
141 root 1.12 $item->value ($item->kv_get ('ext_reseller_orig_value'));
142     $item->kv_del ("ext_reseller_seller");
143 root 1.1 }
144    
145 elmex 1.2 audit_log ($who, 'removed', (join ",", @seller_noted))
146     if @seller_noted;
147    
148 root 1.12 $obs[0]->kv_set (ext_reseller_sales => cf::encode_json $sells)
149 elmex 1.6 if $sells;
150 root 1.1
151     cf::override;
152     },
153     ;
154    
155 elmex 1.10 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 root 1.3 cf::object::attachment "reseller_floor",
168 root 1.1 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 elmex 1.10 give_back_with_message ($who, $what,
176     "The shopkeeper says: Sorry, you can't sell money here.");
177 root 1.1 return cf::override;
178     }
179    
180     if (!$what->flag (cf::FLAG_IDENTIFIED) && $what->need_identify) {
181 elmex 1.10 give_back_with_message ($who, $what,
182     "The shopkeeper says: Sorry, you can't sell unidentified stuff here.");
183 root 1.1 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 elmex 1.10 give_back_with_message ($who, $what,
192     "The shopkeeper says: Sorry, I don't recognize '$name' as currency. "
193 root 1.11 . "Please name your item like '17 platinum' or '10 gold 8 silver'");
194 root 1.1 return cf::override;
195     }
196    
197     while ($name =~ s/^\s*(\d+)\s*(\S+)//) {
198 root 1.11 my ($v, $c) = ($1, $2);
199     if (my $coin = cf::coin_from_name $c) {
200     $value += $v * $coin->value;
201 root 1.1 } else {
202 elmex 1.10 give_back_with_message ($who, $what,
203 root 1.11 "The shopkeeper says: I don't know the currency '$c', you can use one of these currencies: "
204     . (join ", ", cf::coin_names)
205     );
206 root 1.1 return cf::override;
207     }
208     }
209     } else {
210 elmex 1.10 # 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 root 1.11 . "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 elmex 1.10 return cf::override;
220 root 1.1 }
221    
222     if ($value < 0) {
223 elmex 1.10 give_back_with_message ($who, $what,
224     "The shopkeeper says: You can't sell something for a negative value: $value");
225 root 1.1 return cf::override;
226     }
227    
228     my $fee = $value / 100; # 1% selling fee
229    
230     unless ($who->pay_amount ($fee)) {
231 elmex 1.10 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 root 1.1 return cf::override;
235     } else {
236     $who->message (
237 elmex 1.10 "The shopkeeper says: Ok, got the fee of " . cf::cost_string_from_value ($fee)
238     . " for the item",
239 root 1.1 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 root 1.12 $what->kv_set (ext_reseller_seller => $who->name);
265     $what->kv_set (ext_reseller_orig_value => $orig_value);
266 root 1.1 # 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 elmex 1.2 audit_log ($who, 'sells', ob2info ($what));
272    
273 root 1.1 cf::override;
274     },
275     ;