]> git.sur5r.net Git - c128-kasse/blob - src/itemz.c
7e72f57c77cae01af080520a74f333a5a83a786f
[c128-kasse] / src / itemz.c
1 /*
2  * RGB2R-C128-Kassenprogramm
3  * (c) 2007-2008 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 /* NOTE: dummy entry to fix linking */
20 int log_num = 0;
21
22 static void itemz_print_screen() {
23         BYTE i;
24         char buffer[10];
25
26         clrscr();
27         cprintf("itemz (phil_fry, sECuRE, sur5r)\r\n\r\n");
28         cprintf("Datei: ITEMS\r\n\r\n");
29         for (i = 0; i < status.num_items; 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 %x: %s (%s, %d mal verkauft)\r\n",
35                         i, status.status[i].item_name, buffer, status.status[i].times_sold);
36         }
37         cprintf("\r\nn) Neu d) Loeschen s) Speichern m) Credit Modus q) Beenden\r\n");
38 }
39
40 static void new_item() {
41         char *input, *name;
42         int price;
43
44         if (status.num_items == MAX_ITEMS) {
45                 cprintf("\rEs ist bereits die maximale Anzahl an Eintraegen erreicht, druecke RETURN...\r\n");
46                 input = get_input();
47                 return;
48         }
49
50         cprintf("\rName des Eintrags:\r\n");
51         if ((input = get_input()) == NULL || *input == '\0')
52                 return;
53         name = strdup(input);
54         cprintf("\r\nPreis in Cents:\r\n");
55         if ((input = get_input()) == NULL || *input == '\0' || (price = atoi(input)) == 0)
56                 return;
57         cprintf("\r\nWie oft schon verkauft? [0] \r\n");
58         if ((input = get_input()) == NULL || *input == '\0')
59                 return;
60         memset(status.status[status.num_items].item_name, '\0', MAX_ITEM_NAME_LENGTH+1);
61         strncpy(status.status[status.num_items].item_name, name, MAX_ITEM_NAME_LENGTH);
62         status.status[status.num_items].price = price;
63         status.status[status.num_items].times_sold = atoi(input);
64         status.num_items++;
65         free(name);
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() {
75         char *input;
76         BYTE num, last;
77
78         cprintf("\r Welcher Eintrag soll geloescht werden?\r\n");
79         if ((input = get_input()) == NULL || *input == '\0')
80                 return;
81         num = atoi(input);
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                 strcpy(status.status[num].item_name, status.status[last].item_name);
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 itemz_manager(){
97         char *c;
98         while(1){
99                 itemz_print_screen();
100                 c = get_input();
101                 switch (*c) {
102                         case 'n':
103                                 new_item(); break;
104                         case 'd':
105                                 delete_item(); break;
106                         case 's':
107                                 save_items(); break;
108                         case 'm':
109                                 return; // switch to credit mode
110                         case 'q':
111                                 exit(0);
112                         default:
113                                 cprintf("Unbekannter Befehl, druecke RETURN...\r\n");
114                                 get_input(); 
115                 }
116         }
117 }
118
119 int main() {
120         if (VIDEOMODE == 40)
121                 toggle_videomode();
122         credits.num_items = 0;
123         status.num_items = 0;
124         cprintf("itemz loading...\n");
125         load_config();
126         cprintf("itemz: loading ITEMS...\n");
127         load_items();
128         cprintf("itemz: loading CREDITS...\n");
129         load_credits();
130         while (1) {
131                 itemz_manager();
132                 credit_manager();
133         }
134         return 0;
135 }