Advertisement
Guest User

Untitled

a guest
Jun 23rd, 2017
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.39 KB | None | 0 0
  1. #define _XOPEN_SOURCE 500
  2.  
  3. #include <string.h>
  4. #include <stdlib.h>
  5. #include <sys/types.h>          
  6. #include <sys/socket.h>
  7. #include <netdb.h>
  8. #include <unistd.h>
  9. #include <stdio.h>
  10. #include <getopt.h>
  11. #include <assert.h>
  12.  
  13. #define MAX_LINE_LENGTH 101
  14.  
  15.  
  16. int main(int argc, char *argv[]){
  17.    
  18.    
  19.    
  20.     char *botname;
  21.     char *port="9001";
  22.     int s;
  23.     int xaddr;
  24.     struct addrinfo *result;
  25.     FILE *sck;
  26.     char *buf;
  27.     int curr_amount=0;
  28.     int curr_user = 0;
  29.     int t_user;
  30.     int w1, w2;
  31.     int usertotal[3] = {0,0,0};
  32.     int c;
  33.     int cont=1;
  34.     int temp = 0;
  35.     while( (c=getopt(argc, argv, "n:p:")) != EOF){
  36.         switch(c){
  37.             case 'n':
  38.                 botname = optarg;
  39.                 break;
  40.             case 'p':
  41.                 port = optarg;
  42.             default:
  43.                 fprintf(stderr, "USAGE:\n\t%s -n <botname> [-p <server-port>] <server-hostname> \n",argv[0]);
  44.                 exit( EXIT_FAILURE );
  45.                 break;
  46.         }
  47.     }
  48.    
  49.     if(botname == NULL){
  50.         fprintf(stderr, "%s: no botname supplied", argv[0]);
  51.         exit( EXIT_FAILURE);
  52.     }
  53.    
  54.     /* todo errorcheck */
  55.     s = socket(AF_INET, SOCK_STREAM, 0);
  56.    
  57.     /* todo errorcheck */
  58.     xaddr = getaddrinfo(argv[optind], port, NULL, &result);
  59.    
  60.     if(connect(s, result->ai_addr, result->ai_addrlen) == -1){
  61.        
  62.         fprintf(stderr, "%s: connection to server failed", argv[0]);
  63.         close(s);
  64.         exit( EXIT_FAILURE );
  65.     }
  66.    
  67.     /* todo errorcheck */
  68.     sck = fdopen(s,"r");
  69.    
  70.     /* todo errorcheck */
  71.     buf = malloc(MAX_LINE_LENGTH);
  72.    
  73.     while(cont){
  74.        
  75.         fgets(buf, MAX_LINE_LENGTH, sck);
  76.        
  77.         switch(buf[0]){
  78.             case 'H':
  79.                 fprintf(stderr, "helo:  %s",buf);
  80.            
  81.                 snprintf(buf, MAX_LINE_LENGTH, "AUTH %s\n", botname);
  82.                
  83.                 write(s, buf, strlen(buf));
  84.                 break;
  85.             case 'T':
  86.                 if(buf[1]=='H'){
  87.                    
  88.                     /* count shit */
  89.                     fprintf(stderr, "count: %s",buf);
  90.                     strtok(buf, " ");
  91.                     t_user = strtol(strtok(NULL, " "), NULL, 10) ;
  92.                     w1 = strtol(strtok(NULL, " "), NULL, 10) ;
  93.                     w2 = strtol(strtok(NULL, " "), NULL, 10) ;
  94.                    
  95.                     if(t_user != curr_user){
  96.                         usertotal[curr_user] += curr_amount;
  97.                         curr_user = t_user;
  98.                         curr_amount = 0;
  99.                     }
  100.                     if(w1 != w2){
  101.                         curr_amount += w1 + w2;
  102.                     }else{
  103.                         curr_amount = 0;
  104.                     }
  105.                    
  106.                    
  107.                     /*
  108.                     wenn user gleich wie vorher
  109.                         tu nix
  110.                     wenn user != current
  111.                         add currentamount to currentusers total
  112.                         currentamount = 0
  113.                         set currentuser auf user
  114.                     wenn würfel ungleich
  115.                             zum currentamount addieren
  116.                     wenn würfel gleich
  117.                             current = 0
  118.                    
  119.                    
  120.                     */
  121.                    
  122.                 } else {
  123.                    
  124.                     fprintf(stderr, "turn:  %s",buf);
  125.                     /* strategy */
  126.                     if(temp < 6){
  127.                        
  128.                     fprintf(stderr, "rollin %s",buf);
  129.                         write(s, "ROLL\n", 5);
  130.                         temp++;
  131.                     } else {
  132.                        
  133.                     fprintf(stderr, "save:  %s",buf);
  134.                         write(s, "SAVE\n", 5);
  135.                         temp = 0;
  136.                     }
  137.                 }
  138.                    
  139.                 break;
  140.             case 'W':
  141.             case 'D':
  142.                
  143.                 fprintf(stderr, "windef %s",buf);
  144.                 fprintf(stdout, "%s", buf);
  145.                 usertotal[curr_user] += curr_amount;
  146.                 snprintf(buf, MAX_LINE_LENGTH, "BYE %d %d %d\n", usertotal[2], usertotal[0], usertotal[1]);
  147.                
  148.                 fprintf(stderr, "result %s",buf);
  149.            
  150.                 write(s, buf, strlen(buf));
  151.                 break;
  152.             case 'E':
  153.                 fprintf(stderr, "%s\n",buf);
  154.                 cont = 0;
  155.                 break;
  156.             case 'B':
  157.                 fprintf(stderr, "bye:   %s",buf);
  158.                 cont = 0;
  159.                 break;
  160.             default:
  161.                 assert(0);
  162.                 break;
  163.         }  
  164.     }
  165.    
  166.  
  167.    
  168.     free(buf);
  169.     close(s);
  170.     exit( EXIT_SUCCESS );
  171.    
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement