Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Handins] Player Event Logging #1372

Merged
merged 5 commits into from
Feb 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 35 additions & 1 deletion lua_modules/items.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,26 @@ function items.check_turn_in(trade, trade_check)
for key, value in pairs(trade) do
trade_return[key] = value;
end

-- Handin table for source
local handin_data = {};
for x = 1, 4 do
local inst = trade["item" .. x];
if (inst.valid) then
local is_attuned = 0;
if inst:IsInstNoDrop() then
is_attuned = 1;
end

handin_data[x] = string.format("%d|%d|%d", inst:GetID(), inst:GetCharges(), is_attuned);
else
handin_data[x] = "0|0|0";
end
end

trade.other:SetEntityVariable("HANDIN_ITEMS", items.get_handin_items_serialized(handin_data))

trade.other:SetEntityVariable("HANDIN_MONEY", string.format("%d|%d|%d|%d", trade.copper, trade.silver, trade.gold, trade.platinum))

--for every item in trade_check check trade_return
--if item exists in trade_return then
Expand Down Expand Up @@ -83,6 +103,9 @@ end
function items.return_items(npc, client, trade, text)
text = text or true;
local returned = false;

-- Handin table for source
local return_data = {};
for i = 1, 4 do
local inst = trade["item" .. i];
if(inst.valid) then
Expand All @@ -93,13 +116,15 @@ function items.return_items(npc, client, trade, text)
if(client:GetClass() == npc:GetClass() - 19) then
client:TrainDisc(inst:GetID());
else
return_data[i] = string.format("%d|%d|%d", inst:GetID(), inst:GetCharges(), is_attuned);
npc:Say(string.format("You are not a member of my guild. I will not train you!"));
if return_count > 0 then
client:PushItemOnCursor(inst);
returned = true;
end
end
elseif return_count > 0 then
return_data[i] = string.format("%d|%d|%d", inst:GetID(), inst:GetCharges(), is_attuned);
client:PushItemOnCursor(inst);
if(text == true) then
npc:Say(string.format("I have no need for this %s, you can have it back.", client:GetCleanName()));
Expand All @@ -108,6 +133,8 @@ function items.return_items(npc, client, trade, text)
end
end
end

client:SetEntityVariable("RETURN_ITEMS", items.get_handin_items_serialized(return_data))

local money = false;
if(trade.platinum ~= 0) then
Expand All @@ -131,8 +158,11 @@ function items.return_items(npc, client, trade, text)
end

if(money == true) then
client:SetEntityVariable("RETURN_MONEY", string.format("%d|%d|%d|%d", trade.copper, trade.silver, trade.gold, trade.platinum));
client:AddMoneyToPP(trade.copper, trade.silver, trade.gold, trade.platinum, true);
end

eq.send_player_handin_event();

return returned;
end
Expand Down Expand Up @@ -264,4 +294,8 @@ function items.check_bot_turn_in(trade, trade_check)
return true;
end

return items;
function items.get_handin_items_serialized(handin_table)
return table.concat(handin_table, ",")
end

return items;
149 changes: 111 additions & 38 deletions plugins/check_handin.pl
Original file line number Diff line number Diff line change
@@ -1,72 +1,127 @@
# plugin::check_handin($item1 => #required_amount,...);
# autoreturns extra unused items on success
sub check_handin {
my $hashref = shift;
my %required = @_;
foreach my $req (keys %required) {
if ((!defined $hashref->{$req}) || ($hashref->{$req} != $required{$req})) {
return(0);
use Scalar::Util qw(looks_like_number);
my $client = plugin::val('client');
my $copper = plugin::val('copper');
my $silver = plugin::val('silver');
my $gold = plugin::val('gold');
my $platinum = plugin::val('platinum');
my $hashref = shift;

my $return_copper = 0;
my $return_silver = 0;
my $return_gold = 0;
my $return_platinum = 0;

$client->SetEntityVariable("HANDIN_MONEY", "$copper|$silver|$gold|$platinum");
$client->SetEntityVariable("HANDIN_ITEMS", plugin::GetHandinItemsSerialized("Handin", %$hashref));

my %required = @_;
foreach my $req (keys %required) {
if (looks_like_number($req)) {
if (!defined $hashref->{$req} || $hashref->{$req} != $required{$req}) {
$client->SetEntityVariable("RETURN_MONEY", "$copper|$silver|$gold|$platinum");
return 0;
}
} else {
my $lower_req = lc($req);
my $money_string = "$copper|$silver|$gold|$platinum";
if ($lower_req eq "copper") {
if ($copper != $hashref->{$req}) {
$return_copper = $copper;
}
} elsif ($lower_req eq "silver") {
if ($silver != $hashref->{$req}) {
$return_silver = $silver;
}
} elsif ($lower_req eq "gold") {
if ($gold != $hashref->{$req}) {
$return_gold = $gold;
}
} elsif ($lower_req eq "platinum") {
if ($platinum != $hashref->{$req}) {
$return_platinum = $platinum;
}
}
}
}
}
foreach my $req (keys %required) {
if ($required{$req} < $hashref->{$req}) {
$hashref->{$req} -= $required{$req};
} else {
delete $hashref->{$req};
}
}
return 1;

foreach my $req (keys %required) {
if ($required{$req} < $hashref->{$req}) {
$hashref->{$req} -= $required{$req};
} else {
delete $hashref->{$req};
}
}

$client->SetEntityVariable("RETURN_MONEY", "$return_copper|$return_silver|$return_gold|$return_platinum");

return 1;
}

sub return_items {
sub return_items {
my $hashref = plugin::var('$itemcount');
my $client = plugin::val('$client');
my $name = plugin::val('$name');
my $items_returned = 0;
my $money_returned = 0;

my %ItemHash = (
my %item_data = (
0 => [ plugin::val('$item1'), plugin::val('$item1_charges'), plugin::val('$item1_attuned'), plugin::val('$item1_inst') ],
1 => [ plugin::val('$item2'), plugin::val('$item2_charges'), plugin::val('$item2_attuned'), plugin::val('$item2_inst') ],
2 => [ plugin::val('$item3'), plugin::val('$item3_charges'), plugin::val('$item3_attuned'), plugin::val('$item3_inst') ],
3 => [ plugin::val('$item4'), plugin::val('$item4_charges'), plugin::val('$item4_attuned'), plugin::val('$item4_inst') ],
);

foreach my $k (keys(%{$hashref}))
{
next if($k == 0);
my %return_data = ();

foreach my $k (keys(%{$hashref})) {
next if ($k == 0);
my $rcount = $hashref->{$k};
my $r;
for ($r = 0; $r < 4; $r++)
{
if ($rcount > 0 && $ItemHash{$r}[0] && $ItemHash{$r}[0] == $k)
{
if ($client)
{
# remove delivered task items from return for this slot
my $inst = $ItemHash{$r}[3];
for ($r = 0; $r < 4; $r++) {
if ($rcount > 0 && $item_data{$r}[0] && $item_data{$r}[0] == $k) {
if ($client) {
my $inst = $item_data{$r}[3];
my $return_count = $inst->RemoveTaskDeliveredItems();

if ($return_count > 0)
{
$client->SummonItem($k, $inst->GetCharges(), $ItemHash{$r}[2]);
quest::say("I have no need for this $name, you can have it back.");
if ($return_count > 0) {
$client->SummonItem($k, $inst->GetCharges(), $item_data{$r}[2]);
$items_returned = 1;
next;
}
}
else
{
# This shouldn't be needed, but just in case
$return_data{$r} = [$k, $item_data{$r}[1], $item_data{$r}[2]];
$client->SummonItem($k, $item_data{$r}[1], $item_data{$r}[2]);
$items_returned = 1;
} else {
$return_data{$r} = [$k, $item_data{$r}[1], $item_data{$r}[2]];
quest::summonitem($k, 0);
$items_returned = 1;
}
$rcount--;
}
}

delete $hashref->{$k};
}

if ($client->EntityVariableExists("RETURN_MONEY")) {
$money_returned = 1;
my ($copper, $silver, $gold, $platinum) = split(/|/, $client->GetEntityVariable("RETURN_MONEY"));
quest::shout("RETURN $copper $silver $gold $platinum");
$client->AddMoneyToPP($copper, $silver, $gold, $platinum, 1);
}

$client->SetEntityVariable("RETURN_ITEMS", plugin::GetHandinItemsSerialized("Return", %return_data));

if ($items_returned || $money_returned) {
quest::say("I have no need for this $name, you can have it back.");
}

quest::send_player_handin_event();

# Return true if items were returned
return $items_returned;

}

sub return_bot_items {
Expand All @@ -81,8 +136,8 @@ sub return_bot_items {
my $items_returned = 0;

my %item_data = (
1 => [ plugin::val('item2'), plugin::val('item2_charges'), plugin::val('item2_attuned'), plugin::val('item2_inst') ],
0 => [ plugin::val('item1'), plugin::val('item1_charges'), plugin::val('item1_attuned'), plugin::val('item1_inst') ],
1 => [ plugin::val('item2'), plugin::val('item2_charges'), plugin::val('item2_attuned'), plugin::val('item2_inst') ],
2 => [ plugin::val('item3'), plugin::val('item3_charges'), plugin::val('item3_attuned'), plugin::val('item3_inst') ],
3 => [ plugin::val('item4'), plugin::val('item4_charges'), plugin::val('item4_attuned'), plugin::val('item4_inst') ],
4 => [ plugin::val('item5'), plugin::val('item5_charges'), plugin::val('item5_attuned'), plugin::val('item5_inst') ],
Expand Down Expand Up @@ -124,6 +179,24 @@ sub return_bot_items {
return $items_returned;
}

sub GetHandinItemsSerialized {
my $type = shift;
my %hash = @_;
my @variables = ();

if ($type eq "Handin") {
foreach my $key (keys %hash) {
push(@variables, $key . "|" . $hash{$key} . "|0");
}
} else {
foreach my $key (keys %hash) {
push(@variables, $hash{$key}[0] . "|" . $hash{$key}[1] . "|" . $hash{$key}[2]);
}
}

return join(",", @variables);
}

sub mq_process_items {
my $hashref = shift;
my $npc = plugin::val('$npc');
Expand Down