Advertisement
Guest User

Untitled

a guest
Jan 6th, 2017
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.75 KB | None | 0 0
  1. #include "util.h"
  2.  
  3. int Fim_jogo = 0;
  4.  
  5. //Continua a nao funcionar
  6.  
  7. void Escolhe_jogador(Jogador *Struct_Jogadores, int *flagescolher_jogador){
  8.  
  9.     int fd,x,i,k,varaux;
  10.     InputJogador input;
  11.    
  12.     fd = open(FIFO_SERVIDOR_JOGO,O_WRONLY);
  13.  
  14.     k = 0;
  15.     input.userpid = getpid();
  16.     input.escolher_jogador = 1;
  17.     input.sair_utilizador = 0;
  18.     input.jogar = 0;
  19.    
  20.     do{
  21.        
  22.         printw("\n\nJogadores Disponiveis:\n\n");
  23.         for(i=0;i<18;i++){
  24.            if(Struct_Jogadores[i].Humano == 0)
  25.                mvprintw(i,0,"Equipa: %c\t Numero: %c\n",Struct_Jogadores[i].Equipa,
  26.                       Struct_Jogadores[i].Numero);
  27.         }
  28.         //refresh();
  29.         //fflush(stdout);
  30.     do{
  31.        
  32.         printw("\nQual a Equipa do jogador que vai escolher?:");
  33.         scanw(" %c",&input.Equipa);
  34.         input.Equipa = toupper(input.Equipa);
  35.         //refresh();
  36.         if(input.Equipa == 'V' || input.Equipa == 'A')
  37.             varaux = 1;
  38.     }while(varaux != 1);
  39.        
  40.     do{
  41.         printw("\nQual o Numero?:");
  42.         scanw(" %c",&input.Numero);
  43.         //refresh();
  44.     }while(input.Numero < '1' || input.Numero > '9');
  45.        
  46.         x = write(fd,&input,sizeof(input));
  47.         sleep(0.11);
  48.                
  49.         for (i = 0; i < 18; i++)
  50.             if (Struct_Jogadores[i].Equipa == input.Equipa && Struct_Jogadores[i].Numero == input.Numero){
  51.                 k = 1;
  52.                 *flagescolher_jogador = 1;
  53.                 break;
  54.             }      
  55.        
  56.                
  57.                
  58.        
  59.     }while(!k);
  60.     clear();
  61.     refresh();
  62.     close(fd);
  63.    
  64. }
  65.  
  66. void Introduz_Envia_Input(){
  67.  
  68.  
  69.     int fd,x,var_aux;
  70.     InputJogador p;
  71.     int input_aux;
  72.    
  73.     fd = open(FIFO_SERVIDOR_JOGO,O_WRONLY);
  74.  
  75.   do{  
  76.     do{
  77.         p.userpid = getpid();
  78.         p.jogar = 1;
  79.         p.escolher_jogador = 0;
  80.         p.sair_utilizador = 0;
  81.         var_aux = 0;
  82.             input_aux = getch();
  83.             switch(input_aux){
  84.                 case KEY_UP: p.input_teclado = 99; break;//99 a 96
  85.                 case KEY_DOWN: p.input_teclado = 98; break;
  86.                 case KEY_RIGHT: p.input_teclado = 97; break;
  87.                 case KEY_LEFT: p.input_teclado = 96; break;
  88.                 case '0': p.input_teclado = 0; break;  
  89.                 case '1': p.input_teclado = 1; break;
  90.                 case '2': p.input_teclado = 2; break;
  91.                 case '3': p.input_teclado = 3; break;
  92.                 case '4': p.input_teclado = 4; break;
  93.                 case '5': p.input_teclado = 5; break;
  94.                 case '6': p.input_teclado = 6; break;
  95.                 case '7': p.input_teclado = 7; break;
  96.                 case '8': p.input_teclado = 8; break;
  97.                 case '9': p.input_teclado = 9; break;
  98.             }
  99.         if ((p.input_teclado > 95 && p.input_teclado < 100) || (p.input_teclado >= 0 && p.input_teclado < 10)) {
  100.                 var_aux = 1;
  101.             }
  102.     }while(!var_aux);
  103.        
  104.     x = write(fd,&p,sizeof(p));
  105.     //printf("%d\n",p.input_teclado);  
  106.      
  107.   }while(Fim_jogo == 0);
  108.      
  109.   close(fd);
  110.  
  111.    
  112. }
  113.  
  114. void * Thread_Desenha_Tab(void * Info_Jogo){
  115.    
  116.     int fim_jogo = 0,i, j, x = 0, y = 0;
  117.     StrThr *ptr_struct;
  118.  
  119.     ptr_struct = (StrThr *)Info_Jogo;
  120.    
  121.     init_pair(3, COLOR_GREEN, COLOR_GREEN);
  122.     init_pair(4, COLOR_WHITE, COLOR_WHITE);
  123.     init_pair(5, COLOR_BLUE, COLOR_BLUE);
  124.     init_pair(6, COLOR_RED, COLOR_RED);
  125.    
  126.  
  127.    do{ 
  128.     clear();
  129.  
  130.     attron(COLOR_PAIR(3));  //Interior do campo
  131.     for (i = 0; i < 21; i++) {
  132.         x = 0;
  133.         for (j = 0; j < 51; j++) {
  134.             mvprintw(y, x, " "); x++;
  135.         }
  136.         mvprintw(y, x, " "); y++;
  137.     }
  138.     attroff(COLOR_PAIR(3));
  139.  
  140.     attron(COLOR_PAIR(4));
  141.     for (i = 0; i < 51; i++) { mvprintw(0, i, " "); } //Linha cima
  142.     for (i = 0; i < 52; i++) { mvprintw(21, i, " "); } //Linha baixo
  143.     for (i = 0; i < 21; i++) { mvprintw(i, 0, " "); } //Linha esquerda
  144.     for (i = 0; i < 21; i++) { mvprintw(i, 26, " "); } //Linha centro
  145.     for (i = 0; i < 21; i++) { mvprintw(i, 51, " "); } //Linha direita
  146.     attroff(COLOR_PAIR(4));
  147.  
  148.     attron(COLOR_PAIR(5));
  149.     for (i = 6; i <= 15; i++) { mvprintw(i, 0, " "); } //Baliza esquerda
  150.     attroff(COLOR_PAIR(5));
  151.  
  152.     attron(COLOR_PAIR(6));
  153.     for (i = 6; i <= 15; i++) { mvprintw(i, 51, " "); } //Baliza direita
  154.     attroff(COLOR_PAIR(6));
  155.    
  156.     refresh();
  157.    }while(fim_jogo == 0);
  158.    
  159.  
  160. }
  161. void * Thread_Jogar_Cliente(void * ptr_Struct_Jogadores){
  162.  
  163.     int fd_res,cli_pid,z,i;
  164.     Jogador *pjogador;
  165.     char jogarfifocli[30];
  166.    
  167.    
  168.     pjogador = (Jogador *) ptr_Struct_Jogadores;
  169.    
  170.     cli_pid = getpid();
  171.     while(access(FIFO_SERVIDOR_JOGO,F_OK) != 0){
  172.           printf("Nao existe ligacao com o servidor de jogo!\n");
  173.           sleep(2);
  174.         }
  175.    
  176.     sprintf(jogarfifocli,"CLiente%d",cli_pid);
  177.                              
  178.     fd_res  = open(jogarfifocli,O_RDWR); //Abre Fifo cliente onde se recebe as structs com info
  179.                                          //Estruturas dos jogadores a.k.a "Tabuleiro"
  180.    
  181.     do{
  182.         z = read(fd_res,&pjogador,sizeof(pjogador) * 18);
  183.        
  184.     }while(Fim_jogo != 1);
  185.    
  186.     close(fd_res);
  187.     //endwin();            
  188.    
  189. }
  190.  
  191. void fazer_LoginRegisto(Pedido_LoginRegisto * p) {
  192.  
  193.     int i, k;  
  194.  
  195.     do {
  196.         k = 1;
  197.         printw("Username:");
  198.         scanw(" %s[^\n]", p->username);
  199.  
  200.  
  201.         for (i = 0; i < strlen(p->username); i++)
  202.             if (p->username[i] < '0' || (p->username[i] > '9' && tolower(p->username[i]) < 'a') || tolower(p->username[i]) > 'z') {
  203.                 k = 0;
  204.                 break;
  205.             }
  206.  
  207.         if (k == 0 || strlen(p->username) > 15)
  208.             printw("[USERNAME INVALIDO]\n(MAX:15 caracteres -> [a-z] [A-Z] [1-10])\n");
  209.  
  210.     } while (strlen(p->username) > 15 || k != 1);
  211.  
  212.     do {
  213.         k = 1;
  214.         printw("Password:");
  215.         noecho();
  216.         scanw(" %s[^\n]", p->password);
  217.         echo();
  218.  
  219.         for (i = 0; i < strlen(p->password); i++)
  220.             if (p->password[i] < '0' || (p->password[i] > '9' && tolower(p->password[i]) < 'a') || tolower(p->password[i]) > 'z') {
  221.                 k = 0;
  222.                 break;
  223.             }
  224.  
  225.         if (k == 0 || strlen(p->password) > 15)
  226.             printw("[PASSWORD INVALIDA]\n(MAX:15 caracteres -> [a-z] [A-Z] [1-10])\n");
  227.  
  228.     } while (strlen(p->password) > 15 || k != 1);
  229.     clear();
  230.     //refresh();
  231.    
  232.    
  233. }
  234.  
  235. int main(void) {
  236.  
  237.     int y,z,x,verifica,fd_ped,fd_res,menu,maxX,maxY;
  238.     int flagEscolher_jogador = 0;
  239.     Bola BALL;
  240.     Pedido_LoginRegisto p;
  241.     Resposta_LoginRegisto r;
  242.     p.cli_pid = getpid();
  243.     Jogador Struct_Jogadores[18];
  244.     Str_campo Inf_Desenho;
  245.     char fifo_cli[30];
  246.     pthread_t Jogar,Desenha_Tab;
  247.    
  248.     initscr();
  249.     start_color();
  250.     echo();
  251.     cbreak();
  252.     keypad(stdscr,TRUE);
  253.     curs_set(0);
  254.  
  255.    
  256.     Inf_Desenho.pj = Struct_Jogadores;
  257.     Inf_Desenho.pb = &BALL;
  258.    
  259.    
  260.  
  261.     //getmaxyx(stdscr,maxY,maxX);
  262.     //echo();
  263.        
  264.         //Configura e abre o fifo do cliente
  265.         sprintf(fifo_cli,"Cliente%d",p.cli_pid);
  266.         mkfifo(fifo_cli,0600);
  267.         fd_res = open(fifo_cli,O_RDWR);
  268.  
  269.     //Verifica se o servidor está ligado
  270.     while(access(FIFO_SERVIDOR_LOGIN, F_OK) != 0) {
  271.         printw("Nao existe ligacao ao servidor\n");
  272.         sleep(10);
  273.     }
  274.         //Abre a ligação ao servidor de Login
  275.         fd_ped = open(FIFO_SERVIDOR_LOGIN, O_WRONLY);
  276.         if (fd_ped == -1) {
  277.             printw("Erro na abertura do fifo do cliente \n");
  278.             exit(1);
  279.         }
  280.  
  281.     do {
  282.         printw("---------Menu Principal---------\n");
  283.         printw("1 - Login\n");
  284.         printw("2 - Registar\n");
  285.         printw("3 - Sair\n");
  286.         printw("Escolha uma opcao:");
  287.         //refresh();
  288.         scanw("%d", &menu);
  289.         clear();
  290.  
  291.        
  292.         switch(menu){
  293.         case 1:
  294.             p.login = 1;
  295.             p.registo = 0;
  296.             printw("Introduza os seus dados de login\n");
  297.             fazer_LoginRegisto(&p);
  298.             break;
  299.         case 2:
  300.             p.login = 0;
  301.             p.registo = 1;
  302.             printw("Introduza os seus dados de registo\n");
  303.             fazer_LoginRegisto(&p);
  304.             break;
  305.         case 3:
  306.             p.login = 0;
  307.             p.registo = 0;
  308.             close(fd_ped);
  309.             endwin();
  310.             exit(0);
  311.         default:
  312.             printw("Opcao invalida!\n");
  313.             break;
  314.         }
  315.  
  316.         z = write(fd_ped, &p, sizeof(p));
  317.                 x = read(fd_res, &r, sizeof(r));      
  318.        
  319.                 if(r.verifica == 1 && p.login == 1){
  320.                     printw("Login efectuado com sucesso!\n");
  321.                     //clear();
  322.                    
  323.                     y = read(fd_res,&Struct_Jogadores,sizeof(Struct_Jogadores));
  324.                    
  325.                     pthread_create(&Jogar,NULL,&Thread_Jogar_Cliente,(void *)&Struct_Jogadores);
  326.                
  327.                     if(flagEscolher_jogador == 0)
  328.                         Escolhe_jogador(Struct_Jogadores, &flagEscolher_jogador);
  329.                    
  330.                     Introduz_Envia_Input();
  331.                    
  332.                     pthread_create(&Desenha_Tab,NULL,&Thread_Desenha_Tab,(void *)&Inf_Desenho);
  333.                     pthread_join(Jogar,NULL);
  334.                     pthread_join(Desenha_Tab,NULL);
  335.                 }
  336.                
  337.                 if(r.verifica == 1 && p.registo == 1){
  338.                     printw("Registo efectuado com sucesso!\n");
  339.                     printw("Realize o seu login.\n");
  340.                     //refresh();
  341.                 }
  342.                
  343.                 if(r.verifica == 0 && p.login == 1){
  344.                     printw("O login falhou!\n");
  345.                 }
  346.                
  347.                 if(r.verifica == 0 && p.registo == 1){
  348.                     printw("O registo falhou!\n");
  349.                 }
  350.    
  351.     }while(menu!=3 || menu != 4);
  352.  
  353.     close(fd_ped);
  354.     close(fd_res); 
  355.        
  356.     unlink(fifo_cli);
  357.    
  358.     endwin();
  359.    
  360.     return 0;
  361. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement