]> git.sur5r.net Git - c128-kasse/blob - src/kasse.c
kasse: Add comment why we disable IRQs
[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 #include <c128.h>
14 #include <6502.h>
15
16 #include "general.h"
17 #include "config.h"
18 #include "kasse.h"
19 #include "credit_manager.h"
20 #include "c128time.h"
21 #include "print.h"
22 #include "version.h"
23 #include "vdc_patch_charset.h"
24 // drucker 4 oder 5
25 // graphic 4,0,10
26
27 void print_item(BYTE i) {
28   char profit[10];
29   if (format_euro(profit, sizeof(profit), status.status[i].price) == NULL) {
30     cprintf("Preis %ld konnte nicht umgerechnet werden\r\n",
31             status.status[i].price);
32     exit(1);
33   }
34   textcolor(TC_YELLOW);
35   cprintf("%2d", i);
36   textcolor(TC_LIGHT_GRAY);
37   cprintf(": %-" xstr(MAX_ITEM_NAME_LENGTH) "s \xDD%s,   %3dx ",
38           status.status[i].item_name, profit, status.status[i].times_sold);
39 }
40
41 /* Hauptbildschirm ausgeben */
42 static void print_screen(void) {
43   BYTE i = 0;
44   char *time = get_time();
45   char profit[10];
46   clrscr();
47   if (format_euro(profit, 10, money) == NULL) {
48     cprintf("Einnahme %ld konnte nicht umgerechnet werden\r\n", money);
49     exit(1);
50   }
51   textcolor(TC_CYAN);
52   cprintf("C128-Kassenprogramm (phil_fry, sECuRE, sur5r, mxf) " GV "\r\n");
53   textcolor(TC_LIGHT_GRAY);
54   cprintf("\r\nUhrzeit:     %s (wird nicht aktualisiert)\r\n"
55           "Eingenommen: %s, Verkauft: %ld Dinge, Drucken: %s\r\n",
56           time, profit, items_sold, (printing == 1 ? "ein" : "aus"));
57   textcolor(TC_LIGHT_GRAY);
58   cprintf("      \xB0"
59           "\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xB2"
60           "\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xB2"
61           "\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xB2"
62           "\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xAE"
63           "\r\n");
64   for (; i < min(status.num_items, 15); ++i) {
65
66     cprintf("      \xDD");
67     print_item(i);
68     cprintf("\xDD");
69
70     /* if we have more than 15 items, use the second column */
71     if ((i + 15) < status.num_items) {
72       print_item(i + 15);
73       cprintf("\xDD");
74     } else {
75       cprintf("              \xDD                \xDD");
76     }
77
78     cprintf("\r\n");
79   }
80   cprintf("      \xAD"
81           "\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xB1"
82           "\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xB1"
83           "\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xB1"
84           "\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xBD"
85           "\r\n");
86   textcolor(TC_YELLOW);
87   cprintf("   s");
88   textcolor(TC_LIGHT_GRAY);
89   cprintf(") Daten sichern                                  ");
90   textcolor(TC_YELLOW);
91   cprintf("g");
92   textcolor(TC_LIGHT_GRAY);
93   cprintf(") Guthabenverwaltung\r\n");
94   textcolor(TC_YELLOW);
95   cprintf("   z");
96   textcolor(TC_LIGHT_GRAY);
97   cprintf(") Zeit setzen         ");
98   textcolor(TC_YELLOW);
99   cprintf("f");
100   textcolor(TC_LIGHT_GRAY);
101   cprintf(") Freitext verkaufen      ");
102   textcolor(TC_YELLOW);
103   cprintf("q");
104   textcolor(TC_LIGHT_GRAY);
105   cprintf(") Beenden\r\n");
106 }
107
108 /*
109  * Prints a line and logs it to file. Every line can be at max 80 characters.
110  *
111  */
112 static void print_log(char *name, int item_price, int einheiten, char *nickname,
113                       char *rest) {
114   char *time = get_time();
115   char price[10];
116   /* Format:
117      Transaction-ID (Anzahl verkaufter Einträge, inklusive des zu druckenden!)
118      -- 6-stellig
119      Uhrzeit -- 8-stellig
120      Eintragname (= Getränk) -- 9-stellig
121      Preis (in Cents) -- 9-stellig
122      Anzahl -- 2-stellig
123      Nickname (falls es vom Guthaben abgezogen wird) -- 10-stellig
124      restguthaben (9-stellig)
125
126      + 7 leerzeichen
127      --> 48 zeichen
128      */
129   if (format_euro(price, 10, item_price) == NULL) {
130     cprintf("Preis %d konnte nicht umgerechnet werden\r\n", item_price);
131     exit(1);
132   }
133
134   sprintf(print_buffer, "%c[%3u] %s - %-" xstr(
135                             MAX_ITEM_NAME_LENGTH) "s - %s - %s - %d - an %s\r",
136           17, status.transaction_id, time, name, price, rest, einheiten,
137           (*nickname != '\0' ? nickname : "Unbekannt"));
138   status.transaction_id++;
139   print_the_buffer();
140 }
141
142 /* dialog which is called for each bought item */
143 static signed int buy(char *name, unsigned int price) {
144   int negative = 1;
145   char entered[5] = {'1', 0, 0, 0, 0};
146   BYTE i = 0, matches = 0;
147   BYTE c, x, y, nickname_len;
148   int einheiten;
149   char nickname[NICKNAME_MAX_LEN + 1];
150   char rest[11];
151   struct credits_t *credit;
152
153   memset(nickname, '\0', sizeof(nickname));
154   memset(rest, ' ', sizeof(rest));
155   rest[8] = '\0';
156
157   clrscr();
158   cprintf("Wieviel Einheiten \"%s\"? [1] \r\n", name);
159   x = wherex();
160   y = wherey();
161   while (1) {
162     /* Buffer-Ende erreicht? */
163     if (i == 4)
164       break;
165
166     c = cgetc();
167     /* Enter */
168     if (c == PETSCII_CR)
169       break;
170     /* Backspace */
171     if (c == PETSCII_DEL) {
172       if (i == 0)
173         continue;
174       entered[--i] = '\0';
175       cputcxy(x + i, y, ' ');
176       gotoxy(x + i, y);
177       continue;
178     }
179     if (c == 27) {
180       cprintf("Kauf abgebrochen, dr" uUML "cke RETURN...\r\n");
181       get_input();
182       return 1;
183     }
184     if (c == '-' && i == 0) {
185       negative = -1;
186       cputc(c);
187     } else if (c >= PETSCII_0 && c <= PETSCII_9) {
188       entered[i++] = c;
189       cputc(c);
190     }
191
192     /* Ungültige Eingabe (keine Ziffer), einfach ignorieren */
193   }
194   einheiten = atoi(entered) * negative;
195
196   if (einheiten > 100 || einheiten < -100 || einheiten == 0) {
197     cprintf("\r\nEinheit nicht in [-100, 100] oder 0, Abbruch, dr" uUML "cke "
198             "RETURN...\r\n");
199     cgetc();
200     return 1;
201   }
202
203   cprintf("\r\nAuf ein Guthaben kaufen? Wenn ja, Nickname eingeben:\r\n");
204   {
205     BYTE i;
206     BYTE x;
207     BYTE y;
208     BYTE matches;
209     char *uniquematch;
210     input_terminator_t terminator;
211     while (1) {
212       terminator = get_input_terminated_by(INPUT_TERMINATOR_RETURN |
213                                                INPUT_TERMINATOR_SPACE,
214                                            nickname, sizeof(nickname));
215
216       /* Clear the screen from any previous completions */
217       x = wherex();
218       y = wherey();
219       for (i = 1; i < 7; i++) {
220         /* "Completion:" is longer than NICKNAME_MAX_LEN */
221         cclearxy(0, y + i, strlen("Completion:"));
222       }
223       gotoxy(x, y);
224
225       if (terminator != INPUT_TERMINATOR_SPACE) {
226         break;
227       }
228
229       matches = 0;
230       uniquematch = NULL;
231       for (i = 0; i < credits.num_items; i++) {
232         if (strncmp(nickname, credits.credits[i].nickname, strlen(nickname)) !=
233             0) {
234           continue;
235         }
236         matches++;
237         if (matches > 1) {
238           break;
239         }
240         uniquematch = credits.credits[i].nickname;
241       }
242       if (matches == 1) {
243         /* Display the rest of the nickname */
244         textcolor(TC_LIGHT_GREEN);
245         cprintf("%s", uniquematch + strlen(nickname));
246         textcolor(TC_LIGHT_GRAY);
247         strcat(nickname, uniquematch + strlen(nickname));
248       } else {
249         /* Multiple nicknames match what was entered so far. Abort and
250          * display all matches, then prompt the user again. */
251         char completion[NICKNAME_MAX_LEN + 1];
252         BYTE len = strlen(nickname);
253         x = wherex();
254         y = wherey();
255         cprintf("\r\nCompletion:\r\n");
256         matches = 0;
257         for (i = 0; i < credits.num_items; i++) {
258           if (strncmp(nickname, credits.credits[i].nickname, len) != 0) {
259             continue;
260           }
261           if (++matches == 5) {
262             cprintf("...\r\n");
263             break;
264           }
265           strcpy(completion, credits.credits[i].nickname);
266           *(completion + len) = '\0';
267           cprintf("%s", completion);
268           textcolor(TC_LIGHT_GREEN);
269           cprintf("%c", *(credits.credits[i].nickname + len));
270           textcolor(TC_LIGHT_GRAY);
271           cprintf("%s\r\n", completion + len + 1);
272         }
273         gotoxy(x, y);
274       }
275     }
276   }
277
278   if (*nickname != '\0' && *nickname != 32) {
279     nickname_len = strlen(nickname);
280     /* go through credits and remove the amount of money or set nickname
281      * to NULL if no such credit could be found */
282     credit = find_credit(nickname);
283     if (credit != NULL) {
284       while ((signed int)credit->credit < ((signed int)price * einheiten)) {
285         if (format_euro(rest, 10, credit->credit) == NULL) {
286           cprintf("Preis %d konnte nicht umgerechnet werden\r\n",
287                   credit->credit);
288           exit(1);
289         }
290         cprintf(
291             "\r\n%s hat nicht genug Geld (%s). e) einzahlen a) abbruch \r\n",
292             nickname, rest);
293         c = cgetc();
294         if (c == 'e') {
295           deposit_credit(nickname);
296         } else {
297           return 0;
298         }
299       }
300       /* substract money */
301       credit->credit -= (price * einheiten);
302
303       if (format_euro(rest, 10, credit->credit) == NULL) {
304         cprintf("Preis %d konnte nicht umgerechnet werden\r\n", credit->credit);
305         exit(1);
306       }
307
308       textcolor(TC_LIGHT_GREEN);
309       cprintf("\r\nVerbleibendes Guthaben f" uUML "r %s: %s. Dr" uUML
310               "cke RETURN...\r\n",
311               nickname, rest);
312       textcolor(TC_LIGHT_GRAY);
313       get_input();
314       matches++;
315     } else {
316       textcolor(TC_LIGHT_RED);
317       cprintf("\r\nNickname nicht gefunden in der Guthabenverwaltung! Abbruch, "
318               "dr" uUML "cke RETURN...\r\n");
319       textcolor(TC_LIGHT_GRAY);
320       get_input();
321       return 0;
322     }
323   } else {
324     /* Ensure that nickname is NULL if it's empty because it's used in print_log
325      */
326     *nickname = '\0';
327   }
328
329   money += price * einheiten;
330   items_sold += einheiten;
331   if (printing == 1)
332     print_log(name, price, einheiten, nickname, rest);
333
334   return einheiten;
335 }
336
337 void buy_stock(BYTE n) {
338   if (n >= status.num_items || status.status[n].item_name == NULL) {
339     cprintf("FEHLER: Diese Einheit existiert nicht.\r\n");
340     get_input();
341     return;
342   }
343
344   status.status[n].times_sold +=
345       buy(status.status[n].item_name, status.status[n].price);
346 }
347
348 void buy_custom(void) {
349   BYTE c = 0, i = 0;
350   int negative = 1;
351   char entered[5] = {'1', 0, 0, 0, 0};
352   char *input, name[20];
353   int price;
354
355   clrscr();
356   memset(name, '\0', 20);
357   cprintf("\r\nWas soll gekauft werden?\r\n");
358   input = get_input();
359   strncpy(name, input, 20);
360   if (*name == '\0')
361     return;
362
363   cprintf("\r\nWie teuer ist \"%s\" (in cents)?\r\n", name);
364   while (1) {
365     c = cgetc();
366     if (c == 13)
367       break;
368     cputc(c);
369     if (c == 27) {
370       cprintf("Kauf abgebrochen, dr" uUML "cke RETURN...\r\n");
371       get_input();
372       return;
373     } else if (c == '-' && i == 0)
374       negative = -1;
375     else if (c > 47 && c < 58)
376       entered[i++] = c;
377   }
378   price = atoi(entered) * negative;
379
380   cprintf("\r\n");
381
382   buy(name, price);
383 }
384
385 void set_time_interactive(void) {
386   BYTE part[3] = {'0', '0', '\0'};
387   BYTE tp1, tp2, tp3;
388   char *time_input, *time;
389   cprintf("Gib die aktuelle Uhrzeit ein (Format HHMMSS):\r\n");
390   time_input = get_input();
391   part[0] = time_input[0];
392   part[1] = time_input[1];
393   tp1 = atoi(part);
394   part[0] = time_input[2];
395   part[1] = time_input[3];
396   tp2 = atoi(part);
397   part[0] = time_input[4];
398   part[1] = time_input[5];
399   tp3 = atoi(part);
400   set_time(tp1, tp2, tp3);
401
402   time = get_time();
403   cprintf("\r\nZeit gesetzt: %s\r\n", time);
404 }
405
406 int main(void) {
407   char *c;
408   char *time;
409
410   videomode(VIDEOMODE_80x25);
411
412   /* clock CPU at double the speed (a whopping 2 Mhz!) */
413   fast();
414
415   /* Manipulate the VDC with IRQs turned off.
416    * KERNALs default IRQ handler will also try to read the VDC status
417    * register, which could interfere with our code trying to read it.
418    */
419   SEI();
420   vdc_patch_charset();
421   CLI();
422
423   clrscr();
424
425   /* Allocate logging buffer memory */
426   init_log();
427
428   /* Set time initially, c128 doesn't know it */
429   set_time_interactive();
430
431   /* disable interrupt driven VIC screen editor */
432   POKE(0xD8, 255);
433
434   /* Load configuration */
435   load_config();
436
437   /* Load items (= drinks) */
438   load_items();
439   /* Load credits */
440   load_credits();
441
442   time = get_time();
443   sprintf(print_buffer, "%c----------------------------------------------------"
444                         "----------------------------\r",
445           17);
446   print_the_buffer();
447   sprintf(print_buffer, "%cC128-Kasse Version " GV "\r", 17);
448   print_the_buffer();
449
450   sprintf(print_buffer,
451           "%cKasse gestartet um %s. Nutze logfile log-%u, offset %d.\r", 17,
452           time, log_num, log_heap_offset);
453   print_the_buffer();
454
455   print_header();
456
457   while (1) {
458     print_screen();
459     c = get_input();
460     /* ...display dialogs eventually */
461     if (*c > 47 && *c < 58) {
462       /* if the input starts with a digit, we will interpret it as a number
463        * for the item to be sold */
464       buy_stock(atoi(c));
465     } else if (*c == 'f') {
466       buy_custom();
467     } else if (*c == 's') {
468       cprintf("\r\nsaving items.. ");
469       save_items();
470       cprintf("ok\r\nsaving credits.. ");
471       save_credits();
472       cprintf("ok\r\nflushing log.. ");
473       log_flush();
474       cprintf("ok\r\nStatefile/Creditfile/Log gesichert, dr" uUML
475               "cke RETURN...\r\n");
476       get_input();
477     } else if (*c == 'g') {
478       credit_manager();
479     } else if (*c == 'z') {
480       set_time_interactive();
481     } else if (*c == 'q')
482       break;
483   }
484   clrscr();
485   cprintf("\r\nBYEBYE\r\n");
486
487   return 0;
488 }