Guest User

Untitled

a guest
Sep 24th, 2016
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 28.83 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <sys/socket.h>
  5. #include <sys/types.h>
  6. #include <netinet/in.h>
  7. #include <netdb.h>
  8. #include <errno.h>
  9. #include <unistd.h>
  10. #include <sys/un.h>
  11. #include <fcntl.h>
  12. #include <time.h>
  13. #include <pthread.h>
  14. #include <limits.h>
  15.  
  16.  
  17. #define SOCKET_NAME "/tmp/my_first_socket"
  18. #define max 10
  19.  
  20.  
  21. struct clue
  22. {
  23.     int x;
  24.     int y;
  25. };
  26. struct clue sugg[9],firstClue;
  27.  
  28. //LISTA GIOCATORI ONLINE
  29. struct list
  30. {                              
  31.     char name[25];
  32.     int numClue; //NUMERO INDIZI TROVATI
  33.     int last;   //FLAG: 1 SE E' STATO TROVATO L'ULTIMO INDIZIO
  34.     int trovato;
  35.     struct list *next;
  36. };
  37. struct list *players=NULL;
  38.  
  39. int selected[9];    //TIENE TRACCIA DEGLI INDIZI SCOVATI
  40. pthread_mutex_t semaforo=PTHREAD_MUTEX_INITIALIZER ;
  41. pthread_mutex_t semaforo2=PTHREAD_MUTEX_INITIALIZER ;
  42. time_t start,now;   //VARIABILI IMPLEMENTAZIONE TIMER
  43. int count1=0,count2=0; //VARIABILI DI SINCRONIZZAZIONE CLIENT
  44. int tesoro=1;       //FLAG CHE DIVENTA 1 SE VIENE SCOVATO IL TESORO
  45. char map[max][max];         //MAPPA DEL GIOCO
  46.  
  47.  
  48. //RESTITUISCE NUMERO DI UTENTI ONLINE
  49. int numUsers(struct list *players)
  50. {
  51.     if(players==NULL)
  52.     {
  53.         return 0;
  54.     }
  55.     else
  56.     {
  57.         return 1+numUsers(players->next);
  58.     }
  59. }
  60.  
  61.  
  62. //AGGIORNA IL NUMERO DI INDIZI SCOVATI DA UN GIOCATORE
  63. void updateclue(struct list *players,char *username)
  64. {
  65.     if(strcmp(players->name, username)==0)
  66.     {
  67.         players->numClue=players->numClue+1;
  68.     }
  69.     else
  70.     {
  71.         updateclue(players->next,username);
  72.     }
  73. }
  74.  
  75.  
  76. //STAMPA LISTA UTENTI ONLINE
  77. void printUsers(int mySockFd, struct list *players)
  78. {
  79.     char buffer[500];
  80.     char app[3];
  81.     char app2[20];
  82.     int i;
  83.     bzero(buffer,500);
  84.     if(players==NULL)
  85.     {
  86.         write(mySockFd, "0 UTENTI ONLINE",  15);
  87.     }
  88.     else
  89.     {
  90.         struct list *p=players;
  91.         strcat(buffer,"USERNAME->NUMERO INDIZI\n");
  92.         while(p!=NULL)
  93.         {
  94.             i=0;
  95.             while(p->name[i]!='\n')
  96.             {
  97.                 app2[i]=p->name[i];
  98.                 i++;
  99.             }
  100.             app2[i]='\0';
  101.         //  strcat(buffer,"USERNAME : ");
  102.             strcat(buffer, app2);
  103.             strcat(buffer,"->");
  104.             sprintf(app,"%d",p->numClue);
  105.             strcat(buffer,app);
  106.             strcat(buffer,"\n");
  107.             p=p->next;
  108.         }
  109.             write(mySockFd,buffer,strlen(buffer));
  110.     }
  111. }
  112.  
  113.  
  114. //ELIMINA UTENTE DA LISTA GIOCATORI
  115. struct list *deleteUser(struct list *players, char username[])
  116. {
  117.     if(players!=NULL)
  118.     {
  119.         if(strcmp(players->name,username)==0)
  120.         {
  121.             struct list *elem=(struct list *)malloc(sizeof(struct list));
  122.             elem=players;
  123.             players=players->next;
  124.             free(elem);
  125.         }
  126.         else
  127.         {
  128.             players->next=deleteUser(players->next, username);
  129.         }
  130.  
  131.     }
  132.     return players;
  133. }
  134.  
  135.  
  136. //CREA LISTA GIOCATORI ONLINE
  137. void onlineUsers(char username[])
  138. {
  139.     if(players==NULL)
  140.     {
  141.         players=(struct list *)malloc(sizeof(struct list));
  142.         strcpy(players->name, username);
  143.         players->numClue=0;
  144.         players->next=NULL;
  145.     }
  146.     else
  147.     {
  148.         struct list *appoggio=(struct list *)malloc(sizeof(struct list));
  149.         strcpy(appoggio->name, username);
  150.         players->numClue=0;
  151.         players->last=0;
  152.         players->trovato=0;
  153.         appoggio->next=players;
  154.         players=appoggio;
  155.     }
  156. }
  157.  
  158.  
  159. //VIENE STAMPATA LA MAPPA DI GIOCO SUL SERVER E MOSTRATE LE POSIZIONI DEGLI ELEMENTI DI GIOCO
  160. void printServerMap(char map [max][max])
  161. {
  162.     int i, j;
  163.     char a;
  164.     char buf[2];
  165.     int b;
  166.     printf("MATRICE-SERVER:\n");
  167.     for(i=0;i<max;i++)
  168.     {
  169.         for(j=0;j<max;j++)
  170.         {
  171.             if(map [i][j]>='1' && map [i][j]<='9')
  172.             {
  173.                 a=map[i][j];
  174.                 buf[0]=a;
  175.                 buf[1]='\0';
  176.                 printf("%d  " , atoi(buf));
  177.             }
  178.             else
  179.             {
  180.                 printf("%c  ", map[i][j]);
  181.             }
  182.         }
  183.         printf ("\n");
  184.     }
  185. }
  186.  
  187.  
  188. //GENERA LA MAPPA DI GIOCO E RESTITUISCE IL PRIMO INDIZIO
  189. struct clue createMap(char map[max][max], struct clue* sugg)
  190. {
  191.     char ind='1';
  192.     int i=0, j=0, indice1=0, indice2=0, last1=0, last2=0, num_indizi=8, num_ostacoli=5;
  193.     for(i=0;i<9;i++)
  194.         selected[i]=0;
  195.     //AZZERO MAPPA DI GIOCO
  196.     for(i=0;i<max;i++)
  197.     {
  198.         for(j=0;j<max;j++)
  199.         {
  200.             map[i][j]='0';
  201.         }
  202.     }
  203.     i=0;
  204.                             //POSIZIONO INDIZI
  205.     //COORDINATE PRIMO INDIZIO
  206.     firstClue.x=rand()%10;
  207.     firstClue.y=rand()%10;
  208.     //POSIZIONO PRIMO INDIZIO
  209.     map[firstClue.x][firstClue.y]=ind;
  210.     //GENERO RESTANTI INDIZI
  211.     while(atoi(&ind)!=num_indizi+1)
  212.     {
  213.         indice1=rand()%10;
  214.         indice2=rand()%10;
  215.         //SE LA POSIZIONE E' LIBERA
  216.         if(map[indice1][indice2]=='0')
  217.         {
  218.             //POSIZIONO INDIZIO
  219.             map[indice1][indice2]=ind+1 ;
  220.             //AGGIORNO VETTORE SUGGERIMENTI
  221.             sugg[atoi(&ind)-1].x=indice1;
  222.             sugg[atoi(&ind)-1].y=indice2;
  223.             ind++;
  224.         }
  225.     }
  226.     i=0;
  227.                         //POSIZIONO TESORO
  228.     do
  229.     {
  230.         indice1=rand()%10;
  231.         indice2=rand()%10;
  232.         //SE LA POSIZIONE E' LIBERA
  233.         if(map[indice1][indice2]=='0')
  234.         {
  235.             //POSIZIONO TESORO
  236.             map[indice1][indice2]='t';
  237.             i++;
  238.         }
  239.     }while(i!=1);
  240.     //ASSEGNO LE COORDINATE DEL TESORO ALL'ULTIMO SUGGERIMENTO
  241.     sugg[8].x=indice1,
  242.     sugg[8].y=indice2;
  243.     i=0;
  244.                     //POSIZIONO OSTACOLI
  245.     while(i!=num_ostacoli)
  246.     {
  247.         indice1=rand()%10;
  248.         indice2=rand()%10;
  249.         //SE LA POSIZIONE E' LIBERA
  250.         if(map[indice1][indice2]=='0')
  251.         {
  252.             map[indice1][indice2]='x';
  253.             i++;
  254.         }
  255.     }
  256.     printServerMap(map);
  257.     return firstClue;
  258. }
  259.  
  260.  
  261. //IMPOSTA POSIZIONE INIZIALE DI UN GIOCATORE
  262. void startPosition(int mySockFd,int *cor1, int *cor2)
  263. {
  264.     char buffer[256], scelta[256], scelta2[256];
  265.     bzero(buffer, 256);
  266.  
  267.      //UTILIZZO IL SEMAFORO PER EVITARE CHE DUE GIOCATORI INIZINO NELLA STESSA POSIZIONE
  268.     pthread_mutex_lock(&semaforo2);
  269.     do
  270.     {
  271.         *cor1=rand()%10;
  272.         *cor2=rand()%10;
  273.        
  274.     }while(map[*cor1][*cor2]!='0' && map[*cor1][*cor2]!='c');
  275.     //SBLOCCO IL SEMAFORO
  276.     strcpy(buffer, "GIOCATORE POSIZIONATO, PREMERE INVIO...");
  277.     write(mySockFd, buffer, strlen(buffer));
  278.     read(mySockFd, scelta,255);
  279.     pthread_mutex_unlock(&semaforo2);
  280.     bzero(buffer, 256);
  281. }
  282.  
  283.  
  284. //AGGIORNA POSIZIONE ATTUALE DELL'UTENTE
  285. void currPosition(char map[max][max], int cor1, int cor2)
  286. {
  287.     map[cor1][cor2]='o';
  288. }
  289.  
  290.  
  291. //SCRIVE IN UN FILE DI TESTO IL MOMENTO DI RITROVAMENTO DI UN TESORO
  292. void updateFileTreasure(char username[])
  293. {
  294.       int fd;
  295.       char messaggio[256], orario[256];
  296.       time_t ora;
  297.       time(&ora);
  298.       bzero(messaggio, 256);
  299.       bzero(orario, 256);
  300.       strcpy(messaggio, "RITROVAMENTO TESORO: " ) ;
  301.       sprintf(orario, "%s", asctime(localtime(&ora)));
  302.       strcat(messaggio, username);
  303.       strcat(messaggio, orario);
  304.       strcat(messaggio, "\n");
  305.       pthread_mutex_lock(&semaforo);
  306.       fd=open("ritrovo_tesoro.txt", O_WRONLY|O_CREAT);
  307.       lseek(fd, 0, SEEK_END);
  308.       write(fd, messaggio, strlen(messaggio));
  309.       close(fd);
  310.       pthread_mutex_unlock(&semaforo);
  311. }
  312.  
  313.  
  314. //AGGIORNA I VALORI NELLA MAPPA DI GIOCO
  315. int updateMap(int mySockFd,char map[max][max],int cor1,int cor2,int oldcor1,int oldcor2,struct clue* sugg,char *username)
  316. {
  317.     int lst;
  318.     struct list *p,g;
  319.     int flag=0; //FLAG: 0: MOVIMENTO CONSENTITO, 1: MOVIMENTO NON CONSENTITO, 1<=FLAG-9<=9 : INDIZIO, 20:TESORO
  320.     char buf[2];
  321.     char app[5];
  322.     for(p=players;p;p=p->next)
  323.     {
  324.         if(strcmp(p->name,username)==0)
  325.             lst=p->last;
  326.     }
  327.     //SE TROVO IL TESORO
  328.     if(map[cor1][cor2]=='t' || map[cor1][cor2]=='$')
  329.     {
  330.         map[cor1][cor2]='$';
  331.         //TESORO TROVATO
  332.         if(lst==1)
  333.             {
  334.                 tesoro=0;
  335.                 for(p=players;p;p=p->next)
  336.                 {
  337.                     if(strcmp(p->name,username)==0)
  338.                     p->trovato=1;
  339.                 }
  340.                 updateFileTreasure(username);
  341.             }
  342.         if(!(map[oldcor1][oldcor2]>='1' && map[oldcor1][oldcor2]<='9'))
  343.         {
  344.             map[oldcor1][oldcor2]='c';
  345.         }  
  346.         flag=20;
  347.     }
  348.     //SE TROVO UN INDIZIO
  349.     else if(map[cor1][cor2]>='1' && map[cor1][cor2]<='9')
  350.     {
  351.         if(!(map[oldcor1][oldcor2]>='1' && map[oldcor1][oldcor2]<='9') )
  352.         {
  353.             if (map[oldcor1][oldcor2]!='$')
  354.             {
  355.        
  356.             map[oldcor1][oldcor2]='c';
  357.             }
  358.         }
  359.        
  360.         buf[0]=map[cor1][cor2];
  361.         buf[1]='\0';
  362.         flag=9+atoi(buf);
  363.     //  sprintf(app,"%d",flag);
  364.     //  write(mySockFd,app,2);
  365.         if(selected[atoi(buf)-1]==0)
  366.             updateclue(players,username);
  367.         selected[atoi(buf)-1]=1;
  368.         if(map[cor1][cor2]=='9')
  369.         {
  370.             for(p=players;p;p=p->next)
  371.             {
  372.                 if(strcmp(p->name,username)==0)
  373.                 {
  374.                     p->last=1;
  375.                 }
  376.             }
  377.         }
  378.     }
  379.     //SE TROVO UN OSTACOLO
  380.     else if(map[cor1][cor2]=='x')
  381.     {
  382.         map[cor1][cor2]='/';
  383.         flag=1;
  384.     }
  385.     //MOVIMENTO NON CONSENTITO SU UN OSTACOLO GIA SCOVATO O SU UN ALTRO GIOCATORE
  386.     else if(map[cor1][cor2]=='/' || map[cor1][cor2]=='o' || map[cor1][cor2]=='O')
  387.     {
  388.         flag=1;
  389.     }
  390.     //MOVIMENTO SU CASELLE INESPLORATE O ESPLORATE MA NON SIGNIFICATIVE
  391.     else if (map[cor1][cor2]=='0' || map[cor1][cor2]=='c')
  392.     {
  393.         if(!(map[oldcor1][oldcor2]>='1' && map[oldcor1][oldcor2]<='9')  )
  394.         {
  395.             if (map[oldcor1][oldcor2]!='$')
  396.             {
  397.                 map[oldcor1][oldcor2]='c';
  398.             }
  399.         }
  400.         flag=0;
  401.     }
  402.    
  403.     return flag;
  404. }
  405.  
  406.  
  407. //STAMPA MAPPA SUL SOCKET
  408. void printClientMap(int mySockFd, char map[max][max], int flag, int cor1,int cor2,char *username)
  409. {
  410.     int i, j;
  411.     char buffer[10000];
  412.     char n_tes[5];
  413.     char buf[2];
  414.     char app[5];
  415.     bzero(buffer,10000);
  416.     int lst;
  417.     struct list* p;
  418.     for(p=players;p;p=p->next)
  419.     {
  420.         if(strcmp(p->name,username)==0)
  421.         {
  422.             lst=p->last;
  423.         }
  424.     }
  425.  
  426.     sprintf(app, "%f", difftime(time(&now), start));
  427.     strcat(buffer, "(-)TEMPO TRASCORSO: ");
  428.     strcat(buffer, app);
  429.     strcat(buffer, "s \n");
  430.     if(flag==0 || flag==20)
  431.     {
  432.         strcat(buffer, "*{-} MOVIMENTO CONSENTITO! {-}*");
  433.         strcat(buffer, "\n\n");
  434.     }
  435.     else if(flag==-1)
  436.     {
  437.         strcat(buffer, "*{-} INIZIAMO A GIOCARE! {-}*");
  438.         strcat(buffer, n_tes);
  439.         strcat(buffer, " \n\n");
  440.         strcat(buffer, "*** IL PRIMO INDIZIO SI TROVA IN POSIZIONE:  ");
  441.         sprintf(app, "%d", firstClue.x);
  442.         strcat(buffer, app);
  443.         strcat(buffer, ",");
  444.         sprintf(app, "%d", firstClue.y);
  445.         strcat(buffer, app);
  446.         strcat(buffer, "***\n");
  447.     }
  448.     else if(flag==5)
  449.     {
  450.         strcat(buffer, "*{-} MOVIMENTO OLTRE MATRICE NON CONSENTITO!{-}*");
  451.         strcat(buffer, n_tes);
  452.         strcat(buffer, " \n\n");
  453.     }
  454.     else if (flag==1)
  455.     {
  456.         strcat( buffer , "*{-} MOVIMENTO NON CONSENTITO! {-}*");
  457.         strcat(buffer, n_tes);
  458.         strcat(buffer, " \n\n");
  459.     }
  460.     else
  461.     {
  462.         if(flag!=20)
  463.         {
  464.             if(flag==18)
  465.                 strcat(buffer, "*** ULTIMO INDIZIO SCOVATO, IL TESORO SI TROVA NELLA POSIZIONE: ");
  466.             else
  467.                 strcat(buffer, "*** INDIZIO SCOVATO, IL PROSSIMO INDIZIO SI TROVA NELLA POSIZIONE: "); 
  468.             sprintf(n_tes, "%d", sugg[(flag-10)].x);
  469.             strcat(buffer, n_tes);
  470.             strcat(buffer, ",");
  471.             sprintf(n_tes, "%d", sugg[(flag-10)].y);
  472.             strcat(buffer, n_tes);
  473.             strcat(buffer, "***\n\n");
  474.         }
  475.     }
  476.     strcat(buffer,"\t");
  477.     for(i=0;i<max;i++)
  478.     {
  479.         sprintf(app,"%d\t",i);
  480.         strcat(buffer,app);
  481.     }
  482.     strcat(buffer,"\n\t");
  483.     for(i=0;i<max-1;i++)
  484.     {
  485.         strcat(buffer,"_________");
  486.     }
  487.         strcat(buffer,"\n\n");
  488.  
  489.     for(i=0;i<max;i++)
  490.     {
  491.         sprintf(app,"%d  |\t",i);
  492.         strcat(buffer,app);
  493.         for(j=0;j<max;j++)
  494.         {
  495.             //TESORO
  496.             if(map[i][j]=='$' )
  497.             {
  498.                 if(lst==1)
  499.                     strcat(buffer, "$\t");
  500.                 else
  501.                 {
  502.                     if(cor1==i && cor2==j)
  503.                         strcat(buffer, "@\t");
  504.                     else
  505.                         strcat(buffer, ".\t"); 
  506.                 }
  507.                    
  508.             }
  509.             //OSTACOLO
  510.             else if(map[i][j]=='/')
  511.             {
  512.                 strcat ( buffer , "/\t" ) ;
  513.             }
  514.             //CASELLE INESPLORATE
  515.             else if( map[i][j]=='x' || map[i][j]=='t' || map[i][j]=='0')
  516.             {
  517.                 strcat(buffer, "*\t");
  518.             }
  519.             // INDIZIO
  520.             else if(map[i][j]>='1' && map[i][j]<='9')
  521.             {
  522.                 buf[0]=map[i][j];
  523.                 buf[1]='\0';
  524.                 if(selected[atoi(buf)-1]==0)
  525.                     strcat(buffer, "*\t");
  526.                 else
  527.                 {
  528.                     if(cor1==i && cor2==j) //POSIZIONE UTENTE IN CASO DI INDIZIO TROVATO
  529.                     {
  530.                         strcat(buffer,"#\t");  
  531.                     }
  532.                     else
  533.                     {
  534.                        
  535.                         strcat(buffer, buf);
  536.                         strcat(buffer, "\t");
  537.                     }
  538.                 }
  539.             }
  540.             //CASELLE ESPLORATE
  541.             else if(map[i][j]=='c')
  542.             {
  543.                 strcat(buffer, ".\t");
  544.             }
  545.             //POSIZIONE ATTUALE UTENTE
  546.             else if(map[i][j]=='o')
  547.             {
  548.                 strcat(buffer, "@\t");
  549.             }
  550.         }
  551.         strcat(buffer, "\n");
  552.     }
  553.     strcat(buffer, "\n");
  554.     if(tesoro!=0)
  555.     {
  556.         strcat(buffer, "*{-} VUOI MUOVERTI? [A/W/S/D], USCIRE?[EXIT] O STAMPARE LA LISTA DEGLI UTENTI? [LIST] {-}*");
  557.     }
  558.     else
  559.     {
  560.         strcat(buffer, "{$$$} TROVATO  IL TESORO NASCOSTO! GENERATA NUOVA MAPPA! Premi Invio {$$$}");
  561.         tesoro=0;
  562.     }
  563.     write(mySockFd, buffer, strlen(buffer));
  564.     bzero(buffer, 10000);
  565. }
  566.  
  567.  
  568. //AGGIORNO MAPPA IN CASO DI ABBANDONO DI UN UTENTE
  569. void leaveMap(int *cor1, int *cor2)
  570. {
  571.     if(map[*cor1][*cor2]=='o')
  572.     {
  573.         map[*cor1][*cor2]='0';
  574.     }
  575.     else
  576.     {
  577.         *cor1=-1;
  578.         *cor2=-1;
  579.     }
  580.    
  581. }
  582.  
  583.  
  584. void game(int mySockFd,char username[], char map[max][max])
  585. {
  586.     int cor1=0,cor2=0,maxx=INT_MIN;
  587.     char scelta[256];
  588.     int n, flag=0;
  589.     char continua[256];
  590.     int count=0;
  591.     char charr[10];
  592.     char buffer[1000];
  593.     char app[20];
  594.     struct list *p;
  595.     startPosition(mySockFd, &cor1, &cor2);
  596.     currPosition(map, cor1, cor2);
  597.     printClientMap(mySockFd, map, -1,cor1,cor2,username);
  598.     //STAMPA MAPPA DI GIOCO SUL SERVER
  599.     while(1) //ITERA FINO A CHE NON VIENE TROVATO IL TESORO O NON SCADE IL TEMPO
  600.     {
  601.         flag=0;
  602.         bzero(scelta, 256);
  603.         bzero(buffer, 1000);
  604.         n=read(mySockFd, scelta, 255);
  605.         if(n<0)
  606.         {
  607.             perror("Errore lettura da socket scelta\n");
  608.             exit(1);
  609.         }
  610.         //CONTROLLO SE IL GIOCO E' TERMINATO
  611.         if(tesoro==0 || difftime(time(&now), start)>60)
  612.         {
  613.             if (difftime(time(&now), start)>60 && tesoro==1) //SE IL GIOCO E' TERMINATO PER TEMPO SCADUTO
  614.             {
  615.                 for(p=players;p!=NULL;p=p->next)
  616.                 {
  617.                     if(p->numClue>maxx)
  618.                     {
  619.                         maxx=p->numClue;
  620.                         strcpy(app,p->name);
  621.                     }
  622.                 }
  623.                 strcat(buffer, "\nTEMPO TERMINAT0\n");
  624.                 for(p=players;p!=NULL;p=p->next)
  625.                 {
  626.                     if(p->numClue==maxx)
  627.                     {
  628.                         count++;
  629.                     }
  630.                 }
  631.                 if(count>1)
  632.                 {
  633.                     strcat (buffer, "PAREGGIO, VINCONO...\n"); //CASO IN CUI CI SIA UN PAREGGIO
  634.                     for(p=players;p!=NULL;p=p->next)
  635.                     {
  636.                         if(p->numClue==maxx)
  637.                         {
  638.                             strcat(buffer, p->name);
  639.                             strcat(buffer,"\n");
  640.                         }
  641.                     }
  642.                 }
  643.                 else
  644.                 {
  645.                     strcat(buffer, "VINCE...\n"); //COMUNICO CHI HA VINTO LA PARTITA
  646.                     strcat(buffer, app);
  647.                 }
  648.                     count=0;
  649.                     maxx=INT_MIN;
  650.                 }
  651.             else
  652.             {
  653.                 strcat (buffer, "TESORO TROVATO, VINCE...\n");
  654.                 for(p=players;p;p=p->next)
  655.                 {
  656.                     if(p->trovato==1)
  657.                         strcat(buffer,p->name);
  658.                
  659.                 }
  660.                
  661.             }
  662.             pthread_mutex_lock(&semaforo);
  663.             count1++;
  664.             if(count1==1)
  665.             {
  666.                 firstClue=createMap(map, sugg);
  667.                
  668.             }
  669.             pthread_mutex_unlock(&semaforo);
  670.             strcat(buffer, "\nAttendi altri giocatori...");
  671.             write(mySockFd, buffer, strlen(buffer));
  672.             while(count1<numUsers(players) && count1!=0);
  673.             {
  674.                 read(mySockFd, continua, 255);
  675.             }
  676.             startPosition(mySockFd, &cor1, &cor2);
  677.             currPosition(map, cor1, cor2);
  678.             for(p=players;p!=NULL;p=p->next)
  679.             {
  680.                 p->numClue=0;
  681.                 p->last=0;
  682.                 p->trovato=0;
  683.             }
  684.             {
  685.                     tesoro=1;
  686.                     count1=0;
  687.                     flag=-1;
  688.                     time(&start);
  689.             }
  690.         }
  691.         else
  692.         {
  693.             //SPOSTAMENTO A SINISTRA
  694.             if(strcmp(scelta, "A\n")==0 || strcmp(scelta, "a\n")==0)
  695.             {
  696.                 //CONTROLLO CHE NON SI ESCA DALLA MATRICE
  697.                 if((cor2-1)>-1)
  698.                 {
  699.                     flag=updateMap(mySockFd, map, cor1, cor2-1, cor1, cor2, sugg,username);
  700.                    
  701.                     if(flag==0 && tesoro==1)
  702.                     {
  703.                         currPosition(map, cor1, cor2-1);
  704.                         cor2=cor2-1;
  705.                     }
  706.                     else if(flag>=10)
  707.                     {
  708.                        
  709.                         //currPosition_tesoro(map, cor1, cor2-1);
  710.                         //updateclue(players,username);
  711.                         cor2=cor2-1;
  712.                        
  713.                     }
  714.                 }
  715.                 else
  716.                 {
  717.                     flag=5;     //MOVIMENTO FUORI MATRICE
  718.                 }
  719.  
  720.             }
  721.             //SPOSTAMENTO IN ALTO
  722.             else if(strcmp(scelta, "W\n")==0 || strcmp(scelta, "w\n")==0)
  723.             {
  724.                 //CONTROLLO CHE NON SI ESCA DALLA MATRICE
  725.                 if((cor1-1)>-1)
  726.                 {
  727.                     flag=updateMap(mySockFd,map,cor1-1,cor2,cor1,cor2,sugg,username);
  728.                     if (flag==0 && tesoro==1)
  729.                     {
  730.                         currPosition(map,cor1-1,cor2);
  731.                         cor1=cor1-1;
  732.                     }
  733.                     else if(flag>=10)
  734.                     {
  735.                         //updateclue(players,username);
  736.                         cor1=cor1-1;
  737.                     }
  738.                 }
  739.                 else
  740.                 {
  741.                     flag=5; //MOVIMENTO FUORI MATRICE
  742.                 }
  743.  
  744.             }
  745.             //SPOSTAMENTO IN BASSO
  746.             else if(strcmp(scelta, "S\n")==0 || strcmp(scelta, "s\n")==0)
  747.             {
  748.                 //CONTROLLO CHE NON SI ESCA DALLA MATRICE
  749.                 if((cor1+1)<10)
  750.                 {
  751.                     flag=updateMap(mySockFd, map, cor1+1, cor2, cor1, cor2, sugg,username);
  752.                     if(flag==0 && tesoro==1)
  753.                     {
  754.                         currPosition(map, cor1+1, cor2);
  755.                         cor1=cor1+1;
  756.                     }
  757.                     else if(flag>=10)
  758.                     {
  759.                     //  updateclue(players,username);
  760.                         cor1=cor1+1;
  761.                     }
  762.                 }
  763.                 else
  764.                 {
  765.                     flag=5; //MOVIMENTO FUORI MATRICE
  766.                 }
  767.             }
  768.             //SPOSTAMENTO A DESTRA
  769.             else if(strcmp(scelta, "D\n")==0 || strcmp(scelta, "d\n")==0)
  770.             {
  771.                 //CONTROLLO CHE NON SI ESCA DALLA MATRICE
  772.                 if((cor2+1)<10)
  773.                 {
  774.                     flag=updateMap(mySockFd, map, cor1, cor2+1, cor1, cor2, sugg,username);
  775.                     if(flag==0 && tesoro==1)
  776.                     {
  777.                         currPosition(map, cor1, cor2+1);
  778.                         cor2=cor2+1;
  779.                     }
  780.                     else if(flag>=10)
  781.                     {
  782.                     //  updateclue(players,username);
  783.                         cor2=cor2 + 1;
  784.                     }
  785.                 }
  786.                 else
  787.                 {
  788.                     flag=5; //MOVIMENTO FUORI MATRICE
  789.                 }
  790.             }
  791.             else if(strcmp(scelta, "EXIT\n")==0 || strcmp(scelta, "exit\n")==0)
  792.             {
  793.                 printf("Mi disconnetto\n");
  794.                 players=deleteUser(players, username);
  795.                 leaveMap(&cor1, &cor2);
  796.                 pthread_exit(NULL);
  797.             }
  798.             else if(strcmp(scelta, "LIST\n")==0 || strcmp(scelta, "list\n")==0)
  799.             {
  800.                 printUsers(mySockFd,players);  
  801.             }
  802.         }
  803.        
  804.         printClientMap(mySockFd, map, flag,cor1,cor2,username);
  805.     }
  806. }
  807.  
  808.  
  809. //MENU UTENTE
  810. void home(int mySockFd, char username[])
  811. {
  812.     char scelta[256], buffer[256],app[256];
  813.     int n, opzione=0;
  814.     do
  815.     {
  816.         bzero(buffer, 256);
  817.         strcpy(buffer, "\n[*]MENU - SCEGLI OPZIONE:\n[1]GIOCA!\n[2]VISUALIZZA UTENTI CONNESSI\n[3]DISCONNETTI\nSCELTA: ");
  818.         n=write(mySockFd, buffer, strlen(buffer)); // INVIO AL CLIENT LE ALTERNATIVE DELL'UTENTE
  819.         if(n<0)
  820.         {
  821.             perror("Errore scrittura su socket\n");
  822.             exit(1);
  823.         }
  824.         bzero(scelta, 256);
  825.         n=read(mySockFd ,scelta ,255);
  826.         opzione=atoi(scelta);
  827.         if(n<0)
  828.         {
  829.             perror("Errore lettura da socket\n");
  830.             exit(1);
  831.         }
  832.         if(strcmp(scelta, "1\n")==0)
  833.         {
  834.             printf("FUNZIONE GIOCO:\n");
  835.             game(mySockFd, username, map);
  836.             break;
  837.         }
  838.         else if(strcmp(scelta, "2\n")==0)
  839.         {
  840.             printf("FUNZIONE LISTA UTENTI:\n");
  841.             printUsers(mySockFd, players);
  842.             read(mySockFd ,app ,255);
  843.         }
  844.         else if(strcmp(scelta, "3\n")==0)
  845.         {
  846.             break;
  847.         }
  848.         else
  849.         {
  850.             strcpy(buffer, "Operazione non valida. Premere un tasto per continuare.");
  851.             write(mySockFd, buffer, strlen(buffer));
  852.             bzero(buffer, 256);
  853.             read(mySockFd, buffer, 256);
  854.         }
  855.    
  856.     }while(opzione<=0 || opzione==2 || opzione>3);
  857.     bzero(buffer, 256);
  858.     players=deleteUser(players, username);
  859.     strcpy(buffer, "\nAccount disconnesso. Arrivederci! Premere un tasto per continuare." );
  860.     write(mySockFd, buffer, strlen(buffer));
  861.     bzero(buffer, 256);
  862.     read(mySockFd, buffer, 256);
  863. }
  864.  
  865.  
  866. //CONTROLLO DISPONIBILITA' USERNAME
  867. int checkUsername(char username[])
  868. {
  869.     int fd;
  870.      fd=open(username, O_RDONLY);
  871.      if(fd<0)
  872.      {
  873.          return 0 ;
  874.      }
  875.      else
  876.      {
  877.         close(fd);
  878.         return 1;
  879.      }
  880. }
  881.  
  882. //MEMORIZZA DATA E ORA DI ACCESSO DEGLI UTENTI
  883. void updateFile ( char username[] )
  884. {
  885.     int fd;
  886.     char messaggio[256], orario[256];
  887.     time_t ora;
  888.     time(&ora);
  889.     bzero(messaggio, 256);
  890.     bzero(orario, 256);
  891.     strcpy(messaggio, "Effettua l'accesso: ");
  892.     sprintf(orario, "%s", asctime(localtime(&ora)));
  893.     strcat(messaggio, username);
  894.     strcat(messaggio, orario);
  895.     strcat(messaggio, "\n");
  896.     pthread_mutex_lock(&semaforo);
  897.     fd=open("logging.txt", O_WRONLY|O_CREAT) ;
  898.     lseek(fd, 0, SEEK_END);
  899.     write(fd, messaggio, strlen(messaggio));
  900.     close(fd);
  901.     pthread_mutex_unlock(&semaforo);
  902. }
  903.  
  904.  
  905. //ACCESSO E REGISTRAZIONE
  906. void login(int mySockFd)
  907. {
  908.     int n, n2, m, trovato,cont=0;
  909.     int filedes;
  910.     char *res;
  911.     struct list *app;
  912.     char buffer[256], buffer2[256], username[256], password[256], confronta_passwd[256], confronta_user[256];
  913.     do
  914.     {
  915.         write(mySockFd, "\nVuoi [r]egistrarti, [a]ccedere o uscire[EXIT] ? ", 53);
  916.         bzero(buffer, 256);
  917.         n=read(mySockFd, buffer, 255);
  918.         if(n<0)
  919.         {
  920.             perror("Errore lettura da socket\n");
  921.             exit(1);
  922.         }
  923.         if(strcmp(buffer, "EXIT\n")==0)
  924.         {
  925.             printf("Mi disconnetto\n");
  926.             pthread_exit(NULL);
  927.         }
  928.         //SE VOGLIO REGISTRARMI
  929.         else if ( strcmp ( buffer , "r\n" ) == 0 )
  930.         {
  931.             pthread_mutex_lock(&semaforo);
  932.             filedes=open("accesso.txt", O_RDWR | O_CREAT);
  933.             lseek(filedes, 0, SEEK_END);
  934.             //ACQUISIZIONE USERNAME
  935.             do
  936.             {
  937.                 write(mySockFd, "[*]Username:", 12);
  938.                 bzero(username, 256);
  939.                 n=read(mySockFd, username, 255);
  940.                 if(n<0)
  941.                 {
  942.                     perror ("Errore lettura socket\n");
  943.                     exit(1);
  944.                 }
  945.                 trovato=checkUsername(username) ; //CONTROLLO CHE L'USERNAME NON SIA GIA' IN USO
  946.                 if(trovato==1)
  947.                 {
  948.                     bzero(buffer,256);
  949.                     strcpy(buffer, "\nUsername gia'  in uso. Premi un tasto per provarne un altro!");
  950.                     write(mySockFd, buffer, strlen(buffer));
  951.                     read(mySockFd, buffer, 256);
  952.                 }
  953.             }while(trovato==1);
  954.             n=write(filedes, username, strlen(username));
  955.             // ACQUISIZIONE PASSWORD
  956.             write(mySockFd, "[*]Password:", 12);
  957.             bzero(password, 256);
  958.             n=read(mySockFd, password, 255);
  959.             if(n<0)
  960.             {
  961.                 perror("Errore lettura socket\n");
  962.                 exit(1);
  963.             }
  964.             n=write(filedes, password, strlen(password));
  965.             bzero(buffer, 256);
  966.             strcpy(buffer, "*** Registrazione effettuata con SUCCESSO! ***\nPremere un tasto per continuare");
  967.             write(mySockFd, buffer, strlen(buffer));
  968.             bzero(buffer, 256);
  969.             read(mySockFd, buffer, 256);
  970.             close(filedes);
  971.             pthread_mutex_unlock(&semaforo);    //SBLOCCO SEMAFORO
  972.            
  973.         }
  974.         else if(strcmp(buffer, "a\n")==0)
  975.         {
  976.            
  977.             n = write ( mySockFd , "Username:" , 12 ) ;
  978.             if ( n < 0 )
  979.             {
  980.                 perror ("Errore scrittura socket");
  981.                 exit (1);
  982.             }
  983.             bzero ( username , 256 );
  984.             n=read(mySockFd, username, 255);
  985.             if(n< 0)
  986.             {
  987.                 perror ( "Errore lettura socket\n");
  988.                 exit (1);
  989.             }
  990.             for(app=players;app;app=app->next)
  991.             {
  992.                 if(strcmp(app->name,username)==0)
  993.                 break;  
  994.             }
  995.             if(app)
  996.                 cont=2;
  997.             else
  998.             {  
  999.                 n=write(mySockFd, "Password:", 12);
  1000.                 if(n<0)
  1001.                 {
  1002.                     perror("Errore scrittura socket");
  1003.                     exit(1);
  1004.                 }
  1005.                 bzero(password, 256);
  1006.                 n=read(mySockFd, password, 255);
  1007.                 if(n<0)
  1008.                 {
  1009.                     perror("Errore lettura socket\n");
  1010.                     exit(1);
  1011.                 }
  1012.                 pthread_mutex_lock(&semaforo);
  1013.                 filedes=open("accesso.txt", O_RDWR | O_CREAT);
  1014.                 lseek(filedes, 0, SEEK_SET);
  1015.                 int i=0, j=0;
  1016.                 bzero(confronta_user, 256);
  1017.                 bzero(confronta_passwd, 256);
  1018.                 while(n=read(filedes, buffer, 1)>0)
  1019.                 {
  1020.                     if(buffer[0]!='\n')
  1021.                     {   // PRENDO L'USERNAME DAL FILE ACCESSO
  1022.                         confronta_user[i]=buffer[0];
  1023.                         i+=1;
  1024.                     }
  1025.                     else
  1026.                     {
  1027.                         confronta_user[i]='\n';
  1028.                         if(strcmp(confronta_user, username)==0)
  1029.                         {
  1030.                             while (m=read(filedes, buffer2, 1)>0)
  1031.                             {   // PRENDO LA PASSWORD DELL'USERNAME TROVATO
  1032.                                 if(buffer2[0]!='\n')
  1033.                                 {
  1034.                                     confronta_passwd[j]=buffer2[0];
  1035.                                     j+=1;
  1036.                                 }
  1037.                                 else
  1038.                                 {
  1039.                                     confronta_passwd[j]='\n';
  1040.                                     if(strcmp(confronta_passwd,password)!=0) //LA PASSWORD NON COINCIDE
  1041.                                     break;
  1042.                                     else if(strcmp(confronta_passwd, password)==0)
  1043.                                     {
  1044.                                         //LA PASSWORD COINCIDE
  1045.                                         onlineUsers(username);
  1046.                                         cont = 1 ;
  1047.                                         break;
  1048.                                     }
  1049.                                 }
  1050.                             }
  1051.                             break;
  1052.                         }
  1053.                         else
  1054.                         {
  1055.                             i=0;
  1056.                             bzero(confronta_user, 256);
  1057.                         }
  1058.                     }
  1059.                 }
  1060.                 close ( filedes ) ;
  1061.                 pthread_mutex_unlock (&semaforo);
  1062.             }
  1063.             if(cont==0)
  1064.             {
  1065.                 // NOME UTENTE E PASSWORD NON SONO VALIDI
  1066.                 n=write(mySockFd, "Credenziali invalide! Premere un tasto per continuare", 53);
  1067.                if(n<0)
  1068.                {
  1069.                     perror("Errore scrittura socket");
  1070.                     exit(1);
  1071.                 }
  1072.                 bzero(buffer, 256);
  1073.                 n=read(mySockFd, buffer, 255);
  1074.                 if(n<0)
  1075.                 {
  1076.                     perror("Errore lettura da socket");
  1077.                     exit(1);
  1078.                 }
  1079.            }
  1080.            else if(cont==2)
  1081.            {
  1082.                  // UN UTENTE CON NOME IMMESSO E' GIA' IN PARTITA
  1083.                 n=write(mySockFd, "Utente gia' in gioco con questo Username! Premere un tasto per continuare", 72);
  1084.                if(n<0)
  1085.                {
  1086.                     perror("Errore scrittura socket");
  1087.                     exit(1);
  1088.                 }
  1089.                 bzero(buffer, 256);
  1090.                 n=read(mySockFd, buffer, 255);
  1091.                 if(n<0)
  1092.                 {
  1093.                     perror("Errore lettura da socket");
  1094.                     exit(1);
  1095.                 }
  1096.            
  1097.            }
  1098.            else
  1099.            {    // NOME UTENTE E PASSWORD CORRETTE
  1100.                 n=write(mySockFd, "*** Login effettuato con successo! ***\nPremere un tasto per continuare", 70);
  1101.                 if(n<0)
  1102.                 {
  1103.                     perror("Errore scrittura socket");
  1104.                     exit(1);
  1105.                 }
  1106.                 bzero(buffer, 256);
  1107.                 n=read(mySockFd, buffer, 255);
  1108.                 if(n<0)
  1109.                 {
  1110.                     perror("Errore lettura da socket");
  1111.                     exit(1);
  1112.                 }
  1113.                 updateFile(username);
  1114.                 printf("\n %s \n",username);
  1115.                
  1116.                 home(mySockFd, username ) ;
  1117.            }
  1118.         }
  1119.     }while(n>=0);
  1120. }
  1121.  
  1122. //FUNZIONE INIZIALE PER LA GESTIONE DEI THREAD
  1123. void * manageThread(void * arg)
  1124. {
  1125.   int sd=*(int*)arg;
  1126.   login(sd);
  1127.   return;
  1128. }
  1129.  
  1130. int main(int argc, char **argv)
  1131. {
  1132.     srand((unsigned)time(NULL));
  1133.     if(argc<2)
  1134.     {
  1135.         perror("Errore parametri\n");
  1136.         exit(1);
  1137.     }
  1138.     int porta=atoi(argv[1]);
  1139.     int listen_sd, connect_sd;
  1140.     pthread_t tid;
  1141.     int *thread_sd;
  1142.     struct sockaddr_in my_addr, client_addr;
  1143.     socklen_t client_len;
  1144.     my_addr.sin_family=AF_INET;
  1145.     my_addr.sin_addr.s_addr=INADDR_ANY;
  1146.  
  1147.     if((listen_sd=socket(AF_INET, SOCK_STREAM, IPPROTO_TCP))<0)
  1148.     {
  1149.         perror("socket");
  1150.         exit(1);
  1151.     }
  1152.     unlink(SOCKET_NAME);
  1153.     my_addr.sin_port=htons(porta);
  1154.     //EFFETTUO OPERAZIONE DI BINDING
  1155.     if(bind(listen_sd, (struct sockaddr *)&my_addr, sizeof(my_addr))<0)
  1156.     {
  1157.         perror("bind");
  1158.         exit(1);
  1159.     }
  1160.     //SETTO IL SOCKET CREATO IN STATO DI LISTEN
  1161.     if(listen(listen_sd, 1)<0)
  1162.     {
  1163.         perror("listen");
  1164.         exit(1);
  1165.     }
  1166.     //GENERO LA MAPPA DI GIOCO
  1167.     firstClue=createMap(map,sugg); 
  1168.     time(&start);
  1169.     while(1)
  1170.     {
  1171.         client_len = sizeof(client_addr);
  1172.         if((connect_sd=accept(listen_sd, (struct sockaddr *)&client_addr, &client_len))<0)
  1173.         {
  1174.             perror("accept");
  1175.             exit(1);
  1176.         }
  1177.         thread_sd=(int*)malloc(sizeof(int));
  1178.         fprintf(stderr, " new connection \n");
  1179.         *thread_sd = connect_sd;
  1180.         printf("server: new connection from %d \n",connect_sd);
  1181.         pthread_create(&tid, NULL, manageThread, (void *) thread_sd);
  1182.     }
  1183.     close(connect_sd);
  1184.     close(listen_sd);
  1185.     return 0;
  1186. }
Add Comment
Please, Sign In to add comment