Example #1
0
void
str_cli(int sockfd)
{
  char	*sendline, recvline[MESSAGE_MAXLEN], opt[100];
  int ch;
  char option[100];
  message_t newMesg;
  fd_set readset;
  char invite[MAXLENGTH];
  
    while(1)
      {
  	ch = menu();
	switch(ch)
	  {
	  case 1:
	    printf("\n\n----------------------------------------------------------------------------------------\nSIGN IN\n");
	    sendline = message_toString(getIdAndPassword(SIGNIN));
		
		if (send(sockfd, sendline, MESSAGE_MAXLEN, 0) != MESSAGE_MAXLEN) {
	      printf("send() sent a different number of bytes than expected\n");
	    }

	    if (recv(sockfd, recvline, MESSAGE_MAXLEN, 0) < 0) {
	      printf("Server terminated prematurely\n");
	      exit(1);
	    }
	    recvline[strlen(recvline)] = '\0';
	    newMesg = message_parse(recvline);
	    newMesg->arg[1] = '\0';
	    newMesg->arg1[1] = '\0';

	    switch(newMesg->cmd) {
	    	case SIGNIN:
	    	printf("\nWelcome to console chess game\n");
	    	menuPlay();

	    	while(1) {
	    		FD_ZERO(&readset);
				FD_SET(sockfd, &readset);
				FD_SET(fileno(stdin), &readset);
				select(max(sockfd, fileno(stdin)) + 1, &readset, NULL, NULL, NULL);
				if(FD_ISSET(sockfd, &readset)) {
					if(read(sockfd, recvline, MESSAGE_MAXLEN) == 0) {
						printf("Server terminated prematurely\n");
						exit(1);
					}
					//TODO: receive invitation
					newMesg = message_parse(recvline);

					switch(newMesg->cmd) {
					case INVITE_ACCEPT:
					printf("Client %s invited you to his/her game (y/n) ?\n", newMesg->arg1);
					scanf("%s", opt);
					if(strcmp(opt, "y") == 0 || strcmp(opt, "Y") == 0) {
						if (send(sockfd, recvline, MESSAGE_MAXLEN , 0) != MESSAGE_MAXLEN ) {
				            printf("send() sent a different number of bytes than expected\n");
				        }
					}
					else {
						if (send(sockfd, message_toString( message_construct(INVITE_DECLINE, newMesg->arg1, "0", "0", "0") ), MESSAGE_MAXLEN , 0) != MESSAGE_MAXLEN ) {
				            printf("send() sent a different number of bytes than expected\n");
				        }
					}
					break;
					case PICK_SIDE:
					if(strcmp(newMesg->arg, "0") == 0) {
					 	mainConsoleChess(0, sockfd);
					 	menuPlay();
					}
					else if (strcmp(newMesg->arg, "1") == 0) {
						mainConsoleChess(1, sockfd);
						menuPlay();
					}
					case GET_PLAYERLIST:
					parse(newMesg->arg);
					menuPlay();
					break;

					default:
					printf("Cannot get the message\n");
					break;
					}


				} else if(FD_ISSET(fileno(stdin), &readset)) {
					fgets(option, 100, stdin);
					option[strlen(option) -1] = '\0';
					if(strcmp(option, "1") == 0) {
						//TODO: inviting someone
						printf("Type the one'id who you want to invite\n");
						scanf("%s", invite);
						
						if (send(sockfd, message_toString(message_construct(INVITE_SEND, invite, "0", "0", "0")), MESSAGE_MAXLEN, 0) != MESSAGE_MAXLEN) {
		     				 printf("send() sent a different number of bytes than expected\n");
		    			}
		    			printf("Waiting for responding from server...\n");
					    if (recv(sockfd, recvline, MESSAGE_MAXLEN, 0) < 0) {
					      printf("Server terminated prematurely\n");
					      exit(1);
					    }
					    recvline[strlen(recvline)] = '\0';
					    newMesg = message_parse(recvline);
					    newMesg->arg[1] = '\0';
					    if(newMesg->cmd == PICK_SIDE) {
							if(strcmp(newMesg->arg, "0") == 0) {
							 	mainConsoleChess(0, sockfd);
							 	menuPlay();
							}
							else if (strcmp(newMesg->arg, "1") == 0) {
								mainConsoleChess(1, sockfd);
								menuPlay();
							}							    	
					    }
					    else {
					    	printf("He/she declined your request\n");
					    	menuPlay();
					    }

					} else if(strcmp(option, "2") == 0) {
						//TODO: get the current players list from server
						if (send(sockfd, message_toString(message_construct(GET_PLAYERLIST, "0", "0", "0", "0")), MESSAGE_MAXLEN, 0) != MESSAGE_MAXLEN) {
	     					 printf("send() sent a different number of bytes than expected\n");
	    				}
	    				printf("Waiting for responding from server...\n");
					} else if(strcmp(option, "3") == 0) {
						printf("Thanks for enjoying\n");
						break;
					} else {
						printf("Not the right option, baby\n");
						menuPlay();
						continue;
					}
				}
			}
	    	break;


	    	default:
	    	printf("MESSAGE COMMAND: %d\n", newMesg->cmd);
	    	printf("Fail to sign in\n");
	    	break;
	    }

	    continue;

	  case 2:
	    printf("\n\n----------------------------------------------------------------------------------------\nSIGN UP\n");
	    sendline = message_toString(getIdAndPassword(SIGNUP));

	    if (send(sockfd, sendline, MESSAGE_MAXLEN, 0) != MESSAGE_MAXLEN ) {
	      printf("send() sent a different number of bytes than expected\n");
	    }

	    if (recv(sockfd, recvline, MESSAGE_MAXLEN, 0) < 0) {
	      printf("str_cli: server terminated prematurely\n");
	      exit(1);
	    }
	    recvline[strlen(recvline)] = '\0';

	    newMesg = message_parse(recvline);
	    switch(newMesg->cmd) {
	    	case SIGNUP:
	    	printf("\nSuccessfull sign up\n");
	    	break;
	    	default:
	    	printf("Fail to sign up. Maybe this account doesn't exist\n");
	    	break;
	    }
	    continue;

	  case 3:
	    printf("----------------------------------------------------------------------------------------\nExit\n");
	    sendline = message_toString(message_construct(SIGNOUT, "0", "0", "0", "0"));
	    
	    if (send(sockfd, sendline, MESSAGE_MAXLEN, 0) != MESSAGE_MAXLEN) {
	      printf("send() sent a different number of bytes than expected\n");
	    }

	    if (recv(sockfd, recvline, MESSAGE_MAXLEN, 0) < 0) {
	      printf("Server terminated prematurely\n");
	      exit(1);
	    }
	    recvline[strlen(recvline)] = '\0';
	    printf("\nI'm done!\n");
	    exit(0);
	    break;

	  default:
	    printf("Wrong option! Please choose again\n");
	    continue;
	  }
	break;
      }
    printf("\n");
  
	
}
Example #2
0
void main()
{
	int choiceMM = 0, choicePM = 0, choiceOM = 0, choiceBM = 0, choicePPM = 0, choiceLoss = 0;
	float tempSpeed;
	bool first = true;
	bool mm = true;
	bool dPress = false, aPress = false, wPress = false, sPress = false, spacePress = false;
	bool playTennis = false, playSingle = false, playMulti = false;
	bool ballOffScreen = false;
	bool ballOp = false, playOp = false;

	curCX = 250;
	curCY = 250;

	/*cout << "Please choose game mode:\n\n" 
		 << "Tennis: 1\n" 
		 << "Single Player: 2\n" 
		 << "Multiplayer: 3\n";
	cin >> choice;*/

	initContext(SCREEN_X, SCREEN_Y, "Pong");

	while (stepContext())
	{
		// The main Menu
		if(mm)
		{
			cout << "Main; ";

			//This if chunk makes one key = one press on MM
			if (getKey('D') && !dPress)
			{
				choiceMM++;
				dPress = true;
			}
			else if (getKey('A') && !aPress)
			{
				choiceMM--;
				aPress = true;
			}

			if (choiceMM <= -1) { choiceMM = 2; }
			else if (choiceMM >= 3) { choiceMM = 0; }

			if (getKey(' ') && !spacePress) 
			{ 
				mm = false;
				spacePress = true;
			}

			cout << choiceMM << "(1); ";

			menuMain(choiceMM);
		}
		//All other menus
		else if (!mm && !playTennis && !playSingle && !playMulti)
		{
			cout << "Other menu; ";
			//Play menu
			if (choiceMM == 0)
			{
				cout << "Play Menu; ";
				cout << choicePM << "(1); ";

				if (getKey('D') && !dPress)
				{
					switch (choicePM)
					{
					case 0:
						choicePM = 1;
						break;
					case 1:
						choicePM = 0;
						break;
					case 2:
						choicePM = 3;
						break;
					case 3:
						choicePM = 2;
						break;
					}

					dPress = true;
				}
				else if (getKey('A') && !aPress)
				{
					switch (choicePM)
					{
					case 0:
						choicePM = 1;
						break;
					case 1:
						choicePM = 0;
						break;
					case 2:
						choicePM = 3;
						break;
					case 3:
						choicePM = 2;
						break;
					}

					aPress = true;
				}
				else if (getKey('W') && !wPress)
				{  
					switch (choicePM)
					{
					case 0:
						choicePM = 2;
						break;
					case 1:
						choicePM = 3;
						break;
					case 2:
						choicePM = 0;
						break;
					case 3:
						choicePM = 1;
						break;
					}

					wPress = true;
				}
				else if (getKey('S') && !sPress)
				{
					switch (choicePM)
					{
					case 0:
						choicePM = 2;
						break;
					case 1:
						choicePM = 3;
						break;
					case 2:
						choicePM = 0;
						break;
					case 3:
						choicePM = 1;
						break;
					}

					sPress = true;
				}

				if (choicePM == -1) { choicePM = 3; }
				else if (choicePM == 4) { choicePM = 0; }

				if (getKey(' ') && !spacePress)
				{
					if (choicePM == 0)
					{
						playSingle = true;
					}
					else if (choicePM == 1)
					{
						playMulti = true;
					}
					else if (choicePM == 2)
					{
						choicePM = 0;
						choiceMM = 0;
						mm = true;
					}
					else
					{
						playTennis = true;
					}

					spacePress = true;
				}

				menuPlay(choicePM);
			}
			//Options menu
			else if (choiceMM == 1)
			{
				cout << "Op Menu; ";
				
				if (getKey('W') && !wPress)
				{
					choiceOM--;
					wPress = true;
				}
				else if (getKey('S') && !sPress)
				{
					choiceOM++;
					sPress = true;
				}

				if (choiceOM == -1) { choiceOM = 2; }
				else if (choiceOM == 3) { choiceOM = 0; }

				if (ballOp)
				{
					cout << "Ball menu; ";

					if (getKey('A')) { choiceBM--; }
					else if (getKey('D')) { choiceBM++; }
					if (choiceBM < 0) { choiceBM = 0; }
					else if(choiceBM > 1) {choiceBM = 1; }

					menuBallOp(choiceBM);

					if (getKey(' ') && !spacePress)
					{
						if (!choiceBM)
						{
							ballOp = false;
							menuBallOp(3);
						}
						else
						{
							menuBallOp(3);
						}

						spacePress = true;
					}

					cout << choiceBM << "(4); ";
				}
				else if (playOp)
				{
					cout << "Play  mmenu; ";

					if (getKey('A')) { choicePPM--; }
					else if (getKey('D')) { choicePPM++; }
					if (choicePPM < 0) { choicePPM = 0; }
					else if (choicePPM > 1) { choicePPM = 1; }

					menuBallOp(choicePPM);

					if (getKey(' ') && !spacePress)
					{
						if (!choicePPM)
						{
							playOp = false;
							menuPlayOp(3);
						}
						else
						{
							menuPlayOp(3);
						}

						spacePress = true;
					}

					cout << choicePPM << "(4); ";
				}
				else if(getKey(' ') && !spacePress)
				{
					switch (choiceOM)
					{
					case 0:
						ballOp = true;
						break;
					case 1:
						playOp = true;
						break;
					case 2:
						choiceOM = 0;
						choiceMM = 0;
						mm = true;
						break;
					}

					spacePress = true;
				}
				else
				{
					menuOpt(choiceOM);
				}

				cout << choiceOM << "(1); ";
			}
			else if (choiceMM == 2) { termContext(); }
		}
		else if (playTennis)
		{
			if (first)
			{
				moveBall(true, true);
				first = false;
			}

			if (!ballOffScreen)
			{
				movePlayers();
				moveBall(false, true);
			}
			else
			{
				if (getKey('W') && !wPress) { choiceLoss--, wPress = true; }
				else if (getKey('S') && !sPress) { choiceLoss++, sPress = true;  }
				if (choiceLoss < 0) { choiceLoss = 2; }
				else if (choiceLoss > 2) { choiceLoss = 0; }

				menuLoss(choiceLoss);

				if (getKey(' ') && !spacePress)
				{
					switch (choiceLoss)
					{
					case 0:
						curCX = xSpace(1, 2);
						curCY = ySpace(1, 2);
						first = true;
						cSPEED_Y = 0;
						ballOffScreen = false;
						break;
					case 1:
						choicePM = 0;
						choiceMM = 0;
						choiceLoss = 0;
						mm = true;
						playTennis = false;
						curCX = xSpace(1, 2);
						curCY = ySpace(1, 2);
						first = true;
						cSPEED_Y = 0;
						ballOffScreen = false;
						break;
					case 2:
						termContext();
						break;
					}
				}

				spacePress = true;
			}

			cout << choiceLoss << "(4); ";

			drawPlayer(PLAYER1);
			drawCircle(curCX, curCY, R);
			drawWalls(true);

			if (curCX > SCREEN_X || curCX < 0)
			{
				ballOffScreen = true;
			}
		}
		else if (playSingle)
		{
			if (first)
			{
				moveBall(true, false);
				first = false;
			}

			if (!ballOffScreen)
			{
				movePlayers();
				moveBall(false, true);
				moveAI();
			}
			else
			{
				if (getKey('W') && !wPress) { choiceLoss--, wPress = true; }
				else if (getKey('S') && !sPress) { choiceLoss++, sPress = true; }
				if (choiceLoss < 0) { choiceLoss = 2; }
				else if (choiceLoss > 2) { choiceLoss = 0; }

				menuLoss(choiceLoss);

				if (getKey(' ') && !spacePress)
				{
					switch (choiceLoss)
					{
					case 0:
						curCX = xSpace(1, 2);
						curCY = ySpace(1, 2);
						first = true;
						cSPEED_Y = 0;
						ballOffScreen = false;
						break;
					case 1:
						choicePM = 0;
						choiceMM = 0;
						choiceLoss = 0;
						mm = true;
						playSingle = false;
						curCX = xSpace(1, 2);
						curCY = ySpace(1, 2);
						first = true;
						cSPEED_Y = 0;
						ballOffScreen = false;
						break;
					case 2:
						termContext();
						break;
					}
				}

				spacePress = true;
			}

			cout << choiceLoss << "(4); ";

			drawPlayer(PLAYER1);
			drawPlayer(AI);
			drawCircle(curCX, curCY, R);
			drawWalls(false);

			if (curCX > SCREEN_X || curCX < 0)
			{
				ballOffScreen = true;
			}
		}
		else if (playMulti)
		{
			if (first)
			{
				moveBall(true, false);
				first = false;
			}

			if (!ballOffScreen)
			{
				movePlayers();
				moveBall(false, true);
			}
			else
			{
				if (getKey('W') && !wPress) { choiceLoss--, wPress = true; }
				else if (getKey('S') && !sPress) { choiceLoss++, sPress = true; }
				if (choiceLoss < 0) { choiceLoss = 2; }
				else if (choiceLoss > 2) { choiceLoss = 0; }

				menuLoss(choiceLoss);

				if (getKey(' ') && !spacePress)
				{
					switch (choiceLoss)
					{
					case 0:
						curCX = xSpace(1, 2);
						curCY = ySpace(1, 2);
						first = true;
						cSPEED_Y = 0;
						ballOffScreen = false;
						break;
					case 1:
						choicePM = 0;
						choiceMM = 0;
						choiceLoss = 0;
						mm = true;
						playMulti = false;
						curCX = xSpace(1, 2);
						curCY = ySpace(1, 2);
						first = true;
						cSPEED_Y = 0;
						ballOffScreen = false;
						break;
					case 2:
						termContext();
						break;
					}
				}

				cout << choiceLoss << "(4); ";
				spacePress = true;
			}

			drawPlayer(PLAYER1);
			drawPlayer(PLAYER2);
			drawCircle(curCX, curCY, R);
			drawWalls(false);

			if (curCX > SCREEN_X || curCX < 0)
			{
				ballOffScreen = true;
			}
		}

		if (!getKey('W') && wPress) { wPress = false; }
		if (!getKey('A') && aPress) { aPress = false; }
		if (!getKey('S') && sPress) { sPress = false; }
		if (!getKey('D') && dPress) { dPress = false; }
		if (!getKey(' ') && spacePress) { spacePress = false; }

		cout << endl;
	}
}