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