summaryrefslogtreecommitdiff
path: root/src/c/fastforge_logic.c
blob: 8a668041e0c3f1e0893b7904dbbbf2a1cdc47c18 (plain)
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
#include "fastforge_logic.h"

#ifdef time_t
#undef time_t
#endif
#undef _TIME_H_
#include <time.h>
typedef time_t ff_sys_time_t;
#define time_t long

#include <stdio.h>
#include <stdlib.h>


time_t entry_duration_seconds(const FastEntry *entry) {
  if (!entry || entry->start_time == 0 || entry->end_time <= entry->start_time) {
    return 0;
  }
  return entry->end_time - entry->start_time;
}

uint8_t stage_level_for_elapsed(time_t elapsed_seconds) {
  if (elapsed_seconds >= 24 * 3600) {
    return 3;
  }
  if (elapsed_seconds >= 18 * 3600) {
    return 2;
  }
  if (elapsed_seconds >= 12 * 3600) {
    return 1;
  }
  return 0;
}

const char *stage_text_for_elapsed(time_t elapsed_seconds) {
  if (elapsed_seconds >= 24 * 3600) {
    return "DEEP KETOSIS";
  }
  if (elapsed_seconds >= 18 * 3600) {
    return "EARLY KETOSIS";
  }
  if (elapsed_seconds >= 12 * 3600) {
    return "FAT BURN";
  }
  return "GLYCOGEN";
}

void format_hhmmss(time_t seconds, char *buffer, size_t size) {
  if (seconds < 0) {
    seconds = 0;
  }
  snprintf(buffer, size, "%02d:%02d:%02d",
           (int)(seconds / 3600),
           (int)((seconds % 3600) / 60),
           (int)(seconds % 60));
}

void format_duration_hours_minutes(time_t seconds, char *buffer, size_t size) {
  if (seconds < 0) {
    seconds = 0;
  }
  int hours = (int)(seconds / 3600);
  int minutes = (int)((seconds % 3600) / 60);
  snprintf(buffer, size, "%dh %02dm", hours, minutes);
}

time_t local_day_start(time_t timestamp) {
  if (timestamp <= 0) {
    return 0;
  }

  ff_sys_time_t sys_timestamp = (ff_sys_time_t)timestamp;
  struct tm *tm_info = localtime(&sys_timestamp);
  if (!tm_info) {
    return 0;
  }

  struct tm tm_copy = *tm_info;
  tm_copy.tm_hour = 0;
  tm_copy.tm_min = 0;
  tm_copy.tm_sec = 0;
  tm_copy.tm_isdst = -1;
  return (time_t)mktime(&tm_copy);
}

static int compare_time_t_ascending(const void *a, const void *b) {
  const time_t time_a = *(const time_t *)a;
  const time_t time_b = *(const time_t *)b;
  if (time_a < time_b) {
    return -1;
  }
  if (time_a > time_b) {
    return 1;
  }
  return 0;
}

static bool is_next_local_day(time_t first_day, time_t second_day) {
  if (first_day <= 0 || second_day <= 0 || second_day < first_day) {
    return false;
  }

  ff_sys_time_t sys_first_day = (ff_sys_time_t)first_day;
  struct tm *tm_info = localtime(&sys_first_day);
  if (!tm_info) {
    return false;
  }

  struct tm next_day_tm = *tm_info;
  next_day_tm.tm_mday += 1;
  next_day_tm.tm_hour = 0;
  next_day_tm.tm_min = 0;
  next_day_tm.tm_sec = 0;
  next_day_tm.tm_isdst = -1;
  return (time_t)mktime(&next_day_tm) == second_day;
}

void fastforge_streak_recompute(const FastEntry *entries, int count, time_t now, StreakData *out) {
  if (!out) {
    return;
  }

  out->current_streak = 0;
  out->longest_streak = 0;
  out->last_completed_fast_end = 0;

  if (!entries || count <= 0) {
    return;
  }

  size_t completion_capacity = (size_t)count;
  time_t *completion_days = malloc(completion_capacity * sizeof(*completion_days));
  if (!completion_days) {
    return;
  }
  int completion_day_count = 0;

  for (int i = 0; i < count; i++) {
    const FastEntry *entry = &entries[i];
    time_t duration = entry_duration_seconds(entry);
    if (duration <= 0 || entry->end_time <= 0) {
      continue;
    }

    if (entry->end_time > out->last_completed_fast_end) {
      out->last_completed_fast_end = entry->end_time;
    }

    time_t day_start = local_day_start(entry->end_time);
    if (day_start <= 0) {
      continue;
    }

    completion_days[completion_day_count++] = day_start;
  }

  if (completion_day_count <= 0) {
    free(completion_days);
    return;
  }

  qsort(completion_days, (size_t)completion_day_count, sizeof(time_t), compare_time_t_ascending);

  uint16_t run_length = 0;
  uint16_t longest = 0;
  for (int i = 0; i < completion_day_count; i++) {
    if (i == 0 || completion_days[i] == completion_days[i - 1]) {
      if (i == 0) {
        run_length = 1;
      }
      continue;
    }

    if (is_next_local_day(completion_days[i - 1], completion_days[i])) {
      run_length++;
    } else {
      run_length = 1;
    }

    if (run_length > longest) {
      longest = run_length;
    }
  }

  if (longest == 0) {
    longest = 1;
  }

  time_t today_day_start = local_day_start(now);
  time_t last_completion_day = completion_days[completion_day_count - 1];
  if (last_completion_day == today_day_start ||
      is_next_local_day(last_completion_day, today_day_start)) {
    out->current_streak = run_length;
  }
  out->longest_streak = longest;
  free(completion_days);
}

bool running_fast_is_at_target(const FastEntry *entry, time_t now) {
  if (!entry || entry->start_time == 0 || entry->end_time != 0 || entry->target_minutes == 0) {
    return false;
  }
  return now >= entry->start_time + (time_t)entry->target_minutes * 60;
}