2020-04-01 17:20:15 +02:00
--
-- For the item to set signs, being a declaration of a new item, look at items.lua
--
2020-04-02 12:47:55 +02:00
local queue_waiting_time = 5
2020-04-01 17:20:15 +02:00
2020-03-31 23:38:31 +02:00
local function in_game_txt ( arena ) end
2020-04-01 17:20:15 +02:00
2020-03-31 17:04:03 +02:00
minetest.override_item ( " default:sign_wall " , {
on_punch = function ( pos , node , puncher , pointed_thing )
local arenaID = minetest.get_meta ( pos ) : get_int ( " arenaID " )
if arenaID == 0 then return end
local sign_arena = arena_lib.arenas [ arenaID ]
local p_name = puncher : get_player_name ( )
if not sign_arena then return end -- nel caso qualche cartello dovesse buggarsi, si può rompere e non fa crashare
-- se è già in coda o viene fermato (cartello diverso) o si toglie dalla coda (cartello uguale)
2020-03-31 23:38:31 +02:00
if arena_lib.is_player_in_queue ( p_name ) then
local queued_ID = arena_lib.get_queueID_by_player ( p_name )
2020-03-31 17:04:03 +02:00
2020-03-31 23:38:31 +02:00
minetest.chat_send_player ( " singleplayer " , " queued_ID = " .. queued_ID .. " , arenaID = " .. arenaID )
if queued_ID ~= arenaID then
minetest.chat_send_player ( p_name , " [Quake] " .. minetest.colorize ( " #e6482e " , " Devi prima uscire dalla coda di " .. arena_lib.arenas [ queued_ID ] . name .. " ! " ) )
2020-03-31 17:04:03 +02:00
else
sign_arena.players [ p_name ] = nil
arena_lib.update_sign ( pos , sign_arena )
2020-03-31 23:38:31 +02:00
arena_lib.remove_from_queue ( p_name )
2020-03-31 17:04:03 +02:00
minetest.chat_send_player ( p_name , " [Quake] Sei uscito dalla coda " )
arena_lib.send_message_players_in_arena ( arenaID , " [Quake] " .. p_name .. " ha abbandonato la coda " )
-- se non ci sono più abbastanza giocatori, annullo la coda
if arena_lib.get_arena_players_count ( arenaID ) < sign_arena.min_players and sign_arena.in_queue then
2020-03-31 23:38:31 +02:00
--timer:stop()
arena_lib.send_message_players_in_arena ( arenaID , " [Quake] La coda è stata annullata per troppi pochi giocatori " )
2020-03-31 17:04:03 +02:00
end
end
return end
-- se l'arena è piena
if arena_lib.get_arena_players_count ( arenaID ) == sign_arena.max_players then
minetest.chat_send_player ( p_name , minetest.colorize ( " #e6482e " , " [!] L'arena è già piena! " ) )
return end
2020-04-02 12:47:55 +02:00
-- se sta caricando
if sign_arena.in_loading then
minetest.chat_send_player ( p_name , minetest.colorize ( " #e6482e " , " [!] L'arena è in caricamento, riprova tra qualche secondo! " ) )
return end
-- aggiungo il giocatore e aggiorno il cartello
sign_arena.players [ p_name ] = { kills = 0 , deaths = 0 , killstreak = 0 }
arena_lib.update_sign ( pos , sign_arena )
2020-03-31 17:04:03 +02:00
-- notifico i vari giocatori del nuovo player
if sign_arena.in_game then
2020-04-02 12:47:55 +02:00
arena_lib.join_arena ( p_name , arenaID )
2020-03-31 17:04:03 +02:00
arena_lib.send_message_players_in_arena ( arenaID , " [Quake] " .. p_name .. " si è aggiunto alla partita " )
minetest.chat_send_player ( p_name , " [Quake] Sei entrato nell'arena " .. sign_arena.name )
2020-04-02 12:47:55 +02:00
return
2020-03-31 17:04:03 +02:00
else
2020-04-02 12:47:55 +02:00
arena_lib.add_to_queue ( p_name , arenaID )
2020-03-31 17:04:03 +02:00
arena_lib.send_message_players_in_arena ( arenaID , " [Quake] " .. p_name .. " si è aggiunto alla coda " )
minetest.chat_send_player ( p_name , " [Quake] Ti sei aggiunto alla coda per " .. sign_arena.name )
end
local timer = minetest.get_node_timer ( pos )
-- se ci sono abbastanza giocatori, parte il timer di attesa
if arena_lib.get_arena_players_count ( arenaID ) == sign_arena.min_players and not sign_arena.in_queue and not sign_arena.in_game then
2020-04-02 12:47:55 +02:00
arena_lib.send_message_players_in_arena ( arenaID , " [Quake] La partita inizierà tra " .. queue_waiting_time .. " secondi! " )
2020-03-31 17:04:03 +02:00
sign_arena.in_queue = true
2020-04-02 12:47:55 +02:00
timer : start ( queue_waiting_time )
2020-03-31 17:04:03 +02:00
end
-- se raggiungo i giocatori massimi e la partita non è iniziata, parte subito
if arena_lib.get_arena_players_count ( arenaID ) == sign_arena.max_players and sign_arena.in_queue then
timer : stop ( )
timer : start ( 0.01 )
end
--TODO: timer ciclico che avvisa i giocatori quanto tempo manca ogni N secondi
end ,
-- quello che succede una volta che il timer raggiunge lo 0
on_timer = function ( pos )
local arenaID = minetest.get_meta ( pos ) : get_int ( " arenaID " )
local sign_arena = arena_lib.arenas [ arenaID ]
sign_arena.in_queue = false
sign_arena.in_game = true
arena_lib.update_sign ( pos , sign_arena )
arena_lib.load_arena ( arenaID )
return false
end ,
} )
function arena_lib . set_sign ( sender , arena_name )
local arena_ID , arena = arena_lib.get_arena_by_name ( arena_name )
if arena == nil then minetest.chat_send_player ( sender , minetest.colorize ( " #e6482e " , " [!] Quest'arena non esiste! " ) )
return end
if arena_lib.get_arena_spawners_count ( arena_ID ) < arena.max_players then minetest.chat_send_player ( sender , minetest.colorize ( " #e6482e " , " [!] Gli spawner devono essere quanto i giocatori massimi prima di impostare il cartello! " ) )
return end
-- assegno item creazione arene con ID arena nei metadati da restituire al premere sul cartello
2020-04-02 18:38:18 +02:00
local stick = ItemStack ( arena_lib.mod_name .. " :create_sign " )
2020-03-31 17:04:03 +02:00
local meta = stick : get_meta ( )
meta : set_int ( " arenaID " , arena_ID )
minetest.get_player_by_name ( sender ) : set_wielded_item ( stick )
minetest.chat_send_player ( sender , " Click sinistro su un cartello per settare l'arena " )
end
function arena_lib . update_sign ( pos , arena )
-- non uso il getter perché dovrei richiamare 2 funzioni (ID e count)
local p_count = 0
for pl , stats in pairs ( arena.players ) do
p_count = p_count + 1
end
signs_lib.update_sign ( pos , { text = [ [
2020-03-31 23:38:31 +02:00
] ] .. " \n " .. [ [
] ] .. arena.name .. " \n " .. [ [
] ] .. p_count .. " / " .. arena.max_players .. " \n " .. [ [
] ] .. in_game_txt ( arena ) .. " \n " .. [ [
2020-03-31 17:04:03 +02:00
] ] } )
end
2020-03-31 23:38:31 +02:00
----------------------------------------------
---------------FUNZIONI LOCALI----------------
----------------------------------------------
function in_game_txt ( arena )
local txt
if arena.in_celebration then txt = " Concludendo "
elseif arena.in_game then txt = " In partita "
2020-04-02 12:47:55 +02:00
elseif arena.in_loading then txt = " In caricamento "
2020-03-31 23:38:31 +02:00
else txt = " In attesa " end
return txt
end