Пример #1
0
        // Resgister a new client to database
        public static bool RegisterClient(Packets.ProfilePacket packet,
                                          IPEndPoint ip,
                                          ref Models.Client c,
                                          out string message)
        {
            try
            {
                // Some checks before registering the user
                if (!packet.Alea.Equals("0000000"))
                {
                    message = "Alea must be 7 0s";
                    return(false);
                }

                // Declaring the new client
                c = new Models.Client()
                {
                    FirstName   = packet.FirstName,
                    LastName    = packet.LastName,
                    Age         = packet.Age,
                    PhoneNumber = packet.PhoneNumber,
                    Gender      = packet.Gender,
                    Username    = packet.Username,
                    Password    = packet.Password,
                    Email       = packet.Email,
                    Ip          = ip.Address.GetAddressBytes(),
                };

                // Open database and check if client already exists
                using (Models.ServerDatabase db = new Models.ServerDatabase())
                {
                    if (db.Clients.Where(a => a.Username == packet.Username).Count() != 0)
                    {
                        message = "Username " + c.Username + " already exists.";
                        return(false);
                    }
                    db.Clients.InsertOnSubmit(c);
                    db.SubmitChanges();
                }
                message = "Cool!";
                return(true);
            }
            catch (Exception e)
            {
                message = "Exception thrown: " + e.ToString();
                return(false);
            }
        }
Пример #2
0
        public static bool UpdateProfile(Packets.ProfilePacket packet, ref ClientStatus current, out string message)
        {
            try
            {
                if (CheckBasics(current, ClientStatus.Status.Disconnected, packet.Alea, out message))
                {
                    using (var db = new Models.ServerDatabase())
                    {
                        // Check fields that can be changed
                        Models.Client c = db.Clients.Single(r => r.Username == packet.Username);

                        if (c.FirstName != packet.FirstName)
                        {
                            message = "Update Profile Error: First Name can't be updated";
                            return(false);
                        }

                        if (c.LastName != packet.LastName)
                        {
                            message = "Update Profile Error: Last Name can't be updated";
                            return(false);
                        }

                        if (c.Age != packet.Age)
                        {
                            message = "Update Profile Error: Age can't be updated";
                            return(false);
                        }

                        if (c.Gender != packet.Gender)
                        {
                            message = "Update Profile Error: Gender can't be updated";
                            return(false);
                        }

                        // update the profile
                        c.Password    = packet.Password;
                        c.PhoneNumber = packet.PhoneNumber;
                        c.Email       = packet.Email;


                        // Set new reference to current
                        current.Client = c;

                        db.SubmitChanges();
                    }

                    message = "Cool!";
                    return(true);
                }
                else
                {
                    message = "Update Profile Error: " + message;
                    return(false);
                }
            }
            catch (SqlException e)
            {
                message = e.ToString();
                return(false);
            }
        }