mirror of
https://github.com/ErikasKontenis/SabrehavenServer.git
synced 2025-12-10 04:10:45 +01:00
finish guildwar system
This commit is contained in:
@@ -1087,11 +1087,12 @@ void BehaviourDatabase::checkAction(const NpcBehaviourAction* action, Player* pl
|
||||
break;
|
||||
}
|
||||
|
||||
if (IOLoginData::getGuildBalance(playerGuild->getId()) < static_cast<uint64_t>(money)) {
|
||||
|
||||
if (IOGuild::getGuildBalance(playerGuild->getId()) < static_cast<uint64_t>(money)) {
|
||||
break;
|
||||
}
|
||||
|
||||
if (IOLoginData::decreaseGuildBankBalance(playerGuild->getId(), money)) {
|
||||
if (IOGuild::decreaseGuildBankBalance(playerGuild->getId(), money)) {
|
||||
player->setBankBalance(player->getBankBalance() + money);
|
||||
}
|
||||
|
||||
@@ -1117,7 +1118,7 @@ void BehaviourDatabase::checkAction(const NpcBehaviourAction* action, Player* pl
|
||||
break;
|
||||
}
|
||||
|
||||
if (IOLoginData::increaseGuildBankBalance(playerGuild->getId(), money)) {
|
||||
if (IOGuild::increaseGuildBankBalance(playerGuild->getId(), money)) {
|
||||
player->setBankBalance(player->getBankBalance() - money);
|
||||
}
|
||||
|
||||
@@ -1326,7 +1327,7 @@ int32_t BehaviourDatabase::evaluate(NpcBehaviourNode* node, Player* player, cons
|
||||
return false;
|
||||
}
|
||||
|
||||
return IOLoginData::getGuildBalance(playerGuild->getId());
|
||||
return IOGuild::getGuildBalance(playerGuild->getId());
|
||||
}
|
||||
case BEHAVIOUR_TYPE_CLIENTVERSION:
|
||||
return g_game.getClientVersion();
|
||||
|
||||
11
src/chat.cpp
11
src/chat.cpp
@@ -105,6 +105,17 @@ bool ChatChannel::removeUser(const Player& player)
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ChatChannel::hasUser(const Player& player) {
|
||||
return users.find(player.getID()) != users.end();
|
||||
}
|
||||
|
||||
void ChatChannel::sendToAll(const std::string& message, SpeakClasses type) const
|
||||
{
|
||||
for (const auto& it : users) {
|
||||
it.second->sendChannelMessage("", message, type, id);
|
||||
}
|
||||
}
|
||||
|
||||
bool ChatChannel::talk(const Player& fromPlayer, SpeakClasses type, const std::string& text)
|
||||
{
|
||||
if (users.find(fromPlayer.getID()) == users.end()) {
|
||||
|
||||
@@ -41,8 +41,10 @@ class ChatChannel
|
||||
|
||||
bool addUser(Player& player);
|
||||
bool removeUser(const Player& player);
|
||||
bool hasUser(const Player& player);
|
||||
|
||||
bool talk(const Player& fromPlayer, SpeakClasses type, const std::string& text);
|
||||
void sendToAll(const std::string& message, SpeakClasses type) const;
|
||||
|
||||
const std::string& getName() const {
|
||||
return name;
|
||||
|
||||
@@ -39,7 +39,7 @@ uint32_t IOGuild::getGuildIdByName(const std::string& name)
|
||||
void IOGuild::getWarList(uint32_t guildId, GuildWarList& guildWarList)
|
||||
{
|
||||
std::ostringstream query;
|
||||
query << "SELECT `guild1`, `guild2` FROM `guild_wars` WHERE (`guild1` = " << guildId << " OR `guild2` = " << guildId << ") AND `ended` = 0 AND `status` = 1";
|
||||
query << "SELECT `guild1`, `guild2` FROM `guild_wars` WHERE (`guild1` = " << guildId << " OR `guild2` = " << guildId << ") AND `status` IN (1, 4)";
|
||||
|
||||
DBResult_ptr result = Database::getInstance()->storeQuery(query.str());
|
||||
if (!result) {
|
||||
@@ -55,3 +55,29 @@ void IOGuild::getWarList(uint32_t guildId, GuildWarList& guildWarList)
|
||||
}
|
||||
} while (result->next());
|
||||
}
|
||||
|
||||
uint64_t IOGuild::getGuildBalance(uint32_t id)
|
||||
{
|
||||
std::ostringstream query;
|
||||
query << "SELECT `balance` FROM `guilds` WHERE `id` = " << id;
|
||||
DBResult_ptr result = Database::getInstance()->storeQuery(query.str());
|
||||
if (!result) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return result->getNumber<uint64_t>("balance");
|
||||
}
|
||||
|
||||
bool IOGuild::increaseGuildBankBalance(uint32_t guid, uint64_t bankBalance)
|
||||
{
|
||||
std::ostringstream query;
|
||||
query << "UPDATE `guilds` SET `balance` = `balance` + " << bankBalance << " WHERE `id` = " << guid;
|
||||
return Database::getInstance()->executeQuery(query.str());
|
||||
}
|
||||
|
||||
bool IOGuild::decreaseGuildBankBalance(uint32_t guid, uint64_t bankBalance)
|
||||
{
|
||||
std::ostringstream query;
|
||||
query << "UPDATE `guilds` SET `balance` = `balance` - " << bankBalance << " WHERE `id` = " << guid;
|
||||
return Database::getInstance()->executeQuery(query.str());
|
||||
}
|
||||
@@ -27,6 +27,10 @@ class IOGuild
|
||||
public:
|
||||
static uint32_t getGuildIdByName(const std::string& name);
|
||||
static void getWarList(uint32_t guildId, GuildWarList& guildWarList);
|
||||
|
||||
static uint64_t getGuildBalance(uint32_t id);
|
||||
static bool increaseGuildBankBalance(uint32_t guid, uint64_t bankBalance);
|
||||
static bool decreaseGuildBankBalance(uint32_t guid, uint64_t bankBalance);
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
@@ -829,18 +829,6 @@ uint32_t IOLoginData::getGuidByName(const std::string& name)
|
||||
return result->getNumber<uint32_t>("id");
|
||||
}
|
||||
|
||||
uint64_t IOLoginData::getGuildBalance(uint32_t id)
|
||||
{
|
||||
std::ostringstream query;
|
||||
query << "SELECT `balance` FROM `guilds` WHERE `id` = " << id;
|
||||
DBResult_ptr result = Database::getInstance()->storeQuery(query.str());
|
||||
if (!result) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return result->getNumber<uint64_t>("balance");
|
||||
}
|
||||
|
||||
// Return 0 means player not found, 1 player is without vocation, 2 player with vocation
|
||||
uint16_t IOLoginData::canTransferMoneyToByName(const std::string& name)
|
||||
{
|
||||
@@ -930,20 +918,6 @@ void IOLoginData::increaseBankBalance(uint32_t guid, uint64_t bankBalance)
|
||||
Database::getInstance()->executeQuery(query.str());
|
||||
}
|
||||
|
||||
bool IOLoginData::increaseGuildBankBalance(uint32_t guid, uint64_t bankBalance)
|
||||
{
|
||||
std::ostringstream query;
|
||||
query << "UPDATE `guilds` SET `balance` = `balance` + " << bankBalance << " WHERE `id` = " << guid;
|
||||
return Database::getInstance()->executeQuery(query.str());
|
||||
}
|
||||
|
||||
bool IOLoginData::decreaseGuildBankBalance(uint32_t guid, uint64_t bankBalance)
|
||||
{
|
||||
std::ostringstream query;
|
||||
query << "UPDATE `guilds` SET `balance` = `balance` - " << bankBalance << " WHERE `id` = " << guid;
|
||||
return Database::getInstance()->executeQuery(query.str());
|
||||
}
|
||||
|
||||
void IOLoginData::increaseBankBalance(std::string name, uint64_t bankBalance)
|
||||
{
|
||||
Database* db = Database::getInstance();
|
||||
|
||||
@@ -45,14 +45,11 @@ class IOLoginData
|
||||
static bool loadPlayer(Player* player, DBResult_ptr result);
|
||||
static bool savePlayer(Player* player);
|
||||
static uint32_t getGuidByName(const std::string& name);
|
||||
static uint64_t getGuildBalance(uint32_t id);
|
||||
static uint16_t canTransferMoneyToByName(const std::string& name);
|
||||
static bool getGuidByNameEx(uint32_t& guid, bool& specialVip, std::string& name);
|
||||
static std::string getNameByGuid(uint32_t guid);
|
||||
static bool formatPlayerName(std::string& name);
|
||||
static void increaseBankBalance(uint32_t guid, uint64_t bankBalance);
|
||||
static bool increaseGuildBankBalance(uint32_t guid, uint64_t bankBalance);
|
||||
static bool decreaseGuildBankBalance(uint32_t guid, uint64_t bankBalance);
|
||||
static void increaseBankBalance(const std::string name, uint64_t bankBalance);
|
||||
static bool hasBiddedOnHouse(uint32_t guid);
|
||||
|
||||
|
||||
@@ -1012,6 +1012,12 @@ void LuaScriptInterface::registerFunctions()
|
||||
//getWaypointPosition(name)
|
||||
lua_register(luaState, "getWaypointPositionByName", LuaScriptInterface::luaGetWaypointPositionByName);
|
||||
|
||||
//sendChannelMessage(channelId, type, message)
|
||||
lua_register(luaState, "sendChannelMessage", LuaScriptInterface::luaSendChannelMessage);
|
||||
|
||||
//sendGuildChannelMessage(guildId, type, message)
|
||||
lua_register(luaState, "sendGuildChannelMessage", LuaScriptInterface::luaSendGuildChannelMessage);
|
||||
|
||||
#ifndef LUAJIT_VERSION
|
||||
//bit operations for Lua, based on bitlib project release 24
|
||||
//bit.bnot, bit.band, bit.bor, bit.bxor, bit.lshift, bit.rshift
|
||||
@@ -2154,10 +2160,15 @@ void LuaScriptInterface::registerFunctions()
|
||||
registerMethod("Guild", "getId", LuaScriptInterface::luaGuildGetId);
|
||||
registerMethod("Guild", "getName", LuaScriptInterface::luaGuildGetName);
|
||||
registerMethod("Guild", "getMembersOnline", LuaScriptInterface::luaGuildGetMembersOnline);
|
||||
registerMethod("Guild", "setGuildWarEmblem", LuaScriptInterface::luaGuildSetGuildWarEmblem);
|
||||
|
||||
registerMethod("Guild", "addRank", LuaScriptInterface::luaGuildAddRank);
|
||||
registerMethod("Guild", "getRankById", LuaScriptInterface::luaGuildGetRankById);
|
||||
registerMethod("Guild", "getRankByLevel", LuaScriptInterface::luaGuildGetRankByLevel);
|
||||
|
||||
registerMethod("Guild", "getBankBalance", LuaScriptInterface::luaGuildGetBankBalance);
|
||||
registerMethod("Guild", "increaseBankBalance", LuaScriptInterface::luaGuildIncreaseBankBalance);
|
||||
registerMethod("Guild", "decreaseBankBalance", LuaScriptInterface::luaGuildDecreaseBankBalance);
|
||||
|
||||
// Group
|
||||
registerClass("Group", "", LuaScriptInterface::luaGroupCreate);
|
||||
@@ -3572,7 +3583,7 @@ int LuaScriptInterface::luaIsInWar(lua_State* L)
|
||||
return 1;
|
||||
}
|
||||
|
||||
pushBoolean(L, player->isInWar(targetPlayer));
|
||||
lua_pushnumber(L, player->getWarId(targetPlayer));
|
||||
return 1;
|
||||
}
|
||||
|
||||
@@ -3590,6 +3601,40 @@ int LuaScriptInterface::luaGetWaypointPositionByName(lua_State* L)
|
||||
return 1;
|
||||
}
|
||||
|
||||
int LuaScriptInterface::luaSendChannelMessage(lua_State* L)
|
||||
{
|
||||
//sendChannelMessage(channelId, type, message)
|
||||
uint32_t channelId = getNumber<uint32_t>(L, 1);
|
||||
ChatChannel* channel = g_chat->getChannelById(channelId);
|
||||
if (!channel) {
|
||||
pushBoolean(L, false);
|
||||
return 1;
|
||||
}
|
||||
|
||||
SpeakClasses type = getNumber<SpeakClasses>(L, 2);
|
||||
std::string message = getString(L, 3);
|
||||
channel->sendToAll(message, type);
|
||||
pushBoolean(L, true);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int LuaScriptInterface::luaSendGuildChannelMessage(lua_State* L)
|
||||
{
|
||||
//sendGuildChannelMessage(guildId, type, message)
|
||||
uint32_t guildId = getNumber<uint32_t>(L, 1);
|
||||
ChatChannel* channel = g_chat->getGuildChannelById(guildId);
|
||||
if (!channel) {
|
||||
pushBoolean(L, false);
|
||||
return 1;
|
||||
}
|
||||
|
||||
SpeakClasses type = getNumber<SpeakClasses>(L, 2);
|
||||
std::string message = getString(L, 3);
|
||||
channel->sendToAll(message, type);
|
||||
pushBoolean(L, true);
|
||||
return 1;
|
||||
}
|
||||
|
||||
std::string LuaScriptInterface::escapeString(const std::string& string)
|
||||
{
|
||||
std::string s = string;
|
||||
@@ -9225,6 +9270,44 @@ int LuaScriptInterface::luaGuildGetName(lua_State* L)
|
||||
return 1;
|
||||
}
|
||||
|
||||
int LuaScriptInterface::luaGuildSetGuildWarEmblem(lua_State* L)
|
||||
{
|
||||
// guild:setGuildWarEmblem(guild2)
|
||||
const Guild* guild = getUserdata<const Guild>(L, 1);
|
||||
if (!guild) {
|
||||
lua_pushnil(L);
|
||||
return 1;
|
||||
}
|
||||
|
||||
const Guild* guild2 = getUserdata<const Guild>(L, 2);
|
||||
if (!guild2) {
|
||||
lua_pushnil(L);
|
||||
return 1;
|
||||
}
|
||||
|
||||
auto& members = guild->getMembersOnline();
|
||||
for (Player* player : members) {
|
||||
GuildWarList guildWarList;
|
||||
IOGuild::getWarList(player->getGuild()->getId(), guildWarList);
|
||||
player->guildWarList = guildWarList;
|
||||
}
|
||||
|
||||
auto& members2 = guild2->getMembersOnline();
|
||||
for (Player* player : members2) {
|
||||
GuildWarList guildWarList;
|
||||
IOGuild::getWarList(player->getGuild()->getId(), guildWarList);
|
||||
player->guildWarList = guildWarList;
|
||||
g_game.updateCreatureSkull(player);
|
||||
}
|
||||
|
||||
for (Player* player : members) {
|
||||
g_game.updateCreatureSkull(player);
|
||||
}
|
||||
|
||||
pushBoolean(L, true);
|
||||
return 1;
|
||||
}
|
||||
|
||||
int LuaScriptInterface::luaGuildGetMembersOnline(lua_State* L)
|
||||
{
|
||||
// guild:getMembersOnline()
|
||||
@@ -9306,6 +9389,49 @@ int LuaScriptInterface::luaGuildGetRankByLevel(lua_State* L)
|
||||
return 1;
|
||||
}
|
||||
|
||||
int LuaScriptInterface::luaGuildGetBankBalance(lua_State* L)
|
||||
{
|
||||
// guild:getBankBalance()
|
||||
Guild* guild = getUserdata<Guild>(L, 1);
|
||||
if (guild) {
|
||||
lua_pushnumber(L, IOGuild::getGuildBalance(guild->getId()));
|
||||
}
|
||||
else {
|
||||
lua_pushnil(L);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
int LuaScriptInterface::luaGuildIncreaseBankBalance(lua_State* L)
|
||||
{
|
||||
// guild:increaseBankBalance(amount)
|
||||
Guild* guild = getUserdata<Guild>(L, 1);
|
||||
if (guild) {
|
||||
uint32_t amount = getNumber<uint32_t>(L, 2);
|
||||
bool isSuccess = IOGuild::increaseGuildBankBalance(guild->getId(), amount);
|
||||
pushBoolean(L, isSuccess);
|
||||
}
|
||||
else {
|
||||
lua_pushnil(L);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
int LuaScriptInterface::luaGuildDecreaseBankBalance(lua_State* L)
|
||||
{
|
||||
// guild:decreaseBankBalance(amount)
|
||||
Guild* guild = getUserdata<Guild>(L, 1);
|
||||
if (guild) {
|
||||
uint32_t amount = getNumber<uint32_t>(L, 2);
|
||||
bool isSuccess = IOGuild::decreaseGuildBankBalance(guild->getId(), amount);
|
||||
pushBoolean(L, isSuccess);
|
||||
}
|
||||
else {
|
||||
lua_pushnil(L);
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Group
|
||||
int LuaScriptInterface::luaGroupCreate(lua_State* L)
|
||||
{
|
||||
|
||||
@@ -485,6 +485,9 @@ class LuaScriptInterface
|
||||
|
||||
static int luaGetWaypointPositionByName(lua_State* L);
|
||||
|
||||
static int luaSendChannelMessage(lua_State* L);
|
||||
static int luaSendGuildChannelMessage(lua_State* L);
|
||||
|
||||
#ifndef LUAJIT_VERSION
|
||||
static int luaBitNot(lua_State* L);
|
||||
static int luaBitAnd(lua_State* L);
|
||||
@@ -977,11 +980,16 @@ class LuaScriptInterface
|
||||
static int luaGuildGetId(lua_State* L);
|
||||
static int luaGuildGetName(lua_State* L);
|
||||
static int luaGuildGetMembersOnline(lua_State* L);
|
||||
static int luaGuildSetGuildWarEmblem(lua_State* L);
|
||||
|
||||
static int luaGuildAddRank(lua_State* L);
|
||||
static int luaGuildGetRankById(lua_State* L);
|
||||
static int luaGuildGetRankByLevel(lua_State* L);
|
||||
|
||||
static int luaGuildGetBankBalance(lua_State* L);
|
||||
static int luaGuildIncreaseBankBalance(lua_State* L);
|
||||
static int luaGuildDecreaseBankBalance(lua_State* L);
|
||||
|
||||
// Group
|
||||
static int luaGroupCreate(lua_State* L);
|
||||
|
||||
|
||||
@@ -3447,10 +3447,6 @@ Skulls_t Player::getSkullClient(const Creature* creature) const
|
||||
return SKULL_GREEN;
|
||||
}
|
||||
|
||||
if (!player->getGuildWarList().empty() && guild == player->getGuild()) {
|
||||
return SKULL_GREEN;
|
||||
}
|
||||
|
||||
if (player->hasAttacked(this)) {
|
||||
return SKULL_YELLOW;
|
||||
}
|
||||
@@ -3614,6 +3610,33 @@ bool Player::hasLearnedInstantSpell(const std::string& spellName) const
|
||||
return false;
|
||||
}
|
||||
|
||||
uint32_t Player::getWarId(const Player* targetPlayer) const
|
||||
{
|
||||
if (!targetPlayer || !guild) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const Guild* targetPlayerGuild = targetPlayer->getGuild();
|
||||
if (!targetPlayerGuild) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const auto targetGuild = guild->getId();
|
||||
const auto killerGuild = targetPlayerGuild->getId();
|
||||
|
||||
Database* db = Database::getInstance();
|
||||
|
||||
std::ostringstream query;
|
||||
query << "SELECT `id` FROM `guild_wars` WHERE `status` IN (1, 4) AND ((`guild1` = " << killerGuild << " AND `guild2` = " << targetGuild << ") OR (`guild1` = " << targetGuild << " AND `guild2` = " << killerGuild << "))";
|
||||
DBResult_ptr result = db->storeQuery(query.str());
|
||||
|
||||
if (!result) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
return result->getNumber<uint32_t>("id");
|
||||
}
|
||||
|
||||
bool Player::isInWar(const Player* player) const
|
||||
{
|
||||
if (!player || !guild) {
|
||||
|
||||
@@ -201,6 +201,7 @@ class Player final : public Creature, public Cylinder
|
||||
guildNick = nick;
|
||||
}
|
||||
|
||||
uint32_t getWarId(const Player* player) const;
|
||||
bool isInWar(const Player* player) const;
|
||||
bool isInWarList(uint32_t guild_id) const;
|
||||
|
||||
@@ -638,6 +639,11 @@ class Player final : public Creature, public Cylinder
|
||||
}
|
||||
}
|
||||
|
||||
void sendChannelMessage(const std::string& author, const std::string& text, SpeakClasses type, uint16_t channel) {
|
||||
if (client) {
|
||||
client->sendChannelMessage(author, text, type, channel);
|
||||
}
|
||||
}
|
||||
void sendCreatureAppear(const Creature* creature, const Position& pos, bool isLogin) {
|
||||
if (client) {
|
||||
client->sendAddCreature(creature, pos, creature->getTile()->getStackposOfCreature(this, creature), isLogin);
|
||||
|
||||
@@ -1176,6 +1176,18 @@ void ProtocolGame::sendChannel(uint16_t channelId, const std::string& channelNam
|
||||
writeToOutputBuffer(msg);
|
||||
}
|
||||
|
||||
void ProtocolGame::sendChannelMessage(const std::string& author, const std::string& text, SpeakClasses type, uint16_t channel)
|
||||
{
|
||||
NetworkMessage msg;
|
||||
msg.addByte(0xAA);
|
||||
msg.add<uint32_t>(0x00);
|
||||
msg.addString(author);
|
||||
msg.add<uint16_t>(0x00);
|
||||
msg.addByte(type);
|
||||
msg.add<uint16_t>(channel);
|
||||
msg.addString(text);
|
||||
writeToOutputBuffer(msg);
|
||||
}
|
||||
|
||||
void ProtocolGame::sendIcons(uint16_t icons)
|
||||
{
|
||||
|
||||
@@ -140,6 +140,7 @@ class ProtocolGame final : public Protocol
|
||||
void parseCloseChannel(NetworkMessage& msg);
|
||||
|
||||
//Send functions
|
||||
void sendChannelMessage(const std::string& author, const std::string& text, SpeakClasses type, uint16_t channel);
|
||||
void sendClosePrivate(uint16_t channelId);
|
||||
void sendCreatePrivateChannel(uint16_t channelId, const std::string& channelName);
|
||||
void sendChannelsDialog();
|
||||
|
||||
Reference in New Issue
Block a user