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];
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];
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();
115 char price[EUR_FORMAT_MINLEN];
117 Transaction-ID (Anzahl verkaufter Einträge, inklusive des zu druckenden!)
120 Eintragname (= Getränk) -- 9-stellig
121 Preis (in Cents) -- 9-stellig
123 Nickname (falls es vom Guthaben abgezogen wird) -- 10-stellig
124 restguthaben (9-stellig)
129 if (format_euro(price, sizeof(price), item_price) == NULL) {
130 cprintf("Preis %d konnte nicht umgerechnet werden\r\n", item_price);
134 sprintf(print_buffer, "%c[%3u] %s - %-" xstr(
135 MAX_ITEM_NAME_LENGTH) "s - %s - %s - %d - an %s\r",
136 17, status.transaction_id, time, name, price, rest, einheiten,
137 (*nickname != '\0' ? nickname : "Unbekannt"));
138 status.transaction_id++;
142 /* dialog which is called for each bought item */
143 static signed int buy(char *name, unsigned int price) {
145 BYTE c, nickname_len;
147 char nickname[NICKNAME_MAX_LEN + 1];
148 char rest[EUR_FORMAT_MINLEN];
149 struct credits_t *credit;
151 memset(nickname, '\0', sizeof(nickname));
152 memset(rest, '\0', sizeof(rest));
155 cprintf("Wieviel Einheiten \"%s\"? [1] \r\n", name);
157 einheiten = cget_number(1);
159 if (einheiten > 100 || einheiten < -100 || einheiten == 0) {
160 cprintf("\r\nEinheit nicht in [-100, 100] oder 0, Abbruch, dr" uUML "cke "
166 cprintf("\r\nAuf ein Guthaben kaufen? Wenn ja, Nickname eingeben:\r\n");
173 input_terminator_t terminator;
175 terminator = get_input_terminated_by(INPUT_TERMINATOR_RETURN |
176 INPUT_TERMINATOR_SPACE,
177 nickname, sizeof(nickname));
179 /* Clear the screen from any previous completions */
182 for (i = 1; i < 7; i++) {
183 /* "Completion:" is longer than NICKNAME_MAX_LEN */
184 cclearxy(0, y + i, strlen("Completion:"));
188 if (terminator != INPUT_TERMINATOR_SPACE) {
194 for (i = 0; i < credits.num_items; i++) {
195 if (strncmp(nickname, credits.credits[i].nickname, strlen(nickname)) !=
203 uniquematch = credits.credits[i].nickname;
206 /* Display the rest of the nickname */
207 textcolor(TC_LIGHT_GREEN);
208 cprintf("%s", uniquematch + strlen(nickname));
209 textcolor(TC_LIGHT_GRAY);
210 strcat(nickname, uniquematch + strlen(nickname));
212 /* Multiple nicknames match what was entered so far. Abort and
213 * display all matches, then prompt the user again. */
214 char completion[NICKNAME_MAX_LEN + 1];
215 BYTE len = strlen(nickname);
218 cprintf("\r\nCompletion:\r\n");
220 for (i = 0; i < credits.num_items; i++) {
221 if (strncmp(nickname, credits.credits[i].nickname, len) != 0) {
224 if (++matches == 5) {
228 strcpy(completion, credits.credits[i].nickname);
229 *(completion + len) = '\0';
230 cprintf("%s", completion);
231 textcolor(TC_LIGHT_GREEN);
232 cprintf("%c", *(credits.credits[i].nickname + len));
233 textcolor(TC_LIGHT_GRAY);
234 cprintf("%s\r\n", completion + len + 1);
241 if (*nickname != '\0' && *nickname != PETSCII_SP) {
242 nickname_len = strlen(nickname);
243 /* go through credits and remove the amount of money or set nickname
244 * to NULL if no such credit could be found */
245 credit = find_credit(nickname);
246 if (credit != NULL) {
247 while ((signed int)credit->credit < ((signed int)price * einheiten)) {
248 if (format_euro(rest, sizeof(rest), credit->credit) == NULL) {
249 cprintf("Preis %d konnte nicht umgerechnet werden\r\n",
254 "\r\n%s hat nicht genug Geld (%s). e) einzahlen a) abbruch \r\n",
258 deposit_credit(nickname);
263 /* substract money */
264 credit->credit -= (price * einheiten);
266 if (format_euro(rest, sizeof(rest), credit->credit) == NULL) {
267 cprintf("Preis %d konnte nicht umgerechnet werden\r\n", credit->credit);
271 textcolor(TC_LIGHT_GREEN);
272 cprintf("\r\nVerbleibendes Guthaben f" uUML "r %s: %s. Dr" uUML
275 textcolor(TC_LIGHT_GRAY);
279 textcolor(TC_LIGHT_RED);
280 cprintf("\r\nNickname nicht gefunden in der Guthabenverwaltung! Abbruch, "
281 "dr" uUML "cke RETURN...\r\n");
282 textcolor(TC_LIGHT_GRAY);
287 /* Ensure that nickname is NULL if it's empty because it's used in print_log
292 money += price * einheiten;
293 items_sold += einheiten;
295 print_log(name, price, einheiten, nickname, rest);
300 void buy_stock(BYTE n) {
301 if (n >= status.num_items || status.status[n].item_name == NULL) {
302 cprintf("FEHLER: Diese Einheit existiert nicht.\r\n");
307 status.status[n].times_sold +=
308 buy(status.status[n].item_name, status.status[n].price);
311 void buy_custom(void) {
312 char name[MAX_ITEM_NAME_LENGTH + 1];
316 cprintf("\r\nWas soll gekauft werden?\r\n");
317 if (cgetn_input(name, sizeof(name)) == 0)
320 cprintf("\r\nWie teuer ist \"%s\" (in cents)?\r\n", name);
322 price = cget_number(0);
325 cprintf("Kauf abgebrochen, dr" uUML "cke RETURN...\r\n");
333 void set_time_interactive(void) {
334 BYTE part[3] = {'0', '0', '\0'};
336 char *time_input, *time;
337 cprintf("Gib die aktuelle Uhrzeit ein (Format HHMMSS):\r\n");
338 time_input = get_input();
339 part[0] = time_input[0];
340 part[1] = time_input[1];
342 part[0] = time_input[2];
343 part[1] = time_input[3];
345 part[0] = time_input[4];
346 part[1] = time_input[5];
348 set_time(tp1, tp2, tp3);
351 cprintf("\r\nZeit gesetzt: %s\r\n", time);
360 videomode(VIDEOMODE_80x25);
362 /* clock CPU at double the speed (a whopping 2 Mhz!) */
365 /* Manipulate the VDC with IRQs turned off.
366 * KERNALs default IRQ handler will also try to read the VDC status
367 * register, which could interfere with our code trying to read it.
375 /* Allocate logging buffer memory */
378 /* Set time initially, c128 doesn't know it */
379 set_time_interactive();
381 /* disable interrupt driven VIC screen editor */
384 /* Load configuration */
387 /* Load items (= drinks) */
393 sprintf(print_buffer, "%c----------------------------------------------------"
394 "----------------------------\r",
397 sprintf(print_buffer, "%cC128-Kasse Version " GV "\r", 17);
400 sprintf(print_buffer,
401 "%cKasse gestartet um %s. Nutze logfile log-%u, offset %d.\r", 17,
402 time, log_num, log_heap_offset);
410 /* ...display dialogs eventually */
411 if (*c >= PETSCII_0 && *c <= PETSCII_9) {
412 /* if the input starts with a digit, we will interpret it as a number
413 * for the item to be sold */
415 } else if (*c == 'f') {
417 } else if (*c == 's') {
418 cprintf("\r\nsaving items.. ");
420 cprintf("ok\r\nsaving credits.. ");
422 cprintf("ok\r\nflushing log.. ");
424 cprintf("ok\r\nStatefile/Creditfile/Log gesichert, dr" uUML
425 "cke RETURN...\r\n");
427 } else if (*c == 'g') {
429 } else if (*c == 'z') {
430 set_time_interactive();
431 } else if (*c == 'q')
435 cprintf("\r\nBYEBYE\r\n");