This commit is contained in:
RealBadAngel 2013-03-11 21:24:41 +01:00
parent 773382cf2c
commit d01564a19d
7 changed files with 106 additions and 58 deletions

View File

@ -1 +0,0 @@

Binary file not shown.

View File

@ -1,11 +1,11 @@
-- default GUI page -- default GUI page
stargate.default_page = "main" stargate.default_page = "main"
stargate.players={} stargate_network["players"]={}
stargate.current_page={} stargate.current_page={}
stargate.save_data = function() stargate.save_data = function(table_pointer)
local data = minetest.serialize( stargate_network ) local data = minetest.serialize( stargate_network[table_pointer] )
local path = minetest.get_worldpath().."/mod_stargate.data" local path = minetest.get_worldpath().."/stargate_"..table_pointer..".data"
local file = io.open( path, "w" ) local file = io.open( path, "w" )
if( file ) then if( file ) then
file:write( data ) file:write( data )
@ -15,12 +15,12 @@ stargate.save_data = function()
end end
end end
stargate.restore_data = function() stargate.restore_data = function(table_pointer)
local path = minetest.get_worldpath().."/mod_stargate.data" local path = minetest.get_worldpath().."/stargate_"..table_pointer..".data"
local file = io.open( path, "r" ) local file = io.open( path, "r" )
if( file ) then if( file ) then
local data = file:read("*all") local data = file:read("*all")
stargate_network = minetest.deserialize( data ) stargate_network[table_pointer] = minetest.deserialize( data )
file:close() file:close()
return true return true
else return nil else return nil
@ -28,26 +28,42 @@ stargate.restore_data = function()
end end
-- load Stargates network data -- load Stargates network data
if stargate.restore_data()==nil then if stargate.restore_data("registered_players") ~= nil then
print ("[stargate] network data not found. Creating new file.") print(dump(stargate_network["registered_players"]))
if stargate.save_data()==nil then for __,tab in ipairs(stargate_network["registered_players"]) do
print ("[stargate] Cannot load nor create new file!") if stargate.restore_data(tab["player_name"]) == nil then
--crash or something here? print ("[stargate] Error loading data!")
else end
print ("[stargate] New data file created.")
end end
else
print ("[stargate] Error loading data! Creating new file.")
stargate_network["registered_players"]={}
stargate.save_data("registered_players")
print(dump(stargate_network["registered_players"]))
end end
-- register_on_joinplayer -- register_on_joinplayer
minetest.register_on_joinplayer(function(player) minetest.register_on_joinplayer(function(player)
local player_name = player:get_player_name() local player_name = player:get_player_name()
stargate.players[player_name]={} local registered=nil
stargate.players[player_name]["formspec"]="" for __,tab in ipairs(stargate_network["registered_players"]) do
stargate.players[player_name]["current_page"]=stargate.default_page if tab["player_name"] == player_name then registered = true break end
stargate.players[player_name]["own_gates"]={} end
stargate.players[player_name]["own_gates_count"]=0 if registered == nil then
stargate.players[player_name]["public_gates"]={} local new={}
stargate.players[player_name]["public_gates_count"]=0 new["player_name"]=player_name
table.insert(stargate_network["registered_players"],new)
stargate.save_data("registered_players")
stargate.save_data(player_name)
end
stargate_network["players"][player_name]={}
stargate_network["players"][player_name]["formspec"]=""
stargate_network["players"][player_name]["current_page"]=stargate.default_page
stargate_network["players"][player_name]["own_gates"]={}
stargate_network["players"][player_name]["own_gates_count"]=0
stargate_network["players"][player_name]["public_gates"]={}
stargate_network["players"][player_name]["public_gates_count"]=0
stargate_network["players"][player_name]["current_index"]=0
end) end)
stargate.registerGate = function(player_name,pos) stargate.registerGate = function(player_name,pos)
@ -59,7 +75,7 @@ stargate.registerGate = function(player_name,pos)
new_gate["type"]="private" new_gate["type"]="private"
new_gate["description"]="" new_gate["description"]=""
table.insert(stargate_network[player_name],new_gate) table.insert(stargate_network[player_name],new_gate)
if stargate.save_data()==nil then if stargate.save_data(player_name)==nil then
print ("[stargate] Couldnt update network file!") print ("[stargate] Couldnt update network file!")
end end
end end
@ -71,7 +87,7 @@ stargate.unregisterGate = function(player_name,pos)
break break
end end
end end
if stargate.save_data()==nil then if stargate.save_data(player_name)==nil then
print ("[stargate] Couldnt update network file!") print ("[stargate] Couldnt update network file!")
end end
end end
@ -83,35 +99,36 @@ stargate.gateFormspecHandler = function(pos, node, clicker, itemstack)
local owner=meta:get_string("owner") local owner=meta:get_string("owner")
if player_name~=owner then return end if player_name~=owner then return end
local current_gate=nil local current_gate=nil
stargate.players[player_name]["own_gates"]={} stargate_network["players"][player_name]["own_gates"]={}
stargate.players[player_name]["public_gates"]={} stargate_network["players"][player_name]["public_gates"]={}
local own_gates_count=0 local own_gates_count=0
for __,gates in ipairs(stargate_network[player_name]) do for __,gates in ipairs(stargate_network[player_name]) do
if gates["pos"].x==pos.x and gates["pos"].y==pos.y and gates["pos"].z==pos.z then if gates["pos"].x==pos.x and gates["pos"].y==pos.y and gates["pos"].z==pos.z then
current_gate=gates current_gate=gates
else else
own_gates_count=own_gates_count+1 own_gates_count=own_gates_count+1
table.insert(stargate.players[player_name]["own_gates"],gates) table.insert(stargate_network["players"][player_name]["own_gates"],gates)
end end
end end
stargate.players[player_name]["own_gates_count"]=own_gates_count stargate_network["players"][player_name]["own_gates_count"]=own_gates_count
if current_gate==nil then if current_gate==nil then
print ("Gate not registered in network! Please remove it and place once again.") print ("Gate not registered in network! Please remove it and place once again.")
return nil return nil
end end
stargate.players[player_name]["current_index"]=0 stargate_network["players"][player_name]["current_index"]=0
stargate.players[player_name]["current_gate"]=current_gate stargate_network["players"][player_name]["current_gate"]=current_gate
stargate.players[player_name]["dest_type"]="own" stargate_network["players"][player_name]["dest_type"]="own"
local formspec=stargate.get_formspec(player_name,"main") local formspec=stargate.get_formspec(player_name,"main")
stargate.players[player_name]["formspec"]=formspec stargate_network["players"][player_name]["formspec"]=formspec
minetest.show_formspec(player_name, "stargate:main", formspec) print(dump(formspec))
if formspec ~=nil then minetest.show_formspec(player_name, "stargate_main", formspec) end
end end
-- get_formspec -- get_formspec
stargate.get_formspec = function(player_name,page) stargate.get_formspec = function(player_name,page)
if player_name==nil then return "" end if player_name==nil then return nil end
stargate.players[player_name]["current_page"]=page stargate_network["players"][player_name]["current_page"]=page
local current_gate=stargate.players[player_name]["current_gate"] local current_gate=stargate_network["players"][player_name]["current_gate"]
local formspec = "size[14,10]" local formspec = "size[14,10]"
--background --background
formspec = formspec .."background[-0.19,-0.2,;14.38,10.55;ui_form_bg.png]" formspec = formspec .."background[-0.19,-0.2,;14.38,10.55;ui_form_bg.png]"
@ -123,7 +140,7 @@ stargate.get_formspec = function(player_name,page)
formspec = formspec.."label[0,1.1;Destination: ]" formspec = formspec.."label[0,1.1;Destination: ]"
formspec = formspec.."label[0,1.7;Aviable destinations:]" formspec = formspec.."label[0,1.7;Aviable destinations:]"
formspec = formspec.."image_button[3.5,1.8;.6,.6;toggle_icon.png;toggle_dest_type;]" formspec = formspec.."image_button[3.5,1.8;.6,.6;toggle_icon.png;toggle_dest_type;]"
formspec = formspec.."label[4,1.7;Type: "..stargate.players[player_name]["dest_type"].."]" formspec = formspec.."label[4,1.7;Filter: "..stargate_network["players"][player_name]["dest_type"].."]"
if page=="main" then if page=="main" then
formspec = formspec.."image_button[6.5,.6;.6,.6;pencil_icon.png;edit_desc;]" formspec = formspec.."image_button[6.5,.6;.6,.6;pencil_icon.png;edit_desc;]"
@ -134,54 +151,58 @@ stargate.get_formspec = function(player_name,page)
formspec = formspec.."field[7.3,.7;5,1;desc_box;Edit gate description:;"..current_gate["description"].."]" formspec = formspec.."field[7.3,.7;5,1;desc_box;Edit gate description:;"..current_gate["description"].."]"
end end
local list_index=stargate.players[player_name]["current_index"] local list_index=stargate_network["players"][player_name]["current_index"]
print(dump(stargate.players[player_name]["own_gates_count"]))
local page=math.floor(list_index / (30) + 1) local page=math.floor(list_index / (30) + 1)
local pagemax = math.floor((stargate.players[player_name]["own_gates_count"]+1) / (30) + 1) local pagemax = math.floor((stargate_network["players"][player_name]["own_gates_count"]+1) / (30) + 1)
for y=0,9,1 do for y=0,7,1 do
for x=0,2,1 do for x=0,2,1 do
print(dump(list_index)) local gate_temp=stargate_network["players"][player_name]["own_gates"][list_index+1]
print(dump(stargate.players[player_name]["own_gates"][list_index+1]))
local gate_temp=stargate.players[player_name]["own_gates"][list_index+1]
if gate_temp then if gate_temp then
formspec = formspec.."image_button["..(x*5)..","..(4+y*.8)..";.6,.6;dot_icon.png;list_button"..list_index..";]" formspec = formspec.."image_button["..(x*4.5)..","..(2.5+y*.9)..";.6,.6;stargate_icon.png;list_button"..list_index..";]"
formspec = formspec.."label["..(x*5+.8)..","..(4+y*.8)..";("..gate_temp["pos"].x..","..gate_temp["pos"].y..","..gate_temp["pos"].z..")]" formspec = formspec.."label["..(x*4.5+.5)..","..(2.3+y*.9)..";("..gate_temp["pos"].x..","..gate_temp["pos"].y..","..gate_temp["pos"].z..") "..gate_temp["type"].."]"
formspec = formspec.."label["..(x*4.5+.5)..","..(2.7+y*.9)..";"..gate_temp["description"].."]"
end end
list_index=list_index+1 list_index=list_index+1
end end
end end
formspec = formspec.."image_button[6.5,1.8;.6,.6;left_icon.png;page_left;]"
formspec = formspec.."image_button[6.9,1.8;.6,.6;right_icon.png;page_right;]"
formspec=formspec.."label[7.5,1.7;Page: "..page.." of "..pagemax.."]"
return formspec return formspec
end end
-- register_on_player_receive_fields -- register_on_player_receive_fields
minetest.register_on_player_receive_fields(function(player, formname, fields) minetest.register_on_player_receive_fields(function(player, formname, fields)
print(dump(formname))
--if formname ~= "stargate:main" then return end
local player_name = player:get_player_name() local player_name = player:get_player_name()
local current_gate=stargate.players[player_name]["current_gate"] local current_gate=stargate_network["players"][player_name]["current_gate"]
local formspec local formspec
if fields.toggle_type then if fields.toggle_type then
if current_gate["type"] == "private" then if current_gate["type"] == "private" then
current_gate["type"]="public" current_gate["type"] = "public"
else current_gate["type"]="private" end else current_gate["type"] = "private" end
formspec= stargate.get_formspec(player_name,"main") formspec= stargate.get_formspec(player_name,"main")
stargate.players[player_name]["formspec"]=formspec stargate_network["players"][player_name]["formspec"] = formspec
minetest.show_formspec(player_name, "stargate:main", formspec) minetest.show_formspec(player_name, "stargate:main", formspec)
minetest.sound_play("click", {to_player=player_name, gain = 0.5}) minetest.sound_play("click", {to_player=player_name, gain = 0.5})
return return
end end
if fields.toggle_dest_type then if fields.toggle_dest_type then
if stargate.players[player_name]["dest_type"] == "all own" then if stargate_network["players"][player_name]["dest_type"] == "all own" then
stargate.players[player_name]["dest_type"]="all public" stargate_network["players"][player_name]["dest_type"] = "all public"
else stargate.players[player_name]["dest_type"]="all own" end else stargate_network["players"][player_name]["dest_type"] = "all own" end
formspec= stargate.get_formspec(player_name,"main") stargate_network["players"][player_name]["current_index"] = 0
stargate.players[player_name]["formspec"]=formspec formspec = stargate.get_formspec(player_name,"main")
stargate_network["players"][player_name]["formspec"] = formspec
minetest.show_formspec(player_name, "stargate:main", formspec) minetest.show_formspec(player_name, "stargate:main", formspec)
minetest.sound_play("click", {to_player=player_name, gain = 0.5}) minetest.sound_play("click", {to_player=player_name, gain = 0.5})
return return
end end
if fields.edit_desc then if fields.edit_desc then
formspec= stargate.get_formspec(player_name,"edit_desc") formspec= stargate.get_formspec(player_name,"edit_desc")
stargate.players[player_name]["formspec"]=formspec stargate_network["players"][player_name]["formspec"]=formspec
minetest.show_formspec(player_name, "stargate:main", formspec) minetest.show_formspec(player_name, "stargate:main", formspec)
minetest.sound_play("click", {to_player=player_name, gain = 0.5}) minetest.sound_play("click", {to_player=player_name, gain = 0.5})
return return
@ -190,9 +211,37 @@ minetest.register_on_player_receive_fields(function(player, formname, fields)
if fields.save_desc then if fields.save_desc then
current_gate["description"]=fields.desc_box current_gate["description"]=fields.desc_box
formspec= stargate.get_formspec(player_name,"main") formspec= stargate.get_formspec(player_name,"main")
stargate.players[player_name]["formspec"]=formspec stargate_network["players"][player_name]["formspec"]=formspec
minetest.show_formspec(player_name, "stargate:main", formspec) minetest.show_formspec(player_name, "stargate:main", formspec)
minetest.sound_play("click", {to_player=player_name, gain = 0.5}) minetest.sound_play("click", {to_player=player_name, gain = 0.5})
return return
end end
-- page controls
local start=math.floor(stargate_network["players"][player_name]["current_index"]/30 +1 )
local start_i=start
local pagemax = math.floor((stargate_network["players"][player_name]["own_gates_count"]) / (30) + 1)
if fields.page_left then
minetest.sound_play("paperflip2", {to_player=player_name, gain = 1.0})
start_i = start_i - 1
if start_i < 1 then start_i = 1 end
if not (start_i == start) then
stargate_network["players"][player_name]["current_index"] = (start_i-1)*30+1
formspec = stargate.get_formspec(player_name,"main")
stargate_network["players"][player_name]["formspec"] = formspec
minetest.show_formspec(player_name, "stargate:main", formspec)
end
end
if fields.page_right then
minetest.sound_play("paperflip2", {to_player=player_name, gain = 1.0})
start_i = start_i + 1
if start_i > pagemax then start_i = pagemax end
if not (start_i == start) then
stargate_network["players"][player_name]["current_index"] = (start_i-1)*30+1
formspec = stargate.get_formspec(player_name,"main")
stargate_network["players"][player_name]["formspec"] = formspec
minetest.show_formspec(player_name, "stargate:main", formspec)
end
end
end) end)

Binary file not shown.

Before

Width:  |  Height:  |  Size: 731 B

After

Width:  |  Height:  |  Size: 661 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.9 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.7 KiB