This repository has been archived on 2025-01-04. You can view files and clone it, but cannot push or open issues or pull requests.
lwb6/legacyworlds-server-data/db-structure/parts/040-functions/080-buildings.sql
Emmanuel BENOîT e50775ec76 Database definition & tests organisation
* The main loader script has been updated to generate the list of files
it needs to load automatically. As a consequence, files that contained
manually-maintained lists of scripts have been removed, and definition
directories have been renamed accordingly.

* PostgreSQL extension loading and configuration has been moved to a
separate script to be loaded automatically in the main transaction.

* Data and function definition scripts that had the -data or -functions
suffix have been renamed (the suffix is unnecessary).

* Unit tests have been reorganised to follow the definition's structure.

* Documentation has been improved
2012-01-06 11:19:19 +01:00

84 lines
No EOL
1.7 KiB
PL/PgSQL

-- LegacyWorlds Beta 6
-- PostgreSQL database scripts
--
-- Buildings views and management functions
--
-- Copyright(C) 2004-2010, DeepClone Development
-- --------------------------------------------------------
--
-- Construct buildings on a planet
--
-- Parameters:
-- pid Planet identifier
-- bid Building type
-- bcnt Amount of buildings
--
CREATE OR REPLACE FUNCTION verse.do_construct_buildings( pid INT , bid INT , bcnt INT )
RETURNS VOID
STRICT VOLATILE
SECURITY INVOKER
AS $$
BEGIN
LOOP
UPDATE verse.planet_buildings
SET amount = amount + bcnt
WHERE planet_id = pid AND building_id = bid;
EXIT WHEN FOUND;
BEGIN
INSERT INTO verse.planet_buildings( planet_id , building_id , amount , damage )
VALUES( pid , bid , bcnt , 0 );
EXIT;
EXCEPTION
WHEN unique_violation THEN
-- Do nothing, try updating again.
END;
END LOOP;
END;
$$ LANGUAGE plpgsql;
--
-- Destroy buildings on a planet
--
-- Parameters:
-- pid Planet identifier
-- bid Building type
-- bcnt Amount of buildings
--
-- Returns:
-- amount of buildings that were destroyed
--
CREATE OR REPLACE FUNCTION verse.do_destroy_buildings( pid INT , bid INT , bcnt INT )
RETURNS INT
STRICT VOLATILE
SECURITY INVOKER
AS $$
DECLARE
tmp INT;
BEGIN
UPDATE verse.planet_buildings
SET amount = amount - bcnt
WHERE planet_id = pid AND building_id = bid;
IF FOUND THEN
RETURN bcnt;
END IF;
RETURN 0;
EXCEPTION
WHEN check_violation THEN
SELECT INTO tmp amount FROM verse.planet_buildings
WHERE planet_id = pid AND building_id = bid
FOR UPDATE;
UPDATE verse.planet_buildings
SET amount = 0
WHERE planet_id = pid AND building_id = bid;
RETURN tmp;
END;
$$ LANGUAGE plpgsql;