-- LegacyWorlds Beta 6
-- PostgreSQL database scripts
--
-- Game updates - control functions
--
-- Copyright(C) 2004-2010, DeepClone Development
-- --------------------------------------------------------



--
-- Start a tick
--

CREATE OR REPLACE FUNCTION sys.start_tick( OUT tick_id BIGINT )
		STRICT VOLATILE
		SECURITY DEFINER
	AS $$
DECLARE
	n_tick	BIGINT;
	c_tick	BIGINT;
BEGIN
	-- Get next / current tick
	SELECT INTO n_tick , c_tick next_tick , current_tick
		FROM sys.status
		WHERE maintenance_start IS NULL
		FOR UPDATE;
	IF NOT FOUND OR c_tick IS NOT NULL THEN
		tick_id := NULL;
		RETURN;
	END IF;

	-- Prepare game updates
	UPDATE sys.updates SET last_tick = n_tick , status = 'FUTURE'
		WHERE last_tick < n_tick;

	-- Update system status
	UPDATE sys.status SET current_tick = n_tick , next_tick = n_tick + 1;
	
	tick_id := n_tick;
END;
$$ LANGUAGE plpgsql;

GRANT EXECUTE ON FUNCTION sys.start_tick( ) TO :dbuser;



--
-- Marks a tick as completed
--

CREATE OR REPLACE FUNCTION sys.end_tick( IN tick_id BIGINT )
		RETURNS VOID
		STRICT VOLATILE
		SECURITY INVOKER
	AS $$
BEGIN
	UPDATE events.events SET status = 'READY'
		WHERE status = 'TICK' AND tick = tick_id;
	UPDATE sys.status SET current_tick = NULL;
	PERFORM msgs.deliver_internal( );
END;
$$ LANGUAGE plpgsql;



--
-- Check if a tick got "stuck"
--

CREATE OR REPLACE FUNCTION sys.check_stuck_tick( OUT tick_id BIGINT )
		STRICT VOLATILE
		SECURITY DEFINER
	AS $$
DECLARE
	c_tick	BIGINT;
	u_count	INT;
BEGIN
	-- Get next / current tick
	SELECT INTO c_tick current_tick
		FROM sys.status
		WHERE maintenance_start IS NULL
		FOR UPDATE;
	IF NOT FOUND OR c_tick IS NULL THEN
		tick_id := NULL;
		RETURN;
	END IF;
	
	-- Are there any updates left?
	SELECT INTO u_count count(*) FROM sys.updates
		WHERE status = 'FUTURE' AND last_tick = c_tick;
	IF u_count = 0 THEN
		PERFORM sys.end_tick( c_tick );
		tick_id := NULL;
	ELSE
		tick_id := c_tick;
	END IF;
END;
$$ LANGUAGE plpgsql;

GRANT EXECUTE ON FUNCTION sys.check_stuck_tick( ) TO :dbuser;



--
-- Process game updates
--
-- Parameters:
--	c_tick	Current tick
--
-- Returns:
--	TRUE if the function must be called again, FALSE otherwise
--

CREATE OR REPLACE FUNCTION sys.process_updates( IN c_tick BIGINT , OUT has_more BOOLEAN )
		STRICT VOLATILE
		SECURITY DEFINER
	AS $$
DECLARE
	b_size	INT;
	p_utype	update_type;
	utype	update_type;
	uid		BIGINT;
BEGIN
	b_size := sys.get_constant( 'game.batchSize' );
	p_utype := NULL;

	-- Mark at most b_size entries as being updated
	FOR uid , utype IN SELECT id , gu_type FROM sys.updates
						WHERE last_tick = c_tick AND status = 'FUTURE'
						ORDER BY gu_type LIMIT b_size
	LOOP
		IF p_utype IS NULL THEN
			p_utype := utype;
		END IF;
		EXIT WHEN utype <> p_utype;
		UPDATE sys.updates SET status = 'PROCESSING' WHERE id = uid;
	END LOOP;

	has_more := p_utype IS NOT NULL;
	IF has_more THEN
		-- Execute actual updates
		EXECUTE 'SELECT sys.process_' || lower( p_utype::TEXT ) || '_updates( $1 )'
			USING c_tick;
		UPDATE sys.updates SET status = 'PROCESSED'
			WHERE status = 'PROCESSING' AND last_tick = c_tick;
	ELSE
		-- If nothing was found, we're done
		PERFORM sys.end_tick( c_tick );
	END IF;
END;
$$ LANGUAGE plpgsql;

GRANT EXECUTE ON FUNCTION sys.process_updates( BIGINT ) TO :dbuser;


















