GlobalEvent Automatic Znote AAC Shop [TFS 1.0]

Discussion in 'GlobalEvents, Spells & CreatureEvents' started by Printer, Nov 18, 2014.

  1. Printer

    Printer if Printer then print("LUA") end Premium User

    Joined:
    Dec 27, 2009
    Messages:
    5,660
    Likes Received:
    1,950
    Best Answers:
    16
    Add this into globalevents/globalevents.xml
    Code (XML):
    1. <globalevent name="Znote Shop" interval="30000" script="znoteShop.lua"/>
    And go into globalevents/scripts and create new lua and name it 'znoteShop'
    Code (Lua):
    1. function onThink(interval, lastExecution)
    2.     local players = Game.getPlayers()
    3.     if #players == 0 then -- 0 players online, no need to execute the script
    4.         return true
    5.     end
    6.  
    7.     local player
    8.     for i = 1, #players do
    9.         player = players[i]
    10.         local orderQuery = db.storeQuery('SELECT `id`, `type`, `itemid`, `count` FROM `znote_shop_orders` WHERE `account_id` = '.. player:getAccountId() ..' LIMIT 1;')
    11.         if orderQuery then --Lets check if the players online have orderd something from the shop
    12.             local orderId = result.getDataInt(orderQuery, 'id')
    13.             local orderType = result.getDataInt(orderQuery, 'type')
    14.             local orderItemId = result.getDataInt(orderQuery, 'itemid')
    15.             local orderCount = result.getDataInt(orderQuery, 'count')
    16.             result.free(orderQuery)
    17.  
    18.             if orderType == 1 then
    19.                 if player:addItemEx(Game.createItem(orderItemId, orderCount or 1)) ~= RETURNVALUE_NOERROR or
    20.                     player:getFreeCapacity() < ItemType(orderItemId):getWeight(orderCount or 1) then --Lets check if player have slot or cape left. Else send to player inbox
    21.  
    22.                     local parcel = player:getInbox():addItem(2596, 1, false, 1)
    23.                     if not parcel then --If not being able to create parcel we stop the script and retry again.
    24.                         print('[ERROR Znote Shop Globalevents] = Error on creating a parcel.')
    25.                         return true
    26.                     end
    27.  
    28.                     local letter = parcel:addItem(2598, 1, false, 1)
    29.                     letter:setAttribute(ITEM_ATTRIBUTE_TEXT, 'You have received your shop item. Thanks for donating.')
    30.                     parcel:addItem(orderItemId, orderCount or 1, false, 1)
    31.                 end
    32.  
    33.                 db.query('DELETE FROM `znote_shop_orders` WHERE `id` = '.. orderId ..';')
    34.                 player:sendTextMessage(MESSAGE_INFO_DESCR, 'Congratulations! You have received '.. orderCount ..'x '.. ItemType(orderItemId):getName() ..'!')
    35.             end
    36.         end
    37.     end
    38.     return true
    39. end
    Enjoy
     
    Last edited: Nov 18, 2014
    popitox, nemec, tigerx2 and 4 others like this.
  2. Codinablack

    Codinablack Dreamer

    Joined:
    Dec 26, 2013
    Messages:
    1,217
    Likes Received:
    378
    Best Answers:
    4
    Awesome man! Thanks alot for this release! Good work
     
  3. Colors

    Colors

    Joined:
    Mar 22, 2013
    Messages:
    929
    Likes Received:
    258
    Best Answers:
    3
    I had a automatic script like this some time ago, and alot of people complained because this AAC don't have a function to buy items with certain character... So it might be annoying for some people... Specially with custom orders. Anyway, thanks for the awesome job <3
     
  4. Printer

    Printer if Printer then print("LUA") end Premium User

    Joined:
    Dec 27, 2009
    Messages:
    5,660
    Likes Received:
    1,950
    Best Answers:
    16
    Well Znote has no option to select a player :p Until it has, i would happily edit the script.
     
  5. Oldschool'er

    Oldschool'er Tibia since 1998'

    Joined:
    Dec 14, 2010
    Messages:
    2,205
    Likes Received:
    142
    Best Answers:
    0
    why doesnt this work for me....
     
  6. Printer

    Printer if Printer then print("LUA") end Premium User

    Joined:
    Dec 27, 2009
    Messages:
    5,660
    Likes Received:
    1,950
    Best Answers:
    16
    Let me guess, your not using TFS 1.x
     
  7. Fanges

    Fanges New Member

    Joined:
    Jul 5, 2014
    Messages:
    27
    Likes Received:
    4
    Best Answers:
    0
    I've updated to the new TFS, but now i get a error:

    Code (Text):
    1.  
    2. Lua Script Error: [GlobalEvent Interface]
    3. data/globalevents/scripts/shop.lua:onThink
    4. data/globalevents/scripts/shop.lua:12: attempt to call field 'getDataInt' (a nil value)
    5. stack traceback:
    6.     [C]: in function 'getDataInt'
    7.     data/globalevents/scripts/shop.lua:12: in function <data/globalevents/scripts/shop.lua:1>
    8. [Error - GlobalEvents::think] Failed to execute event: Shop
    9.  
    10.  
     
  8. Codinablack

    Codinablack Dreamer

    Joined:
    Dec 26, 2013
    Messages:
    1,217
    Likes Received:
    378
    Best Answers:
    4
    You are missing that function from your compat.lua file, open it up and put in getDataInt = result.number, or something along those lines, check the master branch on github
     
  9. Gesior.pl

    Gesior.pl Mega Noob&LOL 2012

    Joined:
    Sep 18, 2007
    Messages:
    1,885
    Likes Received:
    750
    Best Answers:
    5
    This script uses low CPU for few players online and much CPU (one MySQL query for every player! these queries are simple, but they are synchronized and 200 queries may freez OTS for 0.1 sec) for many online, but number of not realized orders does not affect it.
    In gesior I use version where it executes 1 query and then search for player online for every order, so many not realized order may slow down script, but searching for player online by name is faast.

    Best version (for Znote - when there is no player name in database) would use new TFS 1.1 (maybe 1.0 too) asyncQueries, one 'think event' send queries and let server go on (parse players moves/spells etc.) and another 'think event' wait for mysql response and add items/search player.

    I'm working right now on new version of shop.lua script for TFS 1.1 Gesior2012.
    It will work without any compat.lua, 100% TFS 1.1 code. Delivers items and containers, add descripion with name of buyer to items that works with custom description (not stackable, not rune, not fluid):
    Code (Text):
    1.  
    2. -- ### CONFIG ###
    3. -- message send to player by script "type" (types you can check in "global.lua")
    4. SHOP_MSG_TYPE = 36
    5. -- ### END OF CONFIG ###
    6. function onThink(interval)
    7.     local deliveryInterval = interval / 1000
    8.     local resultId = db.storeQuery("SELECT * FROM z_ots_comunication")
    9.     if(resultId) then
    10.         repeat
    11.             local id = result.getNumber(resultId, "id")
    12.             local player = Player(result.getString(resultId, "name"))
    13.             if(player) then
    14.                 local addItemType = result.getString(resultId, "param5")
    15.  
    16.                 if(addItemType == 'item' or addItemType == 'container') then
    17.                     local itemId = result.getNumber(resultId, "param1")
    18.                     local itemCount = result.getNumber(resultId, "param2")
    19.                     local containerId = result.getNumber(resultId, "param3")
    20.                     local containerCount = result.getNumber(resultId, "param4")
    21.                     local addItemName = result.getString(resultId, "param6")
    22.  
    23.                     local itemReceivedStatus = 0
    24.                     local itemItemType = ItemType(itemId)
    25.                     local containerItemType = ItemType(containerId)
    26.                     local itemsWeight = 0
    27.                     if(itemItemType:isRune() or itemItemType:isFluidContainer() or itemItemType:getCharges() > 0) then
    28.                         itemsWeight = itemItemType:getWeight(1)
    29.                     else
    30.                         itemsWeight = itemItemType:getWeight(itemCount)
    31.                     end
    32.                     if(addItemType == 'container') then
    33.                         itemsWeight = itemsWeight * containerCount + containerItemType:getWeight(1)
    34.                     end
    35.                     local freeCapacity = player:getFreeCapacity()
    36.                     if(itemsWeight <= freeCapacity) then
    37.                         local itemForPlayer = 0
    38.                         if(addItemType == 'container') then
    39.                             itemForPlayer = Container(doCreateItemEx(containerId, 1))
    40.                             itemForPlayer:setAttribute("description", containerItemType:getDescription() .. "\nBought by " .. player:getName() .. ".")
    41.                             local innerContainerItem = 0
    42.                             for i = 1, containerCount do
    43.                                 innerContainerItem = Item(doCreateItemEx(itemId, itemCount))
    44.                                 if(not itemItemType:isRune() and not itemItemType:isFluidContainer() and not itemItemType:isStackable()) then
    45.                                     innerContainerItem:setAttribute("description", itemItemType:getDescription() .. "\nBought by " .. player:getName() .. ".")
    46.                                 end
    47.                                 itemForPlayer:addItemEx(innerContainerItem)
    48.                             end
    49.                         else
    50.                             itemForPlayer = Item(doCreateItemEx(itemId, itemCount))
    51.                             if(not itemItemType:isRune() and not itemItemType:isFluidContainer() and not itemItemType:isStackable()) then
    52.                                 itemForPlayer:setAttribute("description", itemItemType:getDescription() .. "\nBought by " .. player:getName() .. ".")
    53.                             end
    54.                         end
    55.                         itemReceivedStatus = player:addItemEx(itemForPlayer, false)
    56.                         if(type(itemReceivedStatus) == "number" and itemReceivedStatus == 0) then
    57.                             player:sendTextMessage(SHOP_MSG_TYPE, 'You received >> '.. addItemName ..' << from OTS shop.')
    58.                             db.query("DELETE FROM `z_ots_comunication` WHERE `id` = " .. id .. ";")
    59.                             db.query("UPDATE `z_shop_history_item` SET `trans_state`='realized', `trans_real`=" .. os.time() .. " WHERE id = " .. id .. ";")
    60.                         else
    61.                             player:sendTextMessage(SHOP_MSG_TYPE, '>> '.. addItemName ..' << from OTS shop is waiting for you. Please make place for this item in your backpack/hands and wait about '.. deliveryInterval ..' seconds to get it.')
    62.                         end
    63.                     else
    64.                         player:sendTextMessage(SHOP_MSG_TYPE, '>> '.. addItemName ..' << from OTS shop is waiting for you. It weight is '.. (itemsWeight / 100) ..' oz., you have only '.. (freeCapacity / 100) ..' oz. free capacity. Put some items in depot and wait about '.. deliveryInterval ..' seconds to get it.')
    65.                     end
    66.                 end
    67.             end
    68.         until not result.next(resultId)
    69.         result.free(resultId)
    70.     end
    71.     return true
    72. end
    73.  
    I will release it, when I finish Gesior2012 for TFS 1.1 with shop admin.
     
    Last edited: Apr 9, 2015
    Peonso and Aerials1980 like this.
  10. Aerials1980

    Aerials1980 u shall not pass!

    Joined:
    Jul 21, 2015
    Messages:
    14
    Likes Received:
    0
    Best Answers:
    0
    Ty, its works for me a lot!!!
     
  11. atyll

    atyll Member

    Joined:
    Dec 30, 2008
    Messages:
    383
    Likes Received:
    17
    Best Answers:
    0
    local players = Game.getPlayers()

    Game returns null, I'm trying to set it up on OTHire, any ideas?
     
  12. narko

    narko vertrauenswürdig ~

    Joined:
    Oct 19, 2008
    Messages:
    1,309
    Likes Received:
    120
    Best Answers:
    2
    Maybe what you are looking for is getPlayersOnline() since you are using OTHire and it's not based in TFS 1.x
     
  13. Codex NG

    Codex NG Recurrent Flamer

    Joined:
    Jul 24, 2015
    Messages:
    3,000
    Likes Received:
    1,605
    Best Answers:
    12
    Just put this in functions.lua
    Code (Text):
    1.  
    2. Game = {}
    3. function Game.getPlayers()
    4.     return getPlayersOnlineList()
    5. end
    It might not be built for your server, but that doesn't mean you can't make it work for it either..
    You'll just need to make the adjustments on your end to get it working or just edit the script you are trying to implement to use the correct functions :)
     
  14. Verrine

    Verrine Member

    Joined:
    Mar 2, 2016
    Messages:
    123
    Likes Received:
    7
    Best Answers:
    0
    Guys its nice script but i have problem in TFS 1.2 im waiting waiting waiting and nothing happens :< command !shop works good
     
  15. popitox

    popitox Member

    Joined:
    Jul 8, 2008
    Messages:
    36
    Likes Received:
    4
    Best Answers:
    0
    it works 100% on otx 0.3 thank you so much <3
     
  16. polileusz

    polileusz New Member

    Joined:
    Feb 18, 2010
    Messages:
    11
    Likes Received:
    1
    Best Answers:
    0
    I know I'm bit late, but how did you make it work on 0.3? can you share your scripts please?
     

Share This Page

Loading...