NPCs referencing SQL database

Discussion in 'Support' started by luptonicedtea, Nov 8, 2018 at 11:43 PM.

Tags:
  1. luptonicedtea

    luptonicedtea New Member

    Joined:
    May 3, 2016
    Messages:
    16
    Likes Received:
    4
    Best Answers:
    0
    Hello!
    The server is TFS 1.3.
    I'm trying to make an NPC who references the AAC database to determine whether or not to send someone somewhere. I have added a column in znote_accounts called `adventurer`, which is a boolean. The person uses shop points to acquire the "adventurer" status, and once `adventurer` becomes 1, the player can interact with this NPC to travel to a place.

    cbrm has an awesome list of LUA functions, but I can't figure out how to obtain the player's account_id from an NPC. I've tried printing it to the console, but I feel like I've got the order of how NPCs interact with players out of whack.

    At this point, I'm not getting an error message, but I can't make the function getAdventurerStatus perform its task. I would really appreciate any help!

    Code (Lua):
    1. local keywordHandler = KeywordHandler:new()
    2. local npcHandler = NpcHandler:new(keywordHandler)
    3.  
    4. NpcSystem.parseParameters(npcHandler)
    5.  
    6. function onCreatureAppear(cid)            npcHandler:onCreatureAppear(cid)            end
    7. function onCreatureDisappear(cid)        npcHandler:onCreatureDisappear(cid)            end
    8. function onCreatureSay(cid, type, msg)        npcHandler:onCreatureSay(cid, type, msg)        end
    9. function onThink()        npcHandler:onThink()        end
    10.  
    11. local voices = { {text = 'I miss my grand adventures.'} }
    12. npcHandler:addModule(VoiceModule:new(voices))
    13.  
    14. --Get znote_accounts adventurer status
    15. local function getAdventurerStatus(cid)
    16.     local resultId = db.storeQuery("SELECT `adventurer` FROM `znote_accounts` WHERE `id` = " .. getAccountNumberByPlayerName(name))
    17.     print(getAccountNumberByPlayerName(name))
    18.     if resultId ~= 1 then
    19.         doRelocate(getPlayerPosition(cid), 32369, 32232, 7)
    20.     else
    21.         npcHandler:say('You\'re not prepared to go on an adventure!', cid)
    22.     end  
    23. end
    24.  
    25. --Basic
    26. keywordHandler:addKeyword({'hammer'}, StdModule.say, {npcHandler = npcHandler, text = 'Good luck on your adventures! <grumbles> I thought I left my hammer somewhere around here..'})
    27. keywordHandler:addKeyword({'knee'}, StdModule.say, {npcHandler = npcHandler, text = 'No, I\'m still an adventurer.'})
    28. keywordHandler:addKeyword({'old'}, StdModule.say, {npcHandler = npcHandler, text = 'I\'m still an adventurer!'})
    29.  
    30. npcHandler:setMessage(MESSAGE_GREET, 'What do you want from me,|PLAYERNAME|?')
    31. npcHandler:setMessage(MESSAGE_FAREWELL, 'If you dont go looking for adventure, sometimes it finds you!')
    32. npcHandler:setMessage(MESSAGE_WALKAWAY, 'Adventure awaits!')
    33.  
    34. npcHandler:addModule(FocusModule:new())
     
  2. ScorpionOT

    ScorpionOT uber n00b

    Joined:
    Mar 28, 2015
    Messages:
    226
    Likes Received:
    41
    Best Answers:
    6
    Before you run a query with a value acquired from a player's property, you must first sanitize it. In this case it really isn't that relevant, because upon being attributed a nil value for the SQL Query @ db.storeQuery(...), it will simply print an error output to the console. This should happen whenever the "name" (wherein your error lies, because in spite of me not seeing where the getAdventurerStatus function is being referenced, I see it has only one parameter cid which doesn't assist in the definition of "name" [getAccountNumberByPlayerName(name) i.e, this will be nil, as I just explained].

    In order to fix:
    Code (Lua):
    1.  
    2. local name = getPlayerName(cid)
    3. if(name == "" and name == nil and ... (extra sanitization and verification here) ...) then
    4.        return false
    5. end
    6.  
    Add before your definition of resultId.

    Edit: also, please note making sql queries within code is really impractical. Don't do this, prefer to add retrieval from C++ at instatiation.
     
  3. luptonicedtea

    luptonicedtea New Member

    Joined:
    May 3, 2016
    Messages:
    16
    Likes Received:
    4
    Best Answers:
    0
    Great! Thanks for your reply. I know I have a lot to learn about sanitization and the creation of new information at instances in C++. I integrated your suggestion about sanitization, but am still getting no response (in the console or in the NPC chat) that suggests that he is calling on the function getAdventurerStatus.

    You mentioned in your response: "... because in spite of me not seeing where the getAdventurerStatus function is being referenced...", and now I'm wondering if that's my issue. Is it in the right place to get used as a function when someone speaks to the NPC? I see that travel-NPCs have the local function addTravelKeyword in npc scripts which rely on specific travelkeywords to active that function. Do I have to have a keyword to activate the function getAdventurerStatus?

    Thanks!
     
  4. ScorpionOT

    ScorpionOT uber n00b

    Joined:
    Mar 28, 2015
    Messages:
    226
    Likes Received:
    41
    Best Answers:
    6

Share This Page

Loading...