X-Git-Url: https://git.ucc.asn.au/?p=tpg%2Fopendispense2.git;a=blobdiff_plain;f=src%2Fclient%2Fmain.c;h=5f5f1c85a2e33ce099e7e86a9f2b1b8bf4763e64;hp=ee93227ce39fec34540561ce2cb9dc2a39f93bd6;hb=a7c0580c1592fe19ef18ce5b86b0088c835be030;hpb=bc69d6a9b8cf7d71957d67ae4f727bd18e054521 diff --git a/src/client/main.c b/src/client/main.c index ee93227..5f5f1c8 100644 --- a/src/client/main.c +++ b/src/client/main.c @@ -14,6 +14,7 @@ #include // isspace #include #include +#include #include // close #include // gethostbyname @@ -30,6 +31,8 @@ typedef struct sItem { } tItem; // === PROTOTYPES === + int ShowNCursesUI(void); + int sendf(int Socket, const char *Format, ...); int OpenConnection(const char *Host, int Port); void Authenticate(int Socket); @@ -159,6 +162,10 @@ int main(int argc, char *argv[]) // - Hmm... that would require standardising the item ID to be : // Oh, why not :) + #if 1 + i = ShowNCursesUI(); + #else + for(;;) { char *buf; @@ -181,36 +188,40 @@ int main(int argc, char *argv[]) printf("Bad item (should be between 0 and %i)\n", giNumItems); continue; } - - sendf(sock, "DISPENSE %s\n", gaItems[i].Ident); - - len = recv(sock, buffer, BUFSIZ-1, 0); - buffer[len] = '\0'; - trim(buffer); - - responseCode = atoi(buffer); - switch( responseCode ) - { - case 200: - printf("Dispense OK\n"); - break; - case 401: - printf("Not authenticated\n"); - break; - case 402: - printf("Insufficient balance\n"); - break; - case 406: - printf("Bad item name, bug report\n"); - break; - case 500: - printf("Item failed to dispense, is the slot empty?\n"); - break; - default: - printf("Unknown response code %i\n", responseCode); - break; - } - + break; + } + } + #endif + + if( i >= 0 ) + { + // Dispense! + sendf(sock, "DISPENSE %s\n", gaItems[i].Ident); + + len = recv(sock, buffer, BUFSIZ-1, 0); + buffer[len] = '\0'; + trim(buffer); + + responseCode = atoi(buffer); + switch( responseCode ) + { + case 200: + printf("Dispense OK\n"); + break; + case 401: + printf("Not authenticated\n"); + break; + case 402: + printf("Insufficient balance\n"); + break; + case 406: + printf("Bad item name, bug report\n"); + break; + case 500: + printf("Item failed to dispense, is the slot empty?\n"); + break; + default: + printf("Unknown response code %i\n", responseCode); break; } } @@ -220,6 +231,160 @@ int main(int argc, char *argv[]) return 0; } +/** + */ +int ShowNCursesUI(void) +{ + int ch; + int i, times; + int xBase, yBase; + const int displayMinWidth = 34; + const int displayMinItems = 8; + char *titleString = "Dispense"; + int titleStringLen = strlen(titleString); + int itemCount = displayMinItems; + int itemBase = 0; + + int height = itemCount + 3; + int width = displayMinWidth; + + // Enter curses mode + initscr(); + raw(); noecho(); + + xBase = COLS/2 - width/2; + yBase = LINES/2 - height/2; + + for( ;; ) + { + // Header + move( yBase, xBase ); + addch('/'); + times = width/2 - titleStringLen/2 - 2; + while(times --) addch('-'); + addch(' '); + addstr(titleString); + addch(' '); + times = width/2 - titleStringLen/2 - 2; + while(times --) addch('-'); + addch('\\'); + + // Items + for( i = 0; i < itemCount; i ++ ) + { + int _x, _y; + move( yBase + 1 + i, xBase ); + addch('|'); + addch(' '); + + // Check for ... row + if( i == 0 && itemBase > 0 ) { + printw(" ..."); + times = width - 1 - 8; + while(times--) addch(' '); + } + else if( i == itemCount - 1 && itemBase < giNumItems - itemCount ) { + printw(" ..."); + times = width - 1 - 8; + while(times--) addch(' '); + } + // Show an item + else { + if( itemBase + i < 0 || itemBase + i >= giNumItems ) { + printw("%02i %i OOR", itemBase + i, i); + continue ; + } + printw("%02i %s", itemBase + i, gaItems[itemBase + i].Desc); + + getyx(stdscr, _y, _x); + times = width - 6 - (_x - xBase); // TODO: Better handling for large prices + while(times--) addch(' '); + printw("%4i ", gaItems[itemBase + i].Price); + } + + // Scrollbar (if needed) + if( giNumItems > itemCount ) { + if( i == 0 ) { + addch('A'); + } + else if( i == itemCount - 1 ) { + addch('V'); + } + else { + int percentage = itemBase * 100 / (giNumItems-itemCount); + if( i-1 == percentage*(itemCount-3)/100 ) { + addch('#'); + } + else { + addch('|'); + } + } + } + else { + addch('|'); + } + } + + // Footer + move( yBase + 1 + itemCount, xBase ); + addch('\\'); + times = width/2 - titleStringLen/2 - 2; + while(times --) addch('-'); + addch(' '); + addstr(titleString); + addch(' '); + times = width/2 - titleStringLen/2 - 2; + while(times --) addch('-'); + addch('/'); + + move( yBase + 1 + itemCount + 1, xBase ); + { + int count = itemCount-2; + int ofs = itemBase; + if( itemBase == 0 ) count ++; + else ofs ++; + if( itemBase == giNumItems-itemCount) { + count ++; + ofs ++; + } + printw("%i - %i / %i items", itemBase, itemBase+count, giNumItems); + } + + ch = getch(); + + if( ch == '\x1B' ) { + ch = getch(); + if( ch == '[' ) { + ch = getch(); + + switch(ch) + { + case 'B': + if( itemBase < giNumItems - (itemCount) ) + itemBase ++; + break; + case 'A': + if( itemBase > 0 ) + itemBase --; + break; + } + } + else { + + } + } + else { + break; + } + + } + + + // Leave + endwin(); + return -1; +} + // === HELPERS === int sendf(int Socket, const char *Format, ...) {