forked from plainblack/Lacuna-Server-Open
-
Notifications
You must be signed in to change notification settings - Fork 0
/
add_missions.pl
102 lines (85 loc) · 2.56 KB
/
add_missions.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
use 5.010;
use strict;
use lib '/data/Lacuna-Server/lib';
use Lacuna::DB;
use Lacuna;
use Lacuna::Util qw(randint format_date);
use Getopt::Long;
use List::Util qw(shuffle);
$|=1;
our $quiet;
our $mission;
our $zone;
GetOptions(
'quiet' => \$quiet,
'mission=s' => \$mission,
'zone=s' => \$zone,
);
out('Started');
my $start = time;
out('Finding Mission Files');
my @mission_files = get_mission_files();
out('Loading DB...');
our $db = Lacuna->db;
our $missions = $db->resultset('Lacuna::DB::Result::Mission');
out('Deleting missions nobody has completed...');
my $old = $missions->search({date_posted => { '<' => DateTime->now->subtract( hours => 72 )}});
while (my $mission = $old->next) {
$mission->incomplete;
}
if ($mission ne '' && $zone ne '') {
out('Adding specific mission...');
my $mission = Lacuna::DB::Result::Mission->initialize($zone, $mission);
say $mission->params->get('name').' added to '.$zone.'!';
}
else {
out('Adding missions...');
out(scalar @mission_files. " mission files");
my @zones = $db->resultset('Lacuna::DB::Result::Map::Body')->search(
{ empire_id => { '>' => 0 }},
{ distinct => 1 })->get_column('zone')->all;
foreach my $zone (@zones) {
out($zone);
my $current_missions = $db->resultset('Lacuna::DB::Result::Mission')->search(
{ zone => "$zone" });
my @current;
while (my $mission = $current_missions->next) {
push @current, $mission->mission_file_name;
}
out(scalar @current. " current missions");
my @avail;
for my $mission (@mission_files) {
push @avail, $mission unless ( grep { $mission eq $_ } @current);
}
out(scalar @avail. " to pick from");
foreach (1..3) {
last unless scalar @avail > 0;
my $mission_file = splice(@avail, rand(@avail), 1);
my $mission = Lacuna::DB::Result::Mission->initialize($zone, $mission_file);
say $mission->params->get('name');
}
}
}
my $finish = time;
out('Finished');
out((($finish - $start)/60)." minutes have elapsed");
###############
## SUBROUTINES
###############
sub get_mission_files {
opendir my $dir, '/data/Lacuna-Mission/missions/';
my @files = readdir $dir;
closedir $dir;
my @missions;
foreach my $file (@files) {
next unless $file =~ m/\.mission$/;
push @missions, $file;
}
return @missions;
}
sub out {
my $message = shift;
unless ($quiet) {
say format_date(DateTime->now), " ", $message;
}
}