ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/ext/reseller.ext
(Generate patch)

Comparing deliantra/server/ext/reseller.ext (file contents):
Revision 1.2 by elmex, Wed Dec 20 01:29:12 2006 UTC vs.
Revision 1.19 by root, Sun Jan 29 02:47:04 2017 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines