api_helpers.lua 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. function lottachievements.tbv(tb,value,default)
  2. if not default then
  3. default = {}
  4. end
  5. if not tb or type(tb) ~= "table" then
  6. if not value then
  7. value = "[NULL]"
  8. end
  9. minetest.log("error", "lottachievements.tbv - table "..dump(value).." is null, or not a table! Dump: "..dump(tb))
  10. return
  11. end
  12. if not value then
  13. error("[ERROR] lottachievements.tbv was not used correctly!\n"..
  14. "Value: '"..dump(value).."'\n"..
  15. "Dump:"..dump(tb))
  16. return
  17. end
  18. if not tb[value] then
  19. tb[value] = default
  20. end
  21. end
  22. function lottachievements.assertPlayer(playern)
  23. lottachievements.tbv(lottachievements.players, playern)
  24. lottachievements.tbv(lottachievements.players[playern], "name", playern)
  25. lottachievements.tbv(lottachievements.players[playern], "unlocked")
  26. lottachievements.tbv(lottachievements.players[playern], "place")
  27. lottachievements.tbv(lottachievements.players[playern], "count")
  28. lottachievements.tbv(lottachievements.players[playern], "craft")
  29. lottachievements.tbv(lottachievements.players[playern], "eat")
  30. lottachievements.tbv(lottachievements.players[playern], "deaths", 0)
  31. lottachievements.tbv(lottachievements.players[playern], "joins", 0)
  32. lottachievements.tbv(lottachievements.players[playern], "chats", 0)
  33. end
  34. function lottachievements.player(name)
  35. return lottachievements.players[name]
  36. end
  37. function lottachievements._order_lottachievements(name)
  38. local done = {}
  39. local retval = {}
  40. local retval2 = {}
  41. local player = lottachievements.player(name)
  42. local c = 0
  43. if player and player.unlocked then
  44. for _,got in pairs(player.unlocked) do
  45. if lottachievements.def[got] then
  46. done[got] = true
  47. table.insert(retval,{name=got,got=true})
  48. end
  49. end
  50. end
  51. table.sort(retval, function(a,b)
  52. return (lottachievements.def[b.name].id > lottachievements.def[a.name].id)
  53. end)
  54. for _,def in pairs(lottachievements.def) do
  55. if not done[def.name] then
  56. table.insert(retval2,{name=def.name,got=false})
  57. end
  58. end
  59. table.sort(retval2, function(a,b)
  60. return (lottachievements.def[b.name].id > lottachievements.def[a.name].id)
  61. end)
  62. for i,v in pairs(retval2) do
  63. table.insert(retval,v)
  64. end
  65. return retval
  66. end
  67. function lottachievements.completed_achievements(name)
  68. local completed = {}
  69. local player = lottachievements.player(name)
  70. if player and player.unlocked then
  71. for _, got in pairs(player.unlocked) do
  72. if lottachievements.def[got] then
  73. completed[got] = true
  74. end
  75. end
  76. end
  77. return completed
  78. end