meseportal_network.lua 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. local function table_empty(tab)
  2. for key in pairs(tab) do return false end
  3. return true
  4. end
  5. meseportals.save_data = function(table_pointer)
  6. local data = minetest.serialize( meseportals_network[table_pointer] )
  7. local path = minetest.get_worldpath().."/meseportals_"..table_pointer..".data"
  8. local file = io.open( path, "w" )
  9. if( file ) then
  10. file:write( data )
  11. file:close()
  12. return true
  13. else return nil
  14. end
  15. end
  16. meseportals.restore_data = function(table_pointer)
  17. local path = minetest.get_worldpath().."/meseportals_"..table_pointer..".data"
  18. local file = io.open( path, "r" )
  19. if( file ) then
  20. local data = file:read("*all")
  21. meseportals_network[table_pointer] = minetest.deserialize( data )
  22. file:close()
  23. if table_empty(meseportals_network[table_pointer]) then os.remove(path) end
  24. return true
  25. else return nil
  26. end
  27. end
  28. meseportals.load_players = function()
  29. local path = minetest.get_worldpath().."/meseportals.players"
  30. local file = io.open( path, "r" )
  31. if( file ) then
  32. local data = file:read("*all")
  33. meseportals["registered_players"] = minetest.deserialize( data )
  34. file:close()
  35. if table_empty(meseportals["registered_players"]) then os.remove(path) end
  36. return true
  37. else return nil
  38. end
  39. end
  40. meseportals.save_players = function()
  41. if table_empty(meseportals["registered_players"]) then return end
  42. local data = minetest.serialize( meseportals["registered_players"] )
  43. local path = minetest.get_worldpath().."/meseportals.players"
  44. local file = io.open( path, "w" )
  45. if( file ) then
  46. file:write( data )
  47. file:close()
  48. return true
  49. else return nil
  50. end
  51. end
  52. -- load meseportalss network data
  53. if meseportals.load_players() ~= nil then
  54. for __,tab in ipairs(meseportals["registered_players"]) do
  55. if meseportals.restore_data(tab["player_name"]) == nil then
  56. --print ("[meseportals] Error loading data!")
  57. meseportals_network[tab["player_name"]] = {}
  58. end
  59. end
  60. else
  61. print ("[meseportals] Error loading data! Creating new file.")
  62. meseportals["registered_players"]={}
  63. meseportals.save_players()
  64. end
  65. -- register_on_joinplayer
  66. minetest.register_on_joinplayer(function(player)
  67. local player_name = player:get_player_name()
  68. local registered=nil
  69. for __,tab in ipairs(meseportals["registered_players"]) do
  70. if tab["player_name"] == player_name then registered = true break end
  71. end
  72. if registered == nil then
  73. local new={}
  74. new["player_name"]=player_name
  75. table.insert(meseportals["registered_players"],new)
  76. meseportals_network[player_name]={}
  77. meseportals.save_players()
  78. meseportals.save_data(player_name)
  79. end
  80. meseportals_gui["players"][player_name]={
  81. formspec = "",
  82. current_page = meseportals.default_page,
  83. own_portals ={},
  84. own_portals_count =0,
  85. public_portals ={},
  86. public_portals_count =0,
  87. current_index =0,
  88. temp_portal ={},
  89. }
  90. end)
  91. meseportals.unregisterPortal = function(pos)
  92. for _,tab in pairs(meseportals.registered_players) do
  93. local player_name=tab.player_name
  94. for __,portal in ipairs(meseportals_network[player_name]) do
  95. if portal["pos"].x==pos.x and portal["pos"].y==pos.y and portal["pos"].z==pos.z then
  96. table.remove(meseportals_network[player_name], __)
  97. if meseportals.save_data(player_name)==nil then
  98. print ("[meseportals] Couldnt update network file!")
  99. end
  100. end
  101. end
  102. end
  103. end
  104. meseportals.registerPortal = function(player_name,pos,dir)
  105. if meseportals.findPortal(pos) then
  106. --An annoying glitch
  107. meseportals.unregisterPortal(pos)
  108. end
  109. if meseportals_network[player_name]==nil then
  110. meseportals_network[player_name]={}
  111. end
  112. local new_portal ={}
  113. new_portal["pos"]=pos
  114. new_portal["type"]="public"
  115. new_portal["description"]="Portal at (" ..new_portal["pos"].x .."," ..new_portal["pos"].y .."," ..new_portal["pos"].z ..")"
  116. new_portal["dir"]=dir
  117. new_portal["owner"]=player_name
  118. table.insert(meseportals_network[player_name],new_portal)
  119. if meseportals.save_data(player_name)==nil then
  120. print ("[meseportals] Couldnt update network file!")
  121. end
  122. end
  123. meseportals.findPortal = function(pos)
  124. if pos ~= nil then
  125. for _,tab in pairs(meseportals.registered_players) do
  126. local player_name=tab.player_name
  127. for _,portals in pairs(meseportals_network[player_name]) do
  128. if portals
  129. and vector.equals(portals.pos, pos) then
  130. return portals
  131. end
  132. end
  133. end
  134. end
  135. return nil
  136. end