]> git.sur5r.net Git - c128-kasse/blob - src/kasse.c
054dfa11170a3521160bb35fad1a3d89db03ebc2
[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 #include <stdio.h>
8 #include <conio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <cbm.h>
12 #include <c128.h>
13 #include <6502.h>
14
15 #include "general.h"
16 #include "config.h"
17 #include "kasse.h"
18 #include "credit_manager.h"
19 #include "c128time.h"
20 #include "print.h"
21 #include "version.h"
22 #include "vdc_patch_charset.h"
23 #include "globals.h"
24 // drucker 4 oder 5
25 // graphic 4,0,10
26
27 void print_item(BYTE i) {
28   char profit[EUR_FORMAT_MINLEN + 1];
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 profit[EUR_FORMAT_MINLEN + 1];
45   clrscr();
46   if (format_euro(profit, sizeof(profit), money) == NULL) {
47     cprintf("Einnahme %ld konnte nicht umgerechnet werden\r\n", money);
48     profit[0] = '\0';
49   }
50   textcolor(TC_CYAN);
51   /* fill whole line with cyan, so color bits are set up for the clock */
52   cprintf("%-80s", "C128-Kasse (phil_fry, sECuRE, sur5r, mxf) " GV);
53   textcolor(TC_LIGHT_GRAY);
54   cprintf("\r\n\r\n"
55           "Ertrag: %s (%ld Artikel); Drucken: %s\r\n",
56           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, int32_t item_price, int16_t einheiten,
113                       char *nickname, char *rest) {
114   char *time = get_time();
115   uint8_t n;
116   char price[EUR_FORMAT_MINLEN + 1];
117   if (format_euro(price, sizeof(price), item_price) == NULL) {
118     cprintf("Preis %d konnte nicht umgerechnet werden\r\n", item_price);
119     exit(1);
120   }
121
122   /* TODO: teach the EUR sign to the printer.
123    * Until then, we just overwrite it with "E" */
124   price[EUR_FORMAT_MINLEN - 1] = 'E';
125   rest[EUR_FORMAT_MINLEN - 1] = 'E';
126
127   /* clang-format off */
128   n = snprintf(print_buffer, sizeof(print_buffer),
129         /* enable lower case letters -- 1 */
130         "%c"
131         /*  Transaction-ID (Anzahl verkaufter Einträge, inklusive des zu druckenden!)
132             -- 6-stellig */
133         "[%3u] "
134         /* Uhrzeit -- 8-stellig + 3 */
135         "%8s - "
136         /*  Eintragname (= Getränk) -- 9-stellig + 3 */
137         "%-" xstr(MAX_ITEM_NAME_LENGTH) "s - "
138         /*  Preis (in Cents) -- 8-stellig  + 3 */
139         "%" xstr(EUR_FORMAT_MINLEN) "s - "
140         /*  restguthaben (8-stellig) + 3 */
141         "%" xstr(EUR_FORMAT_MINLEN) "s - "
142         /*  Anzahl -- 2-stellig + 3 */
143         "%2d - "
144         /*  Nickname (falls es vom Guthaben abgezogen wird) -- 10-stellig + 4 */
145         "an %" xstr(NICKNAME_MAX_LEN)"s\r",
146         17, status.transaction_id, time, name, price, rest, einheiten,
147         (*nickname != '\0' ? nickname : "Unbekannt"));
148   /* clang-format on */
149   if (n > sizeof(print_buffer)) {
150     cprintf("\r\nprint_log(): print_buffer overflowed!\r\n"
151             "Wanted to write %d bytes\r\n%s\r\n",
152             n, print_buffer);
153     exit(1);
154   }
155
156   status.transaction_id++;
157   print_the_buffer();
158 }
159
160 /* dialog which is called for each bought item */
161 static signed int buy(char *name, int32_t price) {
162   BYTE matches = 0;
163   BYTE c, nickname_len;
164   int16_t einheiten;
165   char nickname[NICKNAME_MAX_LEN + 1];
166   char rest[EUR_FORMAT_MINLEN + 1];
167   struct credits_t *credit;
168
169   clrscr();
170   cprintf("Wieviel Einheiten \"%s\"? [1] \r\n", name);
171
172   einheiten = cget_number(1);
173
174   if (einheiten > 100 || einheiten < -100 || einheiten == 0) {
175     cprintf("\r\nEinheit nicht in [-100, 100] oder 0, Abbruch, dr" uUML "cke "
176             "RETURN...\r\n");
177     cget_return();
178     return 1;
179   }
180
181   cprintf("\r\nAuf ein Guthaben kaufen? Wenn ja, Nickname eingeben:\r\n");
182   nickname_len = cget_nickname(nickname, sizeof(nickname));
183
184   if (nickname_len && *nickname != '\0' && *nickname != PETSCII_SP) {
185     /* go through credits and remove the amount of money or set nickname
186      * to NULL if no such credit could be found */
187     credit = find_credit(nickname);
188     if (credit != NULL) {
189       while ((int32_t)credit->credit < (price * einheiten)) {
190         if (format_euro(rest, sizeof(rest), credit->credit) == NULL) {
191           cprintf("Preis %d konnte nicht umgerechnet werden\r\n",
192                   credit->credit);
193           exit(1);
194         }
195         cprintf(
196             "\r\n%s hat nicht genug Geld (%s). e) einzahlen a) abbruch \r\n",
197             nickname, rest);
198         c = cgetc();
199         if (c == 'e') {
200           deposit_credit(nickname);
201         } else {
202           return 0;
203         }
204       }
205       /* substract money */
206       credit->credit -= (price * einheiten);
207
208       if (format_euro(rest, sizeof(rest), credit->credit) == NULL) {
209         cprintf("Preis %d konnte nicht umgerechnet werden\r\n", credit->credit);
210         exit(1);
211       }
212
213       textcolor(TC_LIGHT_GREEN);
214       cprintf("\r\nVerbleibendes Guthaben f" uUML "r %s: %s. Dr" uUML
215               "cke RETURN...\r\n",
216               nickname, rest);
217       textcolor(TC_LIGHT_GRAY);
218       cget_return();
219       matches++;
220     } else {
221       textcolor(TC_LIGHT_RED);
222       cprintf("\r\nNickname nicht gefunden in der Guthabenverwaltung! Abbruch, "
223               "dr" uUML "cke RETURN...\r\n");
224       textcolor(TC_LIGHT_GRAY);
225       cget_return();
226       return 0;
227     }
228   }
229
230   money += price * einheiten;
231   items_sold += einheiten;
232   if (printing == 1)
233     print_log(name, price, einheiten, nickname, rest);
234
235   return einheiten;
236 }
237
238 void buy_stock(BYTE n) {
239   if (n >= status.num_items || status.status[n].item_name == NULL) {
240     cprintf("FEHLER: Diese Einheit existiert nicht.\r\n");
241     cget_return();
242     return;
243   }
244
245   status.status[n].times_sold +=
246       buy(status.status[n].item_name, status.status[n].price);
247 }
248
249 void buy_custom(void) {
250   char name[MAX_ITEM_NAME_LENGTH + 1];
251   int price;
252
253   clrscr();
254   cprintf("\r\nWas soll gekauft werden?\r\n");
255   if (cgetn_input(name, sizeof(name)) == 0)
256     return;
257
258   cprintf("\r\nWie teuer ist \"%s\" (in cents)?\r\n", name);
259
260   price = cget_number(0);
261
262   if (price == 0) {
263     cprintf("Kauf abgebrochen, dr" uUML "cke RETURN...\r\n");
264     cget_return();
265     return;
266   }
267
268   buy(name, price);
269 }
270
271 void set_time_interactive(void) {
272   char part[3] = {'\0', '\0', '\0'};
273   uint8_t day, tp1, tp2, tp3;
274   char *time_input, *time;
275   cprintf("Gib den aktuellen Tag des Events und Uhrzeit ein\r\n"
276           "Format DHHMMSS, 0-indexiert, z.B. 0174259 für \"erster Tag um "
277           "17:42:59\":\r\n");
278   time_input = get_input();
279   part[0] = time_input[0];
280   day = atoi(part);
281   part[0] = time_input[1];
282   part[1] = time_input[2];
283   tp1 = atoi(part);
284   part[0] = time_input[3];
285   part[1] = time_input[4];
286   tp2 = atoi(part);
287   part[0] = time_input[5];
288   part[1] = time_input[6];
289   tp3 = atoi(part);
290   set_time(day, tp1, tp2, tp3);
291
292   time = get_time();
293   cprintf("\r\nZeit gesetzt: %s\r\n", time);
294 }
295
296 int main(void) {
297   char *c;
298   char *time;
299
300   printing = 1;
301   /* initialize daytime global, start the CIA TOD */
302   set_time(0, 0, 0, 0);
303   kasse_menu = MENU_UNDEFINED;
304
305   videomode(VIDEOMODE_80x25);
306
307   /* clock CPU at double the speed (a whopping 2 Mhz!) */
308   fast();
309
310   /* Manipulate the VDC with IRQs turned off.
311    * KERNALs default IRQ handler will also try to read the VDC status
312    * register, which could interfere with our code trying to read it.
313    */
314   SEI();
315   vdc_patch_charset();
316   CLI();
317
318   clrscr();
319
320   install_daytime_irq();
321
322   /* Allocate logging buffer memory */
323   init_log();
324
325   /* Set time initially, c128 doesn't know it */
326   set_time_interactive();
327
328   /* disable interrupt driven VIC screen editor */
329   POKE(0xD8, 255);
330
331   /* Load configuration */
332   load_config();
333
334   /* Load items (= drinks) */
335   load_items();
336   /* Load credits */
337   load_credits();
338
339   time = get_time();
340   sprintf(print_buffer, "%c----------------------------------------------------"
341                         "----------------------------\r",
342           17);
343   print_the_buffer();
344   sprintf(print_buffer, "%cC128-Kasse Version " GV "\r", 17);
345   print_the_buffer();
346
347   sprintf(print_buffer,
348           "%cKasse gestartet um %s. Nutze logfile log-%u, offset %d.\r", 17,
349           time, log_num, log_heap_offset);
350   print_the_buffer();
351
352   print_header();
353
354   while (1) {
355     print_screen();
356     kasse_menu = MENU_MAIN;
357     c = get_input();
358     kasse_menu = MENU_UNDEFINED;
359     /* ...display dialogs eventually */
360     if (*c >= PETSCII_0 && *c <= PETSCII_9) {
361       /* if the input starts with a digit, we will interpret it as a number
362        * for the item to be sold */
363       buy_stock(atoi(c));
364     } else if (*c == 'f') {
365       buy_custom();
366     } else if (*c == 's') {
367       cprintf("\r\nsaving items.. ");
368       save_items();
369       cprintf("ok\r\nsaving credits.. ");
370       save_credits();
371       cprintf("ok\r\nflushing log.. ");
372       log_flush();
373       cprintf("ok\r\nStatefile/Creditfile/Log gesichert, dr" uUML
374               "cke RETURN...\r\n");
375       cget_return();
376     } else if (*c == 'g') {
377       credit_manager();
378     } else if (*c == 'z') {
379       set_time_interactive();
380     } else if (*c == 'q')
381       break;
382   }
383   clrscr();
384   cprintf("\r\nBYEBYE\r\n");
385
386   return 0;
387 }