]> ruin.nu Git - ndwebbie.git/blob - database/functions/planets.sql
Make use of the newish planet id from stats
[ndwebbie.git] / database / functions / planets.sql
1 CREATE OR REPLACE FUNCTION planetid(x integer, y integer, z integer, tick integer) RETURNS integer
2     AS $_$SELECT pid FROM planet_stats WHERE x = $1 AND y = $2 AND z = $3 AND (tick >= $4  OR tick =( SELECT max(tick) FROM planet_stats)) ORDER BY tick ASC LIMIT 1$_$
3     LANGUAGE sql STABLE;
4
5 CREATE OR REPLACE FUNCTION planetcoords(id integer, tick integer, OUT x integer, OUT y integer, OUT z integer) RETURNS record
6     AS $_$SELECT x,y,z FROM planet_stats WHERE pid = $1 AND (tick >= $2  OR tick =( SELECT max(tick) FROM planet_stats))  ORDER BY tick ASC LIMIT 1$_$
7     LANGUAGE sql STABLE;
8
9 CREATE OR REPLACE FUNCTION find_alliance_id(alli text) RETURNS integer
10     AS $_$
11 DECLARE
12         id INTEGER;
13 BEGIN
14         SELECT aid FROM INTO id alliances WHERE alliance = alli;
15         IF NOT FOUND THEN
16                 INSERT INTO alliances(alliance) VALUES($1)
17                         RETURNING aid INTO id;
18         END IF;
19         RETURN id;
20 END;
21 $_$
22     LANGUAGE plpgsql;
23
24 CREATE OR REPLACE FUNCTION coords(x integer, y integer, z integer) RETURNS text
25     AS $_$
26 SELECT $1 || ':' || $2 || ':' || $3
27 $_$
28     LANGUAGE sql IMMUTABLE;