X-Git-Url: https://git.sur5r.net/?a=blobdiff_plain;f=src%2Fkasse.c;h=aab97fec2a99d08ca8b3ce839524fe32c31064f5;hb=669dd69c90cd261be216792e208c863df373e620;hp=8c02cef1e16d6932e9404c74ef9d9bfb9398d78c;hpb=a0b9ff6838cd92fdfb551ef3a09fcbada5eafbf0;p=c128-kasse diff --git a/src/kasse.c b/src/kasse.c index 8c02cef..aab97fe 100644 --- a/src/kasse.c +++ b/src/kasse.c @@ -1,9 +1,10 @@ /* * RGB2R-C128-Kassenprogramm - * (c) 2007 phil_fry, sECuRE, sur5r + * © 2007-2009 phil_fry, sECuRE, sur5r * See LICENSE for license information * */ +#define _IS_KASSE #include #include #include @@ -15,183 +16,371 @@ #include "kasse.h" #include "credit_manager.h" #include "c128time.h" +#include "print.h" +#include "version.h" // drucker 4 oder 5 // graphic 4,0,10 -char print_buffer[81]; +void print_item(BYTE i) { + char profit[10]; + if (format_euro(profit, sizeof(profit), status.status[i].price) == NULL) { + cprintf("Preis %ld konnte nicht umgerechnet werden\r\n", status.status[i].price); + exit(1); + } + textcolor(TC_YELLOW); + cprintf("%2d", i); + textcolor(TC_LIGHT_GRAY); + cprintf(": %-" xstr(MAX_ITEM_NAME_LENGTH) "s \xDD%s, %3dx ", + status.status[i].item_name, profit, status.status[i].times_sold); +} /* Hauptbildschirm ausgeben */ -static void print_screen() { +static void print_screen(void) { BYTE i = 0; char *time = get_time(); char profit[10]; clrscr(); if (format_euro(profit, 10, money) == NULL) { - cprintf("Einname %ld konnte nicht umgerechnet werden\r\n", money); + cprintf("Einnahme %ld konnte nicht umgerechnet werden\r\n", money); exit(1); } - cprintf("C128-Kassenprogramm (phil_fry, sECuRE, sur5r)\r\ -\r\nUhrzeit: %s (wird nicht aktualisiert)\r\ -Eingenommen: %s, Verkauft: %ld Flaschen, Drucken: %s\r\n\r\n", - time, profit, items_sold, (printing == 1 ? "ein" : "aus")); - for (; i < status.num_items; ++i) - cprintf("Eintrag %x: %s (%d Cents, %d mal verkauft)\r\n", - i, status.status[i].item_name, status.status[i].price, status.status[i].times_sold); - cprintf("\r\nBefehle: s) Daten sichern d) Drucken umschalten\r\ -g) Guthabenverwaltung z) Zeit setzen\r\ -n) Neues Getraenk q) Beenden\r\n"); -} + textcolor(TC_CYAN); + cprintf("C128-Kassenprogramm (phil_fry, sECuRE, sur5r) " GV "\r\n"); + textcolor(TC_LIGHT_GRAY); + cprintf("\r\nUhrzeit: %s (wird nicht aktualisiert)\r\n" + "Eingenommen: %s, Verkauft: %ld Dinge, Drucken: %s\r\n", + time, profit, items_sold, (printing == 1 ? "ein" : "aus")); + textcolor(TC_LIGHT_GRAY); + cprintf(" \xB0" + "\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xB2" + "\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xB2" + "\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xB2" + "\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xAE\r\n"); + for (; i < min(status.num_items, 15); ++i) { -static void log_file(const char *s) { - BYTE c = cbm_open((BYTE)8, (BYTE)8, (BYTE)1, "log"); - if (c != 0) { - c128_perror(c, "cbm_open(log)"); - save_items(); - save_credits(); - exit(1); - } - c = cbm_write((BYTE)8, s, strlen(s)); - if (c != strlen(s)) { - cprintf("Could not save logfile, please make sure the floppy is not full!\n"); - save_items(); - save_credits(); - exit(1); - } - cbm_close((BYTE)8); -} + cprintf(" \xDD"); + print_item(i); + cprintf("\xDD"); -static char retry_or_quit() { - char *c; - do { - cprintf("\r\nr)etry or q)uit?\r\n"); - c = get_input(); - } while ((*c != 'r') && (*c != 'q')); - return *c; + /* if we have more than 15 items, use the second column */ + if ((i+15) < status.num_items) { + print_item(i+15); + cprintf("\xDD"); + } else { + cprintf(" \xDD \xDD"); + } + + cprintf("\r\n"); + } + cprintf(" \xAD" + "\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xB1" + "\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xB1" + "\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xB1" + "\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xBD\r\n"); + textcolor(TC_YELLOW); + cprintf(" s"); + textcolor(TC_LIGHT_GRAY); + cprintf(") Daten sichern "); + textcolor(TC_YELLOW); + cprintf("g"); + textcolor(TC_LIGHT_GRAY); + cprintf(") Guthabenverwaltung\r\n"); + textcolor(TC_YELLOW); + cprintf(" z"); + textcolor(TC_LIGHT_GRAY); + cprintf(") Zeit setzen "); + textcolor(TC_YELLOW); + cprintf("f"); + textcolor(TC_LIGHT_GRAY); + cprintf(") Freitext verkaufen "); + textcolor(TC_YELLOW); + cprintf("q"); + textcolor(TC_LIGHT_GRAY); + cprintf(") Beenden\r\n"); } -/* Druckt eine entsprechende Zeile aus */ -static void print_log(BYTE n, int einheiten, char *nickname) { - BYTE c; +/* + * Prints a line and logs it to file. Every line can be at max 80 characters. + * + */ +static void print_log(char *name, int item_price, int einheiten, char *nickname, char *rest) { char *time = get_time(); char price[10]; /* Format: - Transaction-ID (Anzahl verkaufter Einträge, inklusive des zu druckenden!) - Uhrzeit - Eintragname (= Getränk) - Preis (in Cents) - Anzahl - Nickname (falls es vom Guthaben abgezogen wird) + Transaction-ID (Anzahl verkaufter Einträge, inklusive des zu druckenden!) -- 6-stellig + Uhrzeit -- 8-stellig + Eintragname (= Getränk) -- 9-stellig + Preis (in Cents) -- 9-stellig + Anzahl -- 2-stellig + Nickname (falls es vom Guthaben abgezogen wird) -- 10-stellig + restguthaben (9-stellig) + + + 7 leerzeichen + --> 48 zeichen */ - if (format_euro(price, 10, status.status[n].price) == NULL) { - cprintf("Preis %d konnte nicht umgerechnet werden\r\n", status.status[n].price); + if (format_euro(price, 10, item_price) == NULL) { + cprintf("Preis %d konnte nicht umgerechnet werden\r\n", item_price); exit(1); } - - RETRY:; - sprintf(print_buffer, "[%lu] %s - %s - %s - %d - an %s\r\n", - items_sold, time, status.status[n].item_name, price, - einheiten, (nickname != NULL ? nickname : "Unbekannt")); - c = cbm_open((BYTE)4, (BYTE)4, (BYTE)0, NULL); - if (c != 0) { - c128_perror(c, "cbm_open(printer)"); - if (retry_or_quit() == 'q') { - save_items(); - save_credits(); - exit(1); - } - goto RETRY; - } - c = cbm_write((BYTE)4, print_buffer, strlen(print_buffer)); - if (c != strlen(print_buffer)) { - c128_perror(c, "write(printer)"); - if (retry_or_quit() == 'q') { - save_items(); - save_credits(); - exit(1); - } - goto RETRY; - } - cbm_close((BYTE)4); - log_file(print_buffer); + + sprintf(print_buffer, "%c[%3u] %s - %-" xstr(MAX_ITEM_NAME_LENGTH) "s - %s - %s - %d - an %s\r", 17, + status.transaction_id, time, name, price, rest, + einheiten, (*nickname != '\0' ? nickname : "Unbekannt")); + status.transaction_id++; + print_the_buffer(); } -/* Dialog, der einen durch's Abrechnen der Einträge führt */ -void buy(BYTE n) { +/* dialog which is called for each bought item */ +static signed int buy(char *name, unsigned int price) { int negative = 1; char entered[5] = {'1', 0, 0, 0, 0}; BYTE i = 0, matches = 0; - BYTE c, nickname_len; + BYTE c, x, y, nickname_len; int einheiten; - char *nickname; + char *input; + char nickname[NICKNAME_MAX_LEN+1]; + char rest[11]; struct credits_t *credit; - if (status.status[n].item_name == NULL) { - cprintf("FEHLER: Diese Einheit existiert nicht.\r\n"); - return; - } - cprintf("Wieviel Einheiten \"%s\"? [1] \r\n", status.status[n].item_name); + memset(nickname, '\0', sizeof(nickname)); + memset(rest, ' ', sizeof(rest)); + rest[8] = '\0'; + + clrscr(); + cprintf("Wieviel Einheiten \"%s\"? [1] \r\n", name); + x = wherex(); + y = wherey(); while (1) { - c = getchar(); - if (c == 13) + /* Buffer-Ende erreicht? */ + if (i == 4) break; - else if (c == 27) { + + c = cgetc(); + /* Enter */ + if (c == PETSCII_CR) + break; + /* Backspace */ + if (c == PETSCII_DEL) { + if (i == 0) + continue; + entered[--i] = '\0'; + cputcxy(x+i, y, ' '); + gotoxy(x+i, y); + continue; + } + if (c == 27) { cprintf("Kauf abgebrochen, druecke RETURN...\r\n"); get_input(); - return; - } else if (c == '-' && i == 0) + return 1; + } + if (c == '-' && i == 0) { negative = -1; - else if (c > 47 && c < 58) + cputc(c); + } else if (c >= PETSCII_0 && c <= PETSCII_9) { entered[i++] = c; + cputc(c); + } + + /* Ungültige Eingabe (keine Ziffer), einfach ignorieren */ } einheiten = atoi(entered) * negative; + + if (einheiten > 100 || einheiten < -100 || einheiten == 0) { + cprintf("\r\nEinheit nicht in [-100, 100] oder 0, Abbruch, druecke RETURN...\r\n"); + cgetc(); + return 1; + } toggle_videomode(); - cprintf("%dx %s fuer ", einheiten, status.status[n].item_name); + cprintf("\r\n *** VERKAUF ***\r\n\r\n"); + cprintf("%dx %s", einheiten, name); toggle_videomode(); - + cprintf("\r\nAuf ein Guthaben kaufen? Wenn ja, Nickname eingeben:\r\n"); - nickname = get_input(); - toggle_videomode(); - cprintf("%s\r\n", nickname); - toggle_videomode(); + { + BYTE i; + BYTE x; + BYTE y; + BYTE matches; + char *uniquematch; + input_terminator_t terminator; + while (1) { + terminator = get_input_terminated_by( + INPUT_TERMINATOR_RETURN | + INPUT_TERMINATOR_SPACE, + nickname, + sizeof(nickname)); - if (nickname != NULL && *nickname != '\0' && *nickname != 32) { + /* Clear the screen from any previous completions */ + x = wherex(); + y = wherey(); + for (i = 1; i < 7; i++) { + /* "Completion:" is longer than NICKNAME_MAX_LEN */ + cclearxy(0, y + i, strlen("Completion:")); + } + gotoxy(x, y); + + if (terminator != INPUT_TERMINATOR_SPACE) { + break; + } + + matches = 0; + uniquematch = NULL; + for (i = 0; i < credits.num_items; i++) { + if (strncmp(nickname, credits.credits[i].nickname, strlen(nickname)) != 0) { + continue; + } + matches++; + if (matches > 1) { + break; + } + uniquematch = credits.credits[i].nickname; + } + if (matches == 1) { + /* Display the rest of the nickname */ + textcolor(TC_LIGHT_GREEN); + cprintf("%s", uniquematch + strlen(nickname)); + textcolor(TC_LIGHT_GRAY); + strcat(nickname, uniquematch + strlen(nickname)); + } else { + /* Multiple nicknames match what was entered so far. Abort and + * display all matches, then prompt the user again. */ + char completion[NICKNAME_MAX_LEN+1]; + BYTE len = strlen(nickname); + x = wherex(); + y = wherey(); + cprintf("\r\nCompletion:\r\n"); + matches = 0; + for (i = 0; i < credits.num_items; i++) { + if (strncmp(nickname, credits.credits[i].nickname, len) != 0) { + continue; + } + if (++matches == 5) { + cprintf("...\r\n"); + break; + } + strcpy(completion, credits.credits[i].nickname); + *(completion + len) = '\0'; + cprintf("%s", completion); + textcolor(TC_LIGHT_GREEN); + cprintf("%c", *(credits.credits[i].nickname + len)); + textcolor(TC_LIGHT_GRAY); + cprintf("%s\r\n", completion + len + 1); + } + gotoxy(x, y); + } + } + } + if (*nickname != '\0') { + toggle_videomode(); + cprintf(" fuer %s\r\n", nickname); + toggle_videomode(); + } + + if (*nickname != '\0' && *nickname != 32) { nickname_len = strlen(nickname); /* go through credits and remove the amount of money or set nickname * to NULL if no such credit could be found */ credit = find_credit(nickname); if (credit != NULL) { - if ((signed int)credit->credit < ((signed int)status.status[n].price * einheiten)) { - cprintf("Sorry, %s hat nicht genug Geld :-(\r\n", nickname); - return; + while ((signed int)credit->credit < ((signed int)price * einheiten)) { + if (format_euro(rest, 10, credit->credit) == NULL) { + cprintf("Preis %d konnte nicht umgerechnet werden\r\n", credit->credit); + exit(1); + } + cprintf("\r\n%s hat nicht genug Geld (%s). e) einzahlen a) abbruch \r\n", nickname, rest); + c = cgetc(); + if (c == 'e') { + deposit_credit(nickname); + } else { + return 0; + } } - /* Geld abziehen */ - credit->credit -= (status.status[n].price * einheiten); - cprintf("\r\nVerbleibendes Guthaben fuer %s: %d Cents. Druecke RETURN...\r\n", - nickname, credit->credit); + /* substract money */ + credit->credit -= (price * einheiten); + + if (format_euro(rest, 10, credit->credit) == NULL) { + cprintf("Preis %d konnte nicht umgerechnet werden\r\n", credit->credit); + exit(1); + } + + textcolor(TC_LIGHT_GREEN); + cprintf("\r\nVerbleibendes Guthaben fuer %s: %s. Druecke RETURN...\r\n", + nickname, rest); + textcolor(TC_LIGHT_GRAY); toggle_videomode(); - cprintf("\r\nDein verbleibendes Guthaben betraegt %d Cents.\r\n", credit->credit); + cprintf("\r\nDein Guthaben betraegt noch %s.\r\n", rest); toggle_videomode(); get_input(); matches++; } else { + textcolor(TC_LIGHT_RED); cprintf("\r\nNickname nicht gefunden in der Guthabenverwaltung! Abbruch, druecke RETURN...\r\n"); + textcolor(TC_LIGHT_GRAY); get_input(); - return; + return 0; } } else { /* Ensure that nickname is NULL if it's empty because it's used in print_log */ - nickname = NULL; + *nickname = '\0'; } - status.status[n].times_sold += einheiten; - money += status.status[n].price * einheiten; + money += price * einheiten; items_sold += einheiten; if (printing == 1) - print_log(n, einheiten, nickname); + print_log(name, price, einheiten, nickname, rest); + + return einheiten; +} + +void buy_stock(BYTE n) { + if (n >= status.num_items || status.status[n].item_name == NULL) { + cprintf("FEHLER: Diese Einheit existiert nicht.\r\n"); + get_input(); + return; + } + + status.status[n].times_sold += buy(status.status[n].item_name, status.status[n].price); } -void set_time_interactive() { +void buy_custom(void) { + BYTE c = 0, i = 0; + int negative = 1; + char entered[5] = {'1', 0, 0, 0, 0}; + char *input, name[20]; + int price; + + clrscr(); + memset(name, '\0', 20); + cprintf("\r\nWas soll gekauft werden?\r\n"); + input = get_input(); + strncpy(name, input, 20); + if (*name == '\0') + return; + + cprintf("\r\nWie teuer ist \"%s\" (in cents)?\r\n", name); + while (1) { + c = cgetc(); + if (c == 13) + break; + cputc(c); + if (c == 27) { + cprintf("Kauf abgebrochen, druecke RETURN...\r\n"); + get_input(); + return; + } else if (c == '-' && i == 0) + negative = -1; + else if (c > 47 && c < 58) + entered[i++] = c; + } + price = atoi(entered) * negative; + + cprintf("\r\n"); + + buy(name, price); +} + +void set_time_interactive(void) { BYTE part[3] = {'0', '0', '\0'}; BYTE tp1, tp2, tp3; char *time_input, *time; @@ -209,61 +398,75 @@ void set_time_interactive() { set_time(tp1, tp2, tp3); time = get_time(); - cprintf("Zeit gesetzt: %s\r\n", time); + cprintf("\r\nZeit gesetzt: %s\r\n", time); } -int main() { +int main(void) { char *c; + char *time; + if (VIDEOMODE == 40) toggle_videomode(); - /* Zeit erstmalig setzen */ + clrscr(); + + /* Allocate logging buffer memory */ + init_log(); + + /* Set time initially, c128 doesn't know it */ set_time_interactive(); + POKE(216, 255); - /* Variablen zurechtbiegen */ - credits.num_items = 0; - status.num_items = 0; - /* Konfigurationsdatei laden */ + + /* Load configuration */ load_config(); - /* Einträge (=Getränke) und Zustand laden */ + + /* Load items (= drinks) */ load_items(); - /* Guthaben laden */ + /* Load credits */ load_credits(); + + time = get_time(); + sprintf(print_buffer, "%c--------------------------------------------------------------------------------\r", 17); + print_the_buffer(); + sprintf(print_buffer, "%cC128-Kasse Version " GV "\r", 17); + print_the_buffer(); + + sprintf(print_buffer, "%cKasse gestartet um %s. Nutze logfile log-%u, offset %d.\r", 17, time, log_num, log_heap_offset); + print_the_buffer(); + + print_header(); + while (1) { - /* Bildschirm anzeigen */ print_screen(); - /* Tastatureingaben abfragen */ c = get_input(); - /* und eventuell weitere Dialoge anzeigen */ + /* ...display dialogs eventually */ if (*c > 47 && *c < 58) { - buy((*c) - 48); + /* if the input starts with a digit, we will interpret it as a number + * for the item to be sold */ + buy_stock(atoi(c)); toggle_videomode(); clrscr(); toggle_videomode(); - }else if (*c == 's') { - /* Zustandsdatei schreiben */ + } else if (*c == 'f') { + buy_custom(); + toggle_videomode(); + clrscr(); + toggle_videomode(); + } else if (*c == 's') { save_items(); save_credits(); - cprintf("Statefile/Creditfile gesichert, druecke RETURN...\r\n"); - get_input(); - } else if (*c == 'd') { - /* Drucken an- oder ausschalten */ - printing = (printing == 1 ? 0 : 1); - cprintf("Drucken ist nun %s, druecke RETURN...\r\n", - (printing == 1 ? "eingeschaltet" : "ausgeschaltet")); + log_flush(); + cprintf("\r\nStatefile/Creditfile/Log gesichert, druecke RETURN...\r\n"); get_input(); } else if (*c == 'g') { - /* Guthabenverwalter aufrufen */ credit_manager(); } else if (*c == 'z') { - /* Zeit setzen */ set_time_interactive(); - } else if (*c == 'n') { - strcpy(status.status[status.num_items].item_name, "mate"); - status.status[status.num_items].price = 23; - status.status[status.num_items].times_sold = 5; - status.num_items++; } else if (*c == 'q') break; } - cprintf("BYEBYE\r\n"); + clrscr(); + cprintf("\r\nBYEBYE\r\n"); + + return 0; }