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