Facebook
From Queen Bat, 3 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 57
  1. local config = {
  2.         maxNameLength = 8, -- Do not touch without asking me !!
  3.         minHomes = 1,
  4.         delimeter = "|" -- Don't touch
  5. }
  6.  
  7. function onSay(cid, words, param, channel)
  8.  
  9.         words = string.gsub(words, "/", "")
  10.  
  11.         local homes = db.getResult("SELECT * FROM `homeTowns` WHERE `player_id`=".. getPlayerGUID(cid))
  12.  
  13.         if(words == 'home') then
  14.  
  15.                 if(homes:getID() ~= -1) then
  16.                         local homeTable = string.explode(homes:getDataString("homes"), config.delimeter)
  17.  
  18.                         if(numberOfHomes(homeTable) > config.minHomes) then
  19.                                 if(param == '') then
  20.                                         local playerHomes = "Your Home Locations:\n\n"
  21.                                         for i = 1, table.maxn(homeTable), 1 do
  22.                                                 local homeData = string.explode(homeTable[i], "=")
  23.                                                 if(tostring(homeData[1]) ~= "nil") then
  24.                                                         playerHomes = playerHomes .. tostring(homeData[1]) .."\n"
  25.                                                 end
  26.                                         end
  27.  
  28.                                         doPlayerPopupFYI(cid, playerHomes)
  29.                                 else
  30.                                         local homeExists = false
  31.                                         for i = 1, table.maxn(homeTable), 1 do
  32.                                                 local homeData = string.explode(homeTable[i], "=")
  33.  
  34.                                                 if(tostring(homeData[1]) ~= "nil") then
  35.  
  36.                                                         if param == tostring(homeData[1]) then
  37.                                                                 homeExists = true
  38.                                                                 local pos = string.explode(homeData[2], ",")
  39.                                                                 doTeleportThing(cid, {x = pos[1], y = pos[2], z = pos[3]})
  40.  
  41.                                                                 if(not isPlayerGhost(cid)) then
  42.                                                                         doSendMagicEffect(getCreaturePosition(cid), 10)
  43.                                                                 end
  44.  
  45.                                                         end
  46.  
  47.                                                 end
  48.  
  49.                                         end
  50.  
  51.                                         if not homeExists then
  52.                                                 doPlayerSendCancel(cid, "There is no home with name " .. tostring(param))
  53.                                                 return true
  54.                                         end
  55.                                 end
  56.                         else
  57.                                 if(homeTable[1] == nil) then
  58.                                         doPlayerSendCancel(cid, "You don't have any saved locations. use /sethome <name> to add current location.")
  59.                                         return true
  60.                                 end
  61.  
  62.                                 local homeData = string.explode(homeTable[1], "=")
  63.  
  64.                                 if param ~= homeData[1] and param ~= "" then
  65.                                         doPlayerSendCancel(cid, "There is no home with name " .. tostring(param))
  66.                                         return true
  67.                                 end
  68.                                 local pos = string.explode(homeData[2], ",")
  69.                                 doTeleportThing(cid, {x = pos[1], y = pos[2], z = pos[3]})
  70.  
  71.                                 if(not isPlayerGhost(cid)) then
  72.                                         doSendMagicEffect(getCreaturePosition(cid), 10)
  73.                                 end
  74.                         end
  75.  
  76.                 else
  77. --                      doPlayerSendCancel(cid, "You don't have any saved locations. use /sethome <name> to add current location.")
  78.                         return true
  79.                 end
  80.  
  81.         elseif(words == 'sethome') then
  82.                 if param == '' then
  83.                         doPlayerSendCancel(cid, "Home name missing! Usage: /sethome home123")
  84.                         return true
  85.                 end
  86.  
  87.                 if string.len(param) > config.maxNameLength then
  88.                         doPlayerSendCancel(cid, "Home name can only be 8 characters long!")
  89.                         return true
  90.                 end
  91.  
  92.                 -- check if player already has houses saved
  93.                 if(homes:getID() ~= -1) then
  94.                         local homeTable = string.explode(homes:getDataString("homes"), config.delimeter)
  95.  
  96.                         if(numberOfHomes(homeTable) == 10) then
  97.                                 doPlayerSendCancel(cid, "Can not add new home, you have reached maximum home limit : 10.")
  98.                                 return true
  99.                         end
  100.  
  101.                         local pos = getCreaturePosition(cid)
  102.                         local homename = param
  103.                         param = param .. "=" .. tostring(pos.x) .. "," .. tostring(pos.y) .. "," .. tostring(pos.z) .. config.delimeter
  104.                         local update = db.executeQuery('UPDATE `homeTowns` SET `homes` = "' .. tostring(homes:getDataString("homes")) .. tostring(param) .. '" WHERE `player_id`='..getPlayerGUID(cid))
  105.                         if(not update) then
  106.                                 error("Failed to update player homes :".. getPlayerName(cid) .. ", " .. tostring(param))
  107.                                 return true
  108.                         end
  109.                         doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_ORANGE, tostring(homename) .. " has been added to your home locations.")
  110.                 else
  111.                         local pos = getCreaturePosition(cid)
  112.                         local homename = param
  113.                         param = param .. "=" .. tostring(pos.x) .. "," .. tostring(pos.y) .. "," .. tostring(pos.z) .. config.delimeter
  114.                         local insert = db.executeQuery('INSERT INTO `homeTowns`(`player_id`,`homes`) VALUES('..getPlayerGUID(cid)..', "'.. tostring(param) ..'")')
  115.                         if(not insert) then
  116.                                 error("Failed to insert new home to database :".. getPlayerName(cid) .. ", " .. tostring(param))
  117.                                 return true
  118.                         end
  119.                         doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_ORANGE, tostring(homename) .. " has been added to your home locations.")
  120.  
  121.                 end
  122.  
  123.         elseif(words == 'deletehome') then
  124.  
  125.                 if param == '' then
  126.                         doPlayerSendCancel(cid, "Invalid home name! Usage: /deletehome <name>")
  127.                         return true
  128.                 end
  129.  
  130.                 local homeString = string.explode(tostring(homes:getDataString("homes")), config.delimeter)
  131.                 local finalString = ""
  132.                 for i = 1, table.maxn(homeString), 1 do
  133.                         local homeX = string.explode(tostring(homeString[i]), "=")
  134.  
  135.                         if(homeX[1] ~= param and homeX[1] ~= nil) then
  136.                                 finalString = finalString .. tostring(homeString[i]) .. "|"
  137.                         end
  138.                 end
  139.  
  140.                 local update = db.executeQuery('UPDATE `homeTowns` SET `homes` = "' .. finalString .. '" WHERE `player_id`='..getPlayerGUID(cid))
  141.                 if(not update) then
  142.                         error("Failed to update player homes :".. getPlayerName(cid) .. ", " .. tostring(param))
  143.                 end
  144.  
  145.                 doPlayerSendTextMessage(cid, MESSAGE_STATUS_CONSOLE_ORANGE, tostring(param) .. " has been deleted from your home locations.")
  146.         end
  147.  
  148.  
  149.         return true
  150. end
  151.  
  152. function numberOfHomes(homes)
  153.         return table.getn(homes)-1
  154. end