]> ruin.nu Git - NDIRC.git/commitdiff
Planet tags, for attaching random information to planets
authorMichael Andreen <harv@ruin.nu>
Tue, 25 Aug 2009 11:38:17 +0000 (13:38 +0200)
committerMichael Andreen <harv@ruin.nu>
Tue, 25 Aug 2009 11:38:17 +0000 (13:38 +0200)
Commands/Intel.pm
Commands/PA.pm
database/roles.sql

index b9c00e91d20ebe6085208caca59c53321ea59f66..b52d694eddaef305da830f7a1bd96c40435fbd29 100644 (file)
@@ -149,4 +149,83 @@ sub setchannel
        }
 }
 
+sub newtag
+       : Help(Usage: .newtag tag | Creates a new tag that can be used with addtag, tags can only contain a-z, A-Z, 0-9 and _)
+       : ACL(irc_newtag)
+       : Type(member)
+{
+       my ($self,$c,$msg) = @_;
+       my ($tag) = $msg =~ /^(\w+)$/ or die 'ARGS';
+
+       my $dbh = $c->model;
+       my $query = $dbh->prepare(q{
+INSERT INTO available_planet_tags (tag) VALUES($1)
+               });
+       eval {
+               $query->execute($tag);
+       };
+       if ($@ =~ /duplicate key value/){
+               $c->reply("<b>$tag</b> already exists");
+               return;
+       }elsif ($@){
+               die $@;
+       }
+       $c->reply("Added tag <b>$tag</b>");
+}
+
+sub addtag
+       : Help(Usage: .addtag X:Y:Z tag | Adds the tag to planet X:Y:Z)
+       : ACL(irc_addtag)
+       : Type(member)
+{
+       my ($self,$c,$msg) = @_;
+       my ($x,$y,$z,$tag) = $msg =~ /^(\d+)\D+(\d+)\D+(\d+)\s+(\S+)$/ or die 'ARGS';
+       my $dbh = $c->model;
+
+       my $query = $dbh->prepare(q{
+INSERT INTO planet_tags (uid,pid,tag) VALUES($1,planetid($2,$3,$4,tick()),$5)
+               });
+       eval {
+               $query->execute($c->uid,$x,$y,$z,$tag);
+       };
+       given ($@){
+               when(''){
+                       $c->reply("Added tag <b>$tag</b> to $x:$y:$z");
+               }when (/duplicate key value/){
+                       $c->reply("$x:$y:$z already has tag <b>$tag</b>");
+               }when (/null value in column "pid"/){
+                       $c->reply("No planet with coords $x:$y:$z");
+               }when ($@ =~ /foreign key constraint "planet_tags_tag_fkey"/){
+                       $query = $dbh->prepare(q{
+SELECT array_to_string(array_agg(tag),' ')
+FROM (SELECT tag FROM available_planet_tags
+       WHERE tag % $1 ORDER BY similarity(tag,$1) DESC) t
+                       });
+                       my ($tags) = $dbh->selectrow_array($query,undef,$tag);
+                       $c->reply("<b>$tag</b> is not a valid tag. Either you need to add it with .newtag,"
+                               ." or you want one of: $tags");
+               }
+               default {
+                       die $@;
+               }
+       }
+}
+
+sub deltag
+       : Help(Usage: .deltag X:Y:Z tag | Removes the tag from planet X:Y:Z)
+       : ACL(irc_addtag)
+       : Type(member)
+{
+       my ($self,$c,$msg) = @_;
+       my ($x,$y,$z,$tag) = $msg =~ /^(\d+)\D+(\d+)\D+(\d+)\s+(\S+)$/ or die 'ARGS';
+       my $dbh = $c->model;
+
+       my $query = $dbh->prepare(q{
+DELETE FROM planet_tags WHERE uid = $1 AND pid = planetid($2,$3,$4,tick()) AND tag LIKE $5
+               });
+       $query->execute($c->uid,$x,$y,$z,$tag);
+       my $rows = $query->rows;
+       $c->reply("Removed <b>$rows</b> matching $tag from $x:$y:$z");
+}
+
 1;
index 52fb3a007a1241c3cdc9e234f3c244cc3defde45..7fb453db9f78c513e759d679ad7db1916132a9db 100644 (file)
@@ -55,11 +55,22 @@ sub p
        }
 
        my $f = $c->model->prepare(q{
-SELECT coords(x,y,z),ruler,planet,race,score,size,value,scorerank,sizerank,
-       valuerank, xp, xprank, alliance, relationship, nick, planet_status, hit_us, channel
-FROM current_planet_stats WHERE (x = $1 AND y = $2 and z = $3) OR nick ILIKE $4 LIMIT 1
+WITH p AS (SELECT coords(x,y,z),ruler,planet,race,score,size,value,scorerank,sizerank,
+               valuerank, xp, xprank, alliance, relationship, nick, planet_status, hit_us, channel
+       FROM current_planet_stats
+       WHERE (x = $1 AND y = $2 and z = $3) OR nick ILIKE $4 LIMIT 1
+), t AS (SELECT tag,bool_or(uid = $5) AS own,max(time) AS time
+       FROM planet_tags
+       WHERE pid = (SELECT pid FROM p)
+               AND ($6 OR uid = $5)
+       GROUP BY tag
+       ORDER BY time DESC
+), tags AS (SELECT array_to_string(array_agg(tag || CASE WHEN own THEN '*' ELSE '' END),' ') AS tags
+       FROM t
+)
+SELECT * FROM p, tags;
        });
-       $f->execute($x,$y,$z,$nick);
+       $f->execute($x,$y,$z,$nick,$c->uid,$c->check_user_roles(qw/irc_p_intel/));
        if (my $planet = $f->fetchrow_hashref()){
                for (keys %{$planet}){
                        $planet->{$_} = valuecolor(1,$planet->{$_});
@@ -68,7 +79,7 @@ FROM current_planet_stats WHERE (x = $1 AND y = $2 and z = $3) OR nick ILIKE $4
                if ($c->check_user_roles(qw/irc_p_intel/)){
                        $ally = "Alliance=$planet->{alliance} ($planet->{relationship}), Nick=$planet->{nick} ($planet->{planet_status}), Channel: $planet->{channel}, Hostile Count: $planet->{hit_us},";
                }
-               $c->reply("$planet->{coords} $planet->{ruler} OF $planet->{planet},$ally Race=$planet->{race}, Score=$planet->{score} ($planet->{scorerank}), Size=$planet->{size} ($planet->{sizerank}), Value=$planet->{value} ($planet->{valuerank}), XP=$planet->{xp} ($planet->{xprank})");
+               $c->reply("$planet->{coords} $planet->{ruler} OF $planet->{planet},$ally Race=$planet->{race}, Score=$planet->{score} ($planet->{scorerank}), Size=$planet->{size} ($planet->{sizerank}), Value=$planet->{value} ($planet->{valuerank}), XP=$planet->{xp} ($planet->{xprank}) TAGS: $planet->{tags}");
        }else{
                $c->reply("Couldn't find planet: $msg");
        }
index 24048fb29ad60a3cb197c39e3600f2dbaf5c3554..b649f480aaa81559309d86dfb25556a0afffc288 100644 (file)
@@ -37,6 +37,9 @@ INSERT INTO roles VALUES('irc_getcalc');
 INSERT INTO roles VALUES('irc_sendsms');
 INSERT INTO roles VALUES('irc_smsstatus');
 INSERT INTO roles VALUES('irc_forum_others');
+INSERT INTO roles VALUES('irc_newtag');
+INSERT INTO roles VALUES('irc_addtag');
+INSERT INTO roles VALUES('irc_deltag');
 
 INSERT INTO group_roles (gid,role) VALUES('T','irc_p_nick');
 INSERT INTO group_roles (gid,role) VALUES('T','irc_p_intel');
@@ -78,6 +81,9 @@ INSERT INTO group_roles (gid,role) VALUES('T','irc_forum_others');
 
 INSERT INTO group_roles (gid,role) VALUES('M','irc_gs');
 INSERT INTO group_roles (gid,role) VALUES('M','irc_scan');
+INSERT INTO group_roles (gid,role) VALUES('M','irc_newtag');
+INSERT INTO group_roles (gid,role) VALUES('M','irc_addtag');
+INSERT INTO group_roles (gid,role) VALUES('M','irc_deltag');
 
 INSERT INTO group_roles (gid,role) VALUES('H','irc_p_nick');
 INSERT INTO group_roles (gid,role) VALUES('H','irc_masterop');