• This is a read only backup of the old Emudevs forum. If you want to have anything removed, please message me on Discord: KittyKaev

[SOLVED] Loot Script

Status
Not open for further replies.

Alexunder

Enthusiast
Hey,can anyone help me with a script that if you kill a creature automatically updates a table from database,like if i kill a creature i receive 2 points in my auth->accounts->points table?

Thx and sorry for my bad english...
 
Last edited:

slp13at420

Mad Scientist
something like this:
https://github.com/BlackWolfsDen/misc.git

Code:
[COLOR="#808080"]
// By slp13at420 of EmuDevs.com

std::string DB_COLUMN = "reward_count"; // this is the name of the element in auth.account you added to store this value.

void StoreCredit(uint32 id, uint32 count);
{
	LoginDatabase.PExecute("UPDATE account SET `%s` = '%u' WHERE `id` = '%u';", DB_COLUMN.c_str(), count, id);
};

class KillCreatureCredit : public CreatureScript
{
public: KillCreatureCredit() : CreatureScript("KillCreatureCredit"){ }

	struct KillCreatureCredit_AI : public ScriptedAI
	{
		void JustDied(Unit* unit)
		{
			if (unit->ToPlayer())
			{
				StoreCredit(player->GetSession()->GetAccountId(), 2);
			}
		}
	}
	CreatureAI* GetAI(Creature* creature)const override
	{
		return new KillCreatureCredit_AI(creature);
	}
};

void AddSC_KillCreatureRewarder()
{
	new KillCreatureCredit;
}[/COLOR]

you will need to edit the name to reflect the entry in your auth.account DB you will use to store this value.
every time you kill a creature it will update the auth.account db whatever value you want it to give.
you will need to add the script name to the creature's world.creature_template.ScriptName.

this will push to the sql server at every kill .... multiple players killing ....
That would Bog down even my IBM's and freeze em up ;P

maybe store it in a table then on logout push the data to the sql server.
maybe on login load the data.
 
Last edited:

Tommy

Founder
void OnCreatureKill
CharacterDatabase.Pquery

Look for this keywords. Should be help. If not, check this video:
https://www.youtube.com/watch?v=31wdIpdxoxc&list=PLu6yRPZ4Ec3ZMtFLO6mtPI8fuZ3CLq10B&index=23

As Alexunder pointed out after the fact (as he solved it himself I guess), OnCreatureKill hook is ideal for this.

The approach you came up with slp is quite redundant, heh. The fact that you have to assign the ScriptName to every creature you want it on is a big hassle. It is best to limit creatures within OnCreatureKill hook instead. Besides, he didn't specify this usage on specific creatures, just "if I kill a creature".
 

slp13at420

Mad Scientist
o.0 on PlayerKillCreature lol yea that would cover ALL creatures witch really would query blast
yea that's an easy fix lol just giveme a sec or 3
 

slp13at420

Mad Scientist
ok sooo I haven't worked with miuch of the player events other than login/logout blah blah blah lol

so did some quick surfing and came up with this :
Code:
[COLOR="#808080"]
// By slp13at420 of EmuDevs.com

#include "AccountMgr.h"
#include "chat.h"
#include "Player.h"
#include "ScriptMgr.h"
#include "WorldSession.h"

std::string DB_COLUMN = "reward_count"; // this is the name of the element in auth.account you added to store this value. use default '0' so it wont need another block for OnAccountCreation.
std::unordered_map<uint32, uint32>CKR;

void StoreCredit(uint32 id, uint32 count)
{
	LoginDatabase.PExecute("UPDATE account SET `%s` = '%u' WHERE `id` = '%u';", DB_COLUMN.c_str(), count, id);
};

class KillCreatureCredit_Account_Events : public AccountScript
{
public: KillCreatureCredit_Account_Events() : AccountScript("KillCreatureCredit_Account_Events"){ };

	virtual void OnAccountLogout(uint32 accountId)
	{
		WorldDatabase.PExecute("REPLACE INTO account SET `%s`='%u';", DB_COLUMN.c_str(), CKR[accountId]);

		CKR.erase(accountId);
	};

