📄 gbnclient.c
字号:
gtk_widget_show( button_start ); gtk_widget_set_sensitive(button_start,TRUE); } else if(proto.bnpmsg[0]==0) { ttyfill("ERROR: The board's not OK. Check it, please.\n"); usuario.play=1; } break; case BNJUG: usuario.numjug=proto.bnptip1; sprintf(outbuf,"Connected to bnserver:%s. You are player number:%i",proto.bnpmsg,usuario.numjug); foot_right(outbuf); strcat(outbuf,".\n"); ttyfill(outbuf); usuario.play = 1; gtk_widget_hide( button_connect ); gtk_widget_show( button_disconnect ); gtk_widget_set_sensitive(button_sendboard,TRUE); gtk_widget_set_sensitive(button_status,TRUE); gtk_widget_set_sensitive(button_sendmsg,TRUE); bnwrite(sock,outbuf,BNVER,usuario.usrfrom,bnsup,usuario.numjug); break; case BNVER: sprintf(outbuf,"bnserver version:%i.%i\n",proto.bnpmsg[0],proto.bnpmsg[1]); ttyfill(outbuf); break; case BNMSG: strcpy(outbuf,proto.bnpmsg); strcat(outbuf,"\n"); ttyfill(outbuf); break; case BNALL: ttyfill("bnserver is full. Try later, if you are so kind.\n"); close(sock); break; case BNWAI: ttyfill("You can't start a game while people are playing.Try later\n"); break; case BNHIT: if(usuario.play>=3) { mitabla[(int)proto.bnpmsg[0]][(int)proto.bnpmsg[1]]=(int)proto.bnpmsg[2]; if(usuario.hide==FALSE) { iwtable(temptable); fillboard(temptable,0); /* v0.50 */ } } bnwrite(sock,outbuf,BNREA,usuario.usrfrom,bnsup,usuario.numjug); break; case BNREA: if(usuario.play>=3) { putintemp(proto.bnpmsg); /* v0.60 */ fillboard(proto.bnpmsg,1); /* v0.50 */ inteliclient(proto.bnpmsg); } break; case BNSTR: ttyfill("Great! Please fasten your seatbelt and wait your turn.\n"); sprintf(outbuf,"Available players:"); for(i=0;i<MAXPLAYER;i++) usuario.pages[i]=i; for(i=0;i<100;i++) usuario.tempclit[i]=NOBARCO; j = g_list_length( GTK_NOTEBOOK(notebook_right)->children); for (i = 0; i < j; i++) gtk_notebook_remove_page ( GTK_NOTEBOOK( notebook_right), 0); for(i=0;i<MAXPLAYER;i++) { if(proto.bnpmsg[i]>=2) { sprintf(outbufi," %i",i+1); strcat(outbuf,outbufi); strcpy(usuario.names[i],&proto.bnpmsg[5+i*MAXNAMELEN]); label_right[i] = gtk_label_new( usuario.names[i] ); label_right2[i] = gtk_label_new( usuario.names[i] ); drawing_right[i] = gtk_drawing_area_new(); gtk_drawing_area_size(GTK_DRAWING_AREA(drawing_right[i]),200,200); gtk_signal_connect (GTK_OBJECT (drawing_right[i]), "expose_event", (GtkSignalFunc) expose_event_right, NULL); gtk_signal_connect (GTK_OBJECT (drawing_right[i]), "button_press_event", (GtkSignalFunc) button_press_event_right, NULL); gtk_widget_set_events (drawing_right[i], GDK_EXPOSURE_MASK |GDK_BUTTON_PRESS_MASK); gtk_widget_show( label_right[i] ); gtk_widget_show( label_right2[i] ); gtk_widget_show( drawing_right[i] ); gtk_notebook_append_page_menu ( GTK_NOTEBOOK(notebook_right), drawing_right[i], label_right[i], label_right2[i] ); } else { sacar_usrpage(i); } } strcat(outbuf,".\n"); ttyfill(outbuf); strcpy(outbuf,"You are "); strncat(outbuf,usuario.names[usuario.numjug-1],MAXNAMELEN); foot_left(outbuf); if(usuario.play==2) { usuario.play=3; gtk_widget_set_sensitive(button_start,FALSE); } /* * pone automaticamente al proximo jugador */ i = buscar_usr( usuario.numjug - 1 ); gtk_notebook_set_page( GTK_NOTEBOOK( notebook_right ),i ); gtk_notebook_next_page( GTK_NOTEBOOK(notebook_right)); i = gtk_notebook_current_page( GTK_NOTEBOOK( notebook_right )); usuario.usrfrom = usuario.pages[ i ] +1 ; break; case BNCON: sprintf(outbuf,"Player %i is ready to play\n",proto.bnptip1); ttyfill(outbuf); break; case BNWHO: sprintf(outbuf,"It's %s turn.",usuario.names[proto.bnptip1-1]); foot_right(outbuf); strcat(outbuf,"\n"); ttyfill(outbuf); break; case BNTRN: usuario.play=4; /* oh, it is my turn */ ttyfill("It's your turn.\n"); foot_right("Cool, It's my turn"); break; case BNEXT: i=proto.bnptip1-1; remove_page( sacar_usrpage(i) ); usuario.names[i][0]=0; strcpy(outbuf,proto.bnpmsg); strcat(outbuf,"\n"); ttyfill(outbuf); for(i=0;i<MAXPLAYER;i++) { if(usuario.numjug-1!=i) { if(strcmp(usuario.names[i],"")!=0) usuario.usrfrom=i+1; } }// if(usuario.play>=3)// bnwrite(sock,outbuf,BNREA,usuario.usrfrom,bnsup,usuario.numjug); break; case BNLOS: usuario.gano=-1; usuario.play=1; /* oh, i lost the game */ ttyfill("It's a pity, but the game is over for you\n"); showboard(gameover); for(i=0;i<MAXPLAYER;i++) usuario.names[i][0]=0; gtk_widget_show( button_sendboard ); gtk_widget_hide( button_start ); usuario.firsthit=0; /* new on v0.41 */ // strcpy(outbuf,"My board"); // dname(outbuf,0); // strcpy(outbuf,"Enemy's board"); // dname(outbuf,1); // strcpy(outbuf,"Batalla Naval"); // foot_right(outbuf); break; case BNWIN: usuario.gano=1; usuario.play=1; /* oh, I won the game */ showboard(winner); for(i=0;i<MAXPLAYER;i++) usuario.names[i][0]=0; /* clear all names */ ttyfill("Cool, you are the winner.\n"); gtk_widget_show( button_sendboard ); gtk_widget_hide( button_start ); usuario.firsthit=0; /* new on v0.41 */ // strcpy(outbuf,"My board"); // dname(outbuf,0); // strcpy(outbuf,"Enemy's board"); // dname(outbuf,1); // strcpy(outbuf,"Batalla Naval"); // foot_right(outbuf); break; case BNSOL: ttyfill("If you want to play alone, make the robot, OK?\n"); break; case BNSTS: for(i=0;i<MAXPLAYER;i++) { sprintf(outbuf,"Player %i (%s), name='%s'\n" ,i+1,usuario.status[(int)proto.bnpmsg[i]],&proto.bnpmsg[5+i*MAXNAMELEN]); ttyfill(outbuf); } break; default: sprintf(outbuf,"BNSERVER sending code %X. Ummm... update the client please.\n",proto.bnptip0); ttyfill(outbuf); break; } }}/*************************************************************************** * FUNCIONES INIT ***************************************************************************/void init_datos( void ){ gint i,j; usuario.play = 0; usuario.usrfrom=1; usuario.lecho=1; usuario.gano=0; for(i=0;i<10;i++) { /* clean tabla */ for(j=0;j<10;j++) mitabla[i][j]=0; } usuario.firsthit=0; for(i=0;i<MAXPLAYER;i++) { usuario.names[i][0]=0; /* clear all names */ usuario.pages[i]=i; } strncpy(usuario.status[DISCON],"not connected",16); strncpy(usuario.status[CONNEC]," connected ",16); strncpy(usuario.status[BOARD], "ready to play",16); strncpy(usuario.status[PLAY], " playing ",16); strncpy(usuario.status[TURN], " playing * ",16); usuario.hide=FALSE;}int init_cliente( void ){ gchar outbuf[MSGMAXLEN]; gint i; if(usuario.play==0) { /* Create socket */ sock=socket(AF_INET,SOCK_STREAM,0); if(sock <0) { ttyfill("ERROR: creating stream socket\n"); return(1); } host_info = gethostbyname(usuario.server_name); if(host_info==NULL) { close(sock); ttyfill("ERROR: Unknown host. Press 'Config'\n"); return(2); } server.sin_family=host_info->h_addrtype; memcpy( (char*) &server.sin_addr, host_info->h_addr,host_info->h_length); server.sin_port=htons(usuario.port); if(connect(sock,(struct sockaddr *)&server,sizeof(server))< 0) { close(sock); ttyfill("ERROR: Press 'Config' or start the bnserver. Ok? \n"); return(2); } /* Propio de las GTK, OK ?, perdon de las GDK */ usuario.tag = gdk_input_add( sock, GDK_INPUT_READ, proceso, temp ); strncpy(outbuf,usuario.nombre,MAXNAMELEN); bnwrite(sock,outbuf,BNJUG,GCLI,GBNVERH,GBNVERL); /* WHO AM I */ return(0); } else if(usuario.play!=0) { gtk_widget_show( button_connect ); gtk_widget_hide( button_disconnect ); gtk_widget_show( button_sendboard ); gtk_widget_hide( button_start ); gtk_widget_set_sensitive(button_sendboard,FALSE); gtk_widget_set_sensitive(button_sendmsg,FALSE); gtk_widget_set_sensitive(button_status,FALSE); gtk_widget_set_sensitive(button_sendmsg,FALSE); ttyfill("Press 'Connect' again to join a game.\n"); foot_right("Press 'Connect' again to join a game"); foot_left("Batalla Naval"); usuario.firsthit=0; for(i=0;i<MAXPLAYER;i++) { usuario.names[i][0]=0; usuario.pages[i]=i; } usuario.play=0; bnwrite(sock,outbuf,BNEXT,0,0,usuario.numjug); close(sock); gdk_input_remove( usuario.tag ); } return(0);}void help(){ printf("\n\nBatalla Naval "GBNVER" (c) 1995,96,97,98 Ricardo Quesada\n"); printf("\nsyntax:"); printf("\ngbnclient [-s host_name] [-p server_port] [-u username]"); printf("\n\t-s host_name: The name of the host where bnserver is running"); printf("\n\t default is: localhost"); printf("\n\t-p server_port: The number of the port that bnserver is listening to"); printf("\n\t default is: 1995"); printf("\n\t-u username: Your name or alias"); printf("\n\t default is your login name"); printf("\n\nSend bugs,doubts,fixes to rquesada@dc.uba.ar\n"); exit(-1);}/**************************************************************************** * MAIN * MAIN * MAIN ****************************************************************************/int main (int argc, char *argv[]){ gint i; gtk_set_locale(); /* no se que hace */ gtk_init (&argc, &argv); gtk_rc_parse("gbnclientrc"); strcpy( usuario.server_name,"localhost") ; usuario.port=1995 ; strncpy( usuario.nombre, getenv("LOGNAME"), MAXNAMELEN) ; for(i=1;i<argc;i++) { if(argv[i][0]=='-') { switch(argv[i][1]) { case 'x': bnsup=argv[i][2]; break; case 's': case 'S': if(argc>i+1) strncpy( usuario.server_name,argv[i+1],50); else help(); break; case 'u': case 'U': if(argc>i+1) strncpy( usuario.nombre,argv[i+1],MAXNAMELEN); else help(); break; case 'p': case 'P': if(argc>i+1) usuario.port=atoi(argv[i+1]); else help(); break; case 'h': case 'H': case '?': case '-': default: help(); break; } } else { if(argv[i-1][0]!='-') help(); } } init_datos(); /* inicializa variables */ init_X(); /* inicializa entorno y juego */ init_cliente(); // fillboard(Bboard,0); // fillboard(Nboard,1); gtk_main (); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -