/
SpacePort.pm
1301 lines (1194 loc) · 47.2 KB
/
SpacePort.pm
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package Lacuna::RPC::Building::SpacePort;
use Moose;
use utf8;
no warnings qw(uninitialized);
extends 'Lacuna::RPC::Building';
use Lacuna::Constants qw(SHIP_TYPES);
use Lacuna::Util qw(format_date);
use Data::Dumper;
use feature "switch";
sub app_url {
return '/spaceport';
}
sub model_class {
return 'Lacuna::DB::Result::Building::SpacePort';
}
sub find_target {
my ($self, $target_params) = @_;
unless (ref $target_params eq 'HASH') {
confess [-32602, 'The target parameter should be a hash reference. For example { "star_id" : 9999 }.'];
}
my $target;
if (exists $target_params->{star_id}) {
$target = Lacuna->db->resultset('Lacuna::DB::Result::Map::Star')->find($target_params->{star_id});
}
elsif (exists $target_params->{star_name}) {
$target = Lacuna->db->resultset('Lacuna::DB::Result::Map::Star')->search({ name => $target_params->{star_name} })->first;
}
if (exists $target_params->{body_id}) {
$target = Lacuna->db->resultset('Lacuna::DB::Result::Map::Body')->find($target_params->{body_id});
}
elsif (exists $target_params->{body_name}) {
$target = Lacuna->db->resultset('Lacuna::DB::Result::Map::Body')->search({ name => $target_params->{body_name} })->first;
}
elsif (exists $target_params->{x}) {
$target = Lacuna->db->resultset('Lacuna::DB::Result::Map::Body')->search({ x => $target_params->{x}, y => $target_params->{y} })->first;
unless (defined $target) {
$target = Lacuna->db->resultset('Lacuna::DB::Result::Map::Star')->search({ x => $target_params->{x}, y => $target_params->{y} })->first;
}
}
unless (defined $target) {
confess [ 1002, 'Could not find the target.', $target];
}
return $target;
}
sub get_fleet_for {
my ($self, $session_id, $body_id, $target_params) = @_;
my $empire = $self->get_empire_by_session($session_id);
my $body = $self->get_body($empire, $body_id);
my $target = $self->find_target($target_params);
my $berth = Lacuna->db->resultset('Lacuna::DB::Result::Building')->search( {
class => 'Lacuna::DB::Result::Building::SpacePort',
body_id => $body_id,
efficiency => 100,
} )->get_column('level')->max;
my $ships = Lacuna->db->resultset('Lacuna::DB::Result::Ships')->search(
{
body_id => $body->id,
task => 'docked',
},{
'+select' => [{count => 'id'}],
'+as' => [ qw(quantity) ],
group_by => [ qw(type speed stealth combat hold_size name) ],
},
);
my $summary;
while (my $ship_group = $ships->next) {
eval{ $ship_group->can_send_to_target($target) };
my $reason = $@;
next if $reason;
my $travel_time = Lacuna::DB::Result::Ships->travel_time($body,$target,$ship_group->speed);
my $type_human = Lacuna::DB::Result::Ships->type_human($ship_group->type);
my $summation = {
type => $ship_group->type,
type_human => $type_human,
speed => int($ship_group->speed),
stealth => int($ship_group->stealth),
combat => int($ship_group->combat),
name => $ship_group->name,
hold_size => $ship_group->hold_size,
quantity => $ship_group->get_column('quantity'),
estimated_travel_time => $travel_time,
};
push @$summary, $summation;
}
my %out = (
status => $self->format_status($empire, $body),
ships => $summary,
);
return \%out;
}
sub get_ships_for {
my ($self, $session_id, $body_id, $target_params) = @_;
my $empire = $self->get_empire_by_session($session_id);
my $body = $self->get_body($empire, $body_id);
my $target = $self->find_target($target_params);
my $ships = Lacuna->db->resultset('Lacuna::DB::Result::Ships');
my @incoming;
my $incoming_rs = $ships->search({
task => 'Travelling',
direction => 'out',
'body.empire_id' => $empire->id,
},{
join => 'body',
});
if ($target->isa('Lacuna::DB::Result::Map::Star')) {
$incoming_rs = $incoming_rs->search({foreign_star_id => $target->id});
}
else {
$incoming_rs = $incoming_rs->search({foreign_body_id => $target->id});
}
while (my $ship = $incoming_rs->next) {
$ship->body($body) if ($ship->body_id == $body->id);
push @incoming, $ship->get_status;
}
my $max_berth = $body->max_berth;
my @unavailable;
my @available;
my $available_rs = $ships->search( {task => 'Docked',
body_id=>$body->id });
while (my $ship = $available_rs->next) {
$ship->body($body);
eval{ $ship->can_send_to_target($target) };
my $reason = $@;
if ($reason) {
push @unavailable, { ship => $ship->get_status, reason => $reason };
next;
}
if ($ship->berth_level > $max_berth) {
$reason = [ 1009, 'Max Berth Level to send from this planet is '.$max_berth ];
push @unavailable, { ship => $ship->get_status, reason => $reason };
next;
}
$ship->body($body);
push @available, $ship->get_status($target);
}
my $max_ships = Lacuna->config->get('ships_per_fleet') || 500;
my %out = (
status => $self->format_status($empire, $body),
incoming => \@incoming,
available => \@available,
unavailable => \@unavailable,
fleet_send_limit => $max_ships,
);
unless ($target->isa('Lacuna::DB::Result::Map::Star')) {
my @orbiting;
my $orbiting_rs = $ships->search({task => [qw(Defend Orbiting)], body_id => $body->id, foreign_body_id => $target->id });
while (my $ship = $orbiting_rs->next) {
$ship->body($body);
eval{ $ship->can_recall() };
my $reason = $@;
if ($reason) {
push @unavailable, { ship => $ship->get_status, reason => $reason };
next;
}
$ship->body($body);
push @orbiting, $ship->get_status($target);
}
$out{orbiting} = \@orbiting;
}
if ($target->isa('Lacuna::DB::Result::Map::Body::Asteroid')) {
my $platforms = Lacuna->db->resultset('Lacuna::DB::Result::MiningPlatforms')->search({asteroid_id => $target->id});
while (my $platform = $platforms->next) {
my $empire = $platform->planet->empire;
if (defined $empire) {
push @{$out{mining_platforms}}, {
empire_id => $empire->id,
empire_name => $empire->name,
};
}
else {
$platform->delete;
}
}
}
if ( $target->isa('Lacuna::DB::Result::Map::Body::Asteroid') ||
$target->isa('Lacuna::DB::Result::Map::Body::Planet') ) {
my $excavators = Lacuna->db->resultset('Lacuna::DB::Result::Excavators')->search({body_id => $target->id});
while (my $excav = $excavators->next) {
my $empire = $excav->planet->empire;
if (defined $empire) {
push @{$out{excavators}}, {
empire_id => $empire->id,
empire_name => $empire->name,
};
}
else {
$excav->delete;
}
}
}
return \%out;
}
sub send_ship {
my ($self, $session_id, $ship_id, $target_params) = @_;
my $empire = $self->get_empire_by_session($session_id);
my $target = $self->find_target($target_params);
my $ship = Lacuna->db->resultset('Lacuna::DB::Result::Ships')->find($ship_id);
unless (defined $ship) {
confess [1002, 'Could not locate that ship.'];
}
unless ($ship->body->empire_id == $empire->id) {
confess [1010, 'You do not own that ship.'];
}
my $body = $ship->body;
$body->empire($empire);
$ship->can_send_to_target($target);
if ($ship->hostile_action) {
$empire->current_session->check_captcha;
}
$ship->send(target => $target);
$body->add_to_neutral_entry($ship->combat);
return {
ship => $ship->get_status,
status => $self->format_status($empire),
}
}
sub find_arrival {
my ($self, $arrival_params) = @_;
my $now = DateTime->now;
my $year = $arrival_params->{year} ? $arrival_params->{year} : $now->year;
my $month = $arrival_params->{month} ? $arrival_params->{month} : $now->month;
my $mon_end = DateTime->last_day_of_month(year => $year, month => $month);
my $day = $arrival_params->{day};
my $hour = $arrival_params->{hour};
my $minute = $arrival_params->{minute};
my $second = $arrival_params->{second};
if (not defined $day or $day < 1 or $day > $mon_end->day) {
confess [1009, "Invalid day. [$day][".Dumper($arrival_params)."]"];
}
if (not defined $hour or $hour != int($hour) or $hour < 0 or $hour > 23) {
confess [1002, 'Invalid hour.'];
}
if (not defined $minute or $minute != int($minute) or $minute < 0 or $minute > 59) {
confess [1002, 'Invalid minute.'];
}
if (not defined $second or $second != 0 and $second != 15 and $second != 30 and $second != 45) {
confess [1002, 'Invalid second. Must be 0, 15, 30 or 45'];
}
if ($day < $now->day and $month == $now->month) {
# Then it must be a day next month
$mon_end->add( days => $day);
$year = $mon_end->year;
$month = $mon_end->month;
}
my $arrival = DateTime->new(
year => $year,
month => $month,
day => $day,
hour => $hour,
minute => $minute,
second => $second,
);
return $arrival;
}
sub send_ship_types {
my ($self, $session_id, $body_id, $target_params, $type_params, $arrival_params) = @_;
my $empire = $self->get_empire_by_session($session_id);
my $body = $self->get_body($empire, $body_id);
my $target = $self->find_target($target_params);
my $arrival;
if ($arrival_params->{earliest}) {
}
else {
$arrival_params->{seconds} = 0 unless (defined $arrival_params->{seconds});
$arrival = $self->find_arrival($arrival_params);
}
# calculate the total ships before the expense of any database operations.
my $total_ships = 0;
map {$total_ships += $_->{quantity}} @$type_params;
my $max_ships = Lacuna->config->get('ships_per_fleet') || 500;
if ($total_ships > $max_ships) {
confess [1009, sprintf("Too many ships for a fleet, number must be less than or equal to %d.", $max_ships)];
}
my $ship_ref;
my $do_captcha_check = 0;
my $ag_chk = 0;
my @ag_list = ("sweeper","snark","snark2","snark3",
"observatory_seeker","spaceport_seeker","security_ministry_seeker",
"scanner","surveyor","detonator","bleeder","thud",
"scow","scow_large","scow_fast","scow_mega");
foreach my $type_param (@$type_params) {
foreach my $arg (qw(speed stealth combat quantity)) {
confess [1002, "$arg cannot be negative."] if $type_param->{$arg} < 0;
confess [1002, "$arg must be an integer."] if $type_param->{$arg} != int($type_param->{$arg});
}
my $type = $type_param->{type};
my $quantity = $type_param->{quantity};
confess [1009, "You must send at least one ship"] if ($quantity < 1);
confess [1009, "Cannot send more than one excavator"] if ($type eq 'excavator' and $quantity > 1);
confess [1009, "Cannot send more than one supply pod"] if ($type =~ /supply_pod/ and $quantity > 1);
my $max_berth = $body->max_berth;
unless ($max_berth) {
$max_berth = 1;
}
my $ships_rs = Lacuna->db->resultset('Ships')->search({
body_id => $body->id,
task => 'Docked',
type => $type,
berth_level => {'<=' => $max_berth },
});
# handle optional parameters
$ships_rs = $ships_rs->search({ speed => $type_param->{speed}}) if defined $type_param->{speed};
$ships_rs = $ships_rs->search({ stealth => $type_param->{stealth}}) if defined $type_param->{stealth};
$ships_rs = $ships_rs->search({ combat => $type_param->{combat}}) if defined $type_param->{combat};
$ships_rs = $ships_rs->search({ name => $type_param->{name}}) if defined $type_param->{name};
if ($ships_rs->count < $quantity) {
confess [1009, "Cannot find $quantity of $type ships."];
}
if (grep { $type eq $_ } @ag_list) {
$ag_chk += $quantity;
}
my @ships = $ships_rs->search( undef,
{order_by => 'speed', rows => $quantity }
);
my $ship = $ships[0]; #Need to grab slowest ship
# We only need to check one of the ships
$ship->can_send_to_target($target);
#Check speed of ship. If it can not make it to the target in time, fail
#If time to target is longer than 60 days, fail.
my $seconds_to_target = $ship->calculate_travel_time($target);
my $earliest = DateTime->now->add(seconds=>$seconds_to_target);
if ($arrival_params->{earliest}) {
$arrival = $earliest;
}
elsif ($earliest > $arrival) {
confess [1009, "Cannot set a speed earlier than possible arrival time."];
}
my $two_months = DateTime->now->add(days=>60);
if ($arrival > $two_months) {
confess [1009, "Cannot send a ship that will take longer than 60 days to arrive."];
}
if (not $do_captcha_check and $ship->hostile_action) {
$do_captcha_check = 1;
}
foreach my $ship (@ships) {
$ship_ref->{$ship->id} = $ship;
}
}
if ($do_captcha_check) {
$empire->current_session->check_captcha;
}
# If we get here without exceptions, then all ships can be sent
# Create attack_group
my $cnt = 0;
my %ag_hash = map { $_ => $cnt++ } @ag_list;
my $attack_group = {
speed => 50_000,
stealth => 50_000,
hold_size => 0,
combat => 0,
number_of_docks => 0,
};
my $payload;
$ag_chk = 0 if ($target->isa('Lacuna::DB::Result::Map::Star'));
foreach my $ship (values %$ship_ref) {
if ($ag_chk > 1 and grep { $ship->type eq $_ } @ag_list) {
my $sort_val = $ag_hash{$ship->type};
if ($ship->speed < $attack_group->{speed}) {
$attack_group->{speed} = $ship->speed;
}
if ($ship->speed < $attack_group->{stealth}) {
$attack_group->{stealth} = $ship->stealth;
}
$attack_group->{combat} += $ship->combat;
$attack_group->{hold_size} += $ship->hold_size; #This really is only good for scows
$attack_group->{number_of_docks}++;
my $key = sprintf("%02d:%s:%05d:%05d:%05d:%09d",
$sort_val,
$ship->type,
$ship->combat,
$ship->speed,
$ship->stealth,
$ship->hold_size);
if ($payload->{fleet}->{$key}) {
$payload->{fleet}->{$key}->{quantity}++;
}
else {
$payload->{fleet}->{$key} = {
type => $ship->type,
name => $ship->name,
speed => $ship->speed,
combat => $ship->combat,
stealth => $ship->stealth,
hold_size => $ship->hold_size,
target_building => $ship->target_building,
damage_taken => 0,
quantity => 1,
};
}
$ship->delete;
}
else {
my $distance = $body->calculate_distance_to_target($target);
my $transit_time = $arrival->subtract_datetime_absolute(DateTime->now)->seconds;
my $fleet_speed = int( $distance / ($transit_time/3600) + 0.5);
$ship->fleet_speed($fleet_speed);
$ship->send(target => $target, arrival => $arrival);
$body->add_to_neutral_entry($ship->combat);
}
}
if ($attack_group->{number_of_docks} > 0) {
my $distance = $body->calculate_distance_to_target($target);
my $transit_time = $arrival->subtract_datetime_absolute(DateTime->now)->seconds;
my $fleet_speed = int( $distance / ($transit_time/3600) + 0.5);
my $ag = $body->ships->new({
type => "attack_group",
name => "Attack Group SP",
shipyard_id => "23",
speed => $attack_group->{speed},
combat => $attack_group->{combat},
stealth => $attack_group->{stealth},
hold_size => $attack_group->{hold_size},
date_available => DateTime->now,
date_started => DateTime->now,
fleet_speed => $fleet_speed,
berth_level => 1,
body_id => $body->id,
task => 'Docked',
number_of_docks => $attack_group->{number_of_docks},
})->insert;
$ag->send(target => $target, arrival => $arrival, payload => $payload);
$body->add_to_neutral_entry($attack_group->{combat});
}
return $self->get_fleet_for($session_id, $body_id, $target_params);
}
sub send_fleet {
my ($self, $session_id, $ship_ids, $target_params, $set_speed) = @_;
$set_speed //= 0;
my $empire = $self->get_empire_by_session($session_id);
my $target = $self->find_target($target_params);
my $max_ships = Lacuna->config->get('ships_per_fleet') || 500;
if (@$ship_ids > $max_ships) {
confess [1009, 'Too many ships for a fleet.'];
}
my @fleet;
my $speed = 999999999;
my $excavator = 0;
my $supply_pod = 0;
for my $ship_id (@$ship_ids) {
my $ship = Lacuna->db->resultset('Lacuna::DB::Result::Ships')->find($ship_id);
unless (defined $ship) {
confess [1002, 'Could not locate that ship.'];
}
unless ($ship->body->empire_id == $empire->id) {
confess [1010, 'You do not own that ship.'];
}
$excavator++ if ($ship->type eq 'excavator');
$supply_pod++ if ($ship->type =~ /supply_pod/);
$speed = $ship->speed if ( $speed > $ship->speed );
push @fleet, $ship_id;
}
my $tmp_ship = Lacuna->db->resultset('Lacuna::DB::Result::Ships')->find($fleet[0]);
my $body = $tmp_ship->body;
my $distance = $body->calculate_distance_to_target($target);
my $max_speed = $distance * 12; # Minimum time to arrive is five minutes
my $min_speed = int($distance/1440 + 0.5); # Max time to arrive is two months
$min_speed = 1 if $min_speed < 1;
unless ($excavator <= 1) {
confess [1010, 'Only one Excavator may be sent to a body by this empire.'];
}
unless ($set_speed <= $speed) {
confess [1009, 'Set speed cannot exceed the speed of the slowest ship.'];
}
unless ($set_speed >= 0) {
confess [1009, 'Set speed cannot be less than zero.'];
}
#If time to target is longer than 60 days, fail.
$speed = $set_speed if ($set_speed > 0 && $set_speed < $speed);
$speed = $max_speed if ($speed > $max_speed);
unless ($speed >= $min_speed) {
confess [1009, 'Set speed cannot be set so that ships arrive after 60 days.'];
}
my @ret;
my $captcha_check = 1;
# Check captcha if needed
# Create attack_group
for my $ship_id (@fleet) {
my $ship = Lacuna->db->resultset('Lacuna::DB::Result::Ships')->find($ship_id);
my $body = $ship->body;
$body->empire($empire);
$ship->can_send_to_target($target);
if ($captcha_check && $ship->hostile_action ) {
$empire->current_session->check_captcha;
$captcha_check = 0;
}
$body->add_to_neutral_entry($ship->combat);
$ship->fleet_speed($speed);
$ship->send(target => $target);
push @ret, {
ship => $ship->get_status,
}
}
return {
fleet => \@ret,
status => $self->format_status($empire),
};
}
sub recall_ship {
my ($self, $session_id, $building_id, $ship_id) = @_;
my $empire = $self->get_empire_by_session($session_id);
my $building = $self->get_building($empire, $building_id);
my $ship = Lacuna->db->resultset('Lacuna::DB::Result::Ships')->find($ship_id);
unless (defined $ship) {
confess [1002, 'Could not locate that ship.'];
}
unless ($ship->body->empire_id == $empire->id) {
confess [1010, 'You do not own that ship.'];
}
my $body = $building->body;
$body->empire($empire);
$ship->can_recall();
$ship->fleet_speed(0);
my $target = $self->find_target({body_id => $ship->foreign_body_id});
$ship->send(
target => $target,
direction => 'in',
);
$ship->body->update;
return {
ship => $ship->get_status,
status => $self->format_status($empire),
}
}
sub recall_all {
my ($self, $session_id, $building_id) = @_;
my $empire = $self->get_empire_by_session($session_id);
my $building = $self->get_building($empire, $building_id);
my $body = $building->body;
my @ships = $body->ships_orbiting->search(undef)->all;
my @ret;
for my $ship (@ships) {
unless (defined $ship) {
confess [1002, 'Could not locate that ship.'];
}
unless ($ship->body->empire_id == $empire->id) {
confess [1010, 'You do not own that ship.'];
}
$body->empire($empire);
$ship->can_recall();
$ship->fleet_speed(0);
my $target = $self->find_target({body_id => $ship->foreign_body_id});
$ship->send(
target => $target,
direction => 'in',
);
$ship->body->update;
push @ret, {
ship => $ship->get_status,
}
}
return {
ships => \@ret,
status => $self->format_status($empire),
}
}
sub prepare_send_spies {
my ($self, $session_id, $on_body_id, $to_body_id) = @_;
my $empire = $self->get_empire_by_session($session_id);
if ($on_body_id == $to_body_id) {
confess [1013, "Cannot send spies to one self."];
}
my $on_body = $self->get_body($empire, $on_body_id);
my $to_body = Lacuna->db->resultset('Lacuna::DB::Result::Map::Body')->find($to_body_id);
unless ($to_body->empire_id) {
confess [1009, "Cannot send spies to an uninhabited body."];
}
if ($to_body->empire->is_isolationist) {
confess [ 1013, sprintf('%s is an isolationist empire, and must be left alone.',$to_body->empire->name)];
}
unless ($on_body->empire_id == $to_body->empire_id) {
$empire->current_session->check_captcha;
}
my $max_berth = $on_body->max_berth;
unless ($max_berth) {
$max_berth = 1;
}
my $ships = Lacuna->db->resultset('Lacuna::DB::Result::Ships')->search(
{type => { in => [qw(spy_pod cargo_ship smuggler_ship dory spy_shuttle barge)]},
task=>'Docked', body_id => $on_body_id,
berth_level => {'<=' => $max_berth } },
{order_by => 'name', rows=>100}
);
my @ships;
while (my $ship = $ships->next) {
push @ships, $ship->get_status($to_body);
}
my $dt_parser = Lacuna->db->storage->datetime_parser;
my $now = $dt_parser->format_datetime( DateTime->now );
my $spies = Lacuna->db->resultset('Lacuna::DB::Result::Spies')->search(
{
on_body_id => $on_body->id,
empire_id => $empire->id,
-or => [
task => { in => [ 'Idle', 'Counter Espionage' ], },
-and => [
task => { in => [ 'Unconscious', 'Debriefing' ], },
available_on => { '<' => $now },
],
],
},
{
# match the order_by in L::RPC::B::Intelligence::view_spies
order_by => {
-asc => [ qw/name id/ ],
}
},
);
my @spies;
while (my $spy = $spies->next) {
$spy->on_body($on_body);
if ($spy->is_available) {
push @spies, $spy->get_status;
}
last if (scalar @spies >= 100);
}
undef $spies;
return {
status => $self->format_status($empire),
ships => \@ships,
spies => \@spies,
};
}
sub send_spies {
my ($self, $session_id, $on_body_id, $to_body_id, $ship_id, $spy_ids) = @_;
my $empire = $self->get_empire_by_session($session_id);
if ($on_body_id == $to_body_id) {
confess [1013, "Cannot send spies to one self."];
}
my $on_body = $self->get_body($empire, $on_body_id);
my $to_body = Lacuna->db->resultset('Lacuna::DB::Result::Map::Body')->find($to_body_id);
# make sure it's a valid target
unless ($to_body->empire_id) {
confess [ 1009, 'Cannot send spies to an uninhabited body.'];
}
if ($to_body->empire->is_isolationist) {
confess [ 1013, sprintf('%s is an isolationist empire, and must be left alone.',$to_body->empire->name)];
}
unless ($on_body->empire_id == $to_body->empire_id) {
$empire->current_session->check_captcha;
}
# get the ship
my $ship = Lacuna->db->resultset('Lacuna::DB::Result::Ships')->find($ship_id);
unless (defined $ship) {
confess [1002, "Ship not found."];
}
unless ($ship->is_available) {
confess [1010, "That ship is not available."];
}
my $max_berth = $on_body->max_berth;
unless ($ship->berth_level <= $max_berth) {
confess [1010, "Your spaceport level is not high enough to support a ship with a Berth Level of ".$ship->berth_level."."];
}
# check size
unless (scalar(@{$spy_ids})) {
confess [1013, "You can't send a ship with no spies."];
}
if ($ship->type eq 'spy_pod' && scalar(@{$spy_ids}) == 1) {
# we're ok
}
elsif ($ship->type eq 'spy_shuttle' && scalar(@{$spy_ids}) <= 4) {
# we're ok
}
elsif ($ship->hold_size <= (scalar(@{$spy_ids}) * 350)) {
confess [1010, "The ship cannot hold the spies selected."];
}
# get a spies
my @ids_sent;
my @ids_not_sent;
my $spies = Lacuna->db->resultset('Lacuna::DB::Result::Spies');
foreach my $id (@{$spy_ids}) {
my $spy = $spies->find($id);
if ($spy->is_available and $spy->on_body_id == $on_body_id) {
if ($spy->empire_id == $empire->id) {
my $arrives = DateTime->now->add(seconds=>$ship->calculate_travel_time($to_body));
push @ids_sent, $spy->id;
$spy->send($to_body->id, $arrives)->update;
}
else {
push @ids_not_sent, $spy->id;
}
}
else {
push @ids_not_sent, $spy->id;
}
}
if (scalar @ids_sent) {
# send it
$ship->send(
target => $to_body,
payload => {spies => \@ids_sent }, # add the spies to the payload when we send, otherwise they'll get added again
);
}
return {
ship => $ship->get_status,
spies_sent => \@ids_sent,
spies_not_sent => \@ids_not_sent,
status => $self->format_status($empire, $on_body)
};
}
sub prepare_fetch_spies {
my ($self, $session_id, $on_body_id, $to_body_id) = @_;
my $empire = $self->get_empire_by_session($session_id);
if ($on_body_id == $to_body_id) {
confess [1013, "Cannot fetch spies to one self."];
}
my $to_body = $self->get_body($empire, $to_body_id);
my $on_body = Lacuna->db->resultset('Lacuna::DB::Result::Map::Body')->find($on_body_id);
unless ($on_body->empire_id) {
confess [1013, "Cannot fetch spies from an uninhabited planet."];
}
my $max_berth = $to_body->max_berth;
unless ($max_berth) {
$max_berth = 1;
}
my $ships = Lacuna->db->resultset('Lacuna::DB::Result::Ships')->search(
{type => { in => [qw(cargo_ship smuggler_ship dory spy_shuttle barge)]},
task=>'Docked', body_id => $to_body_id,
berth_level => {'<=' => $max_berth } },
{order_by => 'name', rows=>100}
);
my @ships;
while (my $ship = $ships->next) {
push @ships, $ship->get_status($on_body);
}
my $dt_parser = Lacuna->db->storage->datetime_parser;
my $now = $dt_parser->format_datetime( DateTime->now );
my $spies = Lacuna->db->resultset('Lacuna::DB::Result::Spies')->search(
{
on_body_id => $on_body->id,
empire_id => $empire->id,
-or => [
task => { in => [ 'Idle', 'Counter Espionage' ], },
-and => [
task => { in => [ 'Unconscious', 'Debriefing' ], },
available_on => { '<' => $now },
],
],
},
{order_by => 'name'}
);
my @spies;
while (my $spy = $spies->next) {
$spy->on_body($on_body);
if ($spy->is_available) {
push @spies, $spy->get_status;
}
last if (scalar @spies >= 100);
}
undef $spies;
return {
status => $self->format_status($empire),
ships => \@ships,
spies => \@spies,
};
}
sub fetch_spies {
my ($self, $session_id, $on_body_id, $to_body_id, $ship_id, $spy_ids) = @_;
my $empire = $self->get_empire_by_session($session_id);
if ($on_body_id == $to_body_id) {
confess [1013, "Cannot fetch spies to one self."];
}
my $to_body = $self->get_body($empire, $to_body_id);
my $on_body = Lacuna->db->resultset('Lacuna::DB::Result::Map::Body')->find($on_body_id);
my $max_berth = $to_body->max_berth;
# get a spies
my @ids_fetched;
my @ids_not_fetched;
my $spies = Lacuna->db->resultset('Lacuna::DB::Result::Spies');
foreach my $id (@{$spy_ids}) {
my $spy = $spies->find($id);
if ($spy->on_body_id == $on_body_id) {
push @ids_fetched, $id;
}
else {
push @ids_not_fetched, $id;
}
}
# get the ship
my $ship = Lacuna->db->resultset('Lacuna::DB::Result::Ships')->find($ship_id);
unless (defined $ship) {
confess [1002, "Ship not found."];
}
unless ($ship->is_available || ($ship->can_recall && $ship->foreign_body_id == $on_body_id)) {
confess [1010, "That ship is not available."];
}
unless ($ship->berth_level <= $max_berth) {
confess [1010, "Your spaceport level is not high enough to support a ship with a Berth Level of ".$ship->berth_level."."];
}
unless ($on_body->empire_id) {
confess [1013, "Cannot fetch spies from an uninhabited planet."];
}
unless (scalar(@ids_fetched)) {
confess [1013, "You can't send a ship to collect no one."];
}
# check size
if ($ship->type eq 'spy_shuttle' && scalar(@ids_fetched) <= 4) {
# we're ok
}
elsif ($ship->hold_size <= (scalar(@ids_fetched) * 350)) {
confess [1013, "The ship cannot hold the spies selected."];
}
# send it
$ship->send(
target => $on_body,
payload => { fetch_spies => \@ids_fetched },
);
return {
ship => $ship->get_status,
spies_fetched => \@ids_fetched,
spies_not_fetched => \@ids_not_fetched,
status => $self->format_status($empire, $to_body),
};
}
sub view_ships_travelling {
my ($self, $session_id, $building_id, $page_number) = @_;
my $empire = $self->get_empire_by_session($session_id);
my $building = $self->get_building($empire, $building_id);
$page_number ||= 1;
my $body = $building->body;
my @travelling;
my $ships = $body->ships_travelling->search(undef, {rows=>25, page=>$page_number});
while (my $ship = $ships->next) {
$ship->body($body);
push @travelling, $ship->get_status;
}
return {
status => $self->format_status($empire, $body),
number_of_ships_travelling => $ships->pager->total_entries,
ships_travelling => \@travelling,
};
}
sub _ship_paging_options {
my ($self, $paging) = @_;
# for my $key ( keys %{ $paging } ) {
# # Throw away bad keys
# unless ($key ~~ [qw(page_number items_per_page no_paging)]) {
# delete $paging->{$key};
# next;
# }
# }
if ($paging->{no_paging}) {
$paging = {};
}
else {
$paging->{page_number} ||= 1;
$paging->{items_per_page} ||= 25;
}
return $paging;
}
sub _ship_filter_options {
my ($self, $filter) = @_;
# Valid filter options include...
my $options = {
task => [("Building", "Defend", "Docked", "Mining",
"Orbiting", "Supply Chain", "Travelling",
"Waiting on Trade", "Waste Chain")],
tag => [qw(Trade Colonization Intelligence Exploration War Mining)],
type => [SHIP_TYPES],
};
# Pull in the list of ship types by tag
my %tag;
for my $type ( SHIP_TYPES ) {
my $ship = Lacuna->db->resultset('Lacuna::DB::Result::Ships')->new({ type => $type });
for my $tag ( @{$ship->build_tags} ) {
push @{ $tag{$tag} }, $type;
}
}
for my $key ( keys %{ $filter } ) {
# Throw away bad keys
unless ( $key ~~ [keys %$options] ) {
delete $filter->{$key};
next;
}
# Throw away bad values
my $value = $filter->{$key};
if ( ref($value) eq 'ARRAY' ) {
@$value = grep { $_ ~~ $options->{$key} } @$value;
}
elsif ( ! ref($value) ) {
delete $filter->{$key} unless ( $value ~~ $options->{$key} );
}
else {
delete $filter->{$key};
}
# Convert tags to types (destructive)
if ( $key eq 'tag' ) {
if ( ref($value) eq 'ARRAY' ) {
my @types;
for my $tag ( @$value ) {
push @types, @{ $tag{$tag} };
}
my %uniq = map { $_ => 1 } @types;
$filter->{type} = [ sort keys %uniq ];
}
else {
$filter->{type} = $tag{$value};
}
delete $filter->{tag};
}
}
return $filter;
}
sub _ship_sort_options {
my ($self, $sort) = @_;
# return the default if it's not one of the following or is 'name'
if ( ! $sort || $sort eq 'name' || ! $sort ~~ [qw(combat speed stealth task type)] ) {
return [ 'name' ];
}
# append name to the sort options
return [ $sort, 'name' ];
}