]> git.sur5r.net Git - c128-kasse/blob - src/kasse.c
Remove unused "dual monitor" code; clock CPU faster
[c128-kasse] / src / kasse.c
1 /*
2  * RGB2R-C128-Kassenprogramm
3  * © 2007-2009 phil_fry, sECuRE, sur5r
4  * See LICENSE for license information
5  *
6  */
7 #define _IS_KASSE
8 #include <stdio.h>
9 #include <conio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <cbm.h>
13
14 #include "general.h"
15 #include "config.h"
16 #include "kasse.h"
17 #include "credit_manager.h"
18 #include "c128time.h"
19 #include "print.h"
20 #include "version.h"
21 // drucker 4 oder 5
22 // graphic 4,0,10
23
24 void print_item(BYTE i) {
25   char profit[10];
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);
29     exit(1);
30   }
31   textcolor(TC_YELLOW);
32   cprintf("%2d", i);
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);
36 }
37
38 /* Hauptbildschirm ausgeben */
39 static void print_screen(void) {
40   BYTE i = 0;
41   char *time = get_time();
42   char profit[10];
43   clrscr();
44   if (format_euro(profit, 10, money) == NULL) {
45     cprintf("Einnahme %ld konnte nicht umgerechnet werden\r\n", money);
46     exit(1);
47   }
48   textcolor(TC_CYAN);
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);
55   cprintf("      \xB0"
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"
60           "\r\n");
61   for (; i < min(status.num_items, 15); ++i) {
62
63     cprintf("      \xDD");
64     print_item(i);
65     cprintf("\xDD");
66
67     /* if we have more than 15 items, use the second column */
68     if ((i + 15) < status.num_items) {
69       print_item(i + 15);
70       cprintf("\xDD");
71     } else {
72       cprintf("              \xDD                \xDD");
73     }
74
75     cprintf("\r\n");
76   }
77   cprintf("      \xAD"
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"
82           "\r\n");
83   textcolor(TC_YELLOW);
84   cprintf("   s");
85   textcolor(TC_LIGHT_GRAY);
86   cprintf(") Daten sichern                                  ");
87   textcolor(TC_YELLOW);
88   cprintf("g");
89   textcolor(TC_LIGHT_GRAY);
90   cprintf(") Guthabenverwaltung\r\n");
91   textcolor(TC_YELLOW);
92   cprintf("   z");
93   textcolor(TC_LIGHT_GRAY);
94   cprintf(") Zeit setzen         ");
95   textcolor(TC_YELLOW);
96   cprintf("f");
97   textcolor(TC_LIGHT_GRAY);
98   cprintf(") Freitext verkaufen      ");
99   textcolor(TC_YELLOW);
100   cprintf("q");
101   textcolor(TC_LIGHT_GRAY);
102   cprintf(") Beenden\r\n");
103 }
104
105 /*
106  * Prints a line and logs it to file. Every line can be at max 80 characters.
107  *
108  */
109 static void print_log(char *name, int item_price, int einheiten, char *nickname,
110                       char *rest) {
111   char *time = get_time();
112   char price[10];
113   /* Format:
114      Transaction-ID (Anzahl verkaufter Einträge, inklusive des zu druckenden!)
115      -- 6-stellig
116      Uhrzeit -- 8-stellig
117      Eintragname (= Getränk) -- 9-stellig
118      Preis (in Cents) -- 9-stellig
119      Anzahl -- 2-stellig
120      Nickname (falls es vom Guthaben abgezogen wird) -- 10-stellig
121      restguthaben (9-stellig)
122
123      + 7 leerzeichen
124      --> 48 zeichen
125      */
126   if (format_euro(price, 10, item_price) == NULL) {
127     cprintf("Preis %d konnte nicht umgerechnet werden\r\n", item_price);
128     exit(1);
129   }
130
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++;
136   print_the_buffer();
137 }
138
139 /* dialog which is called for each bought item */
140 static signed int buy(char *name, unsigned int price) {
141   int negative = 1;
142   char entered[5] = {'1', 0, 0, 0, 0};
143   BYTE i = 0, matches = 0;
144   BYTE c, x, y, nickname_len;
145   int einheiten;
146   char nickname[NICKNAME_MAX_LEN + 1];
147   char rest[11];
148   struct credits_t *credit;
149
150   memset(nickname, '\0', sizeof(nickname));
151   memset(rest, ' ', sizeof(rest));
152   rest[8] = '\0';
153
154   clrscr();
155   cprintf("Wieviel Einheiten \"%s\"? [1] \r\n", name);
156   x = wherex();
157   y = wherey();
158   while (1) {
159     /* Buffer-Ende erreicht? */
160     if (i == 4)
161       break;
162
163     c = cgetc();
164     /* Enter */
165     if (c == PETSCII_CR)
166       break;
167     /* Backspace */
168     if (c == PETSCII_DEL) {
169       if (i == 0)
170         continue;
171       entered[--i] = '\0';
172       cputcxy(x + i, y, ' ');
173       gotoxy(x + i, y);
174       continue;
175     }
176     if (c == 27) {
177       cprintf("Kauf abgebrochen, druecke RETURN...\r\n");
178       get_input();
179       return 1;
180     }
181     if (c == '-' && i == 0) {
182       negative = -1;
183       cputc(c);
184     } else if (c >= PETSCII_0 && c <= PETSCII_9) {
185       entered[i++] = c;
186       cputc(c);
187     }
188
189     /* Ungültige Eingabe (keine Ziffer), einfach ignorieren */
190   }
191   einheiten = atoi(entered) * negative;
192
193   if (einheiten > 100 || einheiten < -100 || einheiten == 0) {
194     cprintf("\r\nEinheit nicht in [-100, 100] oder 0, Abbruch, druecke "
195             "RETURN...\r\n");
196     cgetc();
197     return 1;
198   }
199
200   cprintf("\r\nAuf ein Guthaben kaufen? Wenn ja, Nickname eingeben:\r\n");
201   {
202     BYTE i;
203     BYTE x;
204     BYTE y;
205     BYTE matches;
206     char *uniquematch;
207     input_terminator_t terminator;
208     while (1) {
209       terminator = get_input_terminated_by(INPUT_TERMINATOR_RETURN |
210                                                INPUT_TERMINATOR_SPACE,
211                                            nickname, sizeof(nickname));
212
213       /* Clear the screen from any previous completions */
214       x = wherex();
215       y = wherey();
216       for (i = 1; i < 7; i++) {
217         /* "Completion:" is longer than NICKNAME_MAX_LEN */
218         cclearxy(0, y + i, strlen("Completion:"));
219       }
220       gotoxy(x, y);
221
222       if (terminator != INPUT_TERMINATOR_SPACE) {
223         break;
224       }
225
226       matches = 0;
227       uniquematch = NULL;
228       for (i = 0; i < credits.num_items; i++) {
229         if (strncmp(nickname, credits.credits[i].nickname, strlen(nickname)) !=
230             0) {
231           continue;
232         }
233         matches++;
234         if (matches > 1) {
235           break;
236         }
237         uniquematch = credits.credits[i].nickname;
238       }
239       if (matches == 1) {
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));
245       } else {
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);
250         x = wherex();
251         y = wherey();
252         cprintf("\r\nCompletion:\r\n");
253         matches = 0;
254         for (i = 0; i < credits.num_items; i++) {
255           if (strncmp(nickname, credits.credits[i].nickname, len) != 0) {
256             continue;
257           }
258           if (++matches == 5) {
259             cprintf("...\r\n");
260             break;
261           }
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);
269         }
270         gotoxy(x, y);
271       }
272     }
273   }
274
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",
284                   credit->credit);
285           exit(1);
286         }
287         cprintf(
288             "\r\n%s hat nicht genug Geld (%s). e) einzahlen a) abbruch \r\n",
289             nickname, rest);
290         c = cgetc();
291         if (c == 'e') {
292           deposit_credit(nickname);
293         } else {
294           return 0;
295         }
296       }
297       /* substract money */
298       credit->credit -= (price * einheiten);
299
300       if (format_euro(rest, 10, credit->credit) == NULL) {
301         cprintf("Preis %d konnte nicht umgerechnet werden\r\n", credit->credit);
302         exit(1);
303       }
304
305       textcolor(TC_LIGHT_GREEN);
306       cprintf("\r\nVerbleibendes Guthaben fuer %s: %s. Druecke RETURN...\r\n",
307               nickname, rest);
308       textcolor(TC_LIGHT_GRAY);
309       get_input();
310       matches++;
311     } else {
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);
316       get_input();
317       return 0;
318     }
319   } else {
320     /* Ensure that nickname is NULL if it's empty because it's used in print_log
321      */
322     *nickname = '\0';
323   }
324
325   money += price * einheiten;
326   items_sold += einheiten;
327   if (printing == 1)
328     print_log(name, price, einheiten, nickname, rest);
329
330   return einheiten;
331 }
332
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");
336     get_input();
337     return;
338   }
339
340   status.status[n].times_sold +=
341       buy(status.status[n].item_name, status.status[n].price);
342 }
343
344 void buy_custom(void) {
345   BYTE c = 0, i = 0;
346   int negative = 1;
347   char entered[5] = {'1', 0, 0, 0, 0};
348   char *input, name[20];
349   int price;
350
351   clrscr();
352   memset(name, '\0', 20);
353   cprintf("\r\nWas soll gekauft werden?\r\n");
354   input = get_input();
355   strncpy(name, input, 20);
356   if (*name == '\0')
357     return;
358
359   cprintf("\r\nWie teuer ist \"%s\" (in cents)?\r\n", name);
360   while (1) {
361     c = cgetc();
362     if (c == 13)
363       break;
364     cputc(c);
365     if (c == 27) {
366       cprintf("Kauf abgebrochen, druecke RETURN...\r\n");
367       get_input();
368       return;
369     } else if (c == '-' && i == 0)
370       negative = -1;
371     else if (c > 47 && c < 58)
372       entered[i++] = c;
373   }
374   price = atoi(entered) * negative;
375
376   cprintf("\r\n");
377
378   buy(name, price);
379 }
380
381 void set_time_interactive(void) {
382   BYTE part[3] = {'0', '0', '\0'};
383   BYTE tp1, tp2, tp3;
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];
389   tp1 = atoi(part);
390   part[0] = time_input[2];
391   part[1] = time_input[3];
392   tp2 = atoi(part);
393   part[0] = time_input[4];
394   part[1] = time_input[5];
395   tp3 = atoi(part);
396   set_time(tp1, tp2, tp3);
397
398   time = get_time();
399   cprintf("\r\nZeit gesetzt: %s\r\n", time);
400 }
401
402 int main(void) {
403   char *c;
404   char *time;
405
406   if (VIDEOMODE == 40)
407     videomode(80);
408
409   /* clock CPU at double the speed (a whopping 2 Mhz!) */
410   fast();
411
412   clrscr();
413
414   /* Allocate logging buffer memory */
415   init_log();
416
417   /* Set time initially, c128 doesn't know it */
418   set_time_interactive();
419
420   /* disable interrupt driven VIC screen editor */
421   POKE(0xD8, 255);
422
423   /* Load configuration */
424   load_config();
425
426   /* Load items (= drinks) */
427   load_items();
428   /* Load credits */
429   load_credits();
430
431   time = get_time();
432   sprintf(print_buffer, "%c----------------------------------------------------"
433                         "----------------------------\r",
434           17);
435   print_the_buffer();
436   sprintf(print_buffer, "%cC128-Kasse Version " GV "\r", 17);
437   print_the_buffer();
438
439   sprintf(print_buffer,
440           "%cKasse gestartet um %s. Nutze logfile log-%u, offset %d.\r", 17,
441           time, log_num, log_heap_offset);
442   print_the_buffer();
443
444   print_header();
445
446   while (1) {
447     print_screen();
448     c = get_input();
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 */
453       buy_stock(atoi(c));
454     } else if (*c == 'f') {
455       buy_custom();
456     } else if (*c == 's') {
457       save_items();
458       save_credits();
459       log_flush();
460       cprintf("\r\nStatefile/Creditfile/Log gesichert, druecke RETURN...\r\n");
461       get_input();
462     } else if (*c == 'g') {
463       credit_manager();
464     } else if (*c == 'z') {
465       set_time_interactive();
466     } else if (*c == 'q')
467       break;
468   }
469   clrscr();
470   cprintf("\r\nBYEBYE\r\n");
471
472   return 0;
473 }