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