]> git.sur5r.net Git - c128-kasse/blob - src/itemz.c
kasse: allow starting up without a printer
[c128-kasse] / src / itemz.c
1 /*
2  * RGB2R-C128-Kassenprogramm
3  * © 2007-2009 phil_fry, sECuRE, sur5r
4  * See LICENSE for license information
5  *
6  * itemz.c: Verwaltet die ITEMS- und CREDITS-datei
7  *
8  */
9 #include <c128.h>
10 #include <conio.h>
11 #include <stdlib.h>
12 #include <stdio.h>
13 #include <string.h>
14
15 #include "general.h"
16 #include "config.h"
17 #include "credit_manager.h"
18 #include "version.h"
19 #include "vdc_patch_charset.h"
20 #include "globals.h"
21
22 static void itemz_print_screen(void) {
23   BYTE i;
24   char buffer[10];
25
26   clrscr();
27   cprintf("itemz (phil_fry, sECuRE, sur5r, mxf) v:" GV "\r\n\r\n");
28   cprintf("Datei: ITEMS\r\n\r\n");
29   for (i = 0; i < max(status.num_items, 15); i++) {
30     if (format_euro(buffer, 10, status.status[i].price) != buffer) {
31       cprintf("Error: Could not format price %d\r\n", status.status[i].price);
32       exit(1);
33     }
34     cprintf("Eintrag %2d: %s (%s, %d mal verkauft)\r\n", i,
35             status.status[i].item_name, buffer, status.status[i].times_sold);
36   }
37   cprintf("\r\nn) Neu d) L" oUML "schen s) Speichern m) Credit Modus q) "
38           "Beenden\r\nr) Reset des Verkauft-Z" aUML "hlers\r\n");
39 }
40
41 static void new_item(void) {
42   char name[MAX_ITEM_NAME_LENGTH + 1];
43   int price, times_sold;
44
45   if (status.num_items == MAX_ITEMS) {
46     cprintf("\rEs ist bereits die maximale Anzahl an Eintr" aUML
47             "gen erreicht, dr" uUML "cke RETURN...\r\n");
48     cget_return();
49     return;
50   }
51
52   cprintf("\rName des Eintrags:\r\n");
53   if (cgetn_input(name, sizeof(name)) == 0)
54     return;
55   cprintf("\r\nPreis in Cents:\r\n");
56   if ((price = cget_number(0)) <= 0)
57     return;
58   cprintf("\r\nWie oft schon verkauft? [0] \r\n");
59   if ((times_sold = cget_number(0)) < 0)
60     return;
61   memset(status.status[status.num_items].item_name, '\0',
62          MAX_ITEM_NAME_LENGTH + 1);
63   strncpy(status.status[status.num_items].item_name, name,
64           MAX_ITEM_NAME_LENGTH);
65   status.status[status.num_items].price = price;
66   status.status[status.num_items].times_sold = times_sold;
67   status.num_items++;
68 }
69
70 static void _delete_item(BYTE num) {
71   memset(status.status[num].item_name, '\0', MAX_ITEM_NAME_LENGTH);
72   status.status[num].price = 0;
73   status.status[num].times_sold = 0;
74 }
75
76 static void delete_item(void) {
77   BYTE num, last;
78
79   cprintf("\r Welcher Eintrag soll geloescht werden?\r\n");
80   if ((num = cget_number(-1)) < 0)
81     return;
82
83   if (status.num_items > 1) {
84     /* Swap last item with this one and delete the last one to avoid holes */
85     last = (status.num_items - 1);
86     strncpy(status.status[num].item_name, status.status[last].item_name,
87             MAX_ITEM_NAME_LENGTH);
88     status.status[num].price = status.status[last].price;
89     status.status[num].times_sold = status.status[last].times_sold;
90     _delete_item(last);
91   } else {
92     /* Just delete it */
93     _delete_item(num);
94   }
95   status.num_items--;
96 }
97
98 static void reset_counters(void) {
99   BYTE i;
100
101   for (i = 0; i < status.num_items; i++) {
102     status.status[i].times_sold = 0;
103   }
104 }
105
106 static void itemz_manager() {
107   char *c;
108   while (1) {
109     itemz_print_screen();
110     c = get_input();
111     switch (*c) {
112     case 'n':
113       new_item();
114       break;
115     case 'd':
116       delete_item();
117       break;
118     case 's':
119       save_items();
120       break;
121     case 'r':
122       reset_counters();
123       break;
124     case 'm':
125       return; // switch to credit mode
126     case 'q':
127       exit(0);
128     default:
129       cprintf("Unbekannter Befehl, druecke RETURN...\r\n");
130       cget_return();
131     }
132   }
133 }
134
135 int main(void) {
136   videomode(VIDEOMODE_80x25);
137
138   /*  clock CPU at double the speed (a whopping 2 Mhz!) */
139   fast();
140
141   credits.num_items = 0;
142   status.num_items = 0;
143   cprintf("itemz loading...\n");
144   load_config();
145   cprintf("itemz: loading ITEMS...\n");
146   load_items();
147   cprintf("itemz: loading CREDITS...\n");
148   load_credits();
149   while (1) {
150     itemz_manager();
151     credit_manager();
152   }
153   return 0;
154 }