Skip to content

Commit

Permalink
feat. New features (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
pangolp authored Jul 28, 2023
1 parent 20032fa commit b48e72f
Show file tree
Hide file tree
Showing 7 changed files with 151 additions and 69 deletions.
19 changes: 19 additions & 0 deletions .github/workflows/core_codestyle.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: Codestyle Checks
on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:
check-codestyle:
strategy:
fail-fast: false

runs-on: ubuntu-latest
name: Check Codestyling
steps:
- uses: actions/checkout@v2

- name: Check Codestyling
run: source ./apps/ci/ci-codestyle.sh
Empty file added apps/.gitkeep
Empty file.
Empty file added apps/ci/.gitkeep
Empty file.
40 changes: 40 additions & 0 deletions apps/ci/ci-codestyle.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#!/bin/bash
set -e

echo "Codestyle check script:"
echo

declare -A singleLineRegexChecks=(
["LOG_.+GetCounter"]="Use ObjectGuid::ToString().c_str() method instead of ObjectGuid::GetCounter() when logging. Check the lines above"
["[[:blank:]]$"]="Remove whitespace at the end of the lines above"
["\t"]="Replace tabs with 4 spaces in the lines above"
)

for check in ${!singleLineRegexChecks[@]}; do
echo " Checking RegEx: '${check}'"

if grep -P -r -I -n ${check} src; then
echo
echo "${singleLineRegexChecks[$check]}"
exit 1
fi
done

declare -A multiLineRegexChecks=(
["LOG_[^;]+GetCounter"]="Use ObjectGuid::ToString().c_str() method instead of ObjectGuid::GetCounter() when logging. Check the lines above"
["\n\n\n"]="Multiple blank lines detected, keep only one. Check the files above"
)

for check in ${!multiLineRegexChecks[@]}; do
echo " Checking RegEx: '${check}'"

if grep -Pzo -r -I ${check} src; then
echo
echo
echo "${multiLineRegexChecks[$check]}"
exit 1
fi
done

echo
echo "Everything looks good"
6 changes: 3 additions & 3 deletions conf/mod_recruit_friend.conf.dist
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,18 @@ RecruitFriend.enable=true
# default: true
#

RecruitFriend.announceEnable=true
RecruitFriend.announceEnable = true

#
# Enable recruit friend command cooldown
# default: true
#

RecruitFriend.cooldownEnabled=true
RecruitFriend.cooldownEnabled = true

#
# recruit friend cooldown value
# default: 300000 in Milliseconds
#

RecruitFriend.cooldownValue=300000
RecruitFriend.cooldownValue = 300000
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
SET @ENTRY:=35457;
DELETE FROM `acore_string` WHERE `entry`=@ENTRY+0;
DELETE FROM `acore_string` WHERE `entry` IN (@ENTRY+0, @ENTRY+1);
INSERT INTO `acore_string` (`entry`, `content_default`, `locale_koKR`, `locale_frFR`, `locale_deDE`, `locale_zhCN`, `locale_zhTW`, `locale_esES`, `locale_esMX`, `locale_ruRU`) VALUES
(@ENTRY+0, '[Recruit Friend] You have to wait the cooldown to recruit a friend', '', '', '', '', '', '[Reclutar Amigo] Debes esperar el cooldown para reclutar un amigo', '[Reclutar Amigo] Debes esperar el cooldown para reclutar un amigo', '');
(@ENTRY+0, '[Recruit Friend] You must wait for %u sec. to use this command again.', '', '', '', '', '', '[Reclutar Amigo] Debes esperar %u seg para utilizar este comando nuevamente.', '[Reclutar Amigo] Debes esperar %u seg para utilizar este comando nuevamente.', ''),
(@ENTRY+1, '[Recruit Friend] You haven\'t recruited anyone. Use the .recruit add command to do this.', '', '', '', '', '', '[Recruit Friend] No has reclutado a nadie. Utiliza el comando .recruit add para hacerlo.', '[Recruit Friend] No has reclutado a nadie. Utiliza el comando .recruit add para hacerlo.', '');
150 changes: 86 additions & 64 deletions src/RecruitCommand.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,30 @@ enum RecruitFriendTexts
RECRUIT_FRIEND_TARGET_ONESELF,
RECRUIT_FRIEND_NAMES,
RECRUIT_FRIEND_COOLDOWN,
RECRUIT_VIEW_EMPTY
};

std::map<uint32, std::time_t> commandCooldown;
struct RecruitFriendStruct
{
std::map<uint32, std::time_t> commandCooldown;
bool announceEnable, commandEnable, cooldownEnabled;
uint32 cooldownValue;
};

RecruitFriendStruct recruitFriend;

class RecruitFriendAnnouncer : public PlayerScript
{
public:
RecruitFriendAnnouncer() : PlayerScript("RecruitFriendAnnouncer") {}
public:
RecruitFriendAnnouncer() : PlayerScript("RecruitFriendAnnouncer") {}

void OnLogin(Player* player)
void OnLogin(Player* player)
{
if (recruitFriend.announceEnable)
{
if (sConfigMgr->GetOption<bool>("RecruitFriend.announceEnable", true))
{
ChatHandler(player->GetSession()).SendSysMessage(HELLO_RECRUIT_FRIEND);
}
ChatHandler(player->GetSession()).SendSysMessage(HELLO_RECRUIT_FRIEND);
}
}
};

void registerQuery(ChatHandler* handler, const char* commandType)
Expand All @@ -50,6 +58,21 @@ void registerQuery(ChatHandler* handler, const char* commandType)
QueryResult info = LoginDatabase.Query("INSERT INTO `recruit_info` (`accountId`, `accountName`, `characterName`, `ip`, `command`) VALUES ({}, '{}', '{}', '{}', '{}');", myAccountId, accountName.c_str(), characterName.c_str(), ipAccount.c_str(), commandType);
}

void waitToUseCommand(ChatHandler* handler, uint32 myAccountId)
{
std::time_t currentTime = std::time(0);
uint32 delta = std::difftime(currentTime, recruitFriend.commandCooldown[myAccountId]);

if (delta <= (uint32)recruitFriend.cooldownValue / 1000)
{
ChatHandler(handler->GetSession()).PSendSysMessage(RECRUIT_FRIEND_COOLDOWN, ((uint32)recruitFriend.cooldownValue / IN_MILLISECONDS) - delta);
}
else
{
recruitFriend.commandCooldown.erase(myAccountId);
}
}

using namespace Acore::ChatCommands;

class RecruitCommandscript : public CommandScript
Expand All @@ -61,9 +84,9 @@ class RecruitCommandscript : public CommandScript
{
static ChatCommandTable recruitSetCommandTable =
{
{ "add", HandleAddRecruitFriendCommand, SEC_PLAYER, Console::Yes },
{ "reset", HandleResetRecruitFriendCommand, SEC_PLAYER, Console::Yes },
{ "view", HandleViewRecruitFriendCommand, SEC_PLAYER, Console::Yes }
{ "add", HandleAddRecruitFriendCommand, SEC_PLAYER, Console::No },
{ "reset", HandleResetRecruitFriendCommand, SEC_PLAYER, Console::No },
{ "view", HandleViewRecruitFriendCommand, SEC_PLAYER, Console::No }
};

static ChatCommandTable commandTable =
Expand All @@ -74,54 +97,33 @@ class RecruitCommandscript : public CommandScript
return commandTable;
}

static void getTargetAccountIdByName(std::string& name, uint32& accountId)
{
QueryResult result = CharacterDatabase.Query("SELECT `account` FROM `characters` WHERE `name`='{}';", name);
accountId = (*result)[0].Get<int32>();
}

static bool HandleAddRecruitFriendCommand(ChatHandler* handler, std::string args)
static bool HandleAddRecruitFriendCommand(ChatHandler* handler, std::string accountName)
{

if (!sConfigMgr->GetOption<bool>("RecruitFriend.enable", true))
if (!recruitFriend.commandEnable)
{
handler->SendSysMessage(RECRUIT_FRIEND_DISABLE);
return false;
}

if (!args.empty())
return false;

Player* target = nullptr;

std::string playerName;
uint32 targetAccountId = AccountMgr::GetId(accountName);

if (!handler->extractPlayerTarget((char*)args.c_str(), &target, nullptr, &playerName))
if (targetAccountId == 0)
{
handler->PSendSysMessage(LANG_ACCOUNT_NOT_EXIST, accountName.c_str());
handler->SetSentErrorMessage(true);
return false;

uint32 targetAccountId;

if (target)
targetAccountId = target->GetSession()->GetAccountId();
else
getTargetAccountIdByName(playerName, targetAccountId);
}

uint32 myAccountId = handler->GetSession()->GetAccountId();

if(sConfigMgr->GetOption<bool>("RecruitFriend.cooldownEnabled", true))
if (recruitFriend.cooldownEnabled)
{
uint32 cooldownValue = sConfigMgr->GetOption<uint32>("RecruitFriend.cooldownValue", 300000);
std::time_t currentTime = std::time(0);

if( currentTime - commandCooldown[myAccountId] <= cooldownValue)
{
ChatHandler(handler->GetSession()).SendSysMessage(RECRUIT_FRIEND_COOLDOWN);
return true;
}
waitToUseCommand(handler, myAccountId);
if (!recruitFriend.commandCooldown[myAccountId])
recruitFriend.commandCooldown[myAccountId] = std::time(0);
else
{
commandCooldown.erase(myAccountId);
}
return true;
}

registerQuery(handler, "add");
Expand All @@ -135,7 +137,6 @@ class RecruitCommandscript : public CommandScript
else if (targetAccountId != myAccountId)
{
result = LoginDatabase.Query("UPDATE `account` SET `recruiter`={} WHERE `id`={};", targetAccountId, myAccountId);
commandCooldown[myAccountId] = std::time(0);
ChatHandler(handler->GetSession()).SendSysMessage(RECRUIT_FRIEND_SUCCESS);
}
else
Expand All @@ -145,51 +146,51 @@ class RecruitCommandscript : public CommandScript
return true;
}

static bool HandleResetRecruitFriendCommand(ChatHandler* handler, std::string /*args*/)
static bool HandleResetRecruitFriendCommand(ChatHandler* handler)
{
if (!sConfigMgr->GetOption<bool>("RecruitFriend.enable", true))
if (!recruitFriend.commandEnable)
{
handler->SendSysMessage(RECRUIT_FRIEND_DISABLE);
return false;
}

uint32 myAccountId = handler->GetSession()->GetAccountId();

if(sConfigMgr->GetOption<bool>("RecruitFriend.cooldownEnabled", true))
if (recruitFriend.cooldownEnabled)
{
uint32 cooldownValue = sConfigMgr->GetOption<uint32>("RecruitFriend.cooldownValue", 300000);
std::time_t currentTime = std::time(0);

if( currentTime - commandCooldown[myAccountId] <= cooldownValue)
{
ChatHandler(handler->GetSession()).SendSysMessage(RECRUIT_FRIEND_COOLDOWN);
return true;
}
waitToUseCommand(handler, myAccountId);
if (!recruitFriend.commandCooldown[myAccountId])
recruitFriend.commandCooldown[myAccountId] = std::time(0);
else
{
commandCooldown.erase(myAccountId);
}
return true;
}

registerQuery(handler, "reset");

QueryResult result = LoginDatabase.Query("UPDATE `account` SET `recruiter`=0 WHERE `id`={};", myAccountId);

ChatHandler(handler->GetSession()).SendSysMessage(RECRUIT_FRIEND_RESET_SUCCESS);

return true;
}

static bool HandleViewRecruitFriendCommand(ChatHandler* handler, std::string /*args*/)
static bool HandleViewRecruitFriendCommand(ChatHandler* handler)
{
if (!sConfigMgr->GetOption<bool>("RecruitFriend.enable", true))
if (!recruitFriend.commandEnable)
{
handler->SendSysMessage(RECRUIT_FRIEND_DISABLE);
return false;
}

uint32 myAccountId = handler->GetSession()->GetAccountId();

if (recruitFriend.cooldownEnabled)
{
waitToUseCommand(handler, myAccountId);
if (!recruitFriend.commandCooldown[myAccountId])
recruitFriend.commandCooldown[myAccountId] = std::time(0);
else
return true;
}

registerQuery(handler, "view");

QueryResult result = LoginDatabase.Query("SELECT `recruiter` FROM `account` WHERE `id`={};", myAccountId);
Expand All @@ -206,14 +207,35 @@ class RecruitCommandscript : public CommandScript
handler->PSendSysMessage(RECRUIT_FRIEND_NAMES, fieldsCharacters[0].Get<std::string>());
} while (resultCharacters->NextRow());
}
else
ChatHandler(handler->GetSession()).SendSysMessage(RECRUIT_VIEW_EMPTY);
}

return true;
}
};

class RecruitFriendWorld : public WorldScript
{
public:
RecruitFriendWorld() : WorldScript("RecruitFriendWorld") {}

void OnBeforeConfigLoad(bool reload) override
{
if (!reload)
{
sConfigMgr->LoadModulesConfigs();
recruitFriend.announceEnable = sConfigMgr->GetOption<bool>("RecruitFriend.announceEnable", true);
recruitFriend.commandEnable = sConfigMgr->GetOption<bool>("RecruitFriend.enable", true);
recruitFriend.cooldownEnabled = sConfigMgr->GetOption<bool>("RecruitFriend.cooldownEnabled", true);
recruitFriend.cooldownValue = sConfigMgr->GetOption<uint32>("RecruitFriend.cooldownValue", 300000);
}
}
};

void AddRecruitCommandScripts()
{
new RecruitCommandscript();
new RecruitFriendAnnouncer();
new RecruitFriendWorld();
}

0 comments on commit b48e72f

Please sign in to comment.