2 * RGB2R-C128-Kassenprogramm
3 * © 2007-2009 phil_fry, sECuRE, sur5r
4 * See LICENSE for license information
17 #include "credit_manager.h"
24 void print_item(BYTE i) {
26 if (format_euro(profit, sizeof(profit), status.status[i].price) == NULL) {
27 cprintf("Preis %ld konnte nicht umgerechnet werden\r\n",
28 status.status[i].price);
33 textcolor(TC_LIGHT_GRAY);
34 cprintf(": %-" xstr(MAX_ITEM_NAME_LENGTH) "s \xDD%s, %3dx ",
35 status.status[i].item_name, profit, status.status[i].times_sold);
38 /* Hauptbildschirm ausgeben */
39 static void print_screen(void) {
41 char *time = get_time();
44 if (format_euro(profit, 10, money) == NULL) {
45 cprintf("Einnahme %ld konnte nicht umgerechnet werden\r\n", money);
49 cprintf("C128-Kassenprogramm (phil_fry, sECuRE, sur5r) " GV "\r\n");
50 textcolor(TC_LIGHT_GRAY);
51 cprintf("\r\nUhrzeit: %s (wird nicht aktualisiert)\r\n"
52 "Eingenommen: %s, Verkauft: %ld Dinge, Drucken: %s\r\n",
53 time, profit, items_sold, (printing == 1 ? "ein" : "aus"));
54 textcolor(TC_LIGHT_GRAY);
56 "\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xB2"
57 "\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xB2"
58 "\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xB2"
59 "\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xAE"
61 for (; i < min(status.num_items, 15); ++i) {
67 /* if we have more than 15 items, use the second column */
68 if ((i + 15) < status.num_items) {
72 cprintf(" \xDD \xDD");
78 "\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xB1"
79 "\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xB1"
80 "\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xB1"
81 "\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xBD"
85 textcolor(TC_LIGHT_GRAY);
86 cprintf(") Daten sichern ");
89 textcolor(TC_LIGHT_GRAY);
90 cprintf(") Guthabenverwaltung\r\n");
93 textcolor(TC_LIGHT_GRAY);
94 cprintf(") Zeit setzen ");
97 textcolor(TC_LIGHT_GRAY);
98 cprintf(") Freitext verkaufen ");
101 textcolor(TC_LIGHT_GRAY);
102 cprintf(") Beenden\r\n");
106 * Prints a line and logs it to file. Every line can be at max 80 characters.
109 static void print_log(char *name, int item_price, int einheiten, char *nickname,
111 char *time = get_time();
114 Transaction-ID (Anzahl verkaufter Einträge, inklusive des zu druckenden!)
117 Eintragname (= Getränk) -- 9-stellig
118 Preis (in Cents) -- 9-stellig
120 Nickname (falls es vom Guthaben abgezogen wird) -- 10-stellig
121 restguthaben (9-stellig)
126 if (format_euro(price, 10, item_price) == NULL) {
127 cprintf("Preis %d konnte nicht umgerechnet werden\r\n", item_price);
131 sprintf(print_buffer, "%c[%3u] %s - %-" xstr(
132 MAX_ITEM_NAME_LENGTH) "s - %s - %s - %d - an %s\r",
133 17, status.transaction_id, time, name, price, rest, einheiten,
134 (*nickname != '\0' ? nickname : "Unbekannt"));
135 status.transaction_id++;
139 /* dialog which is called for each bought item */
140 static signed int buy(char *name, unsigned int price) {
142 char entered[5] = {'1', 0, 0, 0, 0};
143 BYTE i = 0, matches = 0;
144 BYTE c, x, y, nickname_len;
146 char nickname[NICKNAME_MAX_LEN + 1];
148 struct credits_t *credit;
150 memset(nickname, '\0', sizeof(nickname));
151 memset(rest, ' ', sizeof(rest));
155 cprintf("Wieviel Einheiten \"%s\"? [1] \r\n", name);
159 /* Buffer-Ende erreicht? */
168 if (c == PETSCII_DEL) {
172 cputcxy(x + i, y, ' ');
177 cprintf("Kauf abgebrochen, druecke RETURN...\r\n");
181 if (c == '-' && i == 0) {
184 } else if (c >= PETSCII_0 && c <= PETSCII_9) {
189 /* Ungültige Eingabe (keine Ziffer), einfach ignorieren */
191 einheiten = atoi(entered) * negative;
193 if (einheiten > 100 || einheiten < -100 || einheiten == 0) {
194 cprintf("\r\nEinheit nicht in [-100, 100] oder 0, Abbruch, druecke "
201 cprintf("\r\n *** VERKAUF ***\r\n\r\n");
202 cprintf("%dx %s", einheiten, name);
205 cprintf("\r\nAuf ein Guthaben kaufen? Wenn ja, Nickname eingeben:\r\n");
212 input_terminator_t terminator;
214 terminator = get_input_terminated_by(INPUT_TERMINATOR_RETURN |
215 INPUT_TERMINATOR_SPACE,
216 nickname, sizeof(nickname));
218 /* Clear the screen from any previous completions */
221 for (i = 1; i < 7; i++) {
222 /* "Completion:" is longer than NICKNAME_MAX_LEN */
223 cclearxy(0, y + i, strlen("Completion:"));
227 if (terminator != INPUT_TERMINATOR_SPACE) {
233 for (i = 0; i < credits.num_items; i++) {
234 if (strncmp(nickname, credits.credits[i].nickname, strlen(nickname)) !=
242 uniquematch = credits.credits[i].nickname;
245 /* Display the rest of the nickname */
246 textcolor(TC_LIGHT_GREEN);
247 cprintf("%s", uniquematch + strlen(nickname));
248 textcolor(TC_LIGHT_GRAY);
249 strcat(nickname, uniquematch + strlen(nickname));
251 /* Multiple nicknames match what was entered so far. Abort and
252 * display all matches, then prompt the user again. */
253 char completion[NICKNAME_MAX_LEN + 1];
254 BYTE len = strlen(nickname);
257 cprintf("\r\nCompletion:\r\n");
259 for (i = 0; i < credits.num_items; i++) {
260 if (strncmp(nickname, credits.credits[i].nickname, len) != 0) {
263 if (++matches == 5) {
267 strcpy(completion, credits.credits[i].nickname);
268 *(completion + len) = '\0';
269 cprintf("%s", completion);
270 textcolor(TC_LIGHT_GREEN);
271 cprintf("%c", *(credits.credits[i].nickname + len));
272 textcolor(TC_LIGHT_GRAY);
273 cprintf("%s\r\n", completion + len + 1);
279 if (*nickname != '\0') {
281 cprintf(" fuer %s\r\n", nickname);
285 if (*nickname != '\0' && *nickname != 32) {
286 nickname_len = strlen(nickname);
287 /* go through credits and remove the amount of money or set nickname
288 * to NULL if no such credit could be found */
289 credit = find_credit(nickname);
290 if (credit != NULL) {
291 while ((signed int)credit->credit < ((signed int)price * einheiten)) {
292 if (format_euro(rest, 10, credit->credit) == NULL) {
293 cprintf("Preis %d konnte nicht umgerechnet werden\r\n",
298 "\r\n%s hat nicht genug Geld (%s). e) einzahlen a) abbruch \r\n",
302 deposit_credit(nickname);
307 /* substract money */
308 credit->credit -= (price * einheiten);
310 if (format_euro(rest, 10, credit->credit) == NULL) {
311 cprintf("Preis %d konnte nicht umgerechnet werden\r\n", credit->credit);
315 textcolor(TC_LIGHT_GREEN);
316 cprintf("\r\nVerbleibendes Guthaben fuer %s: %s. Druecke RETURN...\r\n",
318 textcolor(TC_LIGHT_GRAY);
320 cprintf("\r\nDein Guthaben betraegt noch %s.\r\n", rest);
325 textcolor(TC_LIGHT_RED);
326 cprintf("\r\nNickname nicht gefunden in der Guthabenverwaltung! Abbruch, "
327 "druecke RETURN...\r\n");
328 textcolor(TC_LIGHT_GRAY);
333 /* Ensure that nickname is NULL if it's empty because it's used in print_log
338 money += price * einheiten;
339 items_sold += einheiten;
341 print_log(name, price, einheiten, nickname, rest);
346 void buy_stock(BYTE n) {
347 if (n >= status.num_items || status.status[n].item_name == NULL) {
348 cprintf("FEHLER: Diese Einheit existiert nicht.\r\n");
353 status.status[n].times_sold +=
354 buy(status.status[n].item_name, status.status[n].price);
357 void buy_custom(void) {
360 char entered[5] = {'1', 0, 0, 0, 0};
361 char *input, name[20];
365 memset(name, '\0', 20);
366 cprintf("\r\nWas soll gekauft werden?\r\n");
368 strncpy(name, input, 20);
372 cprintf("\r\nWie teuer ist \"%s\" (in cents)?\r\n", name);
379 cprintf("Kauf abgebrochen, druecke RETURN...\r\n");
382 } else if (c == '-' && i == 0)
384 else if (c > 47 && c < 58)
387 price = atoi(entered) * negative;
394 void set_time_interactive(void) {
395 BYTE part[3] = {'0', '0', '\0'};
397 char *time_input, *time;
398 cprintf("Gib die aktuelle Uhrzeit ein (Format HHMMSS):\r\n");
399 time_input = get_input();
400 part[0] = time_input[0];
401 part[1] = time_input[1];
403 part[0] = time_input[2];
404 part[1] = time_input[3];
406 part[0] = time_input[4];
407 part[1] = time_input[5];
409 set_time(tp1, tp2, tp3);
412 cprintf("\r\nZeit gesetzt: %s\r\n", time);
423 /* Allocate logging buffer memory */
426 /* Set time initially, c128 doesn't know it */
427 set_time_interactive();
431 /* Load configuration */
434 /* Load items (= drinks) */
440 sprintf(print_buffer, "%c----------------------------------------------------"
441 "----------------------------\r",
444 sprintf(print_buffer, "%cC128-Kasse Version " GV "\r", 17);
447 sprintf(print_buffer,
448 "%cKasse gestartet um %s. Nutze logfile log-%u, offset %d.\r", 17,
449 time, log_num, log_heap_offset);
457 /* ...display dialogs eventually */
458 if (*c > 47 && *c < 58) {
459 /* if the input starts with a digit, we will interpret it as a number
460 * for the item to be sold */
465 } else if (*c == 'f') {
470 } else if (*c == 's') {
474 cprintf("\r\nStatefile/Creditfile/Log gesichert, druecke RETURN...\r\n");
476 } else if (*c == 'g') {
478 } else if (*c == 'z') {
479 set_time_interactive();
480 } else if (*c == 'q')
484 cprintf("\r\nBYEBYE\r\n");