	virtual void OnAccountLogin(uint32 accountId)
	{
		if (accountId > 0)
		{
			QueryResult CKRPlayerQery = LoginDatabase.PQuery("SELECT `%s` FROM `account` WHERE `id` = '%u';", DB_COLUMN.c_str(), accountId);

			if (CKRPlayerQery)
			{
				do
				{
					Field* fields = CKRPlayerQery->Fetch();
					uint32 Reward_Count = fields[0].GetUInt32();

					CKR[accountId] = Reward_Count;

				} while (CKRPlayerQery->NextRow());
			}
		}
	};
};

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

	virtual void OnCreatureKill(Player* player, Creature* killed)
	{
		ChatHandler(player->GetSession()).PSendSysMessage("You earned x rewards.");

		StoreCredit(player->GetSession()->GetAccountId(), 2);
	};
};

void AddSC_KillCreatureRewarder()
{
	new KillCreatureCredit_Account_Events;
	new KillCreatureCredit;
}
[/COLOR]

total rough draft soo it may need mega tweeking. maybe be able to drop some includes lol.

so rather than just pushing the data to the sql server at EVERY npc kill. it will store the value and then push to the sql server when the player logs out. and load it when they login..

and in theory it should work but in real world it just sits there staring at the wall...
added the column to my db auth.account.
compiled clean.
hmm gonna try re-cmaking it again lol
naw missed adding it to /custom/custom_script_loader.cpp
 
Last edited:

slp13at420

Mad Scientist
Ok tested.
it loads there total count from the auth.account DB and stores it in ram.
each kill increases the players rewards count.
upon logout it will store the reward count to the auth.account DB.
this makes it account bound via account id .

this will trigger on all creature kills for player.

Code:
[COLOR="#808080"]
// By slp13at420 of EmuDevs.com

#include "AccountMgr.h"
#include "chat.h"
#include "Player.h"
#include "ScriptMgr.h"
#include "WorldSession.h"

uint32 reward_value = 2;

std::string DB_COLUMN = "reward_count"; // this is the name of the element in auth.account you added to store this value. use default '0' so it wont need another block for OnAccountCreation.

std::unordered_map<uint32, uint32>CKR;

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

		virtual void OnLogout(Player* player)
		{
			uint32 accountId = player->GetSession()->GetAccountId();

			LoginDatabase.PExecute("UPDATE `account` SET `%s` = '%u' WHERE `id` = '%u';", DB_COLUMN.c_str(), CKR[accountId], accountId);

			CKR.erase(accountId);
		};

		virtual void OnLogin(Player* player, bool /*firstLogin*/)
		{
			uint32 accountId = player->GetSession()->GetAccountId();
			
			QueryResult CKRPlayerQery = LoginDatabase.PQuery("SELECT `%s` FROM `account` WHERE `id` = '%u';", DB_COLUMN.c_str(), accountId);

			if (CKRPlayerQery)
			{
				do
				{
					Field* fields = CKRPlayerQery->Fetch();
					uint32 Reward_Count = fields[0].GetUInt32();

					CKR[accountId] = Reward_Count;

				} while (CKRPlayerQery->NextRow());
			}
		};
		
		void OnCreatureKill(Player* player, Creature* killed) override
		{
			uint32 accountId = player->GetSession()->GetAccountId();

			CKR[accountId] = CKR[accountId] + reward_value;

			ChatHandler(player->GetSession()).PSendSysMessage("You earned %u rewards for a total of %u points.", reward_value, CKR[accountId]);
		};
};

void AddSC_KillCreatureRewarder()
{
	new KillCreatureCredit;
}
[/COLOR]
 
Last edited:

Alexunder

Enthusiast
Thx alot guys for your help, im in a diferent time zone that's why i didnt answer.This script request is for grumboz vip script with magic gold...
If you can edit so the script will be for single creature no all creatures,the script need to be specified in database->creature_template->ScriptName...so if i asign to a specified creature only that creature will give the reward not all creatures.Players will farm low hp creatures insteand of killing the custom ones.
I dont know how this text came out, i didn't write it, i think he came automatically
Code:
void OnCreatureKill
CharacterDatabase.Pquery

Look for this keywords. Should be help. If not, check this video:
https://www.youtube.com/watch?v=31wd...Lq10B&index=23

Thx alot for your help, i dont know how to thank you more....
 
Last edited:

slp13at420

Mad Scientist
ok soooo this is what I came up with:
Code:
[COLOR="#808080"]
// By slp13at420 of EmuDevs.com

#include "AccountMgr.h"
#include "chat.h"
#include "Player.h"
#include "ScriptMgr.h"
#include "WorldSession.h"

uint32 reward_value = 2;

std::string DB_COLUMN = "reward_count"; // this is the name of the element in auth.account you added to store this value. use default '0' so it wont need another block for OnAccountCreation.

std::unordered_map<uint32, uint32>CKR;

std::array<uint32, 255> CreatureList = { 3100, 3127, 600004, 49999, 600003}; // this is where you add each creature entry id. should work for up to 255 unique id's uint8.

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

		virtual void OnLogout(Player* player)
		{
			uint32 accountId = player->GetSession()->GetAccountId();

			LoginDatabase.PExecute("UPDATE `account` SET `%s` = '%u' WHERE `id` = '%u';", DB_COLUMN.c_str(), CKR[accountId], accountId);

			CKR.erase(accountId);
		};

		virtual void OnLogin(Player* player, bool /*firstLogin*/)
		{
			uint32 accountId = player->GetSession()->GetAccountId();

			QueryResult CKRPlayerQery = LoginDatabase.PQuery("SELECT `%s` FROM `account` WHERE `id` = '%u';", DB_COLUMN.c_str(), accountId);

			if (CKRPlayerQery)
			{
				do
				{
					Field* fields = CKRPlayerQery->Fetch();
					uint32 Reward_Count = fields[0].GetUInt32();

					CKR[accountId] = Reward_Count;

				} while (CKRPlayerQery->NextRow());
			}
		};

		virtual void OnCreatureKill(Player* player, Creature* creature)
		{
			uint32 accountId = player->GetSession()->GetAccountId();
			uint32 creatureId = creature->GetEntry();

			for (auto i = CreatureList.begin(); i <= CreatureList.end(); ++i)
			{

				if (creatureId == *i)
				{
					CKR[accountId] = CKR[accountId] + reward_value;

					ChatHandler(player->GetSession()).PSendSysMessage("You earned %u rewards for a total of %u points.", reward_value, CKR[accountId]);

					break;
				}
			}
		};
};

void AddSC_KillCreatureRewarder()
{
	new KillCreatureCredit;
}
[/COLOR]

there is a table where you can add the id's of all the creatures you want to give the reward on kill.
when a player kills a creature this will check the table to see if its in the list and if-so then reward.
 
Last edited:

Alexunder

Enthusiast
Im not a flattery person but i cant believe that you guys help other with such a pleasure.If i requested that script on other forums i get a late response,they request moneygives you shitty scripts or they tell you to learn c++.I know there's good forums but some of them....

Edit:So this is the only way to work, i mean you cant just ignore the ids in c++ so you only put the script name in creature_templates?
 
Last edited:

slp13at420

Mad Scientist
Im not a flattery person but i cant believe that you guys help other with such a pleasure.If i requested that script on other forums i get a late response,they request moneygives you shitty scripts or they tell you to learn c++.I know there's good forums but some of them....
yeaa.. I do see that sometimes when im surfin forums looking for examples. These guyz here run a clean ship :thumbupguy:

Edit:So this is the only way to work, i mean you cant just ignore the ids in c++ so you only put the script name in creature_templates?

no no you can have a script as a CreatureScript and add its name to the creature_template.ScriptName .
some of it would need to be re-done for CreatureScript but you can only place 1 script name in ScriptName so limiting the creature to what is in that script as Tommy was pointing out earlier.


you can add a new element to your creature_template say called `reward` and use 0/1 for yes/no then the core would load those creatures during startup , and then add more directly via the sql but that would require a `.server restart x` to apply the changes.
I like this idea better but I would use reward as amount . 0 for no reward and anything above 0 is rewarded AND this leaves ScriptName empty so you may later add a script to the creature and it will still reward also.
 
Last edited:

slp13at420

Mad Scientist
Ok, i will try thx alot.

no prob enjoy :D

I will be adding a rewrite of this to my C++ VIP System sometime today/tomorrow so you will be able to update you VIP System.
it will work the way I like as I posted above .

- Updated -
updated my CPP Grumbo'z VIP Core with a drop script that will only apply to the creatures you want it to effect.
 
Last edited:
Status
Not open for further replies.
Top