2 * RGB2R-C128-Kassenprogramm
3 * © 2007-2009 phil_fry, sECuRE, sur5r
4 * See LICENSE for license information
18 #include "credit_manager.h"
22 #include "vdc_patch_charset.h"
27 void print_item(BYTE i) {
28 char profit[EUR_FORMAT_MINLEN + 1];
29 if (format_euro(profit, sizeof(profit), status.status[i].price) == NULL) {
30 cprintf("Preis %ld konnte nicht umgerechnet werden\r\n",
31 status.status[i].price);
36 textcolor(TC_LIGHT_GRAY);
37 cprintf(": %-" xstr(MAX_ITEM_NAME_LENGTH) "s \xDD%s, %3dx ",
38 status.status[i].item_name, profit, status.status[i].times_sold);
41 /* Hauptbildschirm ausgeben */
42 static void print_screen(void) {
44 char *time = get_time();
45 char profit[EUR_FORMAT_MINLEN + 1];
47 if (format_euro(profit, sizeof(profit), money) == NULL) {
48 cprintf("Einnahme %ld konnte nicht umgerechnet werden\r\n", money);
52 cprintf("C128-Kassenprogramm (phil_fry, sECuRE, sur5r, mxf) " GV "\r\n");
53 textcolor(TC_LIGHT_GRAY);
54 cprintf("\r\nUhrzeit: %s (wird nicht aktualisiert)\r\n"
55 "Eingenommen: %s, Verkauft: %ld Dinge, Drucken: %s\r\n",
56 time, profit, items_sold, (printing == 1 ? "ein" : "aus"));
57 textcolor(TC_LIGHT_GRAY);
59 "\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xB2"
60 "\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xB2"
61 "\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xB2"
62 "\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xAE"
64 for (; i < min(status.num_items, 15); ++i) {
70 /* if we have more than 15 items, use the second column */
71 if ((i + 15) < status.num_items) {
75 cprintf(" \xDD \xDD");
81 "\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xB1"
82 "\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xB1"
83 "\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xB1"
84 "\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xBD"
88 textcolor(TC_LIGHT_GRAY);
89 cprintf(") Daten sichern ");
92 textcolor(TC_LIGHT_GRAY);
93 cprintf(") Guthabenverwaltung\r\n");
96 textcolor(TC_LIGHT_GRAY);
97 cprintf(") Zeit setzen ");
100 textcolor(TC_LIGHT_GRAY);
101 cprintf(") Freitext verkaufen ");
102 textcolor(TC_YELLOW);
104 textcolor(TC_LIGHT_GRAY);
105 cprintf(") Beenden\r\n");
109 * Prints a line and logs it to file. Every line can be at max 80 characters.
112 static void print_log(char *name, int item_price, int einheiten, char *nickname,
114 char *time = get_time();
116 char price[EUR_FORMAT_MINLEN + 1];
117 if (format_euro(price, sizeof(price), item_price) == NULL) {
118 cprintf("Preis %d konnte nicht umgerechnet werden\r\n", item_price);
122 /* TODO: teach the EUR sign to the printer.
123 * Until then, we just overwrite it with "E" */
124 price[EUR_FORMAT_MINLEN - 1] = 'E';
125 rest[EUR_FORMAT_MINLEN - 1] = 'E';
127 /* clang-format off */
128 n = snprintf(print_buffer, sizeof(print_buffer),
129 /* enable lower case letters -- 1 */
131 /* Transaction-ID (Anzahl verkaufter Einträge, inklusive des zu druckenden!)
134 /* Uhrzeit -- 8-stellig + 3 */
136 /* Eintragname (= Getränk) -- 9-stellig + 3 */
137 "%-" xstr(MAX_ITEM_NAME_LENGTH) "s - "
138 /* Preis (in Cents) -- 7-stellig + 3 */
139 "%" xstr(EUR_FORMAT_MINLEN) "s - "
140 /* restguthaben (7-stellig) + 3 */
141 "%" xstr(EUR_FORMAT_MINLEN) "s - "
142 /* Anzahl -- 2-stellig + 3 */
144 /* Nickname (falls es vom Guthaben abgezogen wird) -- 10-stellig + 4 */
145 "an %" xstr(NICKNAME_MAX_LEN)"s\r",
146 17, status.transaction_id, time, name, price, rest, einheiten,
147 (*nickname != '\0' ? nickname : "Unbekannt"));
148 /* clang-format on */
149 if (n > sizeof(print_buffer)) {
150 cprintf("\r\nprint_log(): print_buffer overflowed!\r\n"
151 "Wanted to write %d bytes\r\n%s\r\n",
156 status.transaction_id++;
160 /* dialog which is called for each bought item */
161 static signed int buy(char *name, unsigned int price) {
163 BYTE c, nickname_len;
165 char nickname[NICKNAME_MAX_LEN + 1];
166 char rest[EUR_FORMAT_MINLEN + 1];
167 struct credits_t *credit;
170 cprintf("Wieviel Einheiten \"%s\"? [1] \r\n", name);
172 einheiten = cget_number(1);
174 if (einheiten > 100 || einheiten < -100 || einheiten == 0) {
175 cprintf("\r\nEinheit nicht in [-100, 100] oder 0, Abbruch, dr" uUML "cke "
181 cprintf("\r\nAuf ein Guthaben kaufen? Wenn ja, Nickname eingeben:\r\n");
182 nickname_len = cget_nickname(nickname, sizeof(nickname));
184 if (nickname_len && *nickname != '\0' && *nickname != PETSCII_SP) {
185 /* go through credits and remove the amount of money or set nickname
186 * to NULL if no such credit could be found */
187 credit = find_credit(nickname);
188 if (credit != NULL) {
189 while ((signed int)credit->credit < ((signed int)price * einheiten)) {
190 if (format_euro(rest, sizeof(rest), credit->credit) == NULL) {
191 cprintf("Preis %d konnte nicht umgerechnet werden\r\n",
196 "\r\n%s hat nicht genug Geld (%s). e) einzahlen a) abbruch \r\n",
200 deposit_credit(nickname);
205 /* substract money */
206 credit->credit -= (price * einheiten);
208 if (format_euro(rest, sizeof(rest), credit->credit) == NULL) {
209 cprintf("Preis %d konnte nicht umgerechnet werden\r\n", credit->credit);
213 textcolor(TC_LIGHT_GREEN);
214 cprintf("\r\nVerbleibendes Guthaben f" uUML "r %s: %s. Dr" uUML
217 textcolor(TC_LIGHT_GRAY);
221 textcolor(TC_LIGHT_RED);
222 cprintf("\r\nNickname nicht gefunden in der Guthabenverwaltung! Abbruch, "
223 "dr" uUML "cke RETURN...\r\n");
224 textcolor(TC_LIGHT_GRAY);
230 money += price * einheiten;
231 items_sold += einheiten;
233 print_log(name, price, einheiten, nickname, rest);
238 void buy_stock(BYTE n) {
239 if (n >= status.num_items || status.status[n].item_name == NULL) {
240 cprintf("FEHLER: Diese Einheit existiert nicht.\r\n");
245 status.status[n].times_sold +=
246 buy(status.status[n].item_name, status.status[n].price);
249 void buy_custom(void) {
250 char name[MAX_ITEM_NAME_LENGTH + 1];
254 cprintf("\r\nWas soll gekauft werden?\r\n");
255 if (cgetn_input(name, sizeof(name)) == 0)
258 cprintf("\r\nWie teuer ist \"%s\" (in cents)?\r\n", name);
260 price = cget_number(0);
263 cprintf("Kauf abgebrochen, dr" uUML "cke RETURN...\r\n");
271 void set_time_interactive(void) {
272 BYTE part[3] = {'0', '0', '\0'};
274 char *time_input, *time;
275 cprintf("Gib die aktuelle Uhrzeit ein (Format HHMMSS):\r\n");
276 time_input = get_input();
277 part[0] = time_input[0];
278 part[1] = time_input[1];
280 part[0] = time_input[2];
281 part[1] = time_input[3];
283 part[0] = time_input[4];
284 part[1] = time_input[5];
286 set_time(tp1, tp2, tp3);
289 cprintf("\r\nZeit gesetzt: %s\r\n", time);
298 videomode(VIDEOMODE_80x25);
300 /* clock CPU at double the speed (a whopping 2 Mhz!) */
303 /* Manipulate the VDC with IRQs turned off.
304 * KERNALs default IRQ handler will also try to read the VDC status
305 * register, which could interfere with our code trying to read it.
313 /* Allocate logging buffer memory */
316 /* Set time initially, c128 doesn't know it */
317 set_time_interactive();
319 /* disable interrupt driven VIC screen editor */
322 /* Load configuration */
325 /* Load items (= drinks) */
331 sprintf(print_buffer, "%c----------------------------------------------------"
332 "----------------------------\r",
335 sprintf(print_buffer, "%cC128-Kasse Version " GV "\r", 17);
338 sprintf(print_buffer,
339 "%cKasse gestartet um %s. Nutze logfile log-%u, offset %d.\r", 17,
340 time, log_num, log_heap_offset);
348 /* ...display dialogs eventually */
349 if (*c >= PETSCII_0 && *c <= PETSCII_9) {
350 /* if the input starts with a digit, we will interpret it as a number
351 * for the item to be sold */
353 } else if (*c == 'f') {
355 } else if (*c == 's') {
356 cprintf("\r\nsaving items.. ");
358 cprintf("ok\r\nsaving credits.. ");
360 cprintf("ok\r\nflushing log.. ");
362 cprintf("ok\r\nStatefile/Creditfile/Log gesichert, dr" uUML
363 "cke RETURN...\r\n");
365 } else if (*c == 'g') {
367 } else if (*c == 'z') {
368 set_time_interactive();
369 } else if (*c == 'q')
373 cprintf("\r\nBYEBYE\r\n");