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