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 "
200 cprintf("\r\nAuf ein Guthaben kaufen? Wenn ja, Nickname eingeben:\r\n");
207 input_terminator_t terminator;
209 terminator = get_input_terminated_by(INPUT_TERMINATOR_RETURN |
210 INPUT_TERMINATOR_SPACE,
211 nickname, sizeof(nickname));
213 /* Clear the screen from any previous completions */
216 for (i = 1; i < 7; i++) {
217 /* "Completion:" is longer than NICKNAME_MAX_LEN */
218 cclearxy(0, y + i, strlen("Completion:"));
222 if (terminator != INPUT_TERMINATOR_SPACE) {
228 for (i = 0; i < credits.num_items; i++) {
229 if (strncmp(nickname, credits.credits[i].nickname, strlen(nickname)) !=
237 uniquematch = credits.credits[i].nickname;
240 /* Display the rest of the nickname */
241 textcolor(TC_LIGHT_GREEN);
242 cprintf("%s", uniquematch + strlen(nickname));
243 textcolor(TC_LIGHT_GRAY);
244 strcat(nickname, uniquematch + strlen(nickname));
246 /* Multiple nicknames match what was entered so far. Abort and
247 * display all matches, then prompt the user again. */
248 char completion[NICKNAME_MAX_LEN + 1];
249 BYTE len = strlen(nickname);
252 cprintf("\r\nCompletion:\r\n");
254 for (i = 0; i < credits.num_items; i++) {
255 if (strncmp(nickname, credits.credits[i].nickname, len) != 0) {
258 if (++matches == 5) {
262 strcpy(completion, credits.credits[i].nickname);
263 *(completion + len) = '\0';
264 cprintf("%s", completion);
265 textcolor(TC_LIGHT_GREEN);
266 cprintf("%c", *(credits.credits[i].nickname + len));
267 textcolor(TC_LIGHT_GRAY);
268 cprintf("%s\r\n", completion + len + 1);
275 if (*nickname != '\0' && *nickname != 32) {
276 nickname_len = strlen(nickname);
277 /* go through credits and remove the amount of money or set nickname
278 * to NULL if no such credit could be found */
279 credit = find_credit(nickname);
280 if (credit != NULL) {
281 while ((signed int)credit->credit < ((signed int)price * einheiten)) {
282 if (format_euro(rest, 10, credit->credit) == NULL) {
283 cprintf("Preis %d konnte nicht umgerechnet werden\r\n",
288 "\r\n%s hat nicht genug Geld (%s). e) einzahlen a) abbruch \r\n",
292 deposit_credit(nickname);
297 /* substract money */
298 credit->credit -= (price * einheiten);
300 if (format_euro(rest, 10, credit->credit) == NULL) {
301 cprintf("Preis %d konnte nicht umgerechnet werden\r\n", credit->credit);
305 textcolor(TC_LIGHT_GREEN);
306 cprintf("\r\nVerbleibendes Guthaben fuer %s: %s. Druecke RETURN...\r\n",
308 textcolor(TC_LIGHT_GRAY);
312 textcolor(TC_LIGHT_RED);
313 cprintf("\r\nNickname nicht gefunden in der Guthabenverwaltung! Abbruch, "
314 "druecke RETURN...\r\n");
315 textcolor(TC_LIGHT_GRAY);
320 /* Ensure that nickname is NULL if it's empty because it's used in print_log
325 money += price * einheiten;
326 items_sold += einheiten;
328 print_log(name, price, einheiten, nickname, rest);
333 void buy_stock(BYTE n) {
334 if (n >= status.num_items || status.status[n].item_name == NULL) {
335 cprintf("FEHLER: Diese Einheit existiert nicht.\r\n");
340 status.status[n].times_sold +=
341 buy(status.status[n].item_name, status.status[n].price);
344 void buy_custom(void) {
347 char entered[5] = {'1', 0, 0, 0, 0};
348 char *input, name[20];
352 memset(name, '\0', 20);
353 cprintf("\r\nWas soll gekauft werden?\r\n");
355 strncpy(name, input, 20);
359 cprintf("\r\nWie teuer ist \"%s\" (in cents)?\r\n", name);
366 cprintf("Kauf abgebrochen, druecke RETURN...\r\n");
369 } else if (c == '-' && i == 0)
371 else if (c > 47 && c < 58)
374 price = atoi(entered) * negative;
381 void set_time_interactive(void) {
382 BYTE part[3] = {'0', '0', '\0'};
384 char *time_input, *time;
385 cprintf("Gib die aktuelle Uhrzeit ein (Format HHMMSS):\r\n");
386 time_input = get_input();
387 part[0] = time_input[0];
388 part[1] = time_input[1];
390 part[0] = time_input[2];
391 part[1] = time_input[3];
393 part[0] = time_input[4];
394 part[1] = time_input[5];
396 set_time(tp1, tp2, tp3);
399 cprintf("\r\nZeit gesetzt: %s\r\n", time);
409 /* clock CPU at double the speed (a whopping 2 Mhz!) */
414 /* Allocate logging buffer memory */
417 /* Set time initially, c128 doesn't know it */
418 set_time_interactive();
420 /* disable interrupt driven VIC screen editor */
423 /* Load configuration */
426 /* Load items (= drinks) */
432 sprintf(print_buffer, "%c----------------------------------------------------"
433 "----------------------------\r",
436 sprintf(print_buffer, "%cC128-Kasse Version " GV "\r", 17);
439 sprintf(print_buffer,
440 "%cKasse gestartet um %s. Nutze logfile log-%u, offset %d.\r", 17,
441 time, log_num, log_heap_offset);
449 /* ...display dialogs eventually */
450 if (*c > 47 && *c < 58) {
451 /* if the input starts with a digit, we will interpret it as a number
452 * for the item to be sold */
454 } else if (*c == 'f') {
456 } else if (*c == 's') {
460 cprintf("\r\nStatefile/Creditfile/Log gesichert, druecke RETURN...\r\n");
462 } else if (*c == 'g') {
464 } else if (*c == 'z') {
465 set_time_interactive();
466 } else if (*c == 'q')
470 cprintf("\r\nBYEBYE\r\n");