From 50d1c7c47afdc33ba31adb9bed4025e3a700f48d Mon Sep 17 00:00:00 2001 From: Iain C Docherty Date: Fri, 12 Apr 2013 19:33:56 -0400 Subject: [PATCH] More efficient starlite, cache for stations and alliances --- lib/Lacuna/DB/Result/Map/StarLite.pm | 23 ++++++++++---- lib/Lacuna/DB/ResultSet/Map/StarLite.pm | 38 ++++++++++++++++++++++- t/540_star_lite.t | 41 +------------------------ 3 files changed, 55 insertions(+), 47 deletions(-) diff --git a/lib/Lacuna/DB/Result/Map/StarLite.pm b/lib/Lacuna/DB/Result/Map/StarLite.pm index 85e42d00..8d5499df 100644 --- a/lib/Lacuna/DB/Result/Map/StarLite.pm +++ b/lib/Lacuna/DB/Result/Map/StarLite.pm @@ -21,6 +21,7 @@ __PACKAGE__->add_columns( star_x => { data_type => 'int', size => 11, default_value => 0 }, star_y => { data_type => 'int', size => 11, default_value => 0 }, star_zone => { data_type => 'varchar', size => 16, is_nullable => 0 }, + station_id => { data_type => 'int', size => 11}, body_id => { data_type => 'int', size => 11}, body_name => { data_type => 'varchar', size => 30, is_nullable => 0 }, body_orbit => { data_type => 'int', default_value => 0 }, @@ -31,7 +32,8 @@ __PACKAGE__->add_columns( empire_id => { data_type => 'int', size => 11}, empire_name => { data_type => 'varchar', size => 30, is_nullable => 0 }, empire_is_isolationist => { data_type => 'tinyint', default_value => 1 }, - body_has_fissure => { data_type => 'int', size => 11}, + empire_alliance_id => { data_type => 'int', size => 11 }, + body_has_fissure => { data_type => 'int', size => 11 }, ); __PACKAGE__->result_source_instance->is_virtual(1); @@ -44,6 +46,7 @@ __PACKAGE__->result_source_instance->view_definition(q[ star.x AS star_x, star.y AS star_y, star.zone as star_zone, + star.station_id AS station_id, body.id AS body_id, body.name AS body_name, body.orbit AS body_orbit, @@ -54,6 +57,7 @@ __PACKAGE__->result_source_instance->view_definition(q[ empire.id AS empire_id, empire.name AS empire_name, empire.is_isolationist AS empire_is_isolationist, + empire.alliance_id AS empire_alliance_id, building.id AS body_has_fissure from star LEFT JOIN probes @@ -64,25 +68,32 @@ __PACKAGE__->result_source_instance->view_definition(q[ ON body.empire_id = empire.id LEFT JOIN building ON body.id = building.body_id - AND building.class='Lacuna::DB::Result::Building::Permanent::Ravine' - WHERE star.x > ? + AND building.class='Lacuna::DB::Result::Building::Permanent::Fissure' + WHERE star.x >= ? AND star.x < ? - AND star.y > ? + AND star.y >= ? AND star.y < ? ORDER BY star.id ]); # bind variables are alliance_id,empire_id,left,right,bottom,top -# get the planet image name +__PACKAGE__->belongs_to('station', 'Lacuna::DB::Result::Map::Body', 'station_id'); + +# get the planet image name +# NOTE: This is not good, since image name generation is now duplicated. +# to 'fix' this would need significant refactoring of the way body images and +# types are calculated +# sub body_image { my ($self) = @_; my ($image) = $self->body_class =~ m/:(\w+)$/; + $image =~ s/A21/debris1/; $image =~ s/G/pg/; $image =~ s/P/p/; $image =~ s/A/a/; $image =~ s/Station/station/; - return $image; + return $image.'-'.$self->body_orbit; } sub body_type { diff --git a/lib/Lacuna/DB/ResultSet/Map/StarLite.pm b/lib/Lacuna/DB/ResultSet/Map/StarLite.pm index 2b5df0fe..165f081f 100644 --- a/lib/Lacuna/DB/ResultSet/Map/StarLite.pm +++ b/lib/Lacuna/DB/ResultSet/Map/StarLite.pm @@ -3,6 +3,7 @@ package Lacuna::DB::ResultSet::Map::StarLite; use Moose; use utf8; no warnings qw(uninitialized); +use Lacuna; extends 'Lacuna::DB::ResultSet'; @@ -11,7 +12,11 @@ extends 'Lacuna::DB::ResultSet'; sub get_star_map { my ($self, $alliance_id, $empire_id, $left, $right, $bottom, $top) = @_; - my $rs = $self->search({}, { bind => [$alliance_id, $empire_id, $left, $right, $bottom, $top] }); + my $cache = Lacuna->cache; + + my $rs = $self->search({}, { + bind => [$alliance_id, $empire_id, $left, $right, $bottom, $top], + }); my $star_id=0; my $star; @@ -32,6 +37,24 @@ sub get_star_map { $star_id = $row->star_id; } + if (defined $row->station_id) { + my $station = $cache->get_and_deserialize('starlite_station',$row->station_id); + if (not $station) { + my $station = { + id => $row->station->id, + name => $row->station->name, + x => $row->station->x, + y => $row->station->y, + alliance => { + name => $row->station->alliance->name, + id => $row->station->alliance_id, + } + }; + # set the expiry to 1hr + $cache->set('starlite_station',$row->station_id, $station, 60 * 60); + } + $star->{station} = $station; + } if (defined $row->body_id) { my $body = { name => $row->body_name, @@ -42,7 +65,20 @@ sub get_star_map { type => $row->body_type, image => $row->body_image, size => $row->body_size, + body_has_fissure => $row->body_has_fissure ? 1 : 0, }; + if (defined $row->empire_id) { + my $alignment = 'hostile'; + $alignment = 'ally' if $row->empire_alliance_id == $alliance_id; + $alignment = 'self' if $row->empire_id == $empire_id; + my $empire = { + id => $row->empire_id, + name => $row->empire_name, + alignment => $alignment, + is_isolationist => $row->empire_is_isolationist, + }; + $body->{empire} = $empire; + } push @{$star->{bodies}}, $body; } } diff --git a/t/540_star_lite.t b/t/540_star_lite.t index 2450115d..a6092356 100644 --- a/t/540_star_lite.t +++ b/t/540_star_lite.t @@ -14,47 +14,8 @@ my $db = Lacuna->db; #); -my $starmap = $db->resultset('Map::StarLite')->get_star_map(0, 422, -10, 10, -10, 10); +my $starmap = $db->resultset('Map::StarLite')->get_star_map(55,1015,491,500,317,360); diag(Dumper($starmap)); -exit; - -my $rs; -my $star_id=0; -my $star; -my @out; -while (my $row = $rs->next) { - if ($row->star_id != $star_id) { - if ($star_id) { - push @out, $star; - } - $star = { - name => $row->star_name, - color => $row->star_color, - x => $row->star_x, - y => $row->star_y, - id => $row->star_id, - bodies => [], - }; - - $star_id = $row->star_id; - } - if (defined $row->body_id) { - my $body = { - name => $row->body_name, - id => $row->body_id, - orbit => $row->body_orbit, - x => $row->body_x, - y => $row->body_y, - type => $row->body_type, - image => $row->body_image, - size => $row->body_size, - }; - push @{$star->{bodies}}, $body; - } -} -push @out, $star; - -diag(Dumper(\@out)); ok(1); -- 2.51.2