]> ruin.nu Git - ndwebbie.git/blob - database/functions/raid_claims.sql
Clean up database directory
[ndwebbie.git] / database / functions / raid_claims.sql
1 CREATE OR REPLACE FUNCTION updated_claim() RETURNS trigger
2     AS $_X$
3 DECLARE
4         target INTEGER;
5 BEGIN
6         CASE TG_OP
7         WHEN 'INSERT' THEN
8                 target := NEW.target;
9         WHEN 'UPDATE' THEN
10                 target := NEW.target;
11                 IF NEW.launched AND NOT OLD.launched THEN
12                         UPDATE users
13                         SET attack_points = attack_points + 1
14                         WHERE uid = OLD.uid;
15
16                         INSERT INTO forum_posts (ftid,uid,message)
17                         VALUES((SELECT ftid FROM users WHERE uid = NEW.uid),NEW.uid
18                                 ,'Gave attack point for confirmation of attack on target '
19                                         || NEW.target || ', wave ' || NEW.wave
20                                 );
21                 END IF;
22         WHEN 'DELETE' THEN
23                 target := OLD.target;
24
25                 IF OLD.launched THEN
26                         UPDATE users
27                         SET attack_points = attack_points - 1
28                         WHERE uid = OLD.uid;
29                 END IF;
30         END CASE;
31         UPDATE raid_targets SET modified = NOW() WHERE id = target;
32         RETURN NEW;
33 END;
34 $_X$ LANGUAGE plpgsql;
35