-- LegacyWorlds Beta 6
-- PostgreSQL database scripts
--
-- Empires and alliances
--
-- Copyright(C) 2004-2010, DeepClone Development
-- --------------------------------------------------------


--
-- Empires
--

CREATE TABLE emp.empires(
	name_id		INT NOT NULL PRIMARY KEY ,
	cash		DOUBLE PRECISION NOT NULL
					CHECK( cash >= 0 ),
	debt		DOUBLE PRECISION NOT NULL DEFAULT 0
					CHECK( debt >= 0)
);

ALTER TABLE emp.empires
	ADD CONSTRAINT fk_empires_name
		FOREIGN KEY (name_id) REFERENCES naming.empire_names;


--
-- Research in progress
--

CREATE TABLE emp.research(
	empire_id		INT NOT NULL ,
	technology_id	INT	NOT NULL ,
	accumulated		DOUBLE PRECISION NOT NULL DEFAULT 0
						CHECK( accumulated >= 0 ) ,
	priority		INT NOT NULL
						CHECK( priority BETWEEN 0 AND 100 ) ,
	PRIMARY KEY( empire_id , technology_id )
);

CREATE INDEX idx_research_technology
	ON emp.research (technology_id);

ALTER TABLE emp.research
	ADD CONSTRAINT fk_research_empire
		FOREIGN KEY (empire_id) REFERENCES emp.empires
			ON DELETE CASCADE ,
	ADD CONSTRAINT fk_research_technology
		FOREIGN KEY (technology_id) REFERENCES tech.technologies
			ON DELETE CASCADE;



--
-- Researched technologies (both implemented and pending implementation)
--

CREATE TABLE emp.researched_technologies(
	empire_id		INT NOT NULL ,
	technology_id	INT NOT NULL ,
	implemented		BOOLEAN NOT NULL ,
	PRIMARY KEY ( empire_id , technology_id )
);

CREATE INDEX idx_researched_technology
	ON emp.researched_technologies ( technology_id );
CREATE INDEX idx_researched_implemented
	ON emp.researched_technologies ( empire_id , implemented );

ALTER TABLE emp.researched_technologies
	ADD CONSTRAINT fk_researched_empire
		FOREIGN KEY ( empire_id ) REFERENCES emp.empires
			ON DELETE CASCADE ,
	ADD CONSTRAINT fk_researched_technology
		FOREIGN KEY ( technology_id ) REFERENCES tech.technologies
			ON DELETE CASCADE;


--
-- Empire planets
-- 

CREATE TABLE emp.planets(
	planet_id		INT NOT NULL PRIMARY KEY ,
	empire_id		INT NOT NULL
);

CREATE INDEX idx_planets_empire
	ON emp.planets (empire_id);

ALTER TABLE emp.planets
	ADD CONSTRAINT fk_eplanets_planet
		FOREIGN KEY (planet_id) REFERENCES verse.planets ,
	ADD CONSTRAINT fk_eplanets_empire
		FOREIGN KEY (empire_id) REFERENCES emp.empires
			ON DELETE CASCADE;


--
-- Planets being abandonned
--

CREATE TABLE emp.abandon(
	planet_id		INT NOT NULL PRIMARY KEY ,
	time_left		INT NOT NULL CHECK ( time_left > 0 )
);

CREATE INDEX idx_abandon_ready
	ON emp.abandon ( ( time_left = 1 ) );

ALTER TABLE emp.abandon
	ADD CONSTRAINT fk_abandon_planet
		FOREIGN KEY (planet_id) REFERENCES emp.planets
			ON DELETE CASCADE;


--
-- Alliances
--

CREATE TABLE emp.alliances(
	id				SERIAL NOT NULL PRIMARY KEY ,
	tag				VARCHAR(5) NOT NULL ,
	name			VARCHAR(128) NOT NULL ,
	leader_id		INT NOT NULL
);

CREATE UNIQUE INDEX idx_alliances_tag
	ON emp.alliances ( lower(tag) );
CREATE UNIQUE INDEX idx_alliances_leader
	ON emp.alliances (leader_id);

ALTER TABLE emp.alliances
	ADD CONSTRAINT fk_alliances_leader
		FOREIGN KEY (leader_id) REFERENCES emp.empires
			ON DELETE CASCADE;


--
-- Alliance membership
--

CREATE TABLE emp.alliance_members(
	empire_id		INT NOT NULL PRIMARY KEY ,
	alliance_id		INT NOT NULL ,
	is_pending		BOOLEAN NOT NULL
						DEFAULT TRUE
);

CREATE INDEX idx_alliancemembers_alliance
	ON emp.alliance_members( alliance_id );

ALTER TABLE emp.alliance_members
	ADD CONSTRAINT fk_alliancemembers_empire
		FOREIGN KEY (empire_id) REFERENCES emp.empires
			ON DELETE CASCADE ,
	ADD CONSTRAINT fk_alliancemembers_alliance
		FOREIGN KEY (alliance_id) REFERENCES emp.alliances
			ON DELETE CASCADE;


--
-- Enemy lists, alliances
--

CREATE TABLE emp.enemy_alliances(
	empire_id		INT NOT NULL ,
	alliance_id		INT NOT NULL ,
	PRIMARY KEY (empire_id,alliance_id)
);

CREATE INDEX idx_enemyalliances_alliance
	ON emp.enemy_alliances (alliance_id);

ALTER TABLE emp.enemy_alliances
	ADD CONSTRAINT fk_enemyalliances_empire
		FOREIGN KEY (empire_id) REFERENCES emp.empires
			ON DELETE CASCADE ,
	ADD CONSTRAINT fk_enemyalliances_alliance
		FOREIGN KEY (alliance_id) REFERENCES emp.alliances
			ON DELETE CASCADE;


--
-- Enemy lists, empires
--

CREATE TABLE emp.enemy_empires(
	empire_id		INT NOT NULL ,
	enemy_id		INT NOT NULL ,
	PRIMARY KEY (empire_id,enemy_id)
);

CREATE INDEX idx_enemyempires_enemy
	ON emp.enemy_empires (enemy_id);

ALTER TABLE emp.enemy_empires
	ADD CONSTRAINT fk_enemyempires_empire
		FOREIGN KEY (empire_id) REFERENCES emp.empires
			ON DELETE CASCADE ,
	ADD CONSTRAINT fk_enemyempires_enemy
		FOREIGN KEY (enemy_id) REFERENCES emp.empires
			ON DELETE CASCADE;