Skip to content

Commit e1e76ce

Browse files
committed
Add newspaper support to main_ui
1 parent 672a897 commit e1e76ce

File tree

5 files changed

+51
-10
lines changed

5 files changed

+51
-10
lines changed

src/rails_lib/backend/backend_d.ml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ type ui_msg =
3434
| TrainArrival of train_arrival_msg
3535
| StockBroker of stock_broker_ui_msg
3636
| OpenStockBroker of {player: int}
37+
| PriorityShipmentCanceled of {player: int}
3738
[@@deriving yojson]
3839

3940
type t = {

src/rails_lib/backend/backend_low.ml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,12 @@ let handle_cycle v =
516516
else ui_msgs)
517517
else ui_msgs
518518
in
519+
(* Cancel any expired priority shipments *)
520+
let canceled = Player.cancel_priority v.players v.cycle v.year v.region
521+
|> List.filter ((=) C.player) |> List.map (fun i -> PriorityShipmentCanceled{player=i})
522+
in
523+
let ui_msgs = canceled @ ui_msgs in
524+
519525
(* adjust time *)
520526
v.time <- v.time + 1;
521527
let v =

src/rails_lib/backend/player.ml

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,25 @@ let incr_broker_timer player =
383383

384384
let set_priority priority player = {player with priority}
385385

386+
let check_cancel_priority player cycle year region =
387+
(* Priority shipments are cancelled when the bonus is < 20 *)
388+
match player.priority with
389+
| None -> false
390+
| Some pr_data ->
391+
let bonus = Priority_shipment.compute_bonus pr_data cycle year region in
392+
bonus < 20
393+
394+
let cancel_priority players cycle year region =
395+
(* Cancel priority and let us know if one was canceled *)
396+
Array.foldi (fun acc i player ->
397+
if check_cancel_priority player cycle year region then (
398+
players.(i) <- {player with priority=None};
399+
i::acc
400+
) else acc
401+
)
402+
[]
403+
players
404+
386405
let update players idx f =
387406
let p = players.(idx) in
388407
let p' = f p in

src/rails_lib/ui/main_ui.ml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,13 @@ let handle_event (s:State.t) v (event:Event.t) =
620620
(fun x -> ModalMsgbox x)
621621
(fun _ () -> v, nobaction)
622622

623+
| Newspaper news_state ->
624+
let v = match Newspaper.handle_event s news_state event with
625+
| `Exit -> {v with mode=Normal}
626+
| _ -> v
627+
in
628+
v, nobaction
629+
623630
| BuildStation build_menu ->
624631
handle_modal_menu_events build_menu
625632
(fun x -> BuildStation x)
@@ -781,6 +788,10 @@ let handle_msgs (s:State.t) v ui_msgs =
781788
let state = Stock_broker.make s in
782789
{v with mode=Stock_broker state}
783790

791+
| Normal, PriorityShipmentCanceled{player} when player = C.player ->
792+
let mode = Newspaper(Newspaper.make s Newspaper.LocalNews "Priority Shipment\nCANCELLED.\n" None) in
793+
{v with mode}
794+
784795
(* TODO: handle demand changed msg *)
785796
| _ -> v
786797
in
@@ -957,6 +968,9 @@ let render (win:R.window) (s:State.t) v =
957968
| ModalMsgbox modal ->
958969
render_mode modal.last;
959970
Menu.MsgBox.render win s modal.menu
971+
| Newspaper news_state ->
972+
render_main win s v;
973+
Newspaper.render win s news_state
960974
| BuildStation modal ->
961975
render_main win s v;
962976
Menu.MsgBox.render win s modal.menu

src/rails_lib/ui/main_ui_d.ml

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -89,21 +89,22 @@ type ('state, 'menu_options, 'payload) modalmenu =
8989
2nd: type of choices from menu
9090
3rd: type of stored data
9191
*)
92-
and 'a mode =
92+
and 'state mode =
9393
| Normal
94-
| ModalMsgbox of ('a, unit, unit) modalmenu
95-
| BuildStation of ('a, Station.kind, unit) modalmenu
96-
| BuildBridge of ('a, Bridge.t, Utils.msg) modalmenu
97-
| BuildHighGrade of ('a, [`BuildTunnel | `BuildTrack], Utils.msg) modalmenu
98-
| BuildTunnel of ('a, bool, Utils.msg * int) modalmenu
99-
| SignalMenu of ('a, [`Normal|`Hold|`Proceed], int * int * Dir.t) modalmenu (* x,y,dir *)
94+
| ModalMsgbox of ('state, unit, unit) modalmenu
95+
| Newspaper of 'state Newspaper_d.t
96+
| BuildStation of ('state, Station.kind, unit) modalmenu
97+
| BuildBridge of ('state, Bridge.t, Utils.msg) modalmenu
98+
| BuildHighGrade of ('state, [`BuildTunnel | `BuildTrack], Utils.msg) modalmenu
99+
| BuildTunnel of ('state, bool, Utils.msg * int) modalmenu
100+
| SignalMenu of ('state, [`Normal|`Hold|`Proceed], int * int * Dir.t) modalmenu (* x,y,dir *)
100101
| StationReport of int * int (* x, y *)
101102
| BuildTrain of [
102103
| `ChooseEngine
103-
| `AddCars of 'a Build_train_d.addcars
104+
| `AddCars of 'state Build_train_d.addcars
104105
]
105-
| TrainReport of 'a Train_report_d.t
106-
| Stock_broker of 'a Stock_broker_d.t
106+
| TrainReport of 'state Train_report_d.t
107+
| Stock_broker of 'state Stock_broker_d.t
107108
| Balance_sheet of Balance_sheet_d.t
108109
| Accomplishments
109110
| Income_statement of Balance_sheet_d.t (* we use the stock part *)

0 commit comments

Comments
 (0)