Пример #1
0
        public static bool AckOrRegContactReq(Packets.ContactReqPacket packet,
                                              ClientStatus current,
                                              bool ack,
                                              out string message)
        {
            try
            {
                if (CheckBasics(current, ClientStatus.Status.Disconnected, packet.Alea, out message))
                {
                    using (var db = new Models.ServerDatabase())
                    {
                        // Check if requests exists on any of both sides
                        if (!db.ContactRequests.Any(c => (c.From.Username == packet.From && c.To.Username == packet.To)))
                        {
                            message = "Contact request doesn't exists.";
                            return(false);
                        }

                        // Remove the contact request
                        db.ContactRequests.DeleteOnSubmit(
                            db.ContactRequests
                            .Single(r => r.From.Username == packet.From && r.To.Username == packet.To)
                            );

                        if (ack)
                        {
                            // Create new record to Contact Table
                            db.Contacts.InsertOnSubmit(new Models.Contact()
                            {
                                Client1 = db.Clients.Single(c => c.Username == packet.From),
                                Client2 = db.Clients.Single(c => c.Username == packet.To)
                            });
                            message = "Contact added correctly.";
                        }
                        else
                        {
                            message = "Contact request refused correctly.";
                        }

                        db.SubmitChanges();
                    }
                }
                else
                {
                    return(false);
                }
                return(true);
            }
            catch (SqlException)
            {
                message = "New contact request error: Database error";
                return(false);
            }
        }
Пример #2
0
        // Get contact requests list for user current
        public static bool ContactRequestsList(Packets.BasicReqPacket packet,
                                               ClientStatus current,
                                               ref List <Models.ContactRequest> sent,
                                               ref List <Models.ContactRequest> recieved,
                                               out string message)
        {
            try
            {
                if (CheckBasics(current, ClientStatus.Status.Disconnected, packet.Alea, out message))
                {
                    var db = new Models.ServerDatabase();

                    // Fill the lists with the requests
                    recieved = db.ContactRequests
                               .Where(r => r.To.Username == current.Client.Username)
                               .ToList();

                    sent = db.ContactRequests
                           .Where(r => r.From.Username == current.Client.Username)
                           .ToList();

                    message = "Cool!";
                    return(true);
                }
                else
                {
                    message = "Contact Requests List Error: " + message;
                    return(false);
                }
            }
            catch (SqlException)
            {
                message = "Contact Requests List Error: Database error.";
                return(false);
            }
        }
Пример #3
0
        public static bool NewContactRequest(Packets.ContactReqPacket packet,
                                             ClientStatus current,
                                             out string message)
        {
            try
            {
                if (packet.To == packet.From)
                {
                    message = "You can't add yourself as a friend....";
                    return(false);
                }
                if (CheckBasics(current, ClientStatus.Status.Disconnected, packet.Alea, out message))
                {
                    using (var db = new Models.ServerDatabase())
                    {
                        // Check if destination exists
                        if (!db.Clients.Any(c => c.Username == packet.To))
                        {
                            message = "Destination doesn't exist.";
                            return(false);
                        }

                        // Check if contact exists
                        if (db.Contacts.Any(c => (c.Client1.Username == packet.To && c.Client2.Username == packet.From) ||
                                            (c.Client1.Username == packet.From && c.Client2.Username == packet.To)))
                        {
                            message = "Contact already exists.";
                            return(false);
                        }

                        // Check if requests exists on any of both sides
                        if (db.ContactRequests.Any(c => (c.From.Username == packet.To && c.To.Username == packet.From) ||
                                                   (c.From.Username == packet.From && c.To.Username == packet.To)))
                        {
                            message = "Contact request already exists.";
                            return(false);
                        }

                        // Create new record to ContactRequests Table
                        db.ContactRequests.InsertOnSubmit(new Models.ContactRequest()
                        {
                            From = db.Clients.Single(c => c.Username == packet.From),
                            To   = db.Clients.Single(c => c.Username == packet.To)
                        });

                        db.SubmitChanges();
                    }
                }
                else
                {
                    return(false);
                }
                message = "Cool!";
                return(true);
            }
            catch (SqlException)
            {
                message = "New contact request error: Database error";
                return(false);
            }
        }
Пример #4
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);
            }
        }