Skip to content

Commit

Permalink
convert single to find or first.
Browse files Browse the repository at this point in the history
  • Loading branch information
lemming552 committed Dec 3, 2014
1 parent 141026a commit 2db06ce
Show file tree
Hide file tree
Showing 63 changed files with 241 additions and 280 deletions.
4 changes: 2 additions & 2 deletions bin/clean_up_empires.pl
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@

out('Updating Viral Log');
my $viral_log = $db->resultset('Lacuna::DB::Result::Log::Viral');
my $add_deletes = $viral_log->search({date_stamp => format_date($start,'%F')},{rows=>1})->single;
my $add_deletes = $viral_log->search({date_stamp => format_date($start,'%F')})->first;
unless (defined $add_deletes) {
$add_deletes = $viral_log->new({date_stamp => format_date($start,'%F')})->insert;
}
Expand All @@ -98,7 +98,7 @@
});
my $cache = Lacuna->cache;
my $create_date = format_date($start->clone->subtract(hours => 1),'%F');
my $add_creates = $viral_log->search({date_stamp => $create_date},{rows=>1})->single;
my $add_creates = $viral_log->search({date_stamp => $create_date})->first;
unless (defined $add_deletes) {
$add_creates = $viral_log->new({date_stamp => $create_date})->insert;
}
Expand Down
2 changes: 1 addition & 1 deletion bin/cult/add_colonies.pl
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
my $test;
do {
$test = $db->resultset('Lacuna::DB::Result::Map::Body')->search({
name => $pname })->single;
name => $pname })->first;
if ($test) {
$orbit++;
$pname = $planet->star->name." ".$orbit;
Expand Down
4 changes: 2 additions & 2 deletions bin/delambert/add_colonies.pl
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@
class => {-like => ['%Planet::GasGiant::G%','%:Planet::P%']},
-and => [size => {'>=' => $sizes->{$level}[0]}, size => {'<' => $sizes->{$level}[1] }],
orbit => {'<' => 8},
},{rows=>1})->single;
})->first;
next STAR if not $body;
out("Putting colony in star system ".$star->name);
last STAR;
Expand Down Expand Up @@ -240,7 +240,7 @@ sub create_empire {
size => { '>=' => 110},
zone => $zone,
empire_id => undef,
},{rows=>1})->single;
})->first;

$empire->insert;
$home->delete_buildings(@{$home->building_cache});
Expand Down
2 changes: 1 addition & 1 deletion bin/draw_star_map.pl
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
for (my $y = 15; $y > -15; $y--) {
foreach (my $x = 15; $x > -15; $x--) {
my $star = $stars->search({x=>$x, y=>$y})->count;
my $body = $bodies->search({x=>$x, y=>$y},{rows=>1})->single;
my $body = $bodies->search({x=>$x, y=>$y})->first;
if ($star) {
print "*";
}
Expand Down
4 changes: 2 additions & 2 deletions bin/lacuna.psgi
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ my $app = builder {
enable "Auth::Basic", authenticator => sub {
my ($username, $password) = @_;
return 0 unless $username;
my $empire = Lacuna->db->resultset('Lacuna::DB::Result::Empire')->search({name => $username, is_admin => 1},{rows=>1})->single;
my $empire = Lacuna->db->resultset('Lacuna::DB::Result::Empire')->search({name => $username, is_admin => 1})->first;
return 0 unless defined $empire;
return $empire->is_password_valid($password);
};
Expand All @@ -254,7 +254,7 @@ my $app = builder {
enable "Auth::Basic", authenticator => sub {
my ($username, $password) = @_;
return 0 unless $username;
my $empire = Lacuna->db->resultset('Lacuna::DB::Result::Empire')->search({name => $username, is_mission_curator => 1},{rows=>1})->single;
my $empire = Lacuna->db->resultset('Lacuna::DB::Result::Empire')->search({name => $username, is_mission_curator => 1})->first;
return 0 unless defined $empire;
return $empire->is_password_valid($password);
};
Expand Down
2 changes: 1 addition & 1 deletion bin/mission_check.pl
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
out('Loading DB');
our $db = Lacuna->db;

my $emp = $db->resultset('Empire')->search({id => $empire_id})->single;
my $emp = $db->resultset('Empire')->find($empire_id);

die "No empire with id:$empire_id found\n" unless $emp;

Expand Down
5 changes: 2 additions & 3 deletions bin/saben/create_colony.pl
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,8 @@

out('Finding colony...');
my $body = $db->resultset('Lacuna::DB::Result::Map::Body')->search(
{ zone => $target_player->home_planet->zone, empire_id => undef, size => { between => [30,35]}},
{ rows => 1 }
)->single;
{ zone => $target_player->home_planet->zone, empire_id => undef, size => { between => [30,35]}}
)->first;
die 'Could not find a colony to occupy.' unless defined $body;
say $body->name;

Expand Down
5 changes: 2 additions & 3 deletions bin/saben/create_colony2.pl
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,8 @@
my $bodies = $db->resultset('Lacuna::DB::Result::Map::Body');
my $target_home = $target_player->home_planet;
my $body = $bodies->search(
{ x => {between => [ $target_home->x - 25, $target_home->x + 25 ]}, y => { between => [ $target_home->y - 25, $target_home->y + 25 ] }, empire_id => undef, size => { between => [30,35]}},
{ rows => 1 }
)->single;
{ x => {between => [ $target_home->x - 25, $target_home->x + 25 ]}, y => { between => [ $target_home->y - 25, $target_home->y + 25 ] }, empire_id => undef, size => { between => [30,35]}}
)->first;
die 'Could not find a colony to occupy.' unless defined $body;
say $body->name;

Expand Down
5 changes: 2 additions & 3 deletions bin/saben/create_colony3.pl
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,8 @@
my $bodies = $db->resultset('Lacuna::DB::Result::Map::Body');
my $target_home = $target_player->home_planet;
my $body = $bodies->search(
{ x => {between => [ $target_home->x - 25, $target_home->x + 25 ]}, y => { between => [ $target_home->y - 25, $target_home->y + 25 ] }, empire_id => undef, size => { between => [30,35]}},
{ rows => 1 }
)->single;
{ x => {between => [ $target_home->x - 25, $target_home->x + 25 ]}, y => { between => [ $target_home->y - 25, $target_home->y + 25 ] }, empire_id => undef, size => { between => [30,35]}}
)->first;
die 'Could not find a colony to occupy.' unless defined $body;
say $body->name;

Expand Down
2 changes: 1 addition & 1 deletion bin/saben/create_empire.pl
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
out('Find home planet...');
my $bodies = $db->resultset('Lacuna::DB::Result::Map::Body');
my $zone = $bodies->get_column('zone')->max;
my $home = $bodies->search({size => 35, zone => $zone },{rows=>1})->single;
my $home = $bodies->search({size => 35, zone => $zone })->first;
$empire->insert;
$empire->found($home);

Expand Down
2 changes: 1 addition & 1 deletion bin/saben/orig_send_attack.pl
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ sub start_attack {
);
return $attack, $timer;
}
my $probe = $ships->search({body_id => $saben_colony->id, type => 'probe', task=>'Docked'},{rows => 1})->single;
my $probe = $ships->search({body_id => $saben_colony->id, type => 'probe', task=>'Docked'})->first;
if (defined $probe) {
out('Has a probe to launch for '.$target_colony->name.'...');
$probe->send(target => $target_colony->star);
Expand Down
4 changes: 2 additions & 2 deletions bin/summarize_economy.pl
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,12 @@
sub summarize {
my $date = shift;
out('Summarizing Economy For '.$date->ymd);
my $today = $economy_log->search({date_stamp => $date->ymd},{rows=>1})->single;
my $today = $economy_log->search({date_stamp => $date->ymd})->first;
if (defined $today) {
$today->delete;
}
$today = $economy_log->new({ date_stamp => $date->ymd });
my $viral = $viral_log->search({date_stamp => $date->ymd},{rows=>1})->single;
my $viral = $viral_log->search({date_stamp => $date->ymd})->first;
if (defined $viral) {
$today->total_users($viral->total_users);
}
Expand Down
6 changes: 3 additions & 3 deletions bin/summarize_server.pl
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ sub summarize_alliances {
$alliance_data{offense_success_rate} /= $alliance_data{member_count};
$alliance_data{defense_success_rate} /= $alliance_data{member_count};
}
my $log = $logs->search({alliance_id => $alliance->id},{rows=>1})->single;
my $log = $logs->search({alliance_id => $alliance->id})->first;
if (defined $log) {
if ($alliance_data{member_count}) {
$log->update(\%alliance_data);
Expand Down Expand Up @@ -367,7 +367,7 @@ sub summarize_empires {
{defense_success_rate} = $empire_data{defense_success_rate} / $empire_data{colony_count};
}
$empire_data{empire_size} = $empire_data{colony_count} * $empire_data{population};
my $log = $logs->search({empire_id => $empire->id},{rows=>1})->single;
my $log = $logs->search({empire_id => $empire->id})->first;
if (defined $log) {
$empire_data{colony_count_delta} = $empire_data{colony_count} - $log->colony_count + $log->colony_count_delta;
$empire_data{empire_size_delta} = ($empire_data{colony_count_delta}) ? $empire_data{colony_count_delta} * $empire_data{population_delta} : $empire_data{population_delta};
Expand Down Expand Up @@ -423,7 +423,7 @@ sub summarize_colonies {
my $spy_logs = $db->resultset('Log::Spies');
while (my $planet = $planets->next) {
out($planet->name);
my $log = $logs->search({planet_id => $planet->id},{rows=>1})->single;
my $log = $logs->search({planet_id => $planet->id})->first;
my %colony_data = (
date_stamp => DateTime->now,
planet_name => $planet->name,
Expand Down
2 changes: 1 addition & 1 deletion bin/summarize_spies.pl
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ sub summarize_spies {
my $logs = $db->resultset('Lacuna::DB::Result::Log::Spies');
while (my $spy = $spies->next) {
out($spy->id.":".$spy->name);
my $log = $logs->search({ spy_id => $spy->id },{ rows => 1 } )->single;
my $log = $logs->search({ spy_id => $spy->id } )->first;
my $offense_success_rate = ($spy->offense_mission_count) ? 100 * $spy->offense_mission_successes / $spy->offense_mission_count : 0;
my $defense_success_rate = ($spy->defense_mission_count) ? 100 * $spy->defense_mission_successes / $spy->defense_mission_count : 0;
my $success_rate = $offense_success_rate + $defense_success_rate;
Expand Down
8 changes: 4 additions & 4 deletions bin/trelvestian/add_colonies.pl
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,10 @@
my @colonies;

if ($server_url =~ /us2/) {
push @colonies, $viable->search({ x => { '>' => 150}, y => { '>' => 150} },{rows=>1})->single;
push @colonies, $viable->search({ x => { '<' => -150}, y => { '>' => 150} },{rows=>1})->single;
push @colonies, $viable->search({ x => { '<' => -150}, y => { '<' => -150} },{rows=>1})->single;
push @colonies, $viable->search({ x => { '>' => 150}, y => { '<' => -150} },{rows=>1})->single;
push @colonies, $viable->search({ x => { '>' => 150}, y => { '>' => 150} })->first;
push @colonies, $viable->search({ x => { '<' => -150}, y => { '>' => 150} })->first;
push @colonies, $viable->search({ x => { '<' => -150}, y => { '<' => -150} })->first;
push @colonies, $viable->search({ x => { '>' => 150}, y => { '<' => -150} })->first;
}
elsif ($server_url =~ /us1/) {
# four planets in 2|2
Expand Down
4 changes: 2 additions & 2 deletions bin/util/move_plans_glyphs.pl
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@
out('Loading DB');
our $db = Lacuna->db;

my $body_from = $db->resultset('Map::Body')->search({id => $from})->single;
my $body_dest = $db->resultset('Map::Body')->search({id => $to})->single;
my $body_from = $db->resultset('Map::Body')->find($from);
my $body_dest = $db->resultset('Map::Body')->find($to);

out(sprintf("%30s -> %30s", $body_from->name, $body_dest->name));

Expand Down
2 changes: 1 addition & 1 deletion bin/util/revamp_map.pl
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
my @habital_types = qw(P1 P2 P3 P4 P5 P6 P7 P8 P9 P10 P11 P12 P13 P14 P15 P16 P17 P18 P19 P20 P21 P22 P23 P24 P25 P26 P27 P28 P29 P30 P31 P32 P33 P34 P35 P36 P37 P38 P39 P40);
my @gas_giant_types = qw(G1 G2 G3 G4 G5);

my $revamp_x = $db_config->search({name => 'revamp_x'})->single;
my $revamp_x = $db_config->search({name => 'revamp_x'})->first;
my $x_min = int($config->get('map_size/x')->[0]);
my $x_max = int($config->get('map_size/x')->[1]);

Expand Down
4 changes: 2 additions & 2 deletions bin/util/seed_fissures.pl
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@
while ($placed < $number) {
my $target = Lacuna->db->resultset('Lacuna::DB::Result::Map::Body')->search(
$search,
{rows => 1, order_by => 'rand()' }
)->single;
{ order_by => 'rand()' }
)->first;
unless (defined $target) {
print "No body found\n";
$placed++;
Expand Down
4 changes: 2 additions & 2 deletions bin/util/swap_bodies.pl
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@
out('Loading DB');
our $db = Lacuna->db;

my $body_from = $db->resultset('Map::Body')->search({id => $from})->single;
my $body_dest = $db->resultset('Map::Body')->search({id => $to})->single;
my $body_from = $db->resultset('Map::Body')->find($from);
my $body_dest = $db->resultset('Map::Body')->find($to);

out(sprintf("%s at %d/%d -> %s at %d/%d",
$body_from->name, $body_from->x, $body_from->y,
Expand Down
38 changes: 19 additions & 19 deletions bin/weekly_medals.pl
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ sub colonies {
my $colonies = $db->resultset('Lacuna::DB::Result::Log::Colony');

# fastest growing
my $colony = $colonies->search(undef, {order_by => [{ -desc => 'population_delta'},'rand()'], rows => 1})->single;
my $colony = $colonies->search(undef, {order_by => [{ -desc => 'population_delta'},'rand()']})->first;
add_medal($colony->empire_id,'fastest_growing_colony');

# largest
$colony = $colonies->search(undef, {order_by => [{ -desc => 'population'},'rand()'], rows => 1})->single;
$colony = $colonies->search(undef, {order_by => [{ -desc => 'population'},'rand()']})->first;
add_medal($colony->empire_id,'largest_colony');

# reset deltas
Expand All @@ -57,35 +57,35 @@ sub empires {
my $empires = $db->resultset('Lacuna::DB::Result::Log::Empire');

# best attacker in the game
my $empire_log = $empires->search(undef, {order_by => [{ -desc => 'offense_success_rate'},'rand()'], rows => 1})->single;
my $empire_log = $empires->search(undef, {order_by => [{ -desc => 'offense_success_rate'},'rand()']})->first;
add_medal($empire_log->empire_id,'best_attacker_in_the_game');

# best attacker of the week
$empire_log = $empires->search(undef, {order_by => [{ -desc => 'offense_success_rate_delta'},'rand()'], rows => 1})->single;
$empire_log = $empires->search(undef, {order_by => [{ -desc => 'offense_success_rate_delta'},'rand()']})->first;
add_medal($empire_log->empire_id, 'best_attacker_of_the_week');

# best defender in the game
$empire_log = $empires->search(undef, {order_by => [{ -desc => 'defense_success_rate'},'rand()'], rows => 1})->single;
$empire_log = $empires->search(undef, {order_by => [{ -desc => 'defense_success_rate'},'rand()']})->first;
add_medal($empire_log->empire_id, 'best_defender_in_the_game');

# best defender of the week
$empire_log = $empires->search(undef, {order_by => [{ -desc => 'defense_success_rate_delta'},'rand()'], rows => 1})->single;
$empire_log = $empires->search(undef, {order_by => [{ -desc => 'defense_success_rate_delta'},'rand()']})->first;
add_medal($empire_log->empire_id,'best_defender_of_the_week');

# dirtiest player in the game
$empire_log = $empires->search(undef, {order_by => [{ -desc => 'dirtiest'},'rand()'], rows => 1})->single;
$empire_log = $empires->search(undef, {order_by => [{ -desc => 'dirtiest'},'rand()']})->first;
add_medal($empire_log->empire_id,'dirtiest_empire_in_the_game');

# dirtiest player of the week
$empire_log = $empires->search(undef, {order_by => [{ -desc => 'dirtiest_delta'},'rand()'], rows => 1})->single;
$empire_log = $empires->search(undef, {order_by => [{ -desc => 'dirtiest_delta'},'rand()']})->first;
add_medal($empire_log->empire_id,'dirtiest_empire_of_the_week');

# fastest growing empire
$empire_log = $empires->search(undef, {order_by => [{ -desc => 'empire_size_delta'},'rand()'], rows => 1})->single;
$empire_log = $empires->search(undef, {order_by => [{ -desc => 'empire_size_delta'},'rand()']})->first;
add_medal($empire_log->empire_id,'fastest_growing_empire');

# largest empire
$empire_log = $empires->search(undef, {order_by => [{ -desc => 'empire_size'},'rand()'], rows => 1})->single;
$empire_log = $empires->search(undef, {order_by => [{ -desc => 'empire_size'},'rand()']})->first;
add_medal($empire_log->empire_id,'largest_empire');

# reset deltas
Expand All @@ -98,39 +98,39 @@ sub spies {
my $spies = $db->resultset('Lacuna::DB::Result::Log::Spies');

# best in the game
my $spy = $spies->search(undef,{order_by => [{ -desc => 'success_rate'}, 'rand()'], rows=>1})->single;
my $spy = $spies->search(undef,{order_by => [{ -desc => 'success_rate'}, 'rand()']})->first;
add_medal($spy->empire_id,'best_spy_in_the_game');

# best of the week
$spy = $spies->search(undef,{order_by => [{ -desc => 'success_rate_delta'},'rand()'], rows=>1})->single;
$spy = $spies->search(undef,{order_by => [{ -desc => 'success_rate_delta'},'rand()']})->first;
add_medal($spy->empire_id,'best_spy_of_the_week');

# best offender in the game
$spy = $spies->search(undef,{order_by => [{ -desc => 'offense_success_rate'},'rand()'], rows=>1})->single;
$spy = $spies->search(undef,{order_by => [{ -desc => 'offense_success_rate'},'rand()']})->first;
add_medal($spy->empire_id,'best_offensive_spy_in_the_game');

# best offender of the week
$spy = $spies->search(undef,{order_by => [{ -desc => 'offense_success_rate_delta'},'rand()'], rows=>1})->single;
$spy = $spies->search(undef,{order_by => [{ -desc => 'offense_success_rate_delta'},'rand()']})->first;
add_medal($spy->empire_id,'best_offensive_spy_of_the_week');

# best defender in the game
$spy = $spies->search(undef,{order_by => [{ -desc => 'defense_success_rate'},'rand()'], rows=>1})->single;
$spy = $spies->search(undef,{order_by => [{ -desc => 'defense_success_rate'},'rand()']})->first;
add_medal($spy->empire_id,'best_defensive_spy_in_the_game');

# best defender of the week
$spy = $spies->search(undef,{order_by => [{ -desc => 'defense_success_rate_delta'},'rand()'], rows=>1})->single;
$spy = $spies->search(undef,{order_by => [{ -desc => 'defense_success_rate_delta'},'rand()']})->first;
add_medal($spy->empire_id,'best_defensive_spy_of_the_week');

# dirtiest in the game
$spy = $spies->search(undef,{order_by => [{ -desc => 'dirtiest'},'rand()'], rows=>1})->single;
$spy = $spies->search(undef,{order_by => [{ -desc => 'dirtiest'},'rand()']})->first;
add_medal($spy->empire_id,'dirtiest_spy_in_the_game');

# dirtiest of the week
$spy = $spies->search(undef,{order_by => [{ -desc => 'dirtiest_delta'},'rand()'], rows=>1})->single;
$spy = $spies->search(undef,{order_by => [{ -desc => 'dirtiest_delta'},'rand()']})->first;
add_medal($spy->empire_id,'dirtiest_spy_of_the_week');

# most improved of the week
$spy = $spies->search(undef,{order_by => [{ -desc => 'level_delta'},'rand()'], rows=>1})->single;
$spy = $spies->search(undef,{order_by => [{ -desc => 'level_delta'},'rand()']})->first;
add_medal($spy->empire_id,'most_improved_spy_of_the_week');

# reset deltas
Expand Down
8 changes: 4 additions & 4 deletions lib/Lacuna/AI.pm
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ has scratch => (

sub next_viable_colony {
my $self = shift;
return $self->viable_colonies->search(undef, { rows => 1, order_by => 'rand()' })->single;
return $self->viable_colonies->search(undef, { order_by => 'rand()' })->first;
}

sub create_empire {
Expand All @@ -57,8 +57,8 @@ sub create_empire {
my $db = Lacuna->db;
my $empire = $db->resultset('Lacuna::DB::Result::Empire')->new(\%attributes)->insert;
# my $zone = $db->resultset('Lacuna::DB::Result::Map::Body')->get_column('zone')->max;
# my $home = $self->viable_colonies->search({zone => $zone},{rows=>1})->single;
my $home = $self->viable_colonies->search(undef,{rows=>1})->single;
# my $home = $self->viable_colonies->search({zone => $zone})->first;
my $home = $self->viable_colonies->search(undef,{ order_by => 'rand()'})->first;
my @to_demolish = @{$home->building_cache};
$home->delete_buildings(\@to_demolish);
$empire->found($home);
Expand Down Expand Up @@ -560,7 +560,7 @@ sub start_attack {
say ' Has one at star already...';
$seconds = 1;
}
my $probe = $db->resultset('Lacuna::DB::Result::Ships')->search({body_id => $attacking_colony->id, type => 'probe', task=>'Docked'},{rows => 1})->single;
my $probe = $db->resultset('Lacuna::DB::Result::Ships')->search({body_id => $attacking_colony->id, type => 'probe', task=>'Docked'})->first;
if (defined $probe and $seconds == 0) {
say ' Has a probe to launch for '.$target_colony->name.'...';
$probe->send(target => $target_colony->star);
Expand Down
2 changes: 1 addition & 1 deletion lib/Lacuna/DB/Result.pm
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ sub new {

sub random {
my $self = shift;
return $self->search( undef, { rows => 1, order_by => 'rand()' })->single;
return $self->search( undef, { order_by => 'rand()' })->first;
}


Expand Down
2 changes: 1 addition & 1 deletion lib/Lacuna/DB/Result/Building/Archaeology.pm
Original file line number Diff line number Diff line change
Expand Up @@ -689,7 +689,7 @@ sub make_plan {
my $glyph = Lacuna->db->resultset('Lacuna::DB::Result::Glyph')->search({
type => $type,
body_id => $self->body_id,
})->single;
})->first;
unless (defined($glyph)) {
confess [ 1002, "You don't have any glyphs of type $type."];
}
Expand Down
Loading

0 comments on commit 2db06ce

Please sign in to comment.