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", status.status[i].price);
32 textcolor(TC_LIGHT_GRAY);
33 cprintf(": %-" xstr(MAX_ITEM_NAME_LENGTH) "s \xDD%s, %3dx ",
34 status.status[i].item_name, profit, status.status[i].times_sold);
37 /* Hauptbildschirm ausgeben */
38 static void print_screen(void) {
40 char *time = get_time();
43 if (format_euro(profit, 10, money) == NULL) {
44 cprintf("Einnahme %ld konnte nicht umgerechnet werden\r\n", money);
48 cprintf("C128-Kassenprogramm (phil_fry, sECuRE, sur5r) " GV "\r\n");
49 textcolor(TC_LIGHT_GRAY);
50 cprintf("\r\nUhrzeit: %s (wird nicht aktualisiert)\r\n"
51 "Eingenommen: %s, Verkauft: %ld Dinge, Drucken: %s\r\n",
52 time, profit, items_sold, (printing == 1 ? "ein" : "aus"));
53 textcolor(TC_LIGHT_GRAY);
55 "\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xB2"
56 "\xC0\xC0\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\xB2"
58 "\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xAE\r\n");
59 for (; i < min(status.num_items, 15); ++i) {
65 /* if we have more than 15 items, use the second column */
66 if ((i+15) < status.num_items) {
70 cprintf(" \xDD \xDD");
76 "\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xB1"
77 "\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xB1"
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\xBD\r\n");
82 textcolor(TC_LIGHT_GRAY);
83 cprintf(") Daten sichern ");
86 textcolor(TC_LIGHT_GRAY);
87 cprintf(") Guthabenverwaltung\r\n");
90 textcolor(TC_LIGHT_GRAY);
91 cprintf(") Zeit setzen ");
94 textcolor(TC_LIGHT_GRAY);
95 cprintf(") Freitext verkaufen ");
98 textcolor(TC_LIGHT_GRAY);
99 cprintf(") Beenden\r\n");
103 * Prints a line and logs it to file. Every line can be at max 80 characters.
106 static void print_log(char *name, int item_price, int einheiten, char *nickname, char *rest) {
107 char *time = get_time();
110 Transaction-ID (Anzahl verkaufter Einträge, inklusive des zu druckenden!) -- 6-stellig
112 Eintragname (= Getränk) -- 9-stellig
113 Preis (in Cents) -- 9-stellig
115 Nickname (falls es vom Guthaben abgezogen wird) -- 10-stellig
116 restguthaben (9-stellig)
121 if (format_euro(price, 10, item_price) == NULL) {
122 cprintf("Preis %d konnte nicht umgerechnet werden\r\n", item_price);
126 sprintf(print_buffer, "%c[%3u] %s - %-" xstr(MAX_ITEM_NAME_LENGTH) "s - %s - %s - %d - an %s\r", 17,
127 status.transaction_id, time, name, price, rest,
128 einheiten, (*nickname != '\0' ? nickname : "Unbekannt"));
129 status.transaction_id++;
133 /* dialog which is called for each bought item */
134 static signed int buy(char *name, unsigned int price) {
136 char entered[5] = {'1', 0, 0, 0, 0};
137 BYTE i = 0, matches = 0;
138 BYTE c, x, y, nickname_len;
141 char nickname[NICKNAME_MAX_LEN+1];
143 struct credits_t *credit;
145 memset(nickname, '\0', sizeof(nickname));
146 memset(rest, ' ', sizeof(rest));
150 cprintf("Wieviel Einheiten \"%s\"? [1] \r\n", name);
154 /* Buffer-Ende erreicht? */
163 if (c == PETSCII_DEL) {
167 cputcxy(x+i, y, ' ');
172 cprintf("Kauf abgebrochen, druecke RETURN...\r\n");
176 if (c == '-' && i == 0) {
179 } else if (c >= PETSCII_0 && c <= PETSCII_9) {
184 /* Ungültige Eingabe (keine Ziffer), einfach ignorieren */
186 einheiten = atoi(entered) * negative;
188 if (einheiten > 100 || einheiten < -100 || einheiten == 0) {
189 cprintf("\r\nEinheit nicht in [-100, 100] oder 0, Abbruch, druecke RETURN...\r\n");
195 cprintf("\r\n *** VERKAUF ***\r\n\r\n");
196 cprintf("%dx %s", einheiten, name);
199 cprintf("\r\nAuf ein Guthaben kaufen? Wenn ja, Nickname eingeben:\r\n");
206 input_terminator_t terminator;
208 terminator = get_input_terminated_by(
209 INPUT_TERMINATOR_RETURN |
210 INPUT_TERMINATOR_SPACE,
214 /* Clear the screen from any previous completions */
217 for (i = 1; i < 7; i++) {
218 /* "Completion:" is longer than NICKNAME_MAX_LEN */
219 cclearxy(0, y + i, strlen("Completion:"));
223 if (terminator != INPUT_TERMINATOR_SPACE) {
229 for (i = 0; i < credits.num_items; i++) {
230 if (strncmp(nickname, credits.credits[i].nickname, strlen(nickname)) != 0) {
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);
274 if (*nickname != '\0') {
276 cprintf(" fuer %s\r\n", nickname);
280 if (*nickname != '\0' && *nickname != 32) {
281 nickname_len = strlen(nickname);
282 /* go through credits and remove the amount of money or set nickname
283 * to NULL if no such credit could be found */
284 credit = find_credit(nickname);
285 if (credit != NULL) {
286 while ((signed int)credit->credit < ((signed int)price * einheiten)) {
287 if (format_euro(rest, 10, credit->credit) == NULL) {
288 cprintf("Preis %d konnte nicht umgerechnet werden\r\n", credit->credit);
291 cprintf("\r\n%s hat nicht genug Geld (%s). e) einzahlen a) abbruch \r\n", nickname, rest);
294 deposit_credit(nickname);
299 /* substract money */
300 credit->credit -= (price * einheiten);
302 if (format_euro(rest, 10, credit->credit) == NULL) {
303 cprintf("Preis %d konnte nicht umgerechnet werden\r\n", credit->credit);
307 textcolor(TC_LIGHT_GREEN);
308 cprintf("\r\nVerbleibendes Guthaben fuer %s: %s. Druecke RETURN...\r\n",
310 textcolor(TC_LIGHT_GRAY);
312 cprintf("\r\nDein Guthaben betraegt noch %s.\r\n", rest);
317 textcolor(TC_LIGHT_RED);
318 cprintf("\r\nNickname nicht gefunden in der Guthabenverwaltung! Abbruch, druecke RETURN...\r\n");
319 textcolor(TC_LIGHT_GRAY);
324 /* Ensure that nickname is NULL if it's empty because it's used in print_log */
328 money += price * einheiten;
329 items_sold += einheiten;
331 print_log(name, price, einheiten, nickname, rest);
336 void buy_stock(BYTE n) {
337 if (n >= status.num_items || status.status[n].item_name == NULL) {
338 cprintf("FEHLER: Diese Einheit existiert nicht.\r\n");
343 status.status[n].times_sold += buy(status.status[n].item_name, status.status[n].price);
346 void buy_custom(void) {
349 char entered[5] = {'1', 0, 0, 0, 0};
350 char *input, name[20];
354 memset(name, '\0', 20);
355 cprintf("\r\nWas soll gekauft werden?\r\n");
357 strncpy(name, input, 20);
361 cprintf("\r\nWie teuer ist \"%s\" (in cents)?\r\n", name);
368 cprintf("Kauf abgebrochen, druecke RETURN...\r\n");
371 } else if (c == '-' && i == 0)
373 else if (c > 47 && c < 58)
376 price = atoi(entered) * negative;
383 void set_time_interactive(void) {
384 BYTE part[3] = {'0', '0', '\0'};
386 char *time_input, *time;
387 cprintf("Gib die aktuelle Uhrzeit ein (Format HHMMSS):\r\n");
388 time_input = get_input();
389 part[0] = time_input[0];
390 part[1] = time_input[1];
392 part[0] = time_input[2];
393 part[1] = time_input[3];
395 part[0] = time_input[4];
396 part[1] = time_input[5];
398 set_time(tp1, tp2, tp3);
401 cprintf("\r\nZeit gesetzt: %s\r\n", time);
412 /* Allocate logging buffer memory */
415 /* Set time initially, c128 doesn't know it */
416 set_time_interactive();
420 /* Load configuration */
423 /* Load items (= drinks) */
429 sprintf(print_buffer, "%c--------------------------------------------------------------------------------\r", 17);
431 sprintf(print_buffer, "%cC128-Kasse Version " GV "\r", 17);
434 sprintf(print_buffer, "%cKasse gestartet um %s. Nutze logfile log-%u, offset %d.\r", 17, time, log_num, log_heap_offset);
442 /* ...display dialogs eventually */
443 if (*c > 47 && *c < 58) {
444 /* if the input starts with a digit, we will interpret it as a number
445 * for the item to be sold */
450 } else if (*c == 'f') {
455 } else if (*c == 's') {
459 cprintf("\r\nStatefile/Creditfile/Log gesichert, druecke RETURN...\r\n");
461 } else if (*c == 'g') {
463 } else if (*c == 'z') {
464 set_time_interactive();
465 } else if (*c == 'q')
469 cprintf("\r\nBYEBYE\r\n");