Last active
August 29, 2015 14:09
-
-
Save andrewmunro/2cfcffd4024272f25f94 to your computer and use it in GitHub Desktop.
Eluna Recruit a Friend script.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
------------------------------------ | |
--------- Defintions ------------ | |
RafDaysAccountsLinked = 30 | |
RecruiterAccounts = {} | |
RecruiteeAccounts = {} --Stored twice for less cpu cycles when querying | |
------------------------------------ | |
--------- Create Tables ---------- | |
local createAccountFriendsTable = [[ | |
CREATE TABLE IF NOT EXISTS `account_friends` ( | |
`id` int(11) unsigned NOT NULL DEFAULT '0', | |
`friend_id` int(11) unsigned NOT NULL DEFAULT '0', | |
`bind_date` int(11) unsigned NOT NULL DEFAULT '0' COMMENT 'Start Date', | |
`expire_date` int(11) unsigned NOT NULL DEFAULT '0' COMMENT 'Expire Date', | |
PRIMARY KEY (`id`,`friend_id`) | |
) ENGINE=MyISAM DEFAULT CHARSET=utf8 PACK_KEYS=0 COMMENT='Stores accounts for Refer-a-Friend System.'; | |
]] | |
AuthDBQuery(createAccountFriendsTable) | |
local createGrantableLevelsItem = [[ | |
REPLACE INTO `item_template` (`entry`, `class`, `subclass`, `name`, `displayid`, `Quality`, `Flags`, `BuyCount`, `BuyPrice`, `SellPrice`, `InventoryType`, `AllowableClass`, `AllowableRace`, `ItemLevel`, `RequiredLevel`, `RequiredSkill`, `RequiredSkillRank`, `requiredspell`, `requiredhonorrank`, `RequiredCityRank`, `RequiredReputationFaction`, `RequiredReputationRank`, `maxcount`, `stackable`, `ContainerSlots`, `stat_type1`, `stat_value1`, `stat_type2`, `stat_value2`, `stat_type3`, `stat_value3`, `stat_type4`, `stat_value4`, `stat_type5`, `stat_value5`, `stat_type6`, `stat_value6`, `stat_type7`, `stat_value7`, `stat_type8`, `stat_value8`, `stat_type9`, `stat_value9`, `stat_type10`, `stat_value10`, `dmg_min1`, `dmg_max1`, `dmg_type1`, `dmg_min2`, `dmg_max2`, `dmg_type2`, `dmg_min3`, `dmg_max3`, `dmg_type3`, `dmg_min4`, `dmg_max4`, `dmg_type4`, `dmg_min5`, `dmg_max5`, `dmg_type5`, `armor`, `holy_res`, `fire_res`, `nature_res`, `frost_res`, `shadow_res`, `arcane_res`, `delay`, `ammo_type`, `RangedModRange`, `spellid_1`, `spelltrigger_1`, `spellcharges_1`, `spellppmRate_1`, `spellcooldown_1`, `spellcategory_1`, `spellcategorycooldown_1`, `spellid_2`, `spelltrigger_2`, `spellcharges_2`, `spellppmRate_2`, `spellcooldown_2`, `spellcategory_2`, `spellcategorycooldown_2`, `spellid_3`, `spelltrigger_3`, `spellcharges_3`, `spellppmRate_3`, `spellcooldown_3`, `spellcategory_3`, `spellcategorycooldown_3`, `spellid_4`, `spelltrigger_4`, `spellcharges_4`, `spellppmRate_4`, `spellcooldown_4`, `spellcategory_4`, `spellcategorycooldown_4`, `spellid_5`, `spelltrigger_5`, `spellcharges_5`, `spellppmRate_5`, `spellcooldown_5`, `spellcategory_5`, `spellcategorycooldown_5`, `bonding`, `description`, `PageText`, `LanguageID`, `PageMaterial`, `startquest`, `lockid`, `Material`, `sheath`, `RandomProperty`, `block`, `itemset`, `MaxDurability`, `area`, `Map`, `BagFamily`, `ScriptName`, `DisenchantID`, `FoodType`, `minMoneyLoot`, `maxMoneyLoot`, `Duration`, `ExtraFlags`) VALUES (25000, 15, 0, 'Recruit a Friend Grantable Level', 32282, 4, 64, 1, 0, 0, 0, -1, -1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 30, 30, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1000, 0, 0, 18349, 0, 0, 0, 1000, 0, -1, 0, 0, 0, 0, -1, 0, -1, 0, 0, 0, 0, -1, 0, -1, 0, 0, 0, 0, -1, 0, -1, 0, 0, 0, 0, 0, 0, 0, 4, 'Use: Grants a level to your target. Target must be a character linked to you through the RAF system.', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, '', 0, 0, 0, 0, 0, 0); | |
]] | |
WorldDBQuery(createGrantableLevelsItem) | |
local createSummonFriendItem = [[ | |
REPLACE INTO `item_template` (`entry`, `class`, `subclass`, `name`, `displayid`, `Quality`, `Flags`, `BuyCount`, `BuyPrice`, `SellPrice`, `InventoryType`, `AllowableClass`, `AllowableRace`, `ItemLevel`, `RequiredLevel`, `RequiredSkill`, `RequiredSkillRank`, `requiredspell`, `requiredhonorrank`, `RequiredCityRank`, `RequiredReputationFaction`, `RequiredReputationRank`, `maxcount`, `stackable`, `ContainerSlots`, `stat_type1`, `stat_value1`, `stat_type2`, `stat_value2`, `stat_type3`, `stat_value3`, `stat_type4`, `stat_value4`, `stat_type5`, `stat_value5`, `stat_type6`, `stat_value6`, `stat_type7`, `stat_value7`, `stat_type8`, `stat_value8`, `stat_type9`, `stat_value9`, `stat_type10`, `stat_value10`, `dmg_min1`, `dmg_max1`, `dmg_type1`, `dmg_min2`, `dmg_max2`, `dmg_type2`, `dmg_min3`, `dmg_max3`, `dmg_type3`, `dmg_min4`, `dmg_max4`, `dmg_type4`, `dmg_min5`, `dmg_max5`, `dmg_type5`, `armor`, `holy_res`, `fire_res`, `nature_res`, `frost_res`, `shadow_res`, `arcane_res`, `delay`, `ammo_type`, `RangedModRange`, `spellid_1`, `spelltrigger_1`, `spellcharges_1`, `spellppmRate_1`, `spellcooldown_1`, `spellcategory_1`, `spellcategorycooldown_1`, `spellid_2`, `spelltrigger_2`, `spellcharges_2`, `spellppmRate_2`, `spellcooldown_2`, `spellcategory_2`, `spellcategorycooldown_2`, `spellid_3`, `spelltrigger_3`, `spellcharges_3`, `spellppmRate_3`, `spellcooldown_3`, `spellcategory_3`, `spellcategorycooldown_3`, `spellid_4`, `spelltrigger_4`, `spellcharges_4`, `spellppmRate_4`, `spellcooldown_4`, `spellcategory_4`, `spellcategorycooldown_4`, `spellid_5`, `spelltrigger_5`, `spellcharges_5`, `spellppmRate_5`, `spellcooldown_5`, `spellcategory_5`, `spellcategorycooldown_5`, `bonding`, `description`, `PageText`, `LanguageID`, `PageMaterial`, `startquest`, `lockid`, `Material`, `sheath`, `RandomProperty`, `block`, `itemset`, `MaxDurability`, `area`, `Map`, `BagFamily`, `ScriptName`, `DisenchantID`, `FoodType`, `minMoneyLoot`, `maxMoneyLoot`, `Duration`, `ExtraFlags`) VALUES (25001, 15, 0, 'Recruit a Friend Summon Friend', 6418, 4, 64, 1, 0, 0, 0, -1, -1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 18349, 0, 0, 0, 3600000, 0, -1, 0, 0, 0, 0, -1, 0, -1, 0, 0, 0, 0, -1, 0, -1, 0, 0, 0, 0, -1, 0, -1, 0, 0, 0, 0, 0, 0, 0, 4, 'Use: Summons your target. Target must be a character linked to you through the RAF system.', 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 'Summon_Friend_Trigger', 0, 0, 0, 0, 0, 0); | |
]] | |
WorldDBQuery(createSummonFriendItem) | |
------------------------------------ | |
--------- RafLink Loader -------- | |
local function AddRafLink(_recruiter, _recruitee) | |
local _startTime = os.time() | |
local _expireTime = startTime + RafDaysAccountsLinked * 24 * 3600 | |
local q = AuthDBQuery("INSERT INTO account_friends (id, friend_id, start_time, expire_date) VALUES (".._recruiter..","..recruitee..",".._startTime..", ".._expireTime")") | |
RecruiterAccounts[_recruiter] = { recruiter=_recruiter, recruitee = _recruitee, startTime = _startTime, expireTime = _expireTime } | |
RecruiteeAccounts[_recruitee] = { recruiter=_recruiter, recruitee = _recruitee, startTime = _startTime, expireTime = _expireTime } | |
end | |
local function RemoveRafLink(link, player) | |
local q = AuthDBQuery("DELETE FROM account_friends WHERE id = "..link.recruiter) | |
RecruiterAccounts[recruiterAccountId] = nil | |
RecruiteeAccounts[recruiterAccountId] = nil | |
end | |
local function LoadDatabase() | |
local Q = AuthDBQuery("SELECT * FROM account_friends;"); | |
if(Q) then | |
repeat | |
RecruiterAccounts[Q:GetUInt32(0)] = { recruiter=Q:GetUInt32(0), recruitee = Q:GetUInt32(1), startTime = Q:GetUInt32(2), expireTime = Q:GetUInt32(3) } | |
RecruiteeAccounts[Q:GetUInt32(1)] = { recruiter=Q:GetUInt32(0), recruitee = Q:GetUInt32(1), startTime = Q:GetUInt32(2), expireTime = Q:GetUInt32(3) } | |
until not Q:NextRow() | |
end | |
end | |
LoadDatabase() | |
local function AddRafLinkIfExists(id) | |
if(RecruiterAccounts[id] or RecruiteeAccounts[id]) then | |
return | |
end | |
local Q = AuthDBQuery("SELECT * FROM account_friends where (id = "..id.." or friend_id = "..id); | |
if(Q) then | |
RecruiterAccounts[Q:GetUInt32(0)] = { recruiter=Q:GetUInt32(0), recruitee = Q:GetUInt32(1), startTime = Q:GetUInt32(2), expireTime = Q:GetUInt32(3) } | |
RecruiteeAccounts[Q:GetUInt32(1)] = { recruiter=Q:GetUInt32(0), recruitee = Q:GetUInt32(1), startTime = Q:GetUInt32(2), expireTime = Q:GetUInt32(3) } | |
end | |
end | |
------------------------------------ | |
------------ Helpers -------------- | |
local function GetLinkedAccount(player) | |
local recruiterLink = RecruiterAccounts[player:GetAccountId()] | |
if recruiterLink then | |
return recruiterLink | |
else | |
--If player is recruitee | |
return RecruiteeAccounts[player:GetAccountId()] | |
end | |
end | |
local function RafLinkExpired(player) | |
return GetLinkedAccount(player).expireTime < os.time() | |
end | |
local function IsRecruitee(player) | |
return player:GetAccountId() == GetLinkedAccount(player).recruitee | |
end | |
local function LinkedPlayerRewarded(player) | |
local member = GetLinkedPlayerInGroup(player) | |
if member and player:GetDistance(member) < 74 then | |
return true | |
end | |
return false | |
end | |
function GetLinkedAccountId(player) | |
local linkedAccount = GetLinkedAccount(player) | |
if not linkedAccount then | |
return nil | |
end | |
if IsRecruitee(player) then | |
return linkedAccount.recruiter | |
else | |
return linkedAccount.recruitee | |
end | |
end | |
function GetLinkedPlayerInGroup(player) | |
local linkedAccountId = GetLinkedAccountId(player) | |
local group = player:GetGroup() | |
if group then | |
for i, member in pairs(group:GetMembers()) do | |
if member:GetAccountId() == linkedAccountId then | |
return member | |
end | |
end | |
end | |
return nil | |
end | |
------------------------------------ | |
------------ Events -------------- | |
local function OnPlayerLogin(event, player) | |
AddRafLinkIfExists(player:GetAccountId()) | |
local link = GetLinkedAccount(player) | |
if link then | |
if RafLinkExpired(player) then | |
player:SendAreaTriggerMessage("|cff7DFF00Your recruit a friend link has now expired!|r"); | |
else | |
local daysToExpire = (link.expireTime - os.time()) / 3600 / 24 | |
player:SendAreaTriggerMessage("|cff7DFF00This account is recruit a friend linked for "..math.ceil(daysToExpire).." more days.|r"); | |
if player:HasItem(25001) == false then | |
player:AddItem(25001, 1) | |
end | |
end | |
end | |
end | |
local function OnPlayerXP(event, player, amount, victim) | |
local link = GetLinkedAccount(player) | |
print(LinkedPlayerRewarded(player)) | |
if link and not alreadyGrantingBonusXp and not RafLinkExpired(player) and LinkedPlayerRewarded(player) then | |
bonus = amount * 2 | |
return amount + bonus | |
end | |
end | |
local function OnLevelChange(event, player) | |
local link = GetLinkedAccount(player) | |
if link and not RafLinkExpired(player) and GetLinkedPlayerInGroup(player) and IsRecruitee(player) and player:GetLevel() % 2 == 0 then | |
player:AddItem(25000, 1) | |
end | |
end | |
RegisterPlayerEvent(3, OnPlayerLogin) | |
RegisterPlayerEvent(12, OnPlayerXP) | |
RegisterPlayerEvent(13, OnLevelChange) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
GrantLevelItemId = 25000 | |
SummonFriendItemId = 25001 | |
local function GrantLevel(event, player, item, target) | |
local target = player:GetSelection() | |
if not target then | |
player:SendAreaTriggerMessage("|cff7DFF00You must be targeting someone to grant a level!|r") | |
elseif target:GetAccountId() ~= GetLinkedAccountId(player) then | |
player:SendAreaTriggerMessage("|cff7DFF00Your target must be your recruit a friend partner to grant a level!|r") | |
elseif target:GetLevel() >= 60 then | |
player:SendAreaTriggerMessage("|cff7DFF00Your target must be below level 60!|r") | |
elseif target:GetLevel() >= player:GetLevel() then | |
player:SendAreaTriggerMessage("|cff7DFF00Your target must be below your own level!|r") | |
else | |
local target = player:GetSelection() | |
target:GiveLevel(target:GetLevel() + 1) | |
target:SendAreaTriggerMessage("|cff7DFF00You have been granted a level by "..player:GetName().."|r") | |
player:SendAreaTriggerMessage("|cff7DFF00You have granted a level to "..target:GetName().."|r") | |
player:RemoveItem(25000, 1) | |
end | |
return true | |
end | |
local function SummonFriend(event, player, item, target) | |
local target = player:GetSelection() | |
if not player:GetSelection() then | |
player:SendAreaTriggerMessage("|cff7DFF00You must be targeting someone to summon them!|r") | |
elseif not GetLinkedPlayerInGroup(player) or target:GetAccountId() ~= GetLinkedAccountId(player) then | |
player:SendAreaTriggerMessage("|cff7DFF00You must be in a party and be targeting your recruit a friend partner to summon them!|r") | |
elseif player:InBattleground() then | |
player:SendAreaTriggerMessage("|cff7DFF00Your cannot summon your friend whilst in a battleground!|r") | |
else | |
player:SummonPlayer(target, player:GetMapId(), player:GetX(), player:GetY(), player:GetZ(), player:GetZoneId()) | |
player:SendAreaTriggerMessage("|cff7DFF00You have requested to summon "..player:GetName().."|r") | |
return true | |
end | |
return false | |
end | |
RegisterItemEvent(GrantLevelItemId, 2, GrantLevel) | |
RegisterItemEvent(SummonFriendItemId, 2, SummonFriend) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment