From bc6cba16665c63c29d54bef5d72e0f7cdf2bca05 Mon Sep 17 00:00:00 2001 From: sECuRE Date: Tue, 23 Oct 2007 15:02:48 +0000 Subject: [PATCH] implement CREDITS into itemz git-svn-id: https://shell.noname-ev.de/svn/kasse/c128@55 af93e077-1a23-4f1e-9cbe-9382a9d578f5 --- include/config.h | 2 +- src/itemz.c | 99 +++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 95 insertions(+), 6 deletions(-) diff --git a/include/config.h b/include/config.h index 2eef4da..5393a56 100644 --- a/include/config.h +++ b/include/config.h @@ -3,7 +3,7 @@ #ifndef CONFIG_H_ #define CONFIG_H_ -#define MAX_ITEMS 15 +#define MAX_ITEMS 16 #define MAX_CREDIT_ITEMS 75 /* Eingenommes Geld in Cent */ diff --git a/src/itemz.c b/src/itemz.c index 41f2fd5..4046c21 100644 --- a/src/itemz.c +++ b/src/itemz.c @@ -17,9 +17,10 @@ enum itemz_mode { MODE_ITEMS, MODE_CREDITS }; static enum itemz_mode mode = MODE_ITEMS; +static BYTE current_credits_page = 0; static void print_screen() { - BYTE i; + BYTE i, pages; char buffer[10]; clrscr(); @@ -34,10 +35,22 @@ static void print_screen() { cprintf("Eintrag %x: %s (%s, %d mal verkauft)\r\n", i, status.status[i].item_name, buffer, status.status[i].times_sold); } - cprintf("\r\nn) Neuer Eintrag d) Eintrag loeschen s) Datei speichern\r\n"); + cprintf("\r\nn) Neu d) Loeschen s) Speichern m) Modus\r\n"); } else { - cprintf("Datei: CREDITS\r\n\r\n"); - /* TODO: display credits */ + /* 16 entries per page */ + pages = (credits.num_items / 16); + if (current_credits_page > pages) + current_credits_page = pages; + cprintf("Datei: CREDITS (Seite %d von %d)\r\n\r\n", current_credits_page, pages); + for (i = (current_credits_page * 16); i < credits.num_items && i < ((current_credits_page+1) * 16); i++) { + if (format_euro(buffer, 10, credits.credits[i].credit) != buffer) { + cprintf("Error: Could not format credit %d\r\n", credits.credits[i].credit); + exit(1); + } + + cprintf("%d: %s: %s\r\n", i, credits.credits[i].nickname, buffer); + } + cprintf("\r\nn) Neu d) Loeschen b) Seite hoch f) Seite runter\r\ne) Aendern s) Speichern m) Modus\r\n"); } } @@ -45,6 +58,12 @@ static void new_item() { char *input, *name; int price; + if (status.num_items == 16) { + cprintf("\rEs ist bereits die maximale Anzahl an Eintraegen erreicht, druecke RETURN...\r\n"); + input = get_input(); + return; + } + cprintf("\rName des Eintrags:\r\n"); if ((input = get_input()) == NULL || *input == '\0') return; @@ -59,6 +78,7 @@ static void new_item() { status.status[status.num_items].price = price; status.status[status.num_items].times_sold = atoi(input); status.num_items++; + free(name); } static void _delete_item(BYTE num) { @@ -89,6 +109,55 @@ static void delete_item() { status.num_items--; } +static void new_credit() { + char *input, *name; + int credit; + + if (credits.num_items == 75) { + cprintf("\rEs ist bereits die maximale Anzahl an Eintraegen erreicht, druecke RETURN...\r\n"); + input = get_input(); + return; + } + + cprintf("\rNickname:\r\n"); + if ((input = get_input()) == NULL || *input == '\0') + return; + name = strdup(input); + cprintf("\r\nGuthaben in Cents:\r\n"); + if ((input = get_input()) == NULL || *input == '\0' || (credit = atoi(input)) == 0) + return; + strcpy(credits.credits[credits.num_items].nickname, name); + credits.credits[credits.num_items].credit = credit; + credits.num_items++; + free(name); +} + +static void _delete_credit(BYTE num) { + memset(credits.credits[num].nickname, '\0', 11); + credits.credits[num].credit = 0; +} + +static void delete_credit() { + char *input; + BYTE num, last; + + cprintf("\r Welcher Eintrag soll geloescht werden?\r\n"); + if ((input = get_input()) == NULL || *input == '\0') + return; + num = atoi(input); + if (credits.num_items > 1) { + /* Swap last item with this one and delete the last one to avoid holes */ + last = (credits.num_items - 1); + strcpy(credits.credits[num].nickname, credits.credits[last].nickname); + credits.credits[num].credit = credits.credits[last].credit; + _delete_credit(last); + } else { + /* Just delete it */ + _delete_credit(num); + } + credits.num_items--; +} + int main() { char *c; toggle_videomode(); @@ -110,12 +179,32 @@ int main() { delete_item(); else if (*c == 's') save_items(); + else if (*c == 'm') + mode = MODE_CREDITS; else { cprintf("Unbekannter Befehl, druecke RETURN...\r\n"); c = get_input(); } } else { - /* TODO: code */ + if (*c == 'm') + mode = MODE_ITEMS; + else if (*c == 'n') + new_credit(); + else if (*c == 'd') + delete_credit(); + else if (*c == 's') + save_credits(); + else if (*c == 'f') { + if (current_credits_page < (credits.num_items / 16)) + current_credits_page++; + } else if (*c == 'b') { + if (current_credits_page > 0) + current_credits_page--; + } + else { + cprintf("Unbekannter Befehl, druecke RETURN...\r\n"); + c = get_input(); + } } } return 0; -- 2.39.5