Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/component/splits.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "components.h"
#include <limits.h>

typedef struct _LSSplits {
LSComponent base;
Expand Down Expand Up @@ -205,7 +206,10 @@ static void splits_draw(LSComponent* self_, ls_game* game, ls_timer* timer)

remove_class(self->split_times[i], "time");
remove_class(self->split_times[i], "done");

// Set split_times label to -
gtk_label_set_text(GTK_LABEL(self->split_times[i]), "-");

if (i < timer->curr_split) {
add_class(self->split_times[i], "done");
if (timer->split_times[i]) {
Expand Down
58 changes: 45 additions & 13 deletions src/timer.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "timer.h"
#include <jansson.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Expand Down Expand Up @@ -54,6 +55,13 @@ static void ls_time_string_format(char* string,
int hours, minutes, seconds;
char dot_subsecs[256];
const char* sign = "";

// Check time is not 0 or maxed out, otherwise -
if (time == LLONG_MAX) {
sprintf(string, "-");
return;
}

if (time < 0) {
time = -time;
sign = "-";
Expand Down Expand Up @@ -297,18 +305,30 @@ int ls_game_create(ls_game** game_ptr, const char* path, char** error_msg)
game->split_times[i] = ls_time_value(
json_string_value(split_ref));
}
// Check whether the split time is 0, if it is set it to max value
if (game->split_times[i] == 0) {
game->split_times[i] = LLONG_MAX;
}
if (i && game->split_times[i] && game->split_times[i - 1]) {
game->segment_times[i] = game->split_times[i] - game->split_times[i - 1];
} else if (!i && game->split_times[0]) {
game->segment_times[0] = game->split_times[0];
}

if (game->best_splits[i] == 0) {
game->best_splits[i] = LLONG_MAX;
}
split_ref = json_object_get(split, "best_time");
if (split_ref) {
game->best_splits[i] = ls_time_value(
json_string_value(split_ref));
} else if (game->split_times[i]) {
game->best_splits[i] = game->split_times[i];
}

if (game->best_segments[i] == 0) {
game->best_segments[i] = LLONG_MAX;
}
split_ref = json_object_get(split, "best_segment");
if (split_ref) {
game->best_segments[i] = ls_time_value(
Expand Down Expand Up @@ -398,12 +418,20 @@ int ls_game_save(const ls_game* game)
json_t* split = json_object();
json_object_set_new(split, "title",
json_string(game->split_titles[i]));
ls_time_string_serialized(str, game->split_times[i]);
json_object_set_new(split, "time", json_string(str));
ls_time_string_serialized(str, game->best_splits[i]);
json_object_set_new(split, "best_time", json_string(str));
ls_time_string_serialized(str, game->best_segments[i]);
json_object_set_new(split, "best_segment", json_string(str));

// Only save the split if it's above 0. Otherwise it's impossible to beat 0
if (game->split_times[i] > 0 && game->split_times[i] < LLONG_MAX) {
ls_time_string_serialized(str, game->split_times[i]);
json_object_set_new(split, "time", json_string(str));
}
if (game->best_splits[i] > 0 && game->best_splits[i] < LLONG_MAX) {
ls_time_string_serialized(str, game->best_splits[i]);
json_object_set_new(split, "best_time", json_string(str));
}
if (game->best_segments[i] > 0 && game->best_segments[i] < LLONG_MAX) {
ls_time_string_serialized(str, game->best_segments[i]);
json_object_set_new(split, "best_segment", json_string(str));
}
json_array_append_new(splits, split);
}
json_object_set_new(json, "splits", splits);
Expand Down Expand Up @@ -472,9 +500,10 @@ static void reset_timer(ls_timer* timer)
memset(timer->split_info, 0, size);
timer->sum_of_bests = 0;
for (i = 0; i < timer->game->split_count; ++i) {
if (timer->best_segments[i]) {
// Check no segments are erroring with LLONG_MAX
if (timer->best_segments[i] && timer->best_segments[i] < LLONG_MAX) {
timer->sum_of_bests += timer->best_segments[i];
} else if (timer->game->best_segments[i]) {
} else if (timer->game->best_segments[i] && timer->game->best_segments[i] < LLONG_MAX) {
timer->sum_of_bests += timer->game->best_segments[i];
} else {
timer->sum_of_bests = 0;
Expand Down Expand Up @@ -557,8 +586,8 @@ void ls_timer_step(ls_timer* timer, long long now)
timer->time += delta; // Accumulate the elapsed time
if (timer->curr_split < timer->game->split_count) {
timer->split_times[timer->curr_split] = timer->time;
// calc delta
if (timer->game->split_times[timer->curr_split]) {
// calc delta and check it's not an error of LLONG_MAX
if (timer->game->split_times[timer->curr_split] && timer->game->split_times[timer->curr_split] < LLONG_MAX) {
timer->split_deltas[timer->curr_split] = timer->split_times[timer->curr_split]
- timer->game->split_times[timer->curr_split];
}
Expand All @@ -574,7 +603,8 @@ void ls_timer_step(ls_timer* timer, long long now)
if (timer->curr_split) {
timer->segment_times[timer->curr_split] -= timer->split_times[timer->curr_split - 1];
}
if (timer->game->segment_times[timer->curr_split]) {
// For previous segment in footer
if (timer->game->segment_times[timer->curr_split] && timer->game->segment_times[timer->curr_split] < LLONG_MAX) {
timer->segment_deltas[timer->curr_split] = timer->segment_times[timer->curr_split]
- timer->game->segment_times[timer->curr_split];
}
Expand Down Expand Up @@ -636,15 +666,17 @@ int ls_timer_split(ls_timer* timer)
// update sum of bests
timer->sum_of_bests = 0;
for (i = 0; i < timer->game->split_count; ++i) {
if (timer->best_segments[i]) {
// Check if any best segment is missing/LLONG_MAX
if (timer->best_segments[i] && timer->best_segments[i] < LLONG_MAX) {
timer->sum_of_bests += timer->best_segments[i];
} else if (timer->game->best_segments[i]) {
} else if (timer->game->best_segments[i] && timer->game->best_segments[i] < LLONG_MAX) {
timer->sum_of_bests += timer->game->best_segments[i];
} else {
timer->sum_of_bests = 0;
break;
}
}

++timer->curr_split;
// stop timer if last split
if (timer->curr_split == timer->game->split_count) {
Expand Down