Skip to content

Commit

Permalink
Confirmation dialog if trying to save worse time
Browse files Browse the repository at this point in the history
  • Loading branch information
JyKin authored and paoloose committed Feb 2, 2024
1 parent a48eeea commit 7456bfe
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
19 changes: 17 additions & 2 deletions urn-gtk.c
Original file line number Diff line number Diff line change
Expand Up @@ -711,8 711,23 @@ static void save_activated(GSimpleAction *action,
gtk_window_get_size(GTK_WINDOW(win), &width, &height);
win->game->width = width;
win->game->height = height;
urn_game_update_splits(win->game, win->timer);
save_game(win->game);
int saving = 1;
if (!urn_is_timer_better(win->game, win->timer)) {
GtkWidget *confirm = gtk_message_dialog_new(GTK_WINDOW(win),
GTK_DIALOG_MODAL,
GTK_MESSAGE_QUESTION,
GTK_BUTTONS_YES_NO,
"This run is worse than saved one, continue?");
gint ret = gtk_dialog_run(GTK_DIALOG(confirm));
if (ret != GTK_RESPONSE_YES) {
saving = 0;
}
gtk_widget_destroy(confirm);
}
if (saving) {
urn_game_update_splits(win->game, win->timer);
save_game(win->game);
}
}
}

Expand Down
17 changes: 17 additions & 0 deletions urn.c
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 533,23 @@ int urn_game_save(const urn_game *game) {
return error;
}

int urn_is_timer_better(urn_game *game, urn_timer *timer) {
int i = game->split_count - 1;
/* find the latest split with a time */
while (i >= 0) {
if (timer->split_times[i] != 0ll || game->split_times[i] != 0ll)
break;
i--;
}
if (i < 0)
return 1;
if (timer->split_times[i] == 0ll)
return 0;
if (game->split_times[i] == 0ll)
return 1;
return timer->split_times[i] < game->split_times[i];
}

void urn_timer_release(urn_timer *timer) {
if (timer->split_times) {
free(timer->split_times);
Expand Down
2 changes: 2 additions & 0 deletions urn.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 76,8 @@ int urn_game_save(const urn_game *game);

void urn_game_release(urn_game *game);

int urn_is_timer_better(urn_game *game, urn_timer *timer);

int urn_timer_create(urn_timer **timer_ptr, urn_game *game);

void urn_timer_release(urn_timer *timer);
Expand Down

0 comments on commit 7456bfe

Please sign in to comment.