-- LegacyWorlds Beta 6
-- PostgreSQL database scripts
--
-- Functions to manipulate player-defined event priority
-- overrides.
--
-- Copyright(C) 2004-2012, DeepClone Development
-- --------------------------------------------------------



/*
 * Set an event priority override
 * ------------------------------
 * 
 * Adds a new event priority override or modify its value. If the event type
 * does not exist, does not allow overrides or if the specified priority is
 * invalid, the error will be ignored silently.
 * 
 * Parameters:
 *		_empire_id		The identifier of the player's empire
 *		_evdef_id		The identifier of the event type
 *		_priority		The custom priority value
 */
DROP FUNCTION IF EXISTS events.evcp_set( INT , TEXT , INT );
CREATE FUNCTION events.evcp_set(
			_empire_id	INT ,
			_evdef_id	TEXT ,
			_priority	INT )
		RETURNS VOID
		LANGUAGE PLPGSQL
		STRICT VOLATILE SECURITY DEFINER
	AS $evcp_set$

DECLARE
	_user_id	INT;

BEGIN
	BEGIN
		INSERT INTO events.custom_priorities(
				evdef_id , address_id , evcp_priority )
			SELECT _evdef_id , owner_id , _priority
				FROM naming.empire_names
				WHERE id = _empire_id;
		IF NOT FOUND
		THEN
			RAISE EXCEPTION 'missing empire';
		END IF;
	EXCEPTION
		WHEN unique_violation THEN
			UPDATE events.custom_priorities
				SET evcp_priority = _priority
				WHERE evdef_id = _evdef_id
					AND address_id = (
						SELECT owner_id
							FROM naming.empire_names
							WHERE id = _empire_id );
	END;

EXCEPTION
	WHEN raise_exception THEN
		PERFORM sys.write_sql_log( 'Events' , 'WARNING'::log_level ,
			'Call to events.evcp_set() from missing empire #' || _empire_id );

	WHEN foreign_key_violation OR check_violation THEN
		PERFORM sys.write_sql_log( 'Events' , 'WARNING'::log_level ,
			'Bad call to events.evcp_set() from empire #' || _empire_id );
END;
$evcp_set$;

REVOKE EXECUTE
	ON FUNCTION events.evcp_set( INT , TEXT , INT )
	FROM PUBLIC;
GRANT EXECUTE
	ON FUNCTION events.evcp_set( INT , TEXT , INT )
	TO :dbuser;



/*
 * Clear an event priority override
 * --------------------------------
 * 
 * Remove an event priority override from the custom priorities table. If
 * there was no override for the specified event type and empire, do nothing.
 * 
 * Parameters:
 *		_empire_id		The identifier of the player's empire
 *		_evdef_id		The identifier of the event type
 */
DROP FUNCTION IF EXISTS events.evcp_clear( INT , TEXT );
CREATE FUNCTION events.evcp_clear(
		_empire_id	INT ,
		_evdef_id	TEXT )
	RETURNS VOID
	LANGUAGE SQL
	STRICT VOLATILE SECURITY DEFINER
AS $evcp_clear$
	DELETE FROM events.custom_priorities
		WHERE evdef_id = $2
			AND address_id = (
				SELECT owner_id
					FROM naming.empire_names
					WHERE id = $1 )
$evcp_clear$;

REVOKE EXECUTE
	ON FUNCTION events.evcp_clear( INT , TEXT )
	FROM PUBLIC;
GRANT EXECUTE
	ON FUNCTION events.evcp_clear( INT , TEXT )
	TO :dbuser;



/*
 * Clear all event priority overrides
 * ----------------------------------
 * 
 * Remove all event priority overrides set by some player.
 * 
 * Parameters:
 *		_empire_id		The identifier of the player's empire
 */
DROP FUNCTION IF EXISTS events.evcp_clear( INT );
CREATE FUNCTION events.evcp_clear( _empire_id INT )
	RETURNS VOID
	LANGUAGE SQL
	STRICT VOLATILE SECURITY DEFINER
AS $evcp_clear$
	DELETE FROM events.custom_priorities
		WHERE address_id = (
				SELECT owner_id
					FROM naming.empire_names
					WHERE id = $1 )
$evcp_clear$;

REVOKE EXECUTE
	ON FUNCTION events.evcp_clear( INT )
	FROM PUBLIC;
GRANT EXECUTE
	ON FUNCTION events.evcp_clear( INT )
	TO :dbuser;
