Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _XOPEN_SOURCE 500
- #include <string.h>
- #include <stdlib.h>
- #include <sys/types.h>
- #include <sys/socket.h>
- #include <netdb.h>
- #include <unistd.h>
- #include <stdio.h>
- #include <getopt.h>
- #include <assert.h>
- #define MAX_LINE_LENGTH 101
- int main(int argc, char *argv[]){
- char *botname;
- char *port="9001";
- int s;
- int xaddr;
- struct addrinfo *result;
- FILE *sck;
- char *buf;
- int curr_amount=0;
- int curr_user = 0;
- int t_user;
- int w1, w2;
- int usertotal[3] = {0,0,0};
- int c;
- int cont=1;
- int temp = 0;
- while( (c=getopt(argc, argv, "n:p:")) != EOF){
- switch(c){
- case 'n':
- botname = optarg;
- break;
- case 'p':
- port = optarg;
- default:
- fprintf(stderr, "USAGE:\n\t%s -n <botname> [-p <server-port>] <server-hostname> \n",argv[0]);
- exit( EXIT_FAILURE );
- break;
- }
- }
- if(botname == NULL){
- fprintf(stderr, "%s: no botname supplied", argv[0]);
- exit( EXIT_FAILURE);
- }
- /* todo errorcheck */
- s = socket(AF_INET, SOCK_STREAM, 0);
- /* todo errorcheck */
- xaddr = getaddrinfo(argv[optind], port, NULL, &result);
- if(connect(s, result->ai_addr, result->ai_addrlen) == -1){
- fprintf(stderr, "%s: connection to server failed", argv[0]);
- close(s);
- exit( EXIT_FAILURE );
- }
- /* todo errorcheck */
- sck = fdopen(s,"r");
- /* todo errorcheck */
- buf = malloc(MAX_LINE_LENGTH);
- while(cont){
- fgets(buf, MAX_LINE_LENGTH, sck);
- switch(buf[0]){
- case 'H':
- fprintf(stderr, "helo: %s",buf);
- snprintf(buf, MAX_LINE_LENGTH, "AUTH %s\n", botname);
- write(s, buf, strlen(buf));
- break;
- case 'T':
- if(buf[1]=='H'){
- /* count shit */
- fprintf(stderr, "count: %s",buf);
- strtok(buf, " ");
- t_user = strtol(strtok(NULL, " "), NULL, 10) ;
- w1 = strtol(strtok(NULL, " "), NULL, 10) ;
- w2 = strtol(strtok(NULL, " "), NULL, 10) ;
- if(t_user != curr_user){
- usertotal[curr_user] += curr_amount;
- curr_user = t_user;
- curr_amount = 0;
- }
- if(w1 != w2){
- curr_amount += w1 + w2;
- }else{
- curr_amount = 0;
- }
- /*
- wenn user gleich wie vorher
- tu nix
- wenn user != current
- add currentamount to currentusers total
- currentamount = 0
- set currentuser auf user
- wenn würfel ungleich
- zum currentamount addieren
- wenn würfel gleich
- current = 0
- */
- } else {
- fprintf(stderr, "turn: %s",buf);
- /* strategy */
- if(temp < 6){
- fprintf(stderr, "rollin %s",buf);
- write(s, "ROLL\n", 5);
- temp++;
- } else {
- fprintf(stderr, "save: %s",buf);
- write(s, "SAVE\n", 5);
- temp = 0;
- }
- }
- break;
- case 'W':
- case 'D':
- fprintf(stderr, "windef %s",buf);
- fprintf(stdout, "%s", buf);
- usertotal[curr_user] += curr_amount;
- snprintf(buf, MAX_LINE_LENGTH, "BYE %d %d %d\n", usertotal[2], usertotal[0], usertotal[1]);
- fprintf(stderr, "result %s",buf);
- write(s, buf, strlen(buf));
- break;
- case 'E':
- fprintf(stderr, "%s\n",buf);
- cont = 0;
- break;
- case 'B':
- fprintf(stderr, "bye: %s",buf);
- cont = 0;
- break;
- default:
- assert(0);
- break;
- }
- }
- free(buf);
- close(s);
- exit( EXIT_SUCCESS );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement