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.11 by root, Thu Apr 10 15:35:16 2008 UTC vs.
Revision 1.13 by root, Sun Aug 31 08:54:19 2008 UTC

1#!perl # mandatory 1#!perl # mandatory
2 2
3sub ob2info { 3sub ob2info {
4 my ($item, $rval) = @_; 4 my ($item, $rval) = @_;
5 sprintf "[%s from %s (%d:%d%s) nrof: %d uuid: %s]", 5 sprintf "[%s from %s (%d:%d%s) nrof: %d uuid: %s]",
6 $item->name, $item->get_ob_key_value ('ext_reseller_seller'), 6 $item->name, $item->kv_get ('ext_reseller_seller'),
7 $item->get_ob_key_value ('ext_reseller_orig_value'), $item->value, 7 $item->kv_get ('ext_reseller_orig_value'), $item->value,
8 (defined $rval ? ":$rval" : ""), $item->nrof, $item->uuid; 8 (defined $rval ? ":$rval" : ""), $item->nrof, $item->uuid;
9} 9}
10 10
11sub audit_log { 11sub audit_log {
12 my ($who, $action, $info) = @_; 12 my ($who, $action, $info) = @_;
34 find_rec $ob, sub { $_[0]->flag (cf::FLAG_UNPAID) }; 34 find_rec $ob, sub { $_[0]->flag (cf::FLAG_UNPAID) };
35} 35}
36 36
37sub find_traded { 37sub find_traded {
38 my ($ob) = @_; 38 my ($ob) = @_;
39 find_rec $ob, sub { $_[0]->get_ob_key_value ('ext_reseller_seller') ne '' }; 39 find_rec $ob, sub { $_[0]->kv_get ('ext_reseller_seller') ne '' };
40} 40}
41 41
42cf::register_script_function "reseller::list_sells" => sub { 42cf::register_script_function "reseller::list_sells" => sub {
43 my ($who, $msg, $npc) = @_; 43 my ($who, $msg, $npc) = @_;
44 my $ext_re_sales = $npc->get_ob_key_value ('ext_reseller_sales'); 44 my $ext_re_sales = $npc->kv_get ('ext_reseller_sales');
45 my $sells = $ext_re_sales && cf::decode_json $ext_re_sales; 45 my $sells = $ext_re_sales && cf::decode_json $ext_re_sales;
46 my $hissells = $sells->{$who->name}; 46 my $hissells = $sells->{$who->name};
47 47
48 unless (keys %{$hissells || {}}) { 48 unless (keys %{$hissells || {}}) {
49 $who->reply ($npc, "I'm sorry, but you sold nothing.\n"); 49 $who->reply ($npc, "I'm sorry, but you sold nothing.\n");
50 return 0; 50 return 0;
51 } 51 }
52 52
53 $who->reply ($npc, "You sold:\n", cf::NDI_BROWN); 53 my $reply = "You sold:\n\n";
54
54 for (keys %$hissells) { 55 for (keys %$hissells) {
55 my $n = $_; 56 my $n = $_;
56 $n =~ s/\s*\(unpaid\)//g; 57 $n =~ s/\s*\(unpaid\)//g;
57 $who->reply ($npc, "$n for " . cf::cost_string_from_value ($hissells->{$_}), cf::NDI_BROWN); 58 $reply .= " B<$n> (for " . cf::cost_string_from_value ($hissells->{$_}) . ")\n";
58 } 59 }
60
61 $who->reply ($npc, $reply);
59 62
60 0 63 0
61}; 64};
62 65
63cf::register_script_function "reseller::pay_player" => sub { 66cf::register_script_function "reseller::pay_player" => sub {
64 my ($who, $msg, $npc) = @_; 67 my ($who, $msg, $npc) = @_;
65 my $ext_re_sales = $npc->get_ob_key_value ('ext_reseller_sales'); 68 my $ext_re_sales = $npc->kv_get ('ext_reseller_sales');
66 my $sells = $ext_re_sales && cf::decode_json $ext_re_sales; 69 my $sells = $ext_re_sales && cf::decode_json $ext_re_sales;
67 my $hissells = $sells->{$who->name}; 70 my $hissells = $sells->{$who->name};
68 71
69 unless (keys %{$hissells || {}}) { 72 unless (keys %{$hissells || {}}) {
70 $who->reply ($npc, "I'm sorry, but you sold nothing.\n"); 73 $who->reply ($npc, "I'm sorry, but you sold nothing.\n");
73 76
74 my $sum = 0; 77 my $sum = 0;
75 $sum += $_ for values %$hissells; 78 $sum += $_ for values %$hissells;
76 79
77 $who->pay_player ($sum); 80 $who->pay_player ($sum);
78 $who->reply ($npc, "Here are the " . cf::cost_string_from_value ($sum) . " for your sales"); 81 $who->reply ($npc, "Here are the " . cf::cost_string_from_value ($sum) . " for your sales.");
79 82
80 audit_log ($who, 'collects', "$sum silver"); 83 audit_log ($who, 'collects', "$sum silver");
81 84
82 $sells->{$who->name} = {}; 85 $sells->{$who->name} = {};
83 86
84 $npc->set_ob_key_value (ext_reseller_sales => cf::encode_json $sells) 87 $npc->kv_set (ext_reseller_sales => cf::encode_json $sells)
85 if $sells; 88 if $sells;
86 89
87 0 90 0
88}; 91};
89 92
97 unless (@obs) { 100 unless (@obs) {
98 warn "Couldn't find shop keeper in " . $who->map->path . "\n"; 101 warn "Couldn't find shop keeper in " . $who->map->path . "\n";
99 return cf::override; 102 return cf::override;
100 } 103 }
101 104
102 my $ext_re_sales = $obs[0]->get_ob_key_value ('ext_reseller_sales'); 105 my $ext_re_sales = $obs[0]->kv_get ('ext_reseller_sales');
103 my $sells = $ext_re_sales && cf::decode_json $ext_re_sales; 106 my $sells = $ext_re_sales && cf::decode_json $ext_re_sales;
104 107
105 my $unpaid_items = {}; 108 my $unpaid_items = {};
106 109
107 for my $item (find_unpaid ($who)) { 110 for my $item (find_unpaid ($who)) {
108 if ($item->get_ob_key_value ('ext_reseller_seller') eq $who->name) { 111 if ($item->kv_get ('ext_reseller_seller') eq $who->name) {
109 audit_log ($who, 'removes', ob2info ($item)); 112 audit_log ($who, 'removes', ob2info ($item));
110 $item->flag (cf::FLAG_UNPAID, 0); 113 $item->flag (cf::FLAG_UNPAID, 0);
111 $item->remove; 114 $item->remove;
112 give_back ($who, $item); 115 give_back ($who, $item);
113 next; 116 next;
115 118
116 my $value = $item->query_cost ($who, cf::F_BUY | cf::F_SHOP); 119 my $value = $item->query_cost ($who, cf::F_BUY | cf::F_SHOP);
117 120
118 warn "Object " . $item->name . " bought by " . $who->name . " on map " 121 warn "Object " . $item->name . " bought by " . $who->name . " on map "
119 . $who->map->path . " for $value silver has no seller set\n" 122 . $who->map->path . " for $value silver has no seller set\n"
120 if $item->get_ob_key_value ('ext_reseller_seller') eq ''; 123 if $item->kv_get ('ext_reseller_seller') eq '';
121 124
122 $unpaid_items->{$item} = [$value, $item]; 125 $unpaid_items->{$item} = [$value, $item];
123 } 126 }
124 127
125 audit_log ($who, 'wants', (join ",", map { ob2info ($_->[1], $_->[0]) } values %$unpaid_items)) 128 audit_log ($who, 'wants', (join ",", map { ob2info ($_->[1], $_->[0]) } values %$unpaid_items))
131 134
132 for my $item (find_traded ($who)) { 135 for my $item (find_traded ($who)) {
133 next if $item->flag (cf::FLAG_UNPAID); 136 next if $item->flag (cf::FLAG_UNPAID);
134 if (my $value = $unpaid_items->{$item}[0]) { 137 if (my $value = $unpaid_items->{$item}[0]) {
135 push @seller_noted, ob2info ($item, $value)."P"; 138 push @seller_noted, ob2info ($item, $value)."P";
136 $sells->{$item->get_ob_key_value ('ext_reseller_seller')}->{$item->name} += $value; 139 $sells->{$item->kv_get ('ext_reseller_seller')}->{$item->name} += $value;
137 } else { 140 } else {
138 push @seller_noted, ob2info ($item)."T"; 141 push @seller_noted, ob2info ($item)."T";
139 } 142 }
140 143
141 $item->value ($item->get_ob_key_value ('ext_reseller_orig_value')); 144 $item->value ($item->kv_get ('ext_reseller_orig_value'));
142 $item->set_ob_key_value (ext_reseller_seller => ''); 145 $item->kv_del ("ext_reseller_seller");
143 } 146 }
144 147
145 audit_log ($who, 'removed', (join ",", @seller_noted)) 148 audit_log ($who, 'removed', (join ",", @seller_noted))
146 if @seller_noted; 149 if @seller_noted;
147 150
148 $obs[0]->set_ob_key_value (ext_reseller_sales => cf::encode_json $sells) 151 $obs[0]->kv_set (ext_reseller_sales => cf::encode_json $sells)
149 if $sells; 152 if $sells;
150 153
151 cf::override; 154 cf::override;
152 }, 155 },
153; 156;
188 191
189 if ($name =~ m/\S/) { 192 if ($name =~ m/\S/) {
190 unless ($name =~ m/\d+\s*\S+/) { 193 unless ($name =~ m/\d+\s*\S+/) {
191 give_back_with_message ($who, $what, 194 give_back_with_message ($who, $what,
192 "The shopkeeper says: Sorry, I don't recognize '$name' as currency. " 195 "The shopkeeper says: Sorry, I don't recognize '$name' as currency. "
193 . "Please name your item like '17 platinum' or '10 gold 8 silver'"); 196 . "Please name your item like '17 platinum' or '10 gold 8 silver.'");
194 return cf::override; 197 return cf::override;
195 } 198 }
196 199
197 while ($name =~ s/^\s*(\d+)\s*(\S+)//) { 200 while ($name =~ s/^\s*(\d+)\s*(\S+)//) {
198 my ($v, $c) = ($1, $2); 201 my ($v, $c) = ($1, $2);
211 # reseller shop as dumpyard: 214 # reseller shop as dumpyard:
212 # $value = $what->query_cost ($who, cf::F_SELL | cf::F_SHOP) / ($what->nrof || 1); 215 # $value = $what->query_cost ($who, cf::F_SELL | cf::F_SHOP) / ($what->nrof || 1);
213 give_back_with_message ($who, $what, 216 give_back_with_message ($who, $what,
214 "Sorry, you can't just sell stuff without assigning a price to it! " 217 "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' " 218 . "Please name your item like '17 platinum' or '10 gold 8 silver' "
216 . "and drop it again. (To rename the item use the 'rename' " 219 . "and drop it again. (To rename the item use the B<rename> "
217 . "context menu item in the inventory)." 220 . "entry in the inventory item popup menu)."
218 ); 221 );
219 return cf::override; 222 return cf::override;
220 } 223 }
221 224
222 if ($value < 0) { 225 if ($value < 0) {
223 give_back_with_message ($who, $what, 226 give_back_with_message ($who, $what,
224 "The shopkeeper says: You can't sell something for a negative value: $value"); 227 "The shopkeeper says: You can't sell something for a negative value: $value.");
225 return cf::override; 228 return cf::override;
226 } 229 }
227 230
228 my $fee = $value / 100; # 1% selling fee 231 my $fee = $value / 100; # 1% selling fee
229 232
230 unless ($who->pay_amount ($fee)) { 233 unless ($who->pay_amount ($fee)) {
231 give_back_with_message ($who, $what, 234 give_back_with_message ($who, $what,
232 "The shopkeeper says: You need " . cf::cost_string_from_value ($fee) 235 "The shopkeeper says: You need " . cf::cost_string_from_value ($fee)
233 . " to pay the 1% fee for this item"); 236 . " to pay the 1% fee for this item.");
234 return cf::override; 237 return cf::override;
235 } else { 238 } else {
236 $who->message ( 239 $who->message (
237 "The shopkeeper says: Ok, got the fee of " . cf::cost_string_from_value ($fee) 240 "The shopkeeper says: Ok, got the fee of " . cf::cost_string_from_value ($fee)
238 . " for the item", 241 . " for the item.",
239 cf::NDI_BROWN 242 cf::NDI_BROWN
240 ); 243 );
241 } 244 }
242 245
243 $what->value ($value); 246 $what->value ($value);
259 . ($what->nrof || 1) . " " . $what->name . " to be sold for at least " 262 . ($what->nrof || 1) . " " . $what->name . " to be sold for at least "
260 . cf::cost_string_from_value ($value) 263 . cf::cost_string_from_value ($value)
261 . ($what->nrof > 1 ? " each" : ""), cf::NDI_BROWN 264 . ($what->nrof > 1 ? " each" : ""), cf::NDI_BROWN
262 ); 265 );
263 266
264 $what->set_ob_key_value (ext_reseller_seller => $who->name); 267 $what->kv_set (ext_reseller_seller => $who->name);
265 $what->set_ob_key_value (ext_reseller_orig_value => $orig_value); 268 $what->kv_set (ext_reseller_orig_value => $orig_value);
266# warn "SET SELLER ON " . $what->name . " + " . $what->{seller}->[0] . "\n"; 269# warn "SET SELLER ON " . $what->name . " + " . $what->{seller}->[0] . "\n";
267 $what->custom_name ($what->name . " (by " . $who->name . ")"); 270 $what->custom_name ($what->name . " (by " . $who->name . ")");
268 $what->flag (cf::FLAG_UNPAID, 1); 271 $what->flag (cf::FLAG_UNPAID, 1);
269 $what->insert_ob_in_map_at ($who->map, $who, cf::INS_BELOW_ORIGINATOR, $who->x, $who->y); 272 $what->insert_ob_in_map_at ($who->map, $who, cf::INS_BELOW_ORIGINATOR, $who->x, $who->y);
270 273

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines