From 784ed86eacf5ebed4d3da2ff166b7542ac8f5cd0 Mon Sep 17 00:00:00 2001 From: Michael Andreen Date: Tue, 25 Aug 2009 13:38:17 +0200 Subject: [PATCH] Planet tags, for attaching random information to planets --- Commands/Intel.pm | 79 ++++++++++++++++++++++++++++++++++++++++++++++ Commands/PA.pm | 21 +++++++++--- database/roles.sql | 6 ++++ 3 files changed, 101 insertions(+), 5 deletions(-) diff --git a/Commands/Intel.pm b/Commands/Intel.pm index b9c00e9..b52d694 100644 --- a/Commands/Intel.pm +++ b/Commands/Intel.pm @@ -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("$tag already exists"); + return; + }elsif ($@){ + die $@; + } + $c->reply("Added tag $tag"); +} + +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 $tag to $x:$y:$z"); + }when (/duplicate key value/){ + $c->reply("$x:$y:$z already has tag $tag"); + }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("$tag 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 $rows matching $tag from $x:$y:$z"); +} + 1; diff --git a/Commands/PA.pm b/Commands/PA.pm index 52fb3a0..7fb453d 100644 --- a/Commands/PA.pm +++ b/Commands/PA.pm @@ -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"); } diff --git a/database/roles.sql b/database/roles.sql index 24048fb..b649f48 100644 --- a/database/roles.sql +++ b/database/roles.sql @@ -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'); -- 2.39.2