Exemplo n.º 1
0
        public static void Handle(string[] Response, NetworkStream ns)
        {
            string Produs   = Response[1];
            int    Distanta = int.Parse(Response[2]);
            string Username = Response[3];

            string UserPosition = string.Empty;

            using (SqlConnection Database = new SqlConnection(_Details.DatabaseConnectionString))
            {
                Database.Open();

                SqlCommand cmd = new SqlCommand("SELECT [LastKnowPosition] FROM Utilizatori WHERE [NumarTelefon]=@UserPhone", Database);
                cmd.Parameters.AddWithValue("UserPhone", Username);

                SqlDataReader Reader = cmd.ExecuteReader();

                if (Reader.Read())
                {
                    UserPosition = Reader[0] as string;
                }

                Reader.Dispose();

                cmd    = new SqlCommand("SELECT [Locatie],[Id_Magazin] FROM BeSmart_Magazine", Database);
                Reader = cmd.ExecuteReader();

                LatitudeLongitude DistanceCalculator = new LatitudeLongitude(UserPosition.Split(',')[0].Replace('.', ','), UserPosition.Split(',')[1].Replace('.', ','));

                List <string> ListaProduse    = new List <string>();
                List <string> PozitieProduse  = new List <string>();
                List <string> DistantaProduse = new List <string>();
                List <string> PretProduse     = new List <string>();

                while (Reader.Read())
                {
                    string PozitieMagazin = Reader[0] as string;
                    string dist           = DistanceCalculator.GetDistance(new LatitudeLongitude(PozitieMagazin.Split(',')[0].Replace('.', ','), PozitieMagazin.Split(',')[1].Replace('.', ',')));

                    if (int.Parse(dist) < Distanta)
                    {
                        using (SqlConnection Database2 = new SqlConnection(_Details.DatabaseConnectionString))
                        {
                            Database2.Open();

                            SqlCommand cmd2 = new SqlCommand("SELECT * FROM BeSmart_Produse WHERE [Id_Magazin]=@IdMagazin", Database2);
                            cmd2.Parameters.AddWithValue("IdMagazin", Reader[1] as int?);
                            SqlDataReader Reader2 = cmd2.ExecuteReader();

                            while (Reader2.Read())
                            {
                                string ActualProd = Reader2[4] as string;

                                if (ActualProd.Contains(Produs))
                                {
                                    ListaProduse.Add(ActualProd);
                                    PretProduse.Add(Reader2[5] as string + " lei");
                                    DistantaProduse.Add(dist + "m");
                                    PozitieProduse.Add(PozitieMagazin);
                                }
                            }
                        }
                    }
                }

                _Utils.WriteStreamString(ns, CryptDecryptData.CryptData(ListaProduse.ToArray()));
                _Utils.WriteStreamString(ns, CryptDecryptData.CryptData(PretProduse.ToArray()));
                _Utils.WriteStreamString(ns, CryptDecryptData.CryptData(DistantaProduse.ToArray()));
                _Utils.WriteStreamString(ns, CryptDecryptData.CryptData(PozitieProduse.ToArray()));
            }
        }
Exemplo n.º 2
0
 public void Send(string[] Messages)
 {
     _Utils.WriteStreamString(Client.ns, CryptDecryptData.CryptData(Messages));
 }
Exemplo n.º 3
0
 public void Send(string Message)
 {
     _Utils.WriteStreamString(Client.ns, CryptDecryptData.CryptData(new string[] { Message }));
 }
Exemplo n.º 4
0
        public GameLayer(Activity context, float Size_X, float Size_Y, string IntentType, string NrTel, string GameId)
            : base(CCColor4B.Black)
        {
            this.context = context;

            this.Size_X     = Size_X;
            this.Size_Y     = Size_Y;
            this.IntentType = IntentType;
            GetDataThread   = new Thread(GetData);

            float offset = Size_Y / 15;

            X_Y_Table             = new CCSprite("X_O_Table");
            X_Y_Table.AnchorPoint = CCPoint.AnchorUpperLeft;
            X_Y_Table.PositionX   = offset;
            X_Y_Table.PositionY   = Size_Y - offset;
            X_Y_Table.ContentSize = new CCSize(Size_Y - 2 * offset, Size_Y - 2 * offset);

            Score             = new CCLabel("Score", "Fonts/MarkerFelt", 200, CCLabelFormat.SystemFont);
            Score.AnchorPoint = CCPoint.AnchorUpperRight;
            Score.PositionX   = Size_X - 100;
            Score.PositionY   = Size_Y - 100;

            Player1             = new CCLabel("You : 0", "Fonts/MarkerFelt", 100, CCLabelFormat.SystemFont);
            Player1.AnchorPoint = CCPoint.AnchorUpperLeft;
            Player1.PositionX   = Score.PositionX - Score.ContentSize.Width;
            Player1.PositionY   = Score.PositionY - 100;

            Player2             = new CCLabel("Player 2 : 0", "Fonts/MarkerFelt", 100, CCLabelFormat.SystemFont);
            Player2.AnchorPoint = CCPoint.AnchorUpperLeft;
            Player2.PositionX   = Score.PositionX - Score.ContentSize.Width;
            Player2.PositionY   = Player1.PositionY - 50;

            CreateTouchArea();

            if (IntentType == "CREATE")
            {
                TcpClient     client = new TcpClient(_Details.ServerIP, _Details.GamesHostPort);
                NetworkStream ns     = client.GetStream();
                Client = new Client(client, ns);

                _TcpDataExchange.WriteStreamString(ns, CryptDecryptData.CryptData(new string[] { _Details.Game_XandO, "CREATE", Utilizator_Trip.Me.NumarTelefon, NrTel }));
                string Response = CryptDecryptData.DecryptData(_TcpDataExchange.ReadStreamString(ns))[0];

                if (Response == "1")
                {
                    AddChild(Player1);
                    AddChild(Player2);
                    AddChild(Score);
                    AddChild(X_Y_Table);

                    MyTurn     = true;
                    PlayerType = PlayerType.X;
                    GetDataThread.Start();
                }
                else
                {
                    context.Finish();
                }
            }
            else
            {
                TcpClient     client = new TcpClient(_Details.ServerIP, _Details.GamesHostPort);
                NetworkStream ns     = client.GetStream();
                Client = new Client(client, ns);

                _TcpDataExchange.WriteStreamString(ns, CryptDecryptData.CryptData(new string[] { _Details.Game_XandO, "JOIN", GameId, NrTel }));
                string Response = CryptDecryptData.DecryptData(_TcpDataExchange.ReadStreamString(ns))[0];

                if (Response == "1")
                {
                    AddChild(Player1);
                    AddChild(Player2);
                    AddChild(Score);
                    AddChild(X_Y_Table);

                    GetDataThread.Start();
                    PlayerType = PlayerType.O;
                    MyTurn     = false;
                }
                else
                {
                    context.Finish();
                }
            }

            X_Clone = new CCSprite("X");
            O_Clone = new CCSprite("O");
            X_Win   = new CCSprite("X_Win");
            O_Win   = new CCSprite("O_Win");
        }
Exemplo n.º 5
0
        public static void Handle(TcpClient Client, NetworkStream ns, string[] Messages, LocationService LocationService, NotificationService NotificationService, List <Excursie> Excursii)
        {
            //1.Destinatia principala
            //2.Tipul participantilor
            //3.Data plecare
            //4.Data intoarcere
            //5.Locatie plecare
            //6.Ora plecare
            //7.Observatii
            //8.Nume organizator
            //9.Numar telefon

            string
                DestinatiePrincipala = Messages[1],
                TipulParticipantilor = Messages[2],
                DataPlecare          = Messages[3],
                DataIntoarcere       = Messages[4],
                LocatiePlecare       = Messages[5],
                OraPlecare           = Messages[6],
                Observatii           = Messages[7],
                NumeOrganizator      = Messages[8],
                NumarTelefon         = Messages[9];

            string UniqueId = GenerateUniqueTripId();
            string URL      = CreateTripURL(NumeOrganizator, NumarTelefon, UniqueId);

            string Response = (0).ToString();

            using (SqlConnection Database = new SqlConnection(_Details.DatabaseConnectionString))
            {
                Database.Open();

                SqlCommand cmd = new SqlCommand("SELECT [Nume],[Prenume] FROM Utilizatori WHERE [NumarTelefon]='" + NumarTelefon + "'", Database);
                {
                    using (SqlDataReader Reader = cmd.ExecuteReader())
                        if (Reader.Read())
                        {
                            NumeOrganizator = (string)Reader[0] + " " + (string)Reader[1];
                        }
                }

                cmd = new SqlCommand(@"INSERT INTO Excursii ([Destinatie Principala],[Tipul Participantilor],[Data Plecare],[Data Intoarcere],[Locatie Plecare],[Ora Plecare],[Observatii],[Nume Organizator],[Numar Telefon],[UniqueId],[URL]) " +
                                     "VALUES ('" + DestinatiePrincipala + "','" +
                                     TipulParticipantilor + "','" +
                                     DataPlecare + "','" +
                                     DataIntoarcere + "','" +
                                     LocatiePlecare + "','" +
                                     OraPlecare + "','" +
                                     Observatii + "','" +
                                     NumeOrganizator + "','" +
                                     NumarTelefon + "','" +
                                     UniqueId + "','" +
                                     URL + "')", Database);

                cmd.ExecuteNonQuery();

                Excursie Excursie = new Excursie(LocationService, NotificationService, DestinatiePrincipala, TipulParticipantilor, DataPlecare, DataIntoarcere, LocatiePlecare, OraPlecare, Observatii, NumeOrganizator, NumarTelefon, UniqueId, URL);
                Excursii.Add(Excursie);

                Response = (1).ToString();
            }
            _Utils.WriteStreamString(ns, CryptDecryptData.CryptData(new string[] { Response }));

            ns.Close();
            Client.Close();
        }
Exemplo n.º 6
0
        private void SendOrUpload(object newClient)
        {
            TcpClient     Client = (TcpClient)newClient;
            NetworkStream ns     = Client.GetStream();

            string[] Tag = CryptDecryptData.DecryptData(_Utils.ReadStreamString(ns));

            switch (Tag[0])
            {
            case _Details.GetGalleryPhotoByIndex:
            {
                string IdExcursie = Tag[1];
                int    PhotoIndex = int.Parse(Tag[2]);

                using (SqlConnection Database = new SqlConnection(_Details.DatabaseConnectionString))
                {
                    Database.Open();

                    SqlCommand    cmd    = new SqlCommand("SELECT [URL] FROM Excursii WHERE [UniqueId]='" + IdExcursie + "'", Database);
                    SqlDataReader Reader = cmd.ExecuteReader();

                    if (Reader.Read())
                    {
                        string[] PhotosPath = Directory.GetFiles(Reader[0] as string + @"\Galerie");

                        Reader.Close();

                        using (FileStream fs = File.Open(PhotosPath[PhotoIndex], FileMode.Open))
                        {
                            int PackSize    = 1000;
                            int TotalLength = (int)fs.Length;
                            int NoOfPackets = (int)Math.Ceiling((double)fs.Length / (double)PackSize);
                            int CurrentPackSize;

                            for (int i = 0; i < NoOfPackets; i++)
                            {
                                if (TotalLength > PackSize)
                                {
                                    CurrentPackSize = PackSize;
                                    TotalLength    -= CurrentPackSize;
                                }
                                else
                                {
                                    CurrentPackSize = TotalLength;
                                }

                                byte[] CurrentBytes = new byte[CurrentPackSize];
                                int    ReadedLength = fs.Read(CurrentBytes, 0, CurrentBytes.Length);

                                ns.Write(CurrentBytes, 0, ReadedLength);
                            }
                        }
                    }
                }

                Client.Close();
            } break;

            case _Details.GetGalleryCount:
            {
                string IdExcursie = Tag[1];

                using (SqlConnection Database = new SqlConnection(_Details.DatabaseConnectionString))
                {
                    Database.Open();

                    SqlCommand    cmd    = new SqlCommand("SELECT [URL] FROM Excursii WHERE [UniqueId]='" + IdExcursie + "'", Database);
                    SqlDataReader Reader = cmd.ExecuteReader();

                    if (Reader.Read())
                    {
                        string[] PhotosPath = Directory.GetFiles(Reader[0] as string + @"\Galerie");
                        _Utils.WriteStreamString(ns, CryptDecryptData.CryptData(new string[] { PhotosPath.Length.ToString() }));

                        Reader.Close();
                    }
                }

                Client.Close();
            } break;

            case _Details.AddPhotoToGallery:
            {
                string       IdExcursie = Tag[1];
                MemoryStream ms         = new MemoryStream();
                byte[]       Buffer     = new byte[1000];

                int ReadedBytes;
                while ((ReadedBytes = ns.Read(Buffer, 0, Buffer.Length)) > 0)
                {
                    ms.Write(Buffer, 0, ReadedBytes);
                }

                using (SqlConnection Database = new SqlConnection(_Details.DatabaseConnectionString))
                {
                    Database.Open();

                    SqlCommand    cmd    = new SqlCommand("SELECT [URL] FROM Excursii WHERE [UniqueId]='" + IdExcursie + "'", Database);
                    SqlDataReader Reader = cmd.ExecuteReader();

                    if (Reader.Read())
                    {
                        Image i       = Image.FromStream(ms);
                        Guid  newGuid = Guid.NewGuid();
                        i.Save((string)Reader[0] + @"\Galerie\" + newGuid.ToString() + ".png", System.Drawing.Imaging.ImageFormat.Png);

                        Reader.Close();
                    }
                }

                Client.Close();
            } break;

            case _Details.UpdateProfilePic:
            {
                string       NrTel  = Tag[1];
                MemoryStream ms     = new MemoryStream();
                byte[]       Buffer = new byte[1000];

                int ReadedBytes;
                while ((ReadedBytes = ns.Read(Buffer, 0, Buffer.Length)) > 0)
                {
                    ms.Write(Buffer, 0, ReadedBytes);
                }

                using (SqlConnection Database = new SqlConnection(_Details.DatabaseConnectionString))
                {
                    Database.Open();

                    SqlCommand    cmd    = new SqlCommand("SELECT [URL] FROM Utilizatori WHERE [NumarTelefon]='" + NrTel + "'", Database);
                    SqlDataReader Reader = cmd.ExecuteReader();

                    if (Reader.Read())
                    {
                        Image i = Image.FromStream(ms);
                        i.Save((string)Reader[0] + @"\Profile.png", System.Drawing.Imaging.ImageFormat.Png);

                        string LowSizePhoto = CreateLowSizePhoto.GetLowSizePhotoBase64((string)Reader[0] + @"\Profile.png");

                        Reader.Close();

                        cmd = new SqlCommand("UPDATE Utilizatori SET [LowSizePhoto]='" + LowSizePhoto + "' WHERE [NumarTelefon]='" + NrTel + "'", Database);
                        cmd.ExecuteNonQuery();
                    }
                }

                Client.Close();
            } break;

            case _Details.GetProfilePic:
            {
                string NrTel = Tag[1];

                using (SqlConnection Database = new SqlConnection(_Details.DatabaseConnectionString))
                {
                    Database.Open();

                    string t = "SELECT * FROM Utilizatori WHERE [NumarTelefon]='" + NrTel + "'";

                    SqlCommand    cmd    = new SqlCommand(t, Database);
                    SqlDataReader Reader = cmd.ExecuteReader();

                    if (Reader.Read())
                    {
                        string UserLowSizePhoto = (string)Reader[8];

                        using (MemoryStream ms = new MemoryStream(Convert.FromBase64String(UserLowSizePhoto)))
                        {
                            int PackSize    = 1000;
                            int TotalLength = (int)ms.Length;
                            int NoOfPackets = (int)Math.Ceiling((double)ms.Length / (double)PackSize);
                            int CurrentPackSize;

                            for (int i = 0; i < NoOfPackets; i++)
                            {
                                if (TotalLength > PackSize)
                                {
                                    CurrentPackSize = PackSize;
                                    TotalLength    -= CurrentPackSize;
                                }
                                else
                                {
                                    CurrentPackSize = TotalLength;
                                }

                                byte[] CurrentBytes = new byte[CurrentPackSize];
                                int    ReadedLength = ms.Read(CurrentBytes, 0, CurrentBytes.Length);

                                ns.Write(CurrentBytes, 0, ReadedLength);
                            }
                        }
                    }

                    Client.Close();
                }
            } break;

            case _Details.GetTripId:
            {
                string NrTel  = Tag[1];
                string TripId = "error";

                using (SqlConnection Database = new SqlConnection(_Details.DatabaseConnectionString))
                {
                    Database.Open();

                    SqlCommand    cmd    = new SqlCommand("SELECT [UniqueId] FROM Excursii WHERE [Numar Telefon]='" + NrTel + "'", Database);
                    SqlDataReader Reader = cmd.ExecuteReader();

                    if (Reader.Read())
                    {
                        TripId = (string)Reader[0];
                    }

                    _Utils.WriteStreamString(ns, CryptDecryptData.CryptData(new string[] { TripId }));

                    ns.Dispose();
                    Client.Close();
                }
            } break;

            case _Details.GetUserName:
            {
                string NrTel          = Tag[1];
                string NumeUtilizator = "error";

                using (SqlConnection Database = new SqlConnection(_Details.DatabaseConnectionString))
                {
                    Database.Open();

                    SqlCommand    cmd    = new SqlCommand("SELECT [Nume],[Prenume] FROM Utilizatori WHERE [NumarTelefon]='" + NrTel + "'", Database);
                    SqlDataReader Reader = cmd.ExecuteReader();

                    if (Reader.Read())
                    {
                        NumeUtilizator = (string)Reader[0] + " " + (string)Reader[1];
                    }

                    _Utils.WriteStreamString(ns, CryptDecryptData.CryptData(new string[] { NumeUtilizator }));

                    ns.Dispose();
                    Client.Close();
                }
            } break;

            case _Details.NewQuestionPool:
            {
                string NrTel  = Tag[1];
                string TripId = Tag[2];

                foreach (Excursie e in Excursii)
                {
                    if (TripId.Equals(e.UniqueId))
                    {
                        e.QuestionPools.Add(new QuestionPool(e, Tag));
                    }
                }
            } break;

            case _Details.VoteQuestionPool:
            {
                string TripId = Tag[1];
                int    index  = int.Parse(Tag[2]);
                string Id     = Tag[3];

                foreach (Excursie e in Excursii)
                {
                    if (TripId.Equals(e.UniqueId))
                    {
                        foreach (QuestionPool q in e.QuestionPools)
                        {
                            if (q.Id.Equals(Id))
                            {
                                q.Vote(index);
                            }
                        }
                    }
                }

                ns.Dispose();
                Client.Close();
            } break;

            case _Details.RequestVotesQuestionPool:
            {
                string TripId = Tag[1];

                string[] Intrebari = null;
                string[] Ids       = null;

                foreach (Excursie e in Excursii)
                {
                    if (TripId.Equals(e.UniqueId))
                    {
                        Intrebari = new string[e.QuestionPools.Count];
                        Ids       = new string[e.QuestionPools.Count];

                        for (int i = 0; i < Intrebari.Length; i++)
                        {
                            Intrebari[i] = e.QuestionPools[i].Intrebare;
                            Ids[i]       = e.QuestionPools[i].Id;
                        }
                    }
                }

                _Utils.WriteStreamString(ns, CryptDecryptData.CryptData(new string[] { string.Join(",", Intrebari), string.Join(",", Ids) }));

                //string Id = CryptDecryptData.DecryptData(_Utils.ReadStreamString(ns))[0];

                foreach (Excursie e in Excursii)
                {
                    if (TripId.Equals(e.UniqueId))
                    {
                        foreach (QuestionPool q in e.QuestionPools)
                        {
                            string[] Votes = new string[q.Variante.Length];

                            for (int i = 0; i < q.Variante.Length; i++)
                            {
                                Votes[i] = q.Variante[i] + "," + q.GetPercentageOf(i);
                            }

                            _Utils.WriteStreamString(ns, CryptDecryptData.CryptData(Votes));
                        }
                    }
                }

                ns.Dispose();
                Client.Close();
            } break;

            case _Details.BeSmartSimpleQuery:
            {
                HandleBeSmartClient.Handle(Tag, ns);

                ns.Dispose();
                Client.Close();
            } break;
            }
        }