Advertisement
Guest User

Untitled

a guest
Apr 20th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1. local nk = require("nakama")
  2.  
  3.  
  4.  
  5. local function UpdateMetadata(context, payload)
  6.  
  7. local user_id = payload.user_id
  8. local user_ids = {
  9. user_id
  10. }
  11. local users = nk.users_fetch_id(user_ids)
  12. local myMetaData = users[1].Metadata
  13. local user_updates = {}
  14.  
  15. function tableHasKey(table,key)
  16. return table[key] ~= nil
  17. end
  18.  
  19. function replace( oldjson , newjson )
  20.  
  21. for key, val in pairs(newjson) do
  22. if (tableHasKey(oldjson,key) ) then
  23. if(type(val) == "table") then
  24. oldjson[key] = replace( oldjson[key] , val )
  25. else
  26. oldjson[key] = val
  27. end
  28. elseif(not tableHasKey(oldjson,key) ) then
  29. oldjson[key] = val
  30. end
  31. end
  32.  
  33. return oldjson
  34. end
  35.  
  36.  
  37.  
  38. user_updates = {
  39. { UserId = user_id, Metadata = replace(myMetaData,payload.json) }
  40. }
  41.  
  42.  
  43. local status, err = pcall(nk.users_update, user_updates)
  44. if (not status) then
  45. print(("User update error: %q"):format(err))
  46. end
  47.  
  48.  
  49. return replace(myMetaData,payload.json)
  50.  
  51. end
  52. nk.register_http(UpdateMetadata, "Metadata")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement