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 profit[EUR_FORMAT_MINLEN + 1];
46 if (format_euro(profit, sizeof(profit), money) == NULL) {
47 cprintf("Einnahme %ld konnte nicht umgerechnet werden\r\n", money);
51 cprintf("C128-Kassenprogramm (phil_fry, sECuRE, sur5r, mxf) " GV "\r\n");
52 textcolor(TC_LIGHT_GRAY);
54 "Ertrag: %s (%ld Artikel); Drucken: %s\r\n",
55 profit, items_sold, (printing == 1 ? "ein" : "aus"));
56 textcolor(TC_LIGHT_GRAY);
58 "\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xB2"
59 "\xC0\xC0\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\xB2"
61 "\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xAE"
63 for (; i < min(status.num_items, 15); ++i) {
69 /* if we have more than 15 items, use the second column */
70 if ((i + 15) < status.num_items) {
74 cprintf(" \xDD \xDD");
80 "\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xB1"
81 "\xC0\xC0\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\xB1"
83 "\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xC0\xBD"
87 textcolor(TC_LIGHT_GRAY);
88 cprintf(") Daten sichern ");
91 textcolor(TC_LIGHT_GRAY);
92 cprintf(") Guthabenverwaltung\r\n");
95 textcolor(TC_LIGHT_GRAY);
96 cprintf(") Zeit setzen ");
99 textcolor(TC_LIGHT_GRAY);
100 cprintf(") Freitext verkaufen ");
101 textcolor(TC_YELLOW);
103 textcolor(TC_LIGHT_GRAY);
104 cprintf(") Beenden\r\n");
108 * Prints a line and logs it to file. Every line can be at max 80 characters.
111 static void print_log(char *name, int32_t item_price, int16_t einheiten,
112 char *nickname, char *rest) {
113 char *time = get_time();
115 char price[EUR_FORMAT_MINLEN + 1];
116 if (format_euro(price, sizeof(price), item_price) == NULL) {
117 cprintf("Preis %d konnte nicht umgerechnet werden\r\n", item_price);
121 /* TODO: teach the EUR sign to the printer.
122 * Until then, we just overwrite it with "E" */
123 price[EUR_FORMAT_MINLEN - 1] = 'E';
124 rest[EUR_FORMAT_MINLEN - 1] = 'E';
126 /* clang-format off */
127 n = snprintf(print_buffer, sizeof(print_buffer),
128 /* enable lower case letters -- 1 */
130 /* Transaction-ID (Anzahl verkaufter Einträge, inklusive des zu druckenden!)
133 /* Uhrzeit -- 8-stellig + 3 */
135 /* Eintragname (= Getränk) -- 9-stellig + 3 */
136 "%-" xstr(MAX_ITEM_NAME_LENGTH) "s - "
137 /* Preis (in Cents) -- 8-stellig + 3 */
138 "%" xstr(EUR_FORMAT_MINLEN) "s - "
139 /* restguthaben (8-stellig) + 3 */
140 "%" xstr(EUR_FORMAT_MINLEN) "s - "
141 /* Anzahl -- 2-stellig + 3 */
143 /* Nickname (falls es vom Guthaben abgezogen wird) -- 10-stellig + 4 */
144 "an %" xstr(NICKNAME_MAX_LEN)"s\r",
145 17, status.transaction_id, time, name, price, rest, einheiten,
146 (*nickname != '\0' ? nickname : "Unbekannt"));
147 /* clang-format on */
148 if (n > sizeof(print_buffer)) {
149 cprintf("\r\nprint_log(): print_buffer overflowed!\r\n"
150 "Wanted to write %d bytes\r\n%s\r\n",
155 status.transaction_id++;
159 /* dialog which is called for each bought item */
160 static signed int buy(char *name, int32_t price) {
162 BYTE c, nickname_len;
164 char nickname[NICKNAME_MAX_LEN + 1];
165 char rest[EUR_FORMAT_MINLEN + 1];
166 struct credits_t *credit;
169 cprintf("Wieviel Einheiten \"%s\"? [1] \r\n", name);
171 einheiten = cget_number(1);
173 if (einheiten > 100 || einheiten < -100 || einheiten == 0) {
174 cprintf("\r\nEinheit nicht in [-100, 100] oder 0, Abbruch, dr" uUML "cke "
180 cprintf("\r\nAuf ein Guthaben kaufen? Wenn ja, Nickname eingeben:\r\n");
181 nickname_len = cget_nickname(nickname, sizeof(nickname));
183 if (nickname_len && *nickname != '\0' && *nickname != PETSCII_SP) {
184 /* go through credits and remove the amount of money or set nickname
185 * to NULL if no such credit could be found */
186 credit = find_credit(nickname);
187 if (credit != NULL) {
188 while ((int32_t)credit->credit < (price * einheiten)) {
189 if (format_euro(rest, sizeof(rest), credit->credit) == NULL) {
190 cprintf("Preis %d konnte nicht umgerechnet werden\r\n",
195 "\r\n%s hat nicht genug Geld (%s). e) einzahlen a) abbruch \r\n",
199 deposit_credit(nickname);
204 /* substract money */
205 credit->credit -= (price * einheiten);
207 if (format_euro(rest, sizeof(rest), credit->credit) == NULL) {
208 cprintf("Preis %d konnte nicht umgerechnet werden\r\n", credit->credit);
212 textcolor(TC_LIGHT_GREEN);
213 cprintf("\r\nVerbleibendes Guthaben f" uUML "r %s: %s. Dr" uUML
216 textcolor(TC_LIGHT_GRAY);
220 textcolor(TC_LIGHT_RED);
221 cprintf("\r\nNickname nicht gefunden in der Guthabenverwaltung! Abbruch, "
222 "dr" uUML "cke RETURN...\r\n");
223 textcolor(TC_LIGHT_GRAY);
229 money += price * einheiten;
230 items_sold += einheiten;
232 print_log(name, price, einheiten, nickname, rest);
237 void buy_stock(BYTE n) {
238 if (n >= status.num_items || status.status[n].item_name == NULL) {
239 cprintf("FEHLER: Diese Einheit existiert nicht.\r\n");
244 status.status[n].times_sold +=
245 buy(status.status[n].item_name, status.status[n].price);
248 void buy_custom(void) {
249 char name[MAX_ITEM_NAME_LENGTH + 1];
253 cprintf("\r\nWas soll gekauft werden?\r\n");
254 if (cgetn_input(name, sizeof(name)) == 0)
257 cprintf("\r\nWie teuer ist \"%s\" (in cents)?\r\n", name);
259 price = cget_number(0);
262 cprintf("Kauf abgebrochen, dr" uUML "cke RETURN...\r\n");
270 void set_time_interactive(void) {
271 char part[3] = {'\0', '\0', '\0'};
272 uint8_t day, tp1, tp2, tp3;
273 char *time_input, *time;
274 cprintf("Gib den aktuellen Tag des Events und Uhrzeit ein\r\n"
275 "(Format DHHMMSS):\r\n");
276 time_input = get_input();
277 part[0] = time_input[0];
279 part[0] = time_input[1];
280 part[1] = time_input[2];
282 part[0] = time_input[3];
283 part[1] = time_input[4];
285 part[0] = time_input[5];
286 part[1] = time_input[6];
288 set_time(day, tp1, tp2, tp3);
291 cprintf("\r\nZeit gesetzt: %s\r\n", time);
300 videomode(VIDEOMODE_80x25);
302 /* clock CPU at double the speed (a whopping 2 Mhz!) */
305 /* Manipulate the VDC with IRQs turned off.
306 * KERNALs default IRQ handler will also try to read the VDC status
307 * register, which could interfere with our code trying to read it.
315 install_daytime_irq();
317 /* Allocate logging buffer memory */
320 /* Set time initially, c128 doesn't know it */
321 set_time_interactive();
323 /* disable interrupt driven VIC screen editor */
326 /* Load configuration */
329 /* Load items (= drinks) */
335 sprintf(print_buffer, "%c----------------------------------------------------"
336 "----------------------------\r",
339 sprintf(print_buffer, "%cC128-Kasse Version " GV "\r", 17);
342 sprintf(print_buffer,
343 "%cKasse gestartet um %s. Nutze logfile log-%u, offset %d.\r", 17,
344 time, log_num, log_heap_offset);
351 kasse_menu = MENU_MAIN;
353 kasse_menu = MENU_UNDEFINED;
354 /* ...display dialogs eventually */
355 if (*c >= PETSCII_0 && *c <= PETSCII_9) {
356 /* if the input starts with a digit, we will interpret it as a number
357 * for the item to be sold */
359 } else if (*c == 'f') {
361 } else if (*c == 's') {
362 cprintf("\r\nsaving items.. ");
364 cprintf("ok\r\nsaving credits.. ");
366 cprintf("ok\r\nflushing log.. ");
368 cprintf("ok\r\nStatefile/Creditfile/Log gesichert, dr" uUML
369 "cke RETURN...\r\n");
371 } else if (*c == 'g') {
373 } else if (*c == 'z') {
374 set_time_interactive();
375 } else if (*c == 'q')
379 cprintf("\r\nBYEBYE\r\n");