A Nix-friendly SQLite-enhanced fork of Flitter, a speedrunning split timer for Unix-style terminals
Revision | 2696389e343469082565b0dfe9feea3d5e3a551d (tree) |
---|---|
Time | 2022-12-23 10:21:21 |
Author | Corbin <cds@corb...> |
Commiter | Corbin |
Start factoring gettimeofday() calls.
I know that this doesn't look like progress, but I'm trying to work
incrementally.
@@ -59,12 +59,13 @@ let time_status timer split_num = | ||
59 | 59 | else |
60 | 60 | color depends on whether currently ahead and how lead/loss compares to last available lead/loss |
61 | 61 | *) |
62 | + let now = Core_unix.gettimeofday () in | |
62 | 63 | if Splits.is_gold timer split_num then Gold |
63 | 64 | else |
64 | - match Splits.ahead_by timer split_num with | |
65 | + match Splits.ahead_by timer split_num ~now with | |
65 | 66 | | None -> Ahead_gain |
66 | 67 | | Some delta -> ( |
67 | - match Splits.ahead_by timer (split_num - 1) with | |
68 | + match Splits.ahead_by timer (split_num - 1) ~now with | |
68 | 69 | | None -> if delta < 0 then Ahead_gain else Behind_loss |
69 | 70 | | Some prev_delta -> |
70 | 71 | if delta < 0 then |
@@ -89,13 +90,13 @@ let show_delta timer split_num = | ||
89 | 90 | *) |
90 | 91 | match timer.state with |
91 | 92 | | Idle -> false |
92 | - | Timing (splits, _, _) | Paused (splits, _, _, _) | Done (splits, _, _) -> ( | |
93 | + | Timing (splits, _, now) | Paused (splits, _, _, now) | Done (splits, _, now) -> ( | |
93 | 94 | if split_num < Array.length splits then true |
94 | 95 | else |
95 | 96 | match time_status timer split_num with |
96 | 97 | | Behind_gain | Behind_loss -> true |
97 | 98 | | Ahead_gain | Ahead_loss | Gold -> ( |
98 | - let sgmt = Splits.segment_time timer split_num in | |
99 | + let sgmt = Splits.segment_time timer split_num ~now in | |
99 | 100 | let gold = timer.golds.(split_num).duration in |
100 | 101 | match (sgmt, gold) with Some s, Some g -> s > g | _ -> false)) |
101 | 102 |
@@ -123,7 +124,8 @@ let split_row timer width i = | ||
123 | 124 | let delta_image = |
124 | 125 | if show_comparison then I.string uncolored_attr "-" |
125 | 126 | else |
126 | - match Splits.ahead_by timer i with | |
127 | + let now = Core_unix.gettimeofday () in | |
128 | + match Splits.ahead_by timer i ~now with | |
127 | 129 | | None -> I.string uncolored_attr "-" |
128 | 130 | | Some delta -> |
129 | 131 | if not (show_delta timer i) then I.string uncolored_attr "" |
@@ -135,9 +137,10 @@ let split_row timer width i = | ||
135 | 137 | |
136 | 138 | (* Compute the image of the split's segment time *) |
137 | 139 | let sgmt_image = |
140 | + let now = Core_unix.gettimeofday () in | |
138 | 141 | let seg_time = |
139 | 142 | if show_comparison then Splits.archived_segment_time timer i |
140 | - else Splits.segment_time timer i | |
143 | + else Splits.segment_time timer i ~now | |
141 | 144 | in |
142 | 145 | |
143 | 146 | match seg_time with |
@@ -147,8 +150,9 @@ let split_row timer width i = | ||
147 | 150 | |
148 | 151 | (* Compute the image of the split's absolute time *) |
149 | 152 | let time = |
153 | + let now = Core_unix.gettimeofday () in | |
150 | 154 | if show_comparison then Splits.archived_split_time timer i |
151 | - else Splits.split_time timer i | |
155 | + else Splits.split_time timer i ~now | |
152 | 156 | in |
153 | 157 | let time_str = |
154 | 158 | match time with Some t -> Duration.to_string t 1 | None -> "-" |
@@ -243,11 +247,11 @@ let previous_segment timer width = | ||
243 | 247 | let time_img = |
244 | 248 | match timer.state with |
245 | 249 | | Idle -> empty_time_img |
246 | - | Timing (splits, _, _) | Paused (splits, _, _, _) | Done (splits, _, _) | |
250 | + | Timing (splits, _, now) | Paused (splits, _, _, now) | Done (splits, _, now) | |
247 | 251 | -> ( |
248 | 252 | let curr_split = Array.length splits in |
249 | - let prev_delta = Splits.ahead_by timer (curr_split - 1) in | |
250 | - let prev_prev_delta = Splits.ahead_by timer (curr_split - 2) in | |
253 | + let prev_delta = Splits.ahead_by timer (curr_split - 1) ~now in | |
254 | + let prev_prev_delta = Splits.ahead_by timer (curr_split - 2) ~now in | |
251 | 255 | match (prev_delta, prev_prev_delta) with |
252 | 256 | | Some pd, Some ppd -> |
253 | 257 | let diff = pd - ppd in |
@@ -265,7 +269,7 @@ let best_possible_time timer width = | ||
265 | 269 | let t = |
266 | 270 | match timer.state with |
267 | 271 | | Idle -> Splits.gold_sum timer 0 (Array.length timer.split_names) |
268 | - | Timing (splits, _, _) | Paused (splits, _, _, _) -> ( | |
272 | + | Timing (splits, _, now) | Paused (splits, _, _, now) -> ( | |
269 | 273 | let curr_split = Array.length splits in |
270 | 274 | let total_splits = Array.length timer.split_names in |
271 | 275 |
@@ -274,7 +278,7 @@ let best_possible_time timer width = | ||
274 | 278 | let last_split_time = |
275 | 279 | if curr_split = 0 then Some 0 else splits.(curr_split - 1) |
276 | 280 | in |
277 | - let curr_seg = Splits.segment_time timer curr_split in | |
281 | + let curr_seg = Splits.segment_time timer curr_split ~now in | |
278 | 282 | |
279 | 283 | match (future_sob, curr_gold, last_split_time, curr_seg) with |
280 | 284 | | ( Some future_sob', |
@@ -1,13 +1,9 @@ | ||
1 | 1 | open Core |
2 | 2 | open Timer_types |
3 | 3 | |
4 | -let split_time timer ?now split_num = | |
4 | +let split_time timer ~now split_num = | |
5 | 5 | if split_num < 0 then Some 0 |
6 | 6 | else |
7 | - let curr_time = | |
8 | - match now with Some t -> t | None -> Core_unix.gettimeofday () | |
9 | - in | |
10 | - | |
11 | 7 | match timer.state with |
12 | 8 | | Idle -> None |
13 | 9 | | Paused (splits, start_time, pause_time, _) -> |
@@ -18,21 +14,21 @@ let split_time timer ?now split_num = | ||
18 | 14 | | Timing (splits, start_time, _) | Done (splits, start_time, _) -> |
19 | 15 | if split_num > Array.length splits then None |
20 | 16 | else if split_num = Array.length splits then |
21 | - Some (Duration.between start_time curr_time) | |
17 | + Some (Duration.between start_time now) | |
22 | 18 | else splits.(split_num) |
23 | 19 | |
24 | 20 | let duration timer = |
25 | 21 | match timer.state with |
26 | 22 | | Idle -> 0 |
27 | - | Timing (splits, _, _) | Paused (splits, _, _, _) | Done (splits, _, _) -> ( | |
28 | - match split_time timer (Array.length splits) with | |
23 | + | Timing (splits, _, now) | Paused (splits, _, _, now) | Done (splits, _, now) -> ( | |
24 | + match split_time ~now timer (Array.length splits) with | |
29 | 25 | | Some t -> t |
30 | 26 | | None -> assert false) |
31 | 27 | |
32 | -let ahead_by timer ?now split_num = | |
28 | +let ahead_by timer ~now split_num = | |
33 | 29 | if split_num < 0 then None |
34 | 30 | else |
35 | - let split_time = split_time timer ?now split_num in | |
31 | + let split_time = split_time timer ~now split_num in | |
36 | 32 | let comp_time = |
37 | 33 | match timer.comparison with |
38 | 34 | | None -> None |
@@ -43,9 +39,9 @@ let ahead_by timer ?now split_num = | ||
43 | 39 | | Some st, Some ct -> Some (st - ct) |
44 | 40 | | _ -> None |
45 | 41 | |
46 | -let segment_time timer ?now split_num = | |
47 | - let t0 = split_time timer ?now (split_num - 1) in | |
48 | - let t1 = split_time timer ?now split_num in | |
42 | +let segment_time timer ~now split_num = | |
43 | + let t0 = split_time timer ~now (split_num - 1) in | |
44 | + let t1 = split_time timer ~now split_num in | |
49 | 45 | |
50 | 46 | match (t0, t1) with Some t0', Some t1' -> Some (t1' - t0') | _ -> None |
51 | 47 |
@@ -56,7 +52,8 @@ let current_split timer = | ||
56 | 52 | Some (Array.length splits) |
57 | 53 | |
58 | 54 | let is_gold timer split_num = |
59 | - match (current_split timer, segment_time timer split_num) with | |
55 | + let now = Core_unix.gettimeofday () in | |
56 | + match (current_split timer, segment_time ~now timer split_num) with | |
60 | 57 | | Some n, Some seg_time -> ( |
61 | 58 | if split_num >= n then false |
62 | 59 | else |
@@ -68,9 +65,9 @@ let is_gold timer split_num = | ||
68 | 65 | let updated_golds timer = |
69 | 66 | match timer.state with |
70 | 67 | | Idle -> timer.golds |
71 | - | Timing (splits, _, _) | Paused (splits, _, _, _) | Done (splits, _, _) -> | |
68 | + | Timing (splits, _, now) | Paused (splits, _, _, now) | Done (splits, _, now) -> | |
72 | 69 | let seg_durations = |
73 | - Array.mapi splits ~f:(fun i _ -> segment_time timer i) | |
70 | + Array.mapi splits ~f:(fun i _ -> segment_time ~now timer i) | |
74 | 71 | in |
75 | 72 | let old_durations = Array.map timer.golds ~f:(fun g -> g.duration) in |
76 | 73 |
@@ -1,13 +1,13 @@ | ||
1 | 1 | val is_gold : Timer_types.timer -> int -> bool |
2 | -val ahead_by : Timer_types.timer -> ?now:float -> int -> int option | |
3 | -val segment_time : Timer_types.timer -> ?now:float -> int -> int option | |
2 | +val ahead_by : Timer_types.timer -> now:float -> int -> int option | |
3 | +val segment_time : Timer_types.timer -> now:float -> int -> int option | |
4 | 4 | val archived_split_time : Timer_types.timer -> int -> int option |
5 | 5 | val archived_segment_time : Timer_types.timer -> int -> int option |
6 | 6 | |
7 | 7 | val archive_done_run : |
8 | 8 | Timer_types.timer -> Duration.t option array -> Timer_types.archived_run |
9 | 9 | |
10 | -val split_time : Timer_types.timer -> ?now:float -> int -> int option | |
10 | +val split_time : Timer_types.timer -> now:float -> int -> int option | |
11 | 11 | val gold_sum : Timer_types.timer -> int -> int -> int option |
12 | 12 | val updated_golds : Timer_types.timer -> Timer_types.gold array |
13 | 13 | val updated_pb : Timer_types.timer -> Timer_types.archived_run option |