2015-06-09 23:09:47 +02:00
2015-08-20 16:08:03 +02:00
local S = fishing_setting.func . S
2015-07-16 00:01:19 +02:00
--function save settings
2015-06-09 23:09:47 +02:00
function fishing_setting . func . save ( )
2015-07-15 15:54:05 +02:00
local input , err = io.open ( fishing_setting.file_settings , " w " )
2015-06-09 23:09:47 +02:00
if input then
input : write ( minetest.serialize ( fishing_setting.settings ) )
input : close ( )
else
2015-07-15 15:54:05 +02:00
minetest.log ( " error " , " open( " .. fishing_setting.file_settings .. " , 'w') failed: " .. err )
2015-06-09 23:09:47 +02:00
end
end
2015-06-17 02:18:12 +02:00
function fishing_setting . func . set_settings ( new_settings , settings )
2015-06-09 23:09:47 +02:00
if settings [ " message " ] ~= nil then
2015-06-17 02:18:12 +02:00
new_settings [ " message " ] = settings [ " message " ]
2015-06-09 23:09:47 +02:00
end
2015-06-28 20:06:16 +02:00
2015-06-09 23:09:47 +02:00
if settings [ " worm_is_mob " ] ~= nil then
2015-06-17 02:18:12 +02:00
new_settings [ " worm_is_mob " ] = settings [ " worm_is_mob " ]
2015-06-28 20:06:16 +02:00
end
2015-06-09 23:09:47 +02:00
if settings [ " worm_chance " ] ~= nil then
2015-06-17 02:18:12 +02:00
new_settings [ " worm_chance " ] = settings [ " worm_chance " ]
2015-06-28 20:06:16 +02:00
end
2015-06-09 23:09:47 +02:00
if settings [ " new_worm_source " ] ~= nil then
2015-06-17 02:18:12 +02:00
new_settings [ " new_worm_source " ] = settings [ " new_worm_source " ]
2015-06-28 20:06:16 +02:00
end
2015-06-09 23:09:47 +02:00
if settings [ " wear_out " ] ~= nil then
2015-06-17 02:18:12 +02:00
new_settings [ " wear_out " ] = settings [ " wear_out " ]
2015-06-09 23:09:47 +02:00
end
if settings [ " bobber_view_range " ] ~= nil then
2015-06-17 02:18:12 +02:00
new_settings [ " bobber_view_range " ] = settings [ " bobber_view_range " ]
2015-06-09 23:09:47 +02:00
end
if settings [ " simple_deco_fishing_pole " ] ~= nil then
2015-06-17 02:18:12 +02:00
new_settings [ " simple_deco_fishing_pole " ] = settings [ " simple_deco_fishing_pole " ]
2015-06-09 23:09:47 +02:00
end
2015-06-17 02:18:12 +02:00
if settings [ " fish_chance " ] ~= nil then
new_settings [ " fish_chance " ] = settings [ " fish_chance " ]
2015-06-09 23:09:47 +02:00
end
2015-07-15 15:54:05 +02:00
if settings [ " treasure_chance " ] ~= nil then
new_settings [ " treasure_chance " ] = settings [ " treasure_chance " ]
2015-06-09 23:09:47 +02:00
end
2015-06-17 02:18:12 +02:00
if settings [ " shark_chance " ] ~= nil then
new_settings [ " shark_chance " ] = settings [ " shark_chance " ]
2015-06-09 23:09:47 +02:00
end
2015-06-28 20:06:16 +02:00
2015-07-15 15:54:05 +02:00
if settings [ " treasure_enable " ] ~= nil then
new_settings [ " treasure_enable " ] = settings [ " treasure_enable " ]
2015-06-09 23:09:47 +02:00
end
2015-06-28 20:06:16 +02:00
2015-06-26 22:33:51 +02:00
if settings [ " escape_chance " ] ~= nil then
new_settings [ " escape_chance " ] = settings [ " escape_chance " ]
2015-06-28 20:06:16 +02:00
end
2015-06-17 02:18:12 +02:00
end
2015-07-16 00:01:19 +02:00
--function load settings from file
2015-06-17 02:18:12 +02:00
function fishing_setting . func . load ( )
2015-06-26 22:33:51 +02:00
local file = io.open ( fishing_setting.file_settings , " r " )
2015-06-17 02:18:12 +02:00
local settings = { }
if file then
settings = minetest.deserialize ( file : read ( " *all " ) )
file : close ( )
if settings and type ( settings ) == " table " then
fishing_setting.func . set_settings ( fishing_setting.settings , settings )
end
2015-06-09 23:09:47 +02:00
end
end
--function return wear tool value (old or new)
function fishing_setting . func . wear_value ( wear )
local used = 0
if wear == " random " then
used = ( 2000 * ( math.random ( 20 , 29 ) ) )
elseif wear == " randomtools " then
used = ( 65535 / ( 30 - ( math.random ( 15 , 29 ) ) ) )
end
return used
end
2015-06-26 22:33:51 +02:00
-- function return table where mods actived
function fishing_setting . func . ignore_mod ( list )
local listOk = { }
for i , v in ipairs ( list ) do
if minetest.get_modpath ( v [ 1 ] ) ~= nil then
table.insert ( listOk , v )
end
end
return listOk
end
2015-06-09 23:09:47 +02:00
2015-06-16 01:42:05 +02:00
--function random hungry by bait type
function fishing_setting . func . hungry_random ( )
for i , a in pairs ( fishing_setting.baits ) do
fishing_setting.baits [ i ] [ " hungry " ] = math.random ( 15 , 80 )
end
2015-06-26 22:33:51 +02:00
--change hungry after random time, min 0h30, max 6h00
minetest.after ( math.random ( 1 , 12 ) * 1800 , function ( ) fishing_setting.func . hungry_random ( ) end )
2015-06-16 01:42:05 +02:00
end
2015-06-09 23:09:47 +02:00
2015-08-20 16:08:03 +02:00
function fishing_setting . func . get_loot ( )
local c = math.random ( 1 , 67 )
for i in pairs ( fishing_setting.prizes [ " stuff " ] ) do
local min = fishing_setting.prizes [ " stuff " ] [ i ] [ 5 ]
local chance = fishing_setting.prizes [ " stuff " ] [ i ] [ 6 ]
local max = min + chance - 1
if c <= max and c >= min then
return fishing_setting.prizes [ " stuff " ] [ i ]
end
end
return " "
end
2015-07-15 15:54:05 +02:00
-- Show notification when a player catches treasure
function fishing_setting . func . notify ( f_name , treasure )
2015-08-20 16:08:03 +02:00
local title = S ( " Lucky %s, he caught the treasure, %s! " ) : format ( f_name , treasure [ 4 ] )
2015-06-16 01:42:05 +02:00
for _ , player in ipairs ( minetest.get_connected_players ( ) ) do
2015-06-26 22:33:51 +02:00
local player_name = player : get_player_name ( )
if player_name == f_name then
2015-08-20 16:08:03 +02:00
minetest.chat_send_player ( player_name , S ( " You caught the treasure, %s! " ) : format ( treasure [ 4 ] ) )
2015-06-26 22:33:51 +02:00
else
minetest.chat_send_player ( player_name , title )
end
2015-06-16 01:42:05 +02:00
end
end
2015-06-09 23:09:47 +02:00
2015-06-16 01:42:05 +02:00
2015-07-15 15:54:05 +02:00
-- Menu: fishing configuration
2015-06-26 22:33:51 +02:00
fishing_setting.func . on_show_settings = function ( player_name )
2015-06-17 02:18:12 +02:00
if not fishing_setting.tmp_setting then
fishing_setting.tmp_setting = { }
fishing_setting.func . set_settings ( fishing_setting.tmp_setting , fishing_setting.settings )
end
2015-07-15 15:54:05 +02:00
local formspec = " size[10.8,9]label[4,0; " .. S ( " Fishing configuration " ) .. " ] " ..
-- Fish chance
" label[1.6,0.5; " .. S ( " Fish chance " ) .. " ] " ..
" button[0,1;1,1;cfish;-1] " ..
" button[1,1;1,1;cfish;-10] " ..
" label[2.1,1.2; " .. tostring ( fishing_setting.tmp_setting [ " fish_chance " ] ) .. " ] " ..
" button[2.7,1;1,1;cfish;+10] " ..
" button[3.7,1;1,1;cfish;+1] " ..
-- Shark chance
" label[1.5,2; " .. S ( " Shark chance " ) .. " ] " ..
" button[0,2.5;1,1;cshark;-1] " ..
" button[1,2.5;1,1;cshark;-10] " ..
" label[2.1,2.7; " .. tostring ( fishing_setting.tmp_setting [ " shark_chance " ] ) .. " ] " ..
" button[2.7,2.5;1,1;cshark;+10] " ..
" button[3.7,2.5;1,1;cshark;+1] " ..
-- Treasure chance
" label[1.5,3.5; " .. S ( " Treasure chance " ) .. " ] " ..
" button[0,4.;1,1;ctreasure;-1] " ..
" button[1,4;1,1;ctreasure;-10] " ..
" label[2.1,4.2; " .. tostring ( fishing_setting.tmp_setting [ " treasure_chance " ] ) .. " ] " ..
" button[2.7,4;1,1;ctreasure;+10] " ..
" button[3.7,4;1,1;ctreasure;+1] " ..
-- Worm chance
" label[7.5,0.5; " .. S ( " Worm chance " ) .. " ] " ..
" button[6,1;1,1;cworm;-1] " ..
" button[7,1;1,1;cworm;-10] " ..
" label[8.1,1.2; " .. tostring ( fishing_setting.tmp_setting [ " worm_chance " ] ) .. " ] " ..
" button[8.7,1;1,1;cworm;+10] " ..
" button[9.7,1;1,1;cworm;+1] " ..
-- Escape chance
" label[7.4,2; " .. S ( " Escape chance " ) .. " ] " ..
" button[6,2.5;1,1;cescape;-1] " ..
" button[7,2.5;1,1;cescape;-10] " ..
" label[8.1,2.7; " .. tostring ( fishing_setting.tmp_setting [ " escape_chance " ] ) .. " ] " ..
" button[8.7,2.5;1,1;cescape;+10] " ..
" button[9.7,2.5;1,1;cescape;+1] " ..
-- Bobber view range
" label[7.2,3.5; " .. S ( " Bobber view range " ) .. " ] " ..
" button[7,4;1,1;bvrange;-1] " ..
" label[8.1,4.2; " .. tostring ( fishing_setting.tmp_setting [ " bobber_view_range " ] ) .. " ] " ..
" button[8.7,4;1,1;bvrange;+1] " ..
-- Messages display
" label[0,5.7; " .. S ( " Display messages in chat " ) .. " ] " ..
" button[3.7,5.5;1,1;dmessages; " .. tostring ( fishing_setting.tmp_setting [ " message " ] ) .. " ] " ..
--poledeco
" label[0,6.5; " .. S ( " Simple pole deco " ) .. " ] " ..
" button[3.7,6.3;1,1;poledeco; " .. tostring ( fishing_setting.tmp_setting [ " simple_deco_fishing_pole " ] ) .. " ] " ..
-- Wearout
" label[0,7.3; " .. S ( " Poles wearout " ) .. " ] " ..
" button[3.7,7.1;1,1;wearout; " .. tostring ( fishing_setting.tmp_setting [ " wear_out " ] ) .. " ] " ..
-- TREASURE_ENABLE
" label[6,5.7; " .. S ( " Enable treasure " ) .. " ] " ..
" button[9.7,5.5;1,1;treasureenable; " .. tostring ( fishing_setting.tmp_setting [ " treasure_enable " ] ) .. " ] " ..
-- NEW_WORM_SOURCE
" label[6,6.5; " .. S ( " New worm source (reboot) " ) .. " ] " ..
" button[9.7,6.3;1,1;newworm; " .. tostring ( fishing_setting.tmp_setting [ " new_worm_source " ] ) .. " ] " ..
-- WORM_IS_MOB
" label[6,7.3; " .. S ( " Worm is a mob (reboot) " ) .. " ] " ..
" button[9.7,7.1;1,1;wormmob; " .. tostring ( fishing_setting.tmp_setting [ " worm_is_mob " ] ) .. " ] " ..
2015-07-16 00:01:19 +02:00
" button_exit[0,8.2;1.5,1;abort; " .. S ( " Abort " ) .. " ] " ..
2015-07-15 15:54:05 +02:00
" button_exit[9.2,8.2;1.5,1;save; " .. S ( " OK " ) .. " ] "
2015-06-26 22:33:51 +02:00
minetest.show_formspec ( player_name , " fishing:settings " , formspec )
2015-06-17 02:18:12 +02:00
end
2015-06-16 01:42:05 +02:00
2015-06-17 02:18:12 +02:00
local inc = function ( value , field , min , max )
2015-07-15 15:54:05 +02:00
local inc = tonumber ( field )
local v = value
if inc ~= nil then
v = value + inc
2015-06-17 02:18:12 +02:00
end
2015-06-16 01:42:05 +02:00
2015-06-17 02:18:12 +02:00
if v > max then
2015-07-15 15:54:05 +02:00
return max
2015-06-17 02:18:12 +02:00
end
if v < min then
2015-07-15 15:54:05 +02:00
return min
2015-06-17 02:18:12 +02:00
end
return v
end
2015-06-16 01:42:05 +02:00
2015-06-17 02:18:12 +02:00
local bool = function ( field )
2015-07-15 15:54:05 +02:00
return field ~= " true "
2015-06-16 01:42:05 +02:00
end
minetest.register_on_player_receive_fields ( function ( player , formname , fields )
2015-06-28 20:06:16 +02:00
local player_name = player : get_player_name ( )
if not player_name then return end
2015-06-16 01:42:05 +02:00
if formname == " fishing:settings " then
2015-07-16 00:01:19 +02:00
if fields [ " save " ] then
2015-06-26 22:33:51 +02:00
fishing_setting.func . set_settings ( fishing_setting.settings , fishing_setting.tmp_setting )
fishing_setting.func . save ( )
fishing_setting.tmp_setting = nil
2015-06-16 01:42:05 +02:00
return
2015-06-17 02:18:12 +02:00
elseif fields [ " quit " ] or fields [ " abort " ] then
fishing_setting.tmp_setting = nil
2015-06-16 01:42:05 +02:00
return
2015-06-17 02:18:12 +02:00
elseif fields [ " cfish " ] then
fishing_setting.tmp_setting [ " fish_chance " ] = inc ( fishing_setting.tmp_setting [ " fish_chance " ] , fields [ " cfish " ] , 1 , 100 )
elseif fields [ " cshark " ] then
fishing_setting.tmp_setting [ " shark_chance " ] = inc ( fishing_setting.tmp_setting [ " shark_chance " ] , fields [ " cshark " ] , 1 , 100 )
2015-07-15 15:54:05 +02:00
elseif fields [ " ctreasure " ] then
fishing_setting.tmp_setting [ " treasure_chance " ] = inc ( fishing_setting.tmp_setting [ " treasure_chance " ] , fields [ " ctreasure " ] , 1 , 100 )
2015-06-17 02:18:12 +02:00
elseif fields [ " bvrange " ] then
fishing_setting.tmp_setting [ " bobber_view_range " ] = inc ( fishing_setting.tmp_setting [ " bobber_view_range " ] , fields [ " bvrange " ] , 4 , 20 )
elseif fields [ " cworm " ] then
fishing_setting.tmp_setting [ " worm_chance " ] = inc ( fishing_setting.tmp_setting [ " worm_chance " ] , fields [ " cworm " ] , 1 , 100 )
2015-06-26 22:33:51 +02:00
elseif fields [ " cescape " ] then
2015-06-28 20:06:16 +02:00
fishing_setting.tmp_setting [ " escape_chance " ] = inc ( fishing_setting.tmp_setting [ " escape_chance " ] , fields [ " cescape " ] , 1 , 50 )
2015-06-17 02:18:12 +02:00
elseif fields [ " dmessages " ] then
fishing_setting.tmp_setting [ " message " ] = bool ( fields [ " dmessages " ] )
elseif fields [ " poledeco " ] then
fishing_setting.tmp_setting [ " simple_deco_fishing_pole " ] = bool ( fields [ " poledeco " ] )
elseif fields [ " wearout " ] then
fishing_setting.tmp_setting [ " wear_out " ] = bool ( fields [ " wearout " ] )
2015-07-15 15:54:05 +02:00
elseif fields [ " treasureenable " ] then
fishing_setting.tmp_setting [ " treasure_enable " ] = bool ( fields [ " treasureenable " ] )
2015-06-17 02:18:12 +02:00
elseif fields [ " newworm " ] then
fishing_setting.tmp_setting [ " new_worm_source " ] = bool ( fields [ " newworm " ] )
elseif fields [ " wormmob " ] then
fishing_setting.tmp_setting [ " worm_is_mob " ] = bool ( fields [ " wormmob " ] )
2015-06-16 01:42:05 +02:00
else
return
end
2015-06-28 20:06:16 +02:00
fishing_setting.func . on_show_settings ( player_name )
elseif formname == " fishing:admin_conf " then
if fields [ " classement " ] then
local formspec = fishing_setting.func . get_stat ( )
minetest.show_formspec ( player_name , " fishing:classement " , formspec )
2015-07-15 15:54:05 +02:00
elseif fields [ " contest " ] then
fishing_setting.func . on_show_settings_contest ( player_name )
2015-06-28 20:06:16 +02:00
elseif fields [ " configuration " ] then
fishing_setting.func . on_show_settings ( player_name )
2015-08-20 16:08:03 +02:00
elseif fields [ " hungerinfo " ] then
fishing_setting.func . get_hunger_info ( player_name )
2015-06-28 20:06:16 +02:00
end
2015-06-16 01:42:05 +02:00
end
2015-06-09 23:09:47 +02:00
end )
2015-07-16 00:01:19 +02:00
--function load settings from file
2015-06-26 22:33:51 +02:00
function fishing_setting . func . load_trophies ( )
local file = io.open ( fishing_setting.file_trophies , " r " )
fishing_setting.trophies = { }
if file then
fishing_setting.trophies = minetest.deserialize ( file : read ( " *all " ) )
file : close ( )
2015-06-28 03:01:16 +02:00
if not fishing_setting.trophies or type ( fishing_setting.trophies ) ~= " table " then
2015-06-26 22:33:51 +02:00
fishing_setting.trophies = { }
end
end
end
function fishing_setting . func . save_trophies ( )
local input = io.open ( fishing_setting.file_trophies , " w " )
if input then
input : write ( minetest.serialize ( fishing_setting.trophies ) )
input : close ( )
else
minetest.log ( " action " , " Open failed (mode:w) of " .. fishing_setting.file_trophies )
end
end
minetest.register_on_shutdown ( function ( )
minetest.log ( " action " , " [fishing] Server shuts down. saving trophies table " )
fishing_setting.func . save_trophies ( )
2015-07-15 15:54:05 +02:00
fishing_setting.func . save_contest ( )
2015-06-26 22:33:51 +02:00
end )
2015-06-28 03:01:16 +02:00
2015-06-28 20:06:16 +02:00
function fishing_setting . func . timetostr ( time )
local countdown = time
local answer = " "
if countdown >= 3600 then
local hours = math.floor ( countdown / 3600 )
countdown = countdown % 3600
2015-07-16 00:01:19 +02:00
answer = hours .. " h "
2015-06-28 20:06:16 +02:00
end
if countdown >= 60 then
local minutes = math.floor ( countdown / 60 )
countdown = countdown % 60
2015-07-16 00:01:19 +02:00
answer = answer .. minutes .. " m "
else
answer = answer .. " 0m "
2015-06-28 20:06:16 +02:00
end
local seconds = countdown
2015-07-16 00:01:19 +02:00
answer = answer .. math.floor ( seconds ) .. " s "
2015-06-28 20:06:16 +02:00
return answer
end
2015-06-26 22:33:51 +02:00
minetest.register_on_joinplayer ( function ( player )
2015-06-28 20:06:16 +02:00
local player_name = player : get_player_name ( )
2015-07-15 15:54:05 +02:00
if fishing_setting.contest [ " contest " ] == true then
2015-08-20 16:08:03 +02:00
minetest.chat_send_player ( player_name , S ( " A fishing contest is in progress. (remaining time %s) " ) : format ( fishing_setting.func . timetostr ( fishing_setting.contest [ " duration " ] ) ) )
2015-06-28 20:06:16 +02:00
end
2015-06-26 22:33:51 +02:00
end )
2015-06-28 20:06:16 +02:00
2015-06-26 22:33:51 +02:00
2015-06-27 15:18:34 +02:00
function fishing_setting . func . add_to_trophies ( player , fish , desc )
2015-06-26 22:33:51 +02:00
local player_name = player : get_player_name ( )
if not player_name then return end
2015-06-28 21:39:41 +02:00
if fish == " fish_raw " or fish == " shark_raw " or fish == " pike_raw " or fish == " clownfish_raw " or fish == " bluewhite_raw " then
2015-06-28 03:01:16 +02:00
if fishing_setting.trophies [ fish ] == nil then
fishing_setting.trophies [ fish ] = { }
end
fishing_setting.trophies [ fish ] [ player_name ] = ( fishing_setting.trophies [ fish ] [ player_name ] or 0 ) + 1
if fishing_setting.trophies [ fish ] [ player_name ] % 100 == 0 then
2015-08-20 16:08:03 +02:00
minetest.chat_send_player ( player_name , S ( " You win a new trophy, you have caught %s " .. fish .. " . " ) : format ( fishing_setting.trophies [ fish ] [ player_name ] ) )
2015-06-26 22:33:51 +02:00
local inv = player : get_inventory ( )
local name = " fishing:trophy_ " .. fish
if inv : room_for_item ( " main " , { name = name , count = 1 , wear = 0 , metadata = " " } ) then
inv : add_item ( " main " , { name = name , count = 1 , wear = 0 , metadata = " " } )
else
minetest.spawn_item ( player : getpos ( ) , { name = name , count = 1 , wear = 0 , metadata = " " } )
end
end
2015-07-16 00:01:19 +02:00
2015-07-15 15:54:05 +02:00
if fishing_setting.contest [ " contest " ] ~= nil and fishing_setting.contest [ " contest " ] == true then
if fishing_setting.contest [ fish ] == nil then
fishing_setting.contest [ fish ] = { }
2015-06-27 23:14:10 +02:00
end
2015-07-15 15:54:05 +02:00
fishing_setting.contest [ fish ] [ player_name ] = ( fishing_setting.contest [ fish ] [ player_name ] or 0 ) + 1
2015-08-20 16:08:03 +02:00
minetest.chat_send_all ( S ( " Yeah, %s caught " .. desc ) : format ( player_name ) )
2015-06-27 23:14:10 +02:00
end
2015-06-26 22:33:51 +02:00
end
end
2015-07-15 15:54:05 +02:00
-- Menu: fishing configuration/contest
2015-06-28 20:06:16 +02:00
fishing_setting.func . on_show_admin_menu = function ( player_name )
2015-08-20 16:08:03 +02:00
local formspec = " size[5,5]label[1.7,0; " .. S ( " Fishing Menu " ) .. " ] " ..
2015-07-15 15:54:05 +02:00
" button[0.5,0.5;4,1;classement; " .. S ( " Contest rankings " ) .. " ] " ..
" button[0.5,1.5;4,1;contest; " .. S ( " Contests " ) .. " ] " ..
" button[0.5,2.5;4,1;configuration; " .. S ( " Configuration " ) .. " ] " ..
2015-08-20 16:08:03 +02:00
" button[0.5,3.5;4,1;hungerinfo; " .. S ( " Hunger info " ) .. " ] " ..
2015-07-15 15:54:05 +02:00
" button_exit[1,4.5;3,1;close; " .. S ( " Close " ) .. " ] "
2015-06-28 20:06:16 +02:00
minetest.show_formspec ( player_name , " fishing:admin_conf " , formspec )
end
2015-06-26 22:33:51 +02:00
if ( minetest.get_modpath ( " unified_inventory " ) ) then
unified_inventory.register_button ( " menu_fishing " , {
type = " image " ,
2015-06-27 20:33:36 +02:00
image = " fishing_fish_raw.png " ,
2015-06-26 22:33:51 +02:00
tooltip = " fishing menu configuration " ,
action = function ( player )
local player_name = player : get_player_name ( )
if not player_name then return end
if minetest.check_player_privs ( player_name , { server = true } ) then
2015-06-28 20:06:16 +02:00
fishing_setting.func . on_show_admin_menu ( player_name )
2015-06-26 22:33:51 +02:00
else
2015-06-28 20:06:16 +02:00
local formspec = fishing_setting.func . get_stat ( )
minetest.show_formspec ( player_name , " fishing:classement " , formspec )
2015-06-26 22:33:51 +02:00
end
end ,
} )
end
2015-06-27 23:14:10 +02:00
2015-07-16 00:01:19 +02:00
--function save settings
2015-07-15 15:54:05 +02:00
function fishing_setting . func . save_contest ( )
local input = io.open ( fishing_setting.file_contest , " w " )
2015-06-27 23:14:10 +02:00
if input then
2015-07-15 15:54:05 +02:00
input : write ( minetest.serialize ( fishing_setting.contest ) )
2015-06-27 23:14:10 +02:00
input : close ( )
else
2015-07-15 15:54:05 +02:00
minetest.log ( " action " , " Open failed (mode:w) of " .. fishing_setting.file_contest )
2015-06-27 23:14:10 +02:00
end
end
2015-07-16 00:01:19 +02:00
--function load councours data from file
2015-07-15 15:54:05 +02:00
function fishing_setting . func . load_contest ( )
local file = io.open ( fishing_setting.file_contest , " r " )
2015-06-27 23:14:10 +02:00
local settings = { }
2015-07-15 15:54:05 +02:00
fishing_setting.contest = { [ " contest " ] = false , [ " duration " ] = 3600 , [ " bobber_nb " ] = 4 }
2015-06-27 23:14:10 +02:00
if file then
2015-07-01 01:43:51 +02:00
settings = minetest.deserialize ( file : read ( " *all " ) )
2015-06-27 23:14:10 +02:00
file : close ( )
2015-07-01 01:43:51 +02:00
if settings ~= nil and type ( settings ) == " table " then
2015-07-15 15:54:05 +02:00
if settings [ " contest " ] ~= nil then
fishing_setting.contest [ " contest " ] = settings [ " contest " ]
2015-07-01 01:43:51 +02:00
end
if settings [ " duration " ] ~= nil then
2015-07-15 15:54:05 +02:00
fishing_setting.contest [ " duration " ] = settings [ " duration " ]
2015-07-01 01:43:51 +02:00
end
if settings [ " bobber_nb " ] ~= nil then
2015-07-15 15:54:05 +02:00
fishing_setting.contest [ " bobber_nb " ] = settings [ " bobber_nb " ]
2015-07-01 01:43:51 +02:00
end
if settings [ " fish_raw " ] ~= nil then
2015-07-15 15:54:05 +02:00
fishing_setting.contest [ " fish_raw " ] = settings [ " fish_raw " ]
2015-07-01 01:43:51 +02:00
end
if settings [ " clownfish_raw " ] ~= nil then
2015-07-15 15:54:05 +02:00
fishing_setting.contest [ " clownfish_raw " ] = settings [ " clownfish_raw " ]
2015-07-01 01:43:51 +02:00
end
if settings [ " bluewhite_raw " ] ~= nil then
2015-07-15 15:54:05 +02:00
fishing_setting.contest [ " bluewhite_raw " ] = settings [ " bluewhite_raw " ]
2015-07-01 01:43:51 +02:00
end
if settings [ " shark_raw " ] ~= nil then
2015-07-15 15:54:05 +02:00
fishing_setting.contest [ " shark_raw " ] = settings [ " shark_raw " ]
2015-07-01 01:43:51 +02:00
end
if settings [ " pike_raw " ] ~= nil then
2015-07-15 15:54:05 +02:00
fishing_setting.contest [ " pike_raw " ] = settings [ " pike_raw " ]
2015-07-01 01:43:51 +02:00
end
2015-06-27 23:14:10 +02:00
end
end
end
2015-07-16 00:01:19 +02:00
function fishing_setting . func . end_contest ( )
fishing_setting.contest [ " contest " ] = false
2015-08-20 16:08:03 +02:00
minetest.chat_send_all ( S ( " End of fishing contest. " ) )
2015-07-16 00:01:19 +02:00
minetest.sound_play ( " fishing_contest_end " , { gain = 0.8 } )
fishing_setting.func . show_result ( )
end
2015-06-27 23:14:10 +02:00
--Menu fishing configuration
2015-07-15 15:54:05 +02:00
fishing_setting.func . on_show_settings_contest = function ( player_name )
2015-06-27 23:14:10 +02:00
if not fishing_setting.tmp_setting then
2015-07-15 15:54:05 +02:00
fishing_setting.tmp_setting = { [ " contest " ] = ( fishing_setting.contest [ " contest " ] or false ) ,
[ " duration " ] = ( math.floor ( fishing_setting.contest [ " duration " ] ) or 3600 ) ,
[ " bobber_nb " ] = ( fishing_setting.contest [ " bobber_nb " ] or 2 ) ,
2015-07-16 00:01:19 +02:00
[ " reset " ] = false
2015-06-28 20:06:16 +02:00
}
2015-06-27 23:14:10 +02:00
end
2015-07-15 15:54:05 +02:00
local formspec = " size[6.1,7]label[1.9,0; " .. S ( " Fishing contest " ) .. " ] " ..
--Time contest
" label[2.2,0.5; " .. S ( " Duration(in sec) " ) .. " ] " ..
2015-06-28 20:06:16 +02:00
" button[0.8,1;1,1;duration;-60] " ..
" button[1.8,1;1,1;duration;-600] " ..
" label[2.7,1.2; " .. tostring ( fishing_setting.tmp_setting [ " duration " ] ) .. " ] " ..
" button[3.5,1;1,1;duration;+600] " ..
" button[4.5,1;1,1;duration;+60] " ..
--bobber nb
2015-07-15 15:54:05 +02:00
" label[2,2; " .. S ( " Bobber number limit " ) .. " ] " ..
2015-06-28 20:06:16 +02:00
" button[1.8,2.5;1,1;bobbernb;-1] " ..
" label[2.9,2.7; " .. tostring ( fishing_setting.tmp_setting [ " bobber_nb " ] ) .. " ] " ..
" button[3.5,2.5;1,1;bobbernb;+1] " ..
2015-07-15 15:54:05 +02:00
--contest enable
" label[0.8,3.8; " .. S ( " Enable contests " ) .. " ] " ..
" button[4.5,3.6;1,1;contest; " .. tostring ( fishing_setting.tmp_setting [ " contest " ] ) .. " ] " ..
2015-06-28 03:01:16 +02:00
--reset
2015-07-16 00:01:19 +02:00
" label[0.8,5.2; " .. S ( " Reset rankings " ) .. " ] " ..
" button[4.5,5;1,1;reset; " .. tostring ( fishing_setting.tmp_setting [ " reset " ] ) .. " ] " ..
" button_exit[0.8,6.2;1.5,1;abort; " .. S ( " Abort " ) .. " ] " ..
2015-07-15 15:54:05 +02:00
" button_exit[4,6.2;1.5,1;save; " .. S ( " OK " ) .. " ] "
minetest.show_formspec ( player_name , " fishing:contest " , formspec )
2015-06-27 23:14:10 +02:00
end
minetest.register_on_player_receive_fields ( function ( player , formname , fields )
2015-07-15 15:54:05 +02:00
if formname == " fishing:contest " then
2015-06-27 23:14:10 +02:00
local name = player : get_player_name ( )
if not name then return end
2015-07-16 00:01:19 +02:00
if fields [ " save " ] then
if fishing_setting.tmp_setting [ " reset " ] == true then
2015-07-15 15:54:05 +02:00
fishing_setting.contest [ " fish_raw " ] = { }
fishing_setting.contest [ " clownfish_raw " ] = { }
fishing_setting.contest [ " bluewhite_raw " ] = { }
fishing_setting.contest [ " shark_raw " ] = { }
fishing_setting.contest [ " pike_raw " ] = { }
2015-06-28 03:01:16 +02:00
end
2015-07-16 00:01:19 +02:00
2015-07-15 15:54:05 +02:00
local progress = ( fishing_setting.contest [ " contest " ] or false )
fishing_setting.contest [ " duration " ] = fishing_setting.tmp_setting [ " duration " ]
fishing_setting.contest [ " contest " ] = fishing_setting.tmp_setting [ " contest " ]
fishing_setting.contest [ " bobber_nb " ] = fishing_setting.tmp_setting [ " bobber_nb " ]
if progress == false and fishing_setting.tmp_setting [ " contest " ] == true then
fishing_setting.contest [ " contest " ] = true
fishing_setting.contest [ " warning_said " ] = false
local time = fishing_setting.func . timetostr ( fishing_setting.contest [ " duration " ] )
2015-08-20 16:08:03 +02:00
minetest.chat_send_all ( S ( " Attention, Fishing contest start (duration %s)!!! " ) : format ( time ) )
2015-06-28 21:39:41 +02:00
minetest.sound_play ( " fishing_contest_start " , { gain = 0.8 } )
2015-07-15 15:54:05 +02:00
elseif progress == true and fishing_setting.tmp_setting [ " contest " ] == false then
2015-07-16 00:01:19 +02:00
fishing_setting.func . end_contest ( )
2015-06-28 03:01:16 +02:00
end
2015-07-15 15:54:05 +02:00
fishing_setting.func . save_contest ( )
2015-06-27 23:14:10 +02:00
fishing_setting.tmp_setting = nil
return
elseif fields [ " quit " ] or fields [ " abort " ] then
fishing_setting.tmp_setting = nil
return
elseif fields [ " duration " ] then
fishing_setting.tmp_setting [ " duration " ] = inc ( fishing_setting.tmp_setting [ " duration " ] , fields [ " duration " ] , 120 , 14400 )
2015-07-15 15:54:05 +02:00
elseif fields [ " contest " ] then
fishing_setting.tmp_setting [ " contest " ] = bool ( fields [ " contest " ] )
2015-06-28 20:06:16 +02:00
elseif fields [ " bobbernb " ] then
2015-07-01 01:43:51 +02:00
fishing_setting.tmp_setting [ " bobber_nb " ] = inc ( fishing_setting.tmp_setting [ " bobber_nb " ] , fields [ " bobbernb " ] , 1 , 8 )
2015-07-16 00:01:19 +02:00
elseif fields [ " reset " ] then
fishing_setting.tmp_setting [ " reset " ] = bool ( fields [ " reset " ] )
2015-06-27 23:14:10 +02:00
else
return
end
2015-07-15 15:54:05 +02:00
fishing_setting.func . on_show_settings_contest ( name )
2015-06-27 23:14:10 +02:00
end
end )
function spairs ( t , order )
-- collect the keys
local keys = { }
for k in pairs ( t ) do keys [ # keys + 1 ] = k end
-- if order function given, sort by it by passing the table and keys a, b,
2015-07-16 00:01:19 +02:00
-- otherwise just sort the keys
2015-06-27 23:14:10 +02:00
if order then
table.sort ( keys , function ( a , b ) return order ( t , a , b ) end )
else
table.sort ( keys )
end
-- return the iterator function
local i = 0
return function ( )
i = i + 1
if keys [ i ] then
return keys [ i ] , t [ keys [ i ] ]
end
end
end
function fishing_setting . func . set_winners ( list )
local win = { }
local i = 1
-- this uses an custom sorting function ordering by score descending
for k , v in spairs ( list , function ( t , a , b ) return t [ b ] < t [ a ] end ) do
table.insert ( win , { [ k ] = v } )
2015-06-28 03:01:16 +02:00
if # win >= 6 then
break
end
end
2015-06-27 23:14:10 +02:00
return win
end
function fishing_setting . func . get_stat ( )
local winners = { }
2015-07-15 15:54:05 +02:00
for k , v in pairs ( fishing_setting.contest ) do
2015-06-28 03:01:16 +02:00
if string.find ( k , " _raw " ) ~= nil then
2015-07-15 15:54:05 +02:00
if fishing_setting.contest [ k ] ~= nil then
winners [ k ] = fishing_setting.func . set_winners ( fishing_setting.contest [ k ] )
2015-06-28 03:01:16 +02:00
else
winners [ k ] = { }
2015-06-28 20:06:16 +02:00
end
end
end
2015-08-20 16:08:03 +02:00
local formspec = { " size[12,8]label[4.6,0; " .. S ( " Fishing contest rankings " ) .. " ] " }
2015-06-27 23:14:10 +02:00
local X = 0
local Y
2015-06-28 03:01:16 +02:00
for fish , fishers in pairs ( winners ) do
2015-06-28 20:06:16 +02:00
Y = 1.1
2015-06-28 03:01:16 +02:00
table.insert ( formspec , " label[ " .. ( X + 0.4 ) .. " ,0.5; " .. string.gsub ( fish , " _raw " , " " ) : upper ( ) .. " ] " ) --fish name
2015-06-27 23:14:10 +02:00
for _ , s in ipairs ( fishers ) do
for pl , nb in pairs ( s ) do
2015-07-15 15:54:05 +02:00
table.insert ( formspec , " label[ " .. ( X ) .. " , " .. Y .. " ; " .. tostring ( nb ) .. " ] " ) -- nb fish caught
2015-06-28 03:01:16 +02:00
table.insert ( formspec , " label[ " .. ( X + 0.5 ) .. " , " .. Y .. " ; " .. tostring ( pl ) .. " ] " ) -- playername
2015-06-27 23:14:10 +02:00
end
2015-06-28 03:01:16 +02:00
Y = Y + 0.4
2015-06-27 23:14:10 +02:00
end
2015-06-28 03:01:16 +02:00
X = X + 2.3
2015-06-27 23:14:10 +02:00
end
2015-08-20 16:08:03 +02:00
table.insert ( formspec , " button_exit[5.4,7.5;1.2,1;close; " .. S ( " Close " ) .. " ] " )
2015-06-27 23:14:10 +02:00
return table.concat ( formspec )
end
2015-08-20 16:08:03 +02:00
function fishing_setting . func . get_hunger_info ( player_name )
local formspec = " size[6,8]label[1.9,0;Fishing Info Center] "
local y = 1
for i , a in pairs ( fishing_setting.baits ) do
formspec = formspec .. " item_image_button[1, " .. tostring ( y ) .. " ;1,1; " .. tostring ( i ) .. " ; " .. tostring ( i ) .. " ;] " ..
" label[2.2, " .. tostring ( y + 0.2 ) .. " ;Chance to fish : " .. tostring ( a [ " hungry " ] ) .. " %] "
y = y + 1
end
formspec = formspec .. " button_exit[2,7.5;2,1;close; " .. S ( " Close " ) .. " ] "
minetest.show_formspec ( player_name , " fishing:material_info " , formspec )
end
2015-06-27 23:14:10 +02:00
2015-06-28 20:06:16 +02:00
minetest.register_chatcommand ( " fishing_menu " , {
params = " " ,
description = " Show fishing menu (admin only) " ,
privs = { server = true } ,
func = function ( player_name , param )
if not player_name then return end
fishing_setting.func . on_show_admin_menu ( player_name )
end
} )
minetest.register_chatcommand ( " fishing_classement " , {
2015-06-27 23:14:10 +02:00
params = " display classement " ,
description = " " ,
privs = { interact = true } ,
func = function ( player_name , param )
if not player_name then return end
local formspec = fishing_setting.func . get_stat ( )
minetest.show_formspec ( player_name , " fishing:classement " , formspec )
end
} )
2015-07-01 01:43:51 +02:00
function fishing_setting . func . show_result ( )
2015-06-28 03:01:16 +02:00
minetest.after ( 3 , function ( )
local formspec = fishing_setting.func . get_stat ( )
for _ , player in pairs ( minetest.get_connected_players ( ) ) do
local player_name = player : get_player_name ( )
if player_name ~= nil then
minetest.show_formspec ( player_name , " fishing:classement " , formspec )
2015-06-28 20:06:16 +02:00
end
2015-06-28 03:01:16 +02:00
end
end )
end