Advertisement
Guest User

Untitled

a guest
Mar 20th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.33 KB | None | 0 0
  1.  
  2. public class TipoPersona {
  3.  
  4.     private String user;
  5.     private String pass;
  6.     private int punteggio;
  7.    
  8.     public TipoPersona(String u, String p, int pu) {
  9.         user= u;
  10.         pass= p;
  11.         punteggio= pu;
  12.     }
  13.    
  14.     public TipoPersona (String u, String p) {
  15.         user= u;
  16.         pass= p;
  17.     }
  18.     public String getUsername() {
  19.         return this.user;
  20.     }
  21.     public String getPassword() {
  22.         return this.pass;
  23.     }
  24.     public int getPunteggio() {
  25.         return this.punteggio;
  26.     }
  27.     public void setPunteggioSeMaggiore(int p) {
  28.         if ( p> punteggio)
  29.                 punteggio = p;
  30.     }
  31. }
  32.  
  33.  
  34.  
  35.  
  36. import java.io.IOException;
  37. import java.net.ServerSocket;
  38. import java.net.Socket;
  39. import java.util.ArrayList;
  40.  
  41. public class Server {
  42.  
  43.     private final static int PORT=3000;
  44.    
  45.     //private final static String address="localhost";
  46.    
  47.  
  48.     private ServerSocket serversocket;
  49.     private ArrayList <ClientConnection> connessioni;
  50.     private ArrayList <TipoPersona> giocatori;
  51.    
  52.    
  53.     public Server() {
  54.        
  55.         System.out.println("Server avviato");
  56.         try
  57.         {
  58.             this.connessioni= new ArrayList <ClientConnection>();
  59.             this.giocatori= new ArrayList <TipoPersona>();
  60.             esegui();
  61.             System.out.println("ei1");
  62.         }
  63.         catch(Exception e)
  64.         {
  65.             System.out.println("Exception: "+e);
  66.             e.printStackTrace();
  67.         }
  68.         finally
  69.         {
  70.             // Always close it:
  71.             try {
  72.                 serversocket.close();
  73.             } catch(IOException e) {
  74.                 System.err.println("Socket not closed");
  75.             }
  76.         }
  77.  
  78.     }
  79.    
  80.     private void esegui() {
  81.        
  82.         ascolto();
  83.         System.out.println("ei2");
  84.        
  85.     }
  86.    
  87.     private void ascolto() {
  88.        
  89.         try {
  90.             System.out.println("ei3a");
  91.             serversocket = new ServerSocket(PORT);
  92.             System.out.println("ei3");
  93.             while (true) {
  94.                 System.out.println("ei4");
  95.                 Socket socket = serversocket.accept();
  96.                 System.out.println("fatto");
  97.                 ClientConnection clientconnection = new ClientConnection (socket, giocatori);
  98.                
  99.                 clientconnection.start();
  100.                 this.connessioni.add(clientconnection);
  101.                 System.out.println("Sto aggiungendo" + connessioni.size() + " elemento" );
  102.                                
  103.             }
  104.         }
  105.         catch (Exception e) {
  106.             System.out.println(e);
  107.             try
  108.             {
  109.                 serversocket.close();
  110.             }
  111.             catch(Exception ex)
  112.             {}
  113.  
  114.            
  115.     }
  116.   }
  117.    
  118.     public ArrayList<ClientConnection> getConnessioni() {
  119.         return connessioni;
  120.     }
  121.  
  122.     public ArrayList<TipoPersona> getGiocatori() {
  123.         return giocatori;
  124.     }
  125.  
  126.     public static void main(String[] args) throws IOException, InterruptedException
  127.     {
  128.         new Server();
  129.        
  130.     }
  131. }
  132.  
  133.  
  134.  
  135. import java.io.BufferedReader;
  136. import java.io.DataOutputStream;
  137. import java.io.IOException;
  138. import java.io.InputStreamReader;
  139. import java.net.Socket;
  140. import java.util.List;
  141.  
  142.  
  143. public class ClientConnection extends Thread {
  144.  
  145.     private List<TipoPersona> giocatori;
  146.     private Socket socket;
  147.     private BufferedReader reader;
  148.     private DataOutputStream writer;
  149.     private int id;
  150.  
  151.     public ClientConnection (Socket clientsocket, List<TipoPersona> giocatori) {
  152.  
  153.  
  154.         this.giocatori = giocatori;
  155.         this.socket= clientsocket;
  156.         try {
  157.  
  158.             this.reader = new BufferedReader (new InputStreamReader (this.socket.getInputStream()));
  159.             this.writer = new DataOutputStream (this.socket.getOutputStream());
  160.  
  161.         }catch (IOException e) {
  162.  
  163.         }
  164.     }
  165.  
  166.     public String readLine() {
  167.         String line = null;
  168.         try {
  169.             line = this.reader.readLine();
  170.  
  171.         }catch (IOException e) {
  172.  
  173.         }
  174.         return line;
  175.     }
  176.     public void writeLine(String line) {
  177.  
  178.         try {
  179.             this.writer.writeBytes(line + "\n");
  180.         }
  181.         catch(IOException e) {
  182.         }
  183.  
  184.     }
  185.  
  186.  
  187.     public void run() {
  188.  
  189.         try {
  190.  
  191.             login();
  192.             gioca();
  193.             chiudi();
  194.         }
  195.  
  196.         catch(Exception e)
  197.         {
  198.             System.out.println("Exception: "+e);
  199.             e.printStackTrace();
  200.         }
  201.         finally
  202.         {
  203.             // Always close it:
  204.             try {
  205.                 socket.close();
  206.             } catch(IOException e) {
  207.                 System.err.println("Socket not closed");
  208.             }
  209.         }
  210.  
  211.     }
  212.  
  213.     public void login() {
  214.  
  215.        
  216.         System.out.println("eila");
  217.         boolean loggato = false;
  218.         while (!loggato) {
  219.            
  220.             readLine();
  221.             //attenzione a inSocket = reader
  222.             String username = readLine();
  223.             System.out.println("username: " + username);
  224.  
  225.             id = 0;
  226.             int i=0;
  227.             boolean trovato = false;
  228.             while(i< giocatori.size() && !trovato) {
  229.                 if (giocatori.get(i).getUsername().equals(username)==true) 
  230.                     trovato = true;
  231.                 i++;
  232.             }
  233.             i--;
  234.             if (trovato == true) {
  235.                 String password = readLine();
  236.                 if((giocatori.get(i).getPassword().equals(password))==true) {
  237.                     trovato=true;
  238.                     id = i;
  239.                     System.out.println("giocatore trovato");
  240.                     writeLine(Boolean.toString(trovato));
  241.                     loggato= true;
  242.                 }
  243.                 else
  244.                     writeLine(Boolean.toString(false));
  245.             }
  246.             else {
  247.                 String password = readLine();
  248.                 System.out.println("password: " + password);
  249.                 TipoPersona p = new TipoPersona(username, password,0);
  250.                 giocatori.add(p);
  251.                 id = giocatori.size()-1;
  252.                 trovato = true;
  253.                 System.out.println("Giocatore aggiunto");
  254.                 writeLine(Boolean.toString(trovato));
  255.                 loggato = true;
  256.             }
  257.         }
  258.     }
  259.  
  260.     public void gioca() {
  261.  
  262.         System.out.println("mmm");
  263.         while(true) {
  264.            
  265.             String scelta = readLine();
  266.             System.out.println(scelta);
  267.             if (scelta.equals( "partita" ) ) {
  268.                 partita();
  269.             }
  270.             else if(scelta.equals("record")) {
  271.                 System.out.println("sending records");
  272.                 vediRecord();
  273.             }
  274.             else {
  275.                 chiudi();
  276.                 break;
  277.             }
  278.         }  
  279.     }
  280.    
  281.     public void partita() {
  282.        
  283.         boolean continua = true;
  284.         int i=0;
  285.         while(continua == true) {
  286.            
  287.             i++;
  288.             String sequenza=generaSequenza(i);
  289.             System.out.println("Inviata a client la sequenza: "+sequenza);
  290.             writeLine(sequenza);
  291.             String seqricevuta= readLine();
  292.             if (sequenza.equals(seqricevuta)) {
  293.                
  294.                 writeLine("true");
  295.                 System.out.println("sequenza corretta");
  296.             }
  297.             else {
  298.                 writeLine("false");
  299.                 System.out.println("sequenza non corretta");
  300.                 i = i-1;
  301.                 giocatori.get(id).setPunteggioSeMaggiore(i);
  302.                 continua = false;
  303.             }
  304.         }
  305.        
  306.     }
  307.    
  308.     private String generaSequenza(int i)
  309.     {
  310.         String result=new String();
  311.        
  312.         for(int j=0;j<i;j++)
  313.         {
  314.             int num=(int)(Math.random()*10);
  315.            
  316.             if(num==10)
  317.                 num=0;
  318.            
  319.             result+=num;
  320.         }
  321.        
  322.         return result;
  323.     }
  324.    
  325.    
  326.     public void vediRecord() {
  327.  
  328.         Integer ngioc = (giocatori.size());
  329.         writeLine( ngioc.toString() );
  330.  
  331.         for (int i=0; i< giocatori.size(); i++) {
  332.             String record = giocatori.get(i).getUsername();
  333.             Integer punt = giocatori.get(i).getPunteggio();
  334.             writeLine(record + punt);
  335.            
  336.         }
  337.  
  338.     }
  339.  
  340.     public void chiudi() { 
  341.  
  342.         try
  343.         {
  344.             socket.close();
  345.         }
  346.         catch(Exception e)
  347.         {
  348.             System.out.println("Exception: "+e);
  349.             e.printStackTrace();
  350.         }
  351.         finally
  352.         {
  353.             // Always close it:
  354.             try
  355.             {
  356.                 socket.close();
  357.             }
  358.             catch(IOException ex)
  359.             {
  360.                 System.err.println("Socket not closed");
  361.             }
  362.         }
  363.  
  364.     }
  365. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement