Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local MAXIMUM_RESERVE_TIME = 60 --minutes
- local keywordHandler = KeywordHandler:new()
- local npcHandler = NpcHandler:new(keywordHandler)
- npcHandler:setMessage(MESSAGE_GREET, "Greetings, |PLAYERNAME|. I am here to offer the opportunity to enage in an {instanced war}. Maybe you have received an {invitation}? Or do you want to {enter} an ongoing war?")
- NpcSystem.parseParameters(npcHandler)
- local arenas = {}
- local selected_arena = {}
- local time = {}
- local wartype = {}
- local playerLimit = {}
- local challengedguild = {}
- local guild1 = {}
- local guild2 = {}
- local challengedArena = {}
- local challengedDuration = {}
- local challengedGuild1 = {}
- local enemyGuild = {}
- local disablePotions = {}
- local numberOfArenas = {}
- local talkState = {}
- function onCreatureAppear(cid) npcHandler:onCreatureAppear(cid) end
- function onCreatureDisappear(cid) npcHandler:onCreatureDisappear(cid) end
- function onCreatureSay(cid, type, msg) npcHandler:onCreatureSay(cid, type, msg) end
- function onThink() npcHandler:onThink() end
- local function greetCallback(cid)
- --local player = Player(cid)
- --if(player:getGuild() == 0 or player:getGuild() == nil) then
- --selfSay("Hmpf.", cid)
- --return false
- --end
- --local guildLevel = player:getGuildLevel()
- --if guildLevel ~= 3 then
- --selfSay("Hmpf. Only guild leaders can speak to me.", cid)
- --return false
- --end
- talkState[cid] = 0
- return true
- end
- local function creatureSayCallback(cid, type, msg)
- local talkUser = NPCHANDLER_CONVBEHAVIOR == CONVERSATION_DEFAULT and 0 or cid
- if not npcHandler:isFocused(cid) then
- return false
- end
- if talkState[talkUser] == 0 then
- --Find the player guild
- local playerGuild = getPlayerGuildId(cid)
- --Find the guild name
- local playerGuildName = getPlayerGuildName(cid)
- -------------------------------------------------------------------------------------------------
- ---------------------------------------------hi--------------------------------------------------
- ---------------------------------------------war-------------------------------------------------
- if msg == "instanced war" or msgcontains(msg, "war") then
- if getPlayerGuildLevel(cid) == GUILDLEVEL_LEADER then
- selfSay("What is the name of the {enemy guild}?", cid)
- talkState[talkUser] = 1
- else
- selfSay("You are not the leader of your guild.", cid)
- talkState[talkUser] = 0
- end
- ---------------------------------------------enter-----------------------------------------------
- elseif(msgcontains(msg, "enter")) then
- if (isInWar(getPlayerGuildId(cid))) then
- --There is a war going on
- --Check for arena reservations
- local warArenaPos = getWarArena(cid)
- if(warArenaPos == nil) then
- selfSay("The limit of players on the arena has been reached.", cid)
- return false
- end
- query = "SELECT * FROM `guildwar_arenas` WHERE `inuse` = 1 and `guild2` = " .. getPlayerGuildId(cid) .. " or `guild1` = " .. getPlayerGuildId(cid)
- queryResult = db.storeQuery(query)
- local wartype = result.getDataString(queryResult, "type")
- result.free(queryResult)
- if(warArenaPos == false) then
- selfSay("No arena was reserved for your war.", cid)
- return false
- else
- doPlayerSendTextMessage(cid, 22, "You have been teleported to the arena.")
- setPlayerStorageValue(cid, 18774, 1)
- if wartype == 0 then
- setPlayerStorageValue(cid, 18775, 0)
- elseif wartype == 1 then
- setPlayerStorageValue(cid, 18775, 1)
- elseif wartype == 2 then
- setPlayerStorageValue(cid, 18775, 2)
- end
- doTeleportThing(cid, warArenaPos, false)
- end
- else
- selfSay("Your guild is not in a war.", cid)
- return false
- end
- return false
- ---------------------------------------------invitation------------------------------------------
- elseif(msgcontains(msg, "invitation")) then
- if(msg == playerGuildName) then
- selfSay("The only battlefield one can fight oneself is in one's mind.",cid)
- talkState[talkUser] = 0
- return false
- end
- query = "SELECT * FROM `guildwar_arenas` WHERE `pending` = 1 and `guild2` = " .. getPlayerGuildId(cid) .. " or `guild1` = " .. getPlayerGuildId(cid)
- queryResult = db.storeQuery(query)
- local pending = result.getDataInt(queryResult, "pending")
- if(pending == 0) then
- selfSay("There are no pending war invitations for your guild,", cid)
- result.free(queryResult)
- return false
- end
- --See if it was found
- if(queryResult == false) then
- selfSay("There are no pending war invitations for your guild,", cid)
- result.free(queryResult)
- return false
- else
- local guild1 = result.getDataInt(queryResult, "guild1")
- local guild2 = result.getDataInt(queryResult, "guild2")
- local arena = result.getDataString(queryResult, "name")
- local wartype = result.getDataString(queryResult, "type")
- local duration = result.getDataString(queryResult, "duration")
- local potionDisabled = result.getDataInt(queryResult, "disablePotions")
- local limitOfPlayers= result.getDataInt(queryResult, "maxplayers")
- local potionstring = "{with all potions}"
- challengedArena[cid] = arena
- challengedDuration[cid] = duration
- challengedGuild1[cid] = guild1
- if(wartype == "0") then
- warstring = "{default}"
- elseif(wartype == "1") then
- warstring = "{no UE}"
- elseif(wartype == "2") then
- warstring = "{SD only}"
- end
- if(potionDisabled == 1) then
- potionstring = "{without ultimate potions}"
- end
- if getPlayerGuildId(cid) == guild1 then
- query = "SELECT `name` FROM `guilds` WHERE `id` = " .. guild1
- else
- query = "SELECT `name` FROM `guilds` WHERE `id` = " .. guild2
- end
- queryResult = db.storeQuery(query)
- selfSay("You have been invited to a " .. warstring .. " war " .. potionstring .. " by {" .. result.getDataString(queryResult, "name") .. ",} the arena chosen is {" .. arena .. "}, the duration is set to {" .. duration .. " minutes} and the limit of players is {" .. limitOfPlayers .. "} on each side. Do you want to {accept}?", cid)
- talkState[talkUser] = 12
- result.free(queryResult)
- return false
- end
- end
- ---------------------------------------------war-----------------------------------------------
- ---------------------------------------------enemy guild name----------------------------------
- elseif talkState[talkUser] == 1 then
- --Find the player guild
- local playerGuild = getPlayerGuildId(cid)
- --Find the guild name
- local playerGuildName = getPlayerGuildName(cid)
- if(msg == playerGuildName) then
- selfSay("The only battlefield one can fight oneself is in one's mind.",cid)
- talkState[talkUser] = 1
- return false
- end
- --Prepare a db query
- --local query = "SELECT * FROM `guild_wars` WHERE `guild_id` = " .. getPlayerGuildId(cid) .. " or `enemy_id` = " .. getPlayerGuildId(cid)
- local query = "SELECT * FROM `guild_wars` WHERE NOT `status` = 5 and `guild_id` = " .. getPlayerGuildId(cid) .. " OR `enemy_id` = " .. getPlayerGuildId(cid) .. " AND NOT `status` =5"
- --Use it
- local queryResult = db.storeQuery(query)
- local temporaryStatus = false
- while (queryResult ~= false and temporaryStatus == false) do
- guild1[cid] = result.getDataInt(queryResult, "guild_id")
- guild2[cid] = result.getDataInt(queryResult, "enemy_id")
- local status = result.getDataInt(queryResult, "status")
- if status == 1 and (getGuildId(msg) == guild1[cid] or getGuildId(msg) == guild2[cid]) then
- temporaryStatus = true
- else
- result.next(queryResult)
- end
- end
- result.free(queryResult)
- --Find out if the guild is in war
- if(temporaryStatus == false) then
- print("Erro aqui!! UM")
- selfSay("Your guild is not in a war with ".. msg .. ".",cid)
- talkState[talkUser] = 0
- return false
- end
- challengedguild[cid] = msg
- free_arenas_string = getFreeArenas(cid)
- if(free_arenas_string == nil or free_arenas_string == "" or free_arenas_string == false or free_arenas_string == true) then
- selfSay("Unfortunately there are no free arenas. I'm sorry.", cid)
- talkState[talkUser] = 0
- result.free(queryResult)
- else
- selfSay("The free arenas are " .. free_arenas_string .. " Which one do you want?", cid)
- talkState[talkUser] = 2
- result.free(queryResult)
- end
- ---------------------------------------------war-----------------------------------------------
- ---------------------------------------------enemy guild name----------------------------------
- ---------------------------------------------selected arena------------------------------------
- elseif talkState[talkUser] == 2 then
- local isValid = false
- local arena = 0
- print("free arenas:")
- --for k,v in pairs(arenas[cid]) do print(k, v) end
- print("number of free arenas: " .. #arenas[cid] )
- for arena = 0, #arenas[cid], 1 do
- if(arenas[cid][arena] ~= nil) then
- if(msgcontains(msg,arenas[cid][arena])) then
- selected_arena[cid] = arenas[cid][arena]
- isValid = true
- selfSay(selected_arena[cid] .. ", very well. For how much time, in {minutes}, do you want to fight?", cid)
- talkState[talkUser] = 3
- return false
- end
- end
- end
- if(not isValid) then
- selfSay("What? There was no option with that name.", cid)
- return false
- end
- ---------------------------------------------war-----------------------------------------------
- ---------------------------------------------enemy guild name----------------------------------
- ---------------------------------------------selected arena------------------------------------
- ---------------------------------------------selected time-------------------------------------
- elseif talkState[talkUser] == 3 then
- time[cid] = tonumber(msg)
- if(time[cid] > MAXIMUM_RESERVE_TIME) then
- time[cid] = 0
- selfSay("The limit is 60 minutes, unfortunately.", cid)
- return false
- end
- selfSay("Perfect, do you want a {default} war, {no UE} war or {SD only} war?", cid)
- talkState[talkUser] = 4
- ---------------------------------------------war-----------------------------------------------
- ---------------------------------------------enemy guild name----------------------------------
- ---------------------------------------------selected arena------------------------------------
- ---------------------------------------------selected time-------------------------------------
- ---------------------------------------------selected type-------------------------------------
- elseif talkState[talkUser] == 4 then
- if(msgcontains(msg, "default")) then
- wartype[cid] = 0
- elseif(msgcontains(msg, "UE") or msgcontains(msg, "ue")) then
- wartype[cid] = 1
- elseif(msgcontains(msg, "SD") or msgcontains(msg, "sd")) then
- wartype[cid] = 2
- else
- selfSay("What? There was no option with that name.", cid)
- return false
- end
- talkState[talkUser] = 6
- disablePotions[cid] = 0
- selfSay("Lastly, would you like to limit the number of players? If yes, say the {number} of players allowed. If not, say {no}.", cid)
- return false
- ---------------------------------------------war-----------------------------------------------
- ---------------------------------------------enemy guild name----------------------------------
- ---------------------------------------------selected arena------------------------------------
- ---------------------------------------------selected time-------------------------------------
- ---------------------------------------------selected type-------------------------------------
- ---------------------------------------------potions-------------------------------------------
- elseif talkState[talkUser] == 5 then
- if(msgcontains(msg, "disable") or msgcontains(msg, "yes")) then
- disablePotions[cid] = 1
- elseif(msgcontains(msg,"no")) then
- disablePotions[cid] = 0
- else
- selfSay("Would you like to disable the supreme potions potions? {Yes} or {no}.", cid)
- talkState[talkUser] = 5
- return false
- end
- selfSay("Lastly, would you like to limit the number of players? If yes, say the {number} of players allowed. If not, say {no}.", cid)
- talkState[talkUser] = 6
- return false
- ---------------------------------------------war-----------------------------------------------
- ---------------------------------------------enemy guild name----------------------------------
- ---------------------------------------------selected arena------------------------------------
- ---------------------------------------------selected time-------------------------------------
- ---------------------------------------------selected type-------------------------------------
- ---------------------------------------------potions-------------------------------------------
- ---------------------------------------------player limit--------------------------------------
- elseif talkState[talkUser] == 6 then
- if(msgcontains(msg,"no")) then
- playerLimit[cid] = 1000
- else
- if(tonumber(msg) == nil) then
- playerLimit[cid] = 1000
- else
- playerLimit[cid] = tonumber(msg)
- end
- end
- selfSay("The war invitation is open now. The leader of the enemy guild must accept the invitation. The invitation expires in {one minute}. Good luck, " .. getPlayerName(cid) .. ".", cid)
- registerWarInvitation(selected_arena[cid], guild1[cid], guild2[cid], time[cid], wartype[cid], cid, disablePotions[cid], playerLimit[cid])
- broadcastMessage("Leader of guild " .. getPlayerGuildName(cid) .. " " .. getPlayerName(cid) .. " has challenged guild " .. challengedguild[cid] .. " to an instanced war on " .. selected_arena[cid] .. " with a duration of " .. time[cid] .. " minutes. The challenge will expire in 60 seconds.")
- talkState[talkUser] = 0
- ---------------------------------------------invitation-----------------------------------------------
- elseif talkState[talkUser] == 12 then
- if(msgcontains(msg,"yes") or msgcontains(msg,"accept")) then
- query = "UPDATE `guildwar_arenas` SET `playersOnTeamA`=0, `playersOnTeamB`=0, `start` = " .. os.time() .. ", `pending` = 0, `inuse` = 1 WHERE `name` = \"" .. challengedArena[cid] .. "\""
- db.query(query)
- selfSay("The arena is now open. Say {enter} to enter.", cid)
- addEvent(freeArena, 10 + 60*challengedDuration[cid]*1000, challengedArena[cid])
- talkState[talkUser] = 0
- broadcastMessage("Leader of guild " .. getPlayerGuildName(cid) .. " " .. getPlayerName(cid) .. " has accepted the challenge.")
- return false
- elseif(msgcontains(msg,"no")) then
- query = "UPDATE `guildwar_arenas` SET `end` = " .. os.time() .. ", `inuse` = 0, `guild1` = 0, `guild2` = 0 WHERE `name` = \"" .. challengedArena[cid] .. "\""
- db.query(query)
- selfSay("Alright, alright. The invitation was {refused}.", cid)
- talkState[talkUser] = 0
- end
- end
- return true
- end
- --local function onAddFocus(cid)
- --time[cid] = 0
- --time[cid] = 0
- --wartype[cid] = 0
- --selected_arena[cid] = 0
- --end
- --local function onReleaseFocus(cid)
- --time[cid] = nil
- --wartype[cid] = nil
- --selected_arena[cid] = nil
- --end
- --npcHandler:setCallback(CALLBACK_ONADDFOCUS, onAddFocus)
- --npcHandler:setCallback(CALLBACK_ONRELEASEFOCUS, onReleaseFocus)
- npcHandler:setCallback(CALLBACK_GREET, greetCallback)
- npcHandler:setCallback(CALLBACK_MESSAGE_DEFAULT, creatureSayCallback)
- npcHandler:addModule(FocusModule:new())
- ---------------------------------------------------------------------
- -----LIB FOR WAR SYSTEM----------------------------------------------
- ---------------------------------------------------------------------
- function registerWarInvitation(arena, guild1, guild2, duration, wartype, cid, disablePotions, playerLimit)
- local invitationQuery = "UPDATE `guildwar_arenas` SET `challenger` = " .. getPlayerGuildId(cid) .. ", `maxplayers`= ".. playerLimit .. ", `disablePotions`= ".. disablePotions .. ", `type`= " .. wartype .. ", `duration` = " .. duration .. " ,`start` = " .. os.time() .. ", `end` = 0, `inuse` = 0, `pending` = 1, `guild1` = " .. guild1 .. ", `guild2` = " .. guild2 .. " WHERE `name` = \"" .. arena .. "\""
- db.query(invitationQuery)
- addEvent(revokeWarInvitation, 60*1000, arena)
- return true
- end
- function revokeWarInvitation(arena)
- --print("The invitation has expired.")
- query = "UPDATE `guildwar_arenas` SET `pending` = 0 WHERE `name` = \"" .. arena .. "\""
- db.query(query)
- end
- function freeArena(arenaName)
- print("Reserve time is over, freeing arena " .. arenaName .. ".")
- local query = "SELECT * FROM `guildwar_arenas` WHERE `name` = \"" .. arenaName .. "\""
- local queryResult = db.storeQuery(query)
- if(query == false) then
- print("[Warning] Could not free arena: arena not found on table.")
- return false
- end
- local guild1 = result.getDataInt(queryResult, "guild1")
- local guild2 = result.getDataInt(queryResult, "guild2")
- print("Guilds involved: " .. guild1 .. " and " .. guild2)
- local inuse = result.getDataInt(queryResult, "inuse")
- if(inuse == 0) then
- print("[Warning] Could not free arena: arena was already free.")
- return false
- end
- query = "UPDATE `guildwar_arenas` SET `end` = " .. os.time() .. ", `inuse` = 0, `guild1` = 0, `guild2` = 0 WHERE `name` = \"" .. arenaName .. "\""
- db.query(query)
- print (arenaName .. " is now free to be used again.")
- local onlinePlayers = getPlayersOnline()
- for unused, targetPlayer in ipairs(onlinePlayers) do
- if(getPlayerStorageValue(targetPlayer, STORAGE_PLAYER_IN_ARENA) == 1) then
- print(getPlayerName(targetPlayer) .. " was inside the arena;")
- print(getPlayerName(targetPlayer) .. " is a member of guild " .. getPlayerGuildId(targetPlayer))
- if((getPlayerGuildId(targetPlayer) == guild1 or getPlayerGuildId(targetPlayer) == guild2)) then
- doTeleportThing(targetPlayer, getTownTemplePosition(getPlayerTown(targetPlayer)), false)
- setPlayerStorageValue(targetPlayer, STORAGE_PLAYER_IN_ARENA, 0)
- setPlayerStorageValue(targetPlayer, STORAGE_PLAYER_WAR_TYPE, 0)
- setPlayerStorageValue(targetPlayer, STORAGE_PLAYER_DISABLED_POTIONS, 0)
- print(getPlayerName(targetPlayer) .. " has been sent back to temple.")
- end
- end
- end
- result.free(queryResult)
- end
- function getFreeArenas(cid)
- local listOfFreeArenas = {}
- local arena_names = ""
- --Find available arenas
- --See if it was found
- query = "SELECT * FROM `guildwar_arenas` WHERE `inuse` = 0"
- --print("Running query " .. query)
- queryResult = db.storeQuery(query)
- if(queryResult == false) then
- return false
- end
- arenas[cid] = {}
- while queryResult ~= false do
- local currentArenaName = result.getDataString(queryResult, "name")
- if(currentArenaName == nil) then print ("nil") end
- table.insert(arenas[cid], currentArenaName)
- print("inserted " .. currentArenaName)
- arena_names = arena_names .. "{" .. currentArenaName .. "}, "
- if (not result.next(queryResult)) then
- break
- end
- end
- numberOfArenas[cid] = #arenas[cid]
- --arenas[cid] = listOfFreeArenas
- result.free(queryResult)
- return arena_names
- end
- function isInWar(playerGuildId)
- --Prepare a db query
- local query = "SELECT * FROM `guild_wars` WHERE `guild_id` = " .. playerGuildId .. " or `enemy_id` = " .. playerGuildId
- --Use it
- local queryResult = db.storeQuery(query)
- --Find out if the guild is in war
- if(queryResult == false) then
- return false
- end
- --Extract the results
- local guild1 = result.getDataInt(queryResult, "guild_id")
- local guild2 = result.getDataInt(queryResult, "enemy_id")
- local status = result.getDataInt(queryResult, "status")
- --Free result
- result.free(queryResult)
- if(status == 0) then
- return false
- else
- return true
- end
- end
- function getWarArena(cid)
- local playerGuildId = getPlayerGuildId(cid)
- print("[DEBUG] Guild Id = " .. tostring(playerGuildId))
- --Check for arena reservations
- query = "SELECT * FROM `guildwar_arenas` WHERE `guild1` = " .. playerGuildId .. " or `guild2` = " .. playerGuildId .." and `inuse`=1"
- queryResult = db.storeQuery(query)
- if(queryResult == false) then
- result.free(queryResult)
- return false
- end
- --Extract query results
- local arena_mapname = result.getDataString(queryResult, "name")
- local arena_inuse = result.getDataInt(queryResult, "inuse")
- local numberOfPlayersOnTeamA = result.getDataInt(queryResult, "playersOnTeamA")
- local numberOfPlayersOnTeamB = result.getDataInt(queryResult, "playersOnTeamB")
- local maxPlayers = result.getDataInt(queryResult, "maxplayers")
- if(arena_inuse == 0) then
- result.free(queryResult)
- return false
- end
- local guild1 = result.getDataInt(queryResult, "guild1")
- local guild2 = result.getDataInt(queryResult, "guild2")
- local arena_team_a_pos = {x = result.getDataInt(queryResult, "team_a_posx"), y = result.getDataInt(queryResult, "team_a_posy"), z = result.getDataInt(queryResult, "team_a_posz")}
- local arena_team_b_pos = {x = result.getDataInt(queryResult, "team_b_posx"), y = result.getDataInt(queryResult, "team_b_posy"), z = result.getDataInt(queryResult, "team_b_posz")}
- local war_type = result.getDataInt(queryResult, "type")
- local disabledPotions = result.getDataInt(queryResult,"disablePotions")
- local limitOfPlayers = result.getDataInt(queryResult,"maxplayers")
- --Player on the guild1 will be sent to arena pos A
- --Players on the guild2 will be sent to arena pos B
- if(playerGuildId == guild1) then
- if(numberOfPlayersOnTeamA == maxPlayers) then
- return nil
- end
- result.free(queryResult)
- local updatequery = "UPDATE `guildwar_arenas` SET `playersOnTeamA` = " .. tostring(numberOfPlayersOnTeamA+1) .. " WHERE `name` = \"" .. arena_mapname .. "\""
- setPlayerStorageValue(cid, STORAGE_PLAYER_IN_ARENA, 1)
- setPlayerStorageValue(cid, STORAGE_PLAYER_TEAM, 1)
- setPlayerStorageValue(cid, STORAGE_PLAYER_WAR_TYPE, war_type)
- setPlayerStorageValue(cid, STORAGE_PLAYER_DISABLED_POTIONS, disabledPotions)
- db.query(updatequery)
- result.free(queryResult)
- return arena_team_a_pos
- elseif(playerGuildId == guild2) then
- if(numberOfPlayersOnTeamB == maxPlayers) then
- return nil
- end
- result.free(queryResult)
- setPlayerStorageValue(cid, STORAGE_PLAYER_IN_ARENA, 1)
- setPlayerStorageValue(cid, STORAGE_PLAYER_TEAM, 2)
- setPlayerStorageValue(cid, STORAGE_PLAYER_WAR_TYPE, war_type)
- setPlayerStorageValue(cid, STORAGE_PLAYER_DISABLED_POTIONS, disabledPotions)
- local updatequery = "UPDATE `guildwar_arenas` SET `playersOnTeamB` = " .. tostring(numberOfPlayersOnTeamB+1) .. " WHERE `name` = \"" .. arena_mapname .. "\""
- db.query(updatequery)
- result.free(queryResult)
- return arena_team_b_pos
- else
- print("Warning: Error on enter war script.")
- print("Player guild id: " .. playerGuildId)
- print("Guild1 id: " .. guild1)
- print("Guild2 id: " .. guild2)
- result.free(queryResult)
- return false
- end
- result.free(queryResult)
- return false
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement