Skip to content

Commit cbf4da2

Browse files
committed
Refactor
1 parent 043def2 commit cbf4da2

File tree

3 files changed

+60
-26
lines changed

3 files changed

+60
-26
lines changed

src/rails_lib/backend/backend_low.ml

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -507,15 +507,54 @@ let try_create_priority_shipment ?(force=false) v =
507507
end
508508
| _ -> None
509509

510+
let clear_player_and_train_priority_shipments v player_list =
511+
List.iter (fun i ->
512+
let player = v.players.(i) in
513+
let trains = Trainmap.clear_priority_shipment player.trains in
514+
let player = [%up {player with trains; priority=None}] in
515+
v.players.(i) <- player
516+
) player_list
517+
510518
let try_cancel_priority_shipments ?(force=false) v =
511519
(* Try to cancel and create corresponding messages *)
512-
match Player.cancel_priority_shipment ~force v.players ~cycle:v.cycle ~year:v.year v.region with
520+
let try_cancel_priority_shipment_all players ~cycle ~year region =
521+
(* Cancel priority and let us know which players' were canceled *)
522+
let cancel_players =
523+
Array.foldi (fun acc i player ->
524+
if Player.has_priority player &&
525+
(Player.check_cancel_priority_shipment player ~cycle ~year region || force)
526+
then i::acc else acc) []
527+
players
528+
in
529+
clear_player_and_train_priority_shipments v cancel_players;
530+
cancel_players
531+
in
532+
match try_cancel_priority_shipment_all v.players ~cycle:v.cycle ~year:v.year v.region with
513533
| _::_ as players ->
514-
let station_map = Station_map.clear_priority_shipment_for_all v.stations ~players in
515-
[%upf v.stations <- station_map];
534+
let stations = Station_map.clear_priority_shipment_for_all v.stations ~players in
535+
[%upf v.stations <- stations];
516536
List.map (fun i -> PriorityShipmentCanceled{player=i}) players
517537
| _ -> []
518538

539+
let check_priority_delivery v =
540+
(* Check if a priority shipment has been delivered *)
541+
let check_priority_delivery_all players stations ~cycle ~year region =
542+
(* Check for priority delivery completion *)
543+
let deliver_players =
544+
Array.foldi (fun acc i player ->
545+
if Player.has_priority player && Player.check_priority_delivery player stations
546+
then i::acc else acc) []
547+
players
548+
in
549+
clear_player_and_train_priority_shipments v deliver_players;
550+
deliver_players
551+
in
552+
match check_priority_delivery_all v.players v.stations ~cycle:v.cycle ~year:v.year v.region with
553+
| _::_ as players ->
554+
let stations = Station_map.clear_priority_shipment_for_all v.stations ~players in
555+
[%upf v.stations <- stations];
556+
List.map (fun i -> PriorityShipmentDelivered{player=i}) players
557+
| _ -> []
519558

520559
(** Most time-based work happens here **)
521560
let handle_cycle v =
@@ -568,7 +607,10 @@ let handle_cycle v =
568607
else ui_msgs
569608
in
570609
(* Cancel any expired priority shipments *)
571-
let ui_msgs = (try_cancel_priority_shipments v) @ ui_msgs in
610+
let cancel_ui_msgs = try_cancel_priority_shipments v in
611+
let delivered_priority_ui_msgs = check_priority_delivery v in
612+
613+
let ui_msgs = cancel_ui_msgs @ ui_msgs in
572614

573615
(* adjust time *)
574616
v.time <- v.time + 1;

src/rails_lib/backend/player.ml

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -389,28 +389,15 @@ let get_priority player = player.priority
389389

390390
let check_cancel_priority_shipment player ~cycle ~year region =
391391
(* Priority shipments are cancelled when the bonus is < 20 *)
392-
match player.priority with
393-
| None -> false
394-
| Some pr_data ->
395-
Priority_shipment.should_be_cancelled pr_data ~cycle ~year region
396-
397-
let cancel_priority_shipment ?(force=false) players ~cycle ~year region =
398-
(* Cancel priority and let us know which players' were canceled *)
399-
let cancel_players =
400-
Array.foldi (fun acc i player ->
401-
if has_priority player &&
402-
(check_cancel_priority_shipment player ~cycle ~year region || force)
403-
then i::acc else acc) []
404-
players
405-
in
406-
List.iter (fun i ->
407-
let player = players.(i) in
408-
let trains = Trainmap.clear_priority_shipment player.trains in
409-
let player = [%up {player with trains; priority=None}] in
410-
players.(i) <- player
411-
)
412-
cancel_players;
413-
cancel_players
392+
Option.map_or ~default:false
393+
(fun pr_data -> Priority_shipment.should_be_cancelled pr_data ~cycle ~year region)
394+
player.priority
395+
396+
let check_priority_delivery player stations =
397+
(* Return whether a priority delivery has been fulfilled *)
398+
Option.map_or ~default:false
399+
(fun pr_data -> Priority_shipment.check_priority_delivery pr_data stations)
400+
player.priority
414401

415402
let update players idx f =
416403
let p = players.(idx) in

src/rails_lib/backend/priority_shipment.ml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ let compute_bonus pr_data ~cycle ~year region =
7070
let should_be_cancelled pr_data ~cycle ~year region =
7171
compute_bonus pr_data ~cycle ~year region < C.priority_min_bonus
7272

73+
let check_priority_delivery pr_data stations =
74+
let dest_loc = pr_data.dst_loc in
75+
let station = Station_map.get_exn dest_loc stations in
76+
Station.holds_priority_shipment station
77+
7378
let create_text shipment (region:Region.t) station_map =
7479
let type_s = match shipment.freight, region with
7580
| `Mail, _ -> "Rare vaccine required."

0 commit comments

Comments
 (0)