bool CKnightsManager::CheckAlliance(CKnights * pMainClan, CKnights * pTargetClan)
{
_KNIGHTS_ALLIANCE * pMainAlliance = g_pMain->GetAlliancePtr(pMainClan->GetAllianceID()) ,
* pTargetAlliance = g_pMain->GetAlliancePtr(pTargetClan->GetAllianceID());
if(pMainAlliance == pTargetAlliance && (pMainAlliance != nullptr || pTargetAlliance != nullptr))
return true;
else
return false;
}
void CKnightsManager::KnightsAllianceDestroy(CUser* pUser, Packet & pkt)
{
return;
}
/**
* @brief Handles the client packet responsible for creating an alliance
* with another clan.
*
* @param pUser The user.
* @param pkt The packet.
*/
void CKnightsManager::KnightsAllianceCreate(CUser* pUser, Packet & pkt)
{
if (pUser == nullptr
|| pUser->isDead())
return;
CKnights * pKnights = g_pMain->GetClanPtr(pUser->m_bKnights);
if(pKnights->m_byFlag > 1 && pKnights->m_strChief == pUser->GetName())
{
Packet result(WIZ_KNIGHTS_PROCESS, uint8(KNIGHTS_ALLY_CREATE));
result << uint8(1) << pUser->m_bKnights;
pUser->Send(&result);
g_pMain->AddDatabaseRequest(result, pUser);
}
}
/**
* @brief Handles the client packet responsible for forming an alliance
* with another clan. We must already lead an alliance.
*
* NOTE: Most of the logic (error codes also) is for the most part
* identical to the creation of the alliance.
* It just doesn't _create_ an alliance (and requires the alliance to exist).
*
* @param pUser The user.
* @param pkt The packet.
*/
void CKnightsManager::KnightsAllianceInsert(CUser* pUser, Packet & pkt)
{
if (pUser == nullptr
|| pUser->isDead())
return;
Packet result(WIZ_KNIGHTS_PROCESS, uint8(KNIGHTS_ALLY_INSERT));
CKnights * pKnights = g_pMain->GetClanPtr(pUser->m_bKnights);
uint16 TargetClanLeaderID = pkt.read<uint16>();
CUser * pTargetUser = g_pMain->GetUserPtr(TargetClanLeaderID);
CKnights * pTargetKnights = g_pMain->GetClanPtr(pTargetUser->m_bKnights);
_KNIGHTS_ALLIANCE * pAlliance = g_pMain->GetAlliancePtr(pUser->m_bKnights);
if (pAlliance == nullptr)
{
KnightsAllianceCreate(pUser,pkt);
//g_pMain->ReloadKnightAndUserRanks();
pAlliance = g_pMain->GetAlliancePtr(pUser->m_bKnights);
}
if (pUser->GetNation() != pTargetUser->GetNation() || !pTargetUser->isClanLeader())
return;
if(pAlliance != nullptr)
{
CKnights * pKnights1 = g_pMain->GetClanPtr(pAlliance->sMercenaryClan_1),
* pKnights2 = g_pMain->GetClanPtr(pAlliance->sMercenaryClan_2),
* pKnights3 = g_pMain->GetClanPtr(pAlliance->sSubAllianceKnights);
if( pKnights->m_byFlag > 1 &&
!pTargetKnights->isInAlliance() &&
pKnights->m_strChief == pUser->GetName() &&
pAlliance != nullptr &&
pAlliance->sMainAllianceKnights == pKnights->GetAllianceID() &&
pAlliance->sMercenaryClan_1 != pTargetKnights->m_sIndex &&
pAlliance->sMercenaryClan_2 != pTargetKnights->m_sIndex &&
pAlliance->sSubAllianceKnights != pTargetKnights->m_sIndex)
{
result << uint8(1) << pUser->m_bKnights << pTargetUser->m_bKnights << pKnights->m_sCape;
pTargetUser->Send(&result);
g_pMain->AddDatabaseRequest(result, pUser);
pTargetKnights->m_sCape = pKnights->m_sCape;
std::string noticeText;
g_pMain->GetServerResource(IDS_INSERT_KNIGHTS_ALLIANCE, ¬iceText, pTargetKnights->GetName().c_str());
pKnights->SendChatAlliance("%s", noticeText.c_str());
if(pKnights1 != nullptr)
pKnights1->SendChatAlliance("%s", noticeText.c_str());
if(pKnights2 != nullptr)
pKnights2->SendChatAlliance("%s", noticeText.c_str());
if(pKnights3 != nullptr)
pKnights3->SendChatAlliance("%s", noticeText.c_str());
}
}
}
void CKnightsManager::KnightsAllianceRequest(CUser* pUser, Packet & pkt)
{
if (pUser == nullptr
|| pUser->isDead())
return;
//KnightsAllianceInsert(pUser,pkt);
Packet result(WIZ_KNIGHTS_PROCESS, uint8(KNIGHTS_ALLY_REQ));
CKnights * pKnights = g_pMain->GetClanPtr(pUser->m_bKnights);
uint16 TargetClanLeaderID = pkt.read<uint16>();
CUser * pTargetUser = g_pMain->GetUserPtr(TargetClanLeaderID);
CKnights * pTargetKnights = g_pMain->GetClanPtr(pTargetUser->m_bKnights);
_KNIGHTS_ALLIANCE * pAlliance = g_pMain->GetAlliancePtr(pUser->m_bKnights);
if( pAlliance == nullptr)
{
KnightsAllianceCreate(pUser,pkt);
//g_pMain->ReloadKnightAndUserRanks();
pAlliance = g_pMain->GetAlliancePtr(pUser->m_bKnights);
}
if (pUser->GetNation() != pTargetUser->GetNation() || !pTargetUser->isClanLeader())
return;
if(pAlliance != nullptr)
{
CKnights * pKnights1 = g_pMain->GetClanPtr(pAlliance->sMercenaryClan_1),
* pKnights2 = g_pMain->GetClanPtr(pAlliance->sMercenaryClan_2),
* pKnights3 = g_pMain->GetClanPtr(pAlliance->sSubAllianceKnights);
if( pKnights->m_byFlag > 1 &&
!pTargetKnights->isInAlliance() &&
pKnights->m_strChief == pUser->GetName() &&
pAlliance != nullptr &&
pAlliance->sMainAllianceKnights == pKnights->GetAllianceID() &&
pAlliance->sMercenaryClan_1 != pTargetKnights->m_sIndex &&
pAlliance->sMercenaryClan_2 != pTargetKnights->m_sIndex &&
pAlliance->sSubAllianceKnights != pTargetKnights->m_sIndex)
{
KnightsAllianceInsert(pUser,pkt);
g_pMain->AddDatabaseRequest(result, pUser);
result.SByte();
result << uint8(1) << pKnights->GetName();
pTargetUser->Send(&result);
}
}
}
void CKnightsManager::KnightsAllianceRemove(CUser* pUser, Packet & pkt)
{
if (pUser == nullptr
|| pUser->isDead())
return;
Packet result(WIZ_KNIGHTS_PROCESS, uint8(KNIGHTS_ALLY_REMOVE)); // ally disband
CKnights * pKnights = g_pMain->GetClanPtr(pUser->m_bKnights) , *pTargetKnights = g_pMain->GetClanPtr(pKnights->m_sAlliance);
_KNIGHTS_ALLIANCE * pAlliance = g_pMain->GetAlliancePtr(pKnights->m_sAlliance);
if( pAlliance == nullptr)
{
KnightsAllianceCreate(pUser,pkt);
//->ReloadKnightAndUserRanks();
pAlliance = g_pMain->GetAlliancePtr(pUser->m_bKnights);
}
if(pAlliance != nullptr)
{
CKnights * pKnights1 = g_pMain->GetClanPtr(pAlliance->sMercenaryClan_1),
* pKnights2 = g_pMain->GetClanPtr(pAlliance->sMercenaryClan_2),
* pKnights3 = g_pMain->GetClanPtr(pAlliance->sSubAllianceKnights);
if( pKnights->m_strChief == pUser->GetName() ||
pTargetKnights->m_strChief == pUser->GetName() &&
!pTargetKnights->isInAlliance() &&
pAlliance != nullptr &&
pAlliance->sMainAllianceKnights == pKnights->GetAllianceID() ||
pAlliance->sMercenaryClan_1 == pKnights->m_sIndex ||
pAlliance->sMercenaryClan_2 == pKnights->m_sIndex ||
pAlliance->sSubAllianceKnights == pKnights->m_sIndex)
{
result << uint8(1) << pKnights->m_sAlliance << pUser->m_bKnights << uint16(-1);
pUser->Send(&result);
pTargetKnights->m_sCape = -1;
g_pMain->AddDatabaseRequest(result, pUser);
std::string noticeText;
g_pMain->GetServerResource(IDS_REMOVE_KNIGHTS_ALLIANCE, ¬iceText, pKnights->GetName().c_str());
pTargetKnights->SendChatAlliance("%s", noticeText.c_str());
if(pKnights1 != nullptr)
pKnights1->SendChatAlliance("%s", noticeText.c_str());
if(pKnights2 != nullptr)
pKnights2->SendChatAlliance("%s", noticeText.c_str());
if(pKnights3 != nullptr)
pKnights3->SendChatAlliance("%s", noticeText.c_str());
}
}
}
void CKnightsManager::KnightsAlliancePunish(CUser* pUser, Packet & pkt)
{
if (pUser == nullptr
|| pUser->isDead())
return;
Packet result(WIZ_KNIGHTS_PROCESS, uint8(KNIGHTS_ALLY_PUNISH)); // knights ban
uint16 TargetClanID = pkt.read<uint16>(),MainCapeID;
CKnights *pTargetKnights = g_pMain->GetClanPtr(TargetClanID) , *pKnights = g_pMain->GetClanPtr(pUser->m_bKnights);
_KNIGHTS_ALLIANCE * pAlliance = g_pMain->GetAlliancePtr(pUser->m_bKnights);
if( pAlliance != nullptr)
{
CKnights * pKnights1 = g_pMain->GetClanPtr(pAlliance->sMercenaryClan_1),
* pKnights2 = g_pMain->GetClanPtr(pAlliance->sMercenaryClan_2),
* pKnights3 = g_pMain->GetClanPtr(pAlliance->sSubAllianceKnights);
CUser *pTargetUser = nullptr;
if (pTargetKnights != nullptr
&& !pTargetKnights->m_strChief.empty())
{
pTargetUser = g_pMain->GetUserPtr(pTargetKnights->m_strChief, TYPE_CHARACTER);
}
if (pAlliance->sMainAllianceKnights == pKnights->GetID())
{
MainCapeID = pKnights->m_sCape;
}
else
MainCapeID = -1;
if( pKnights->m_byFlag > 1 &&
pKnights->m_strChief == pUser->GetName() &&
pAlliance != nullptr &&
pTargetKnights->isInAlliance() &&
pAlliance->sMainAllianceKnights == pKnights->GetAllianceID() ||
pAlliance->sMercenaryClan_1 == pTargetKnights->m_sIndex ||
pAlliance->sMercenaryClan_2 == pTargetKnights->m_sIndex ||
pAlliance->sSubAllianceKnights == pTargetKnights->m_sIndex)
{
if (pTargetUser != nullptr)
{
result << uint8(1) << pUser->m_bKnights << pTargetUser->m_bKnights << MainCapeID;
pTargetUser->Send(&result);
}
else
{
result << uint8(1) << pUser->m_bKnights << TargetClanID << MainCapeID;
pUser->Send(&result);
}
g_pMain->AddDatabaseRequest(result, pUser);
pTargetKnights->m_sCape = MainCapeID;
std::string noticeText;
g_pMain->GetServerResource(IDS_PUNISH_KNIGHTS_ALLIANCE, ¬iceText, pTargetKnights->GetName().c_str());
pKnights->SendChatAlliance("%s", noticeText.c_str());
if(pKnights1 != nullptr)
pKnights1->SendChatAlliance("%s", noticeText.c_str());
if(pKnights2 != nullptr)
pKnights2->SendChatAlliance("%s", noticeText.c_str());
if(pKnights3 != nullptr)
pKnights3->SendChatAlliance("%s", noticeText.c_str());
}
}
}
/**
* @brief Handles the client packet responsible for displaying
* all clans that are part of the user's clan's alliance.
*
* @param pUser The user.
* @param pkt The packet.
*/
void CKnightsManager::KnightsAllianceList(CUser* pUser, Packet & pkt)
{
if (pUser == nullptr
|| !pUser->isInClan())
return;
Packet result(WIZ_KNIGHTS_PROCESS, uint8(KNIGHTS_ALLY_LIST));
_KNIGHTS_ALLIANCE * pAlliance;
CKnights * pClan = g_pMain->GetClanPtr(pUser->GetClanID());
if (pClan == nullptr
|| !pClan->isInAlliance()
|| (pAlliance = g_pMain->GetAlliancePtr(pClan->GetAllianceID())) == nullptr)
{
result << uint8(0);
pUser->Send(&result);
return;
}
uint16 clans[] =
{
pAlliance->sMainAllianceKnights, pAlliance->sSubAllianceKnights,
pAlliance->sMercenaryClan_1, pAlliance->sMercenaryClan_2
};
size_t wpos = result.wpos();
uint8 clanCount = 0;
result << clanCount;
result.SByte();
foreach_array(i, clans)
{
uint16 sClanID = clans[i];
CKnights * pTmp = g_pMain->GetClanPtr(sClanID);
if (pTmp == nullptr)
continue;
result << pTmp->GetID() << pTmp->GetName() << pTmp->isInAlliance(); /* unknown flag */
clanCount++;
}
if (clanCount == 0)
return;
result.put(wpos, clanCount);
pUser->Send(&result);
}