Ejemplo n.º 1
0
        private void loadResultados(TextView results, TextView media, TextView ganador)
        {
            string user = (string)Intent.Extras.Get("USUARIO");

            model.Resultado[]       resultados         = ServerConnection.resultadosExperimento(user);
            IList <model.Resultado> resultadosFiltered = new List <model.Resultado>();

            //Filtramos los de la ronda actual
            foreach (model.Resultado r in resultados)
            {
                if (r.Ronda == Convert.ToInt32(Intent.Extras.Get("RONDA")))
                {
                    resultadosFiltered.Add(r);
                }
            }
            //Calculos
            float mediaResultados = 0;

            model.Resultado mejorResultado = new model.Resultado();
            foreach (model.Resultado r in resultadosFiltered)
            {
                mediaResultados += r.ValorNumerico;
            }
            mediaResultados /= resultados.Length;

            float diff = 1000; //Numero muy grande

            foreach (model.Resultado r in resultadosFiltered)
            {
                if (diff > Math.Abs(r.ValorNumerico - mediaResultados))
                {
                    diff           = Math.Abs(r.ValorNumerico - mediaResultados);
                    mejorResultado = r;
                }
            }

            //Set resultados en la interfaz
            results.Text = "Resultados: " + resultados.Length;
            media.Text   = "Media: " + mediaResultados;
            ganador.Text = "Ganador: " + mejorResultado.Usuario + ", Valor: " + mejorResultado.ValorNumerico;
        }
Ejemplo n.º 2
0
        public static model.Resultado[] resultadosExperimento(string usuario)
        {
            //Creacion del socket
            IPAddress  ip           = IPAddress.Parse(ipServer);
            IPEndPoint endPoint     = new IPEndPoint(ip, portAdmin);
            Socket     clientSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            model.Resultado[] resultados;

            try
            {
                //Conectamos al servidor
                clientSocket.Connect(endPoint);

                //Trabajamos con el socket
                clientSocket.Send(BitConverter.GetBytes(5)); //Codigo de la operacion

                //Enviamos
                clientSocket.Send(BitConverter.GetBytes(usuario.Length));        //Longitud del string
                clientSocket.Send(System.Text.Encoding.ASCII.GetBytes(usuario)); //Username

                //Recibimos
                byte[] buffer = new byte[4];
                byte[] bufferString;

                clientSocket.Receive(buffer);
                int longitud = bytesToInt(buffer);

                resultados = new model.Resultado[longitud];
                model.Resultado resultado;
                for (int i = 0; i < longitud; i++)
                {
                    resultado = new model.Resultado();
                    clientSocket.Receive(buffer);
                    resultado.Grupo = bytesToInt(buffer);

                    clientSocket.Receive(buffer);
                    resultado.Ronda = bytesToInt(buffer);

                    clientSocket.Receive(buffer);
                    int longString = bytesToInt(buffer);
                    bufferString = new byte[longString];
                    clientSocket.Receive(bufferString);
                    resultado.Usuario = System.Text.Encoding.ASCII.GetString(bufferString);

                    clientSocket.Receive(buffer);
                    longString   = bytesToInt(buffer);
                    bufferString = new byte[longString];
                    clientSocket.Receive(bufferString);
                    resultado.Etiqueta = System.Text.Encoding.ASCII.GetString(bufferString);

                    clientSocket.Receive(buffer);
                    longString   = bytesToInt(buffer);
                    bufferString = new byte[longString];
                    clientSocket.Receive(bufferString);
                    resultado.ValorTexto = System.Text.Encoding.ASCII.GetString(bufferString);

                    clientSocket.Receive(buffer);
                    resultado.ValorNumerico = bytesToFloat(buffer);

                    resultados[i] = resultado;
                }
                //Cerramos la conexion
                clientSocket.Shutdown(SocketShutdown.Both);
                clientSocket.Close();

                return(resultados);
            }
            catch (Exception) { }

            return(null);
        }