示例#1
0
        public List <CompetitionWithUser> GetAllCompetitions()
        {
            using (var context = new DivingCompDbContext())
            {
                //create two lists to, one for the result and one to acquire all info from the db.
                List <CompetitionWithUser> result = new List <CompetitionWithUser>();
                List <Competition>         c      = context.Competitions.ToList <Competition>();

                //add each comp to the result
                foreach (Competition comp in c)
                {
                    CompetitionWithUser temp = new CompetitionWithUser();
                    temp.ID       = comp.ID;
                    temp.Name     = comp.Name;
                    temp.Start    = comp.Start;
                    temp.Finished = comp.Finished;
                    temp.Jumps    = comp.Jumps;

                    List <User> users  = context.Users.Where(u => context.CompetitionUsers.Any(cu => u.ID == cu.UID & cu.CID == comp.ID)).ToList();
                    List <User> judges = context.Users.Where(j => context.CompetitionJudges.Any(cj => j.ID == cj.UID & cj.CID == comp.ID)).ToList();

                    temp.Users  = users;
                    temp.Judges = judges;
                    result.Add(temp);
                }
                return(result);
            }
        }
示例#2
0
        //return a competition with the user ID that is provided.
        public CompetitionWithUser GetCompetitionWithUserFromID(int ID)
        {
            using (var context = new DivingCompDbContext())
            {
                Competition         comp = context.Competitions.Where(x => x.ID == ID).FirstOrDefault();
                CompetitionWithUser cwu  = new CompetitionWithUser();

                cwu.ID       = comp.ID;
                cwu.Name     = comp.Name;
                cwu.Start    = comp.Start;
                cwu.Finished = comp.Finished;
                cwu.Jumps    = comp.Jumps;
                List <User> users  = context.Users.Where(u => context.CompetitionUsers.Any(cu => u.ID == cu.UID & cu.CID == comp.ID)).ToList();
                List <User> judges = context.Users.Where(j => context.CompetitionJudges.Any(cj => j.ID == cj.UID & cj.CID == comp.ID)).ToList();
                cwu.Users  = users;
                cwu.Judges = users;
                return(cwu);
            }
        }
示例#3
0
        public Response CreateResponse()
        {
            //Our protocoll is build of a message and a response.
            //All sent messages expect one response.
            Database db = new Database();

            //Create the response to be sent back
            Response rsp = new Response();

            string[] part;


            //Switch on MessageType for easy handling
            switch (this.Type)
            {
            case MessageType.NoType:
                //This could be a default but we ended up with having a no_message type.
                Console.WriteLine("Recieved notype message: " + this.Data);
                break;

            case MessageType.Login:
                //Set the response type for parsing on other end.
                rsp.Type = MessageType.Login;

                string[] userString = this.Data.Split("\r\n");
                //Try to find a user with same name
                rsp.user = db.GetUserBySSN(userString[0]);
                if (rsp.user != null)     //this could be made into a one-liner, kept apart for ease of readability
                {
                    //user was found, fetch salt and hash
                    string salt = db.GetSaltByID(rsp.user.ID);
                    string hash = db.GetHashByID(rsp.user.ID);


                    //Authenticate user with salt & hash
                    if (crypto.AuthenticateLogin(userString[1], hash, salt) == true)
                    {
                        //return some message for client to continue
                        rsp.Data      = "success";
                        rsp.user.Hash = "***";
                        rsp.user.Salt = "***";
                    }
                    else
                    {
                        rsp.Data      = "no user";
                        rsp.user.Hash = "***";
                        rsp.user.Salt = "***";
                        //don't tell the client it has a correct username but wrong password.
                        //this makes bruteforcing easier!
                        //therefore we use "no user" on both wrong username and wrong password.
                        //aswell as "hide" the hash, salt and SSN.
                    }
                }
                else
                {
                    rsp.Data = "no user";
                    //no user was found, alert the client.
                }
                break;

            case MessageType.Register:
                rsp.Type = MessageType.Register;

                string[] registerUser = this.Data.Split("\r\n");
                //try fo fetch user
                rsp.user = db.GetUserBySSN(registerUser[1]);
                if (rsp.user != null)
                {
                    rsp.Data      = "USER EXISTS!";
                    rsp.user.Hash = "***";
                    rsp.user.Salt = "***";
                }
                else
                {
                    //Generate some salt and hash
                    User tempUser = crypto.GenerateSaltHash(registerUser[2]);
                    //register user
                    db.RegisterUser(registerUser[0], registerUser[1], tempUser.Salt, tempUser.Hash);
                }
                break;

            case MessageType.Competition:
                rsp.Type = MessageType.Competition;
                //split string in case we want to know more
                part = this.Data.Split("\r\n");
                List <CompetitionWithUser> comp;
                CompetitionWithResult      compresult;

                //Competition messagetypes will be as following:

                //Type: Competition,
                //Data: "prefix \r\n suffix"
                //User: dont think this is honestly needed here!

                //We switch on data.prefix, and is suffix is needed, use that to identify specific competitions.
                switch (part[0])
                {
                case "GetAll":
                    //fetch all competitions
                    rsp.Type = MessageType.Competition;
                    comp     = db.GetAllCompetitions();

                    //Newtonsoft.Json is used to turn objects to nice looking strings
                    rsp.Data = JsonConvert.SerializeObject(comp);
                    break;

                case "GetActive":
                    //Fetch all competitions which are started but not ended
                    rsp.Type   = MessageType.Competition;
                    compresult = db.GetActiveCompetitions();          //db.GetActiveCompetitions(); <- detta stod här innan jag va här o pilla /Thomas

                    rsp.Data = JsonConvert.SerializeObject(compresult);
                    break;

                case "CreateCompetition":
                    rsp.Type = MessageType.Competition;

                    CompetitionWithUser CompInfo = JsonConvert.DeserializeObject <CompetitionWithUser>(part[1]);
                    List <Jump>         jumps    = JsonConvert.DeserializeObject <List <Jump> >(part[2]);

                    //if the conversion did succeed.
                    if (CompInfo != null)
                    {
                        //create a competition and tell the client it succeeded.
                        db.CreateCompetition(CompInfo, jumps);
                        rsp.Data = "Competition created";
                    }
                    else
                    {
                        //creation did not succeed.
                        rsp.Data = "Competition failed";
                    }
                    break;
                }
                break;

            case MessageType.ScoreToJump:
                rsp.Type = MessageType.ScoreToJump;

                //Put info from the client to a new object to work with.
                Result ScoreInfo = JsonConvert.DeserializeObject <Result>(this.Data);
                //done to be sure..
                if (ScoreInfo != null)
                {
                    db.SetScoreToJump(ScoreInfo);
                    //rsp.Data = "Score set";
                }
                else
                {
                    //rsp.Data = "Score not set";
                }

                break;

            case MessageType.Judges:
                rsp.Type = MessageType.Judges;

                //returns all judges
                List <User> AllJudges = db.GetAllJudges();
                rsp.Data = JsonConvert.SerializeObject(AllJudges);

                break;

            case MessageType.Jumpers:
                rsp.Type = MessageType.Jumpers;

                //returns all jumpers.
                List <User> AllJumpers = db.GetAllJumpers();
                rsp.Data = JsonConvert.SerializeObject(AllJumpers);

                break;

            case MessageType.User:
                part = this.Data.Split("\r\n");
                switch (part[0])
                {
                case "Get All":
                    rsp.Type = MessageType.User;

                    //return all Users.
                    List <User> AllUsers = db.GetAllUsers();
                    rsp.Data = JsonConvert.SerializeObject(AllUsers);
                    break;
                }
                break;

            case MessageType.ChangeUser:
                User u = JsonConvert.DeserializeObject <User>(this.Data);
                rsp.Type = MessageType.ChangeUser;
                if (db.EditUser(u))
                {
                    rsp.Data = JsonConvert.SerializeObject(db.GetAllUsers());
                }
                else
                {
                    rsp.Data = "Error Updating User";
                }
                break;


            default:
                break;
            }

            //returns response to be sent
            return(rsp);
        }
示例#4
0
        public void CreateCompetition(CompetitionWithUser CompInfo, List <Jump> jumps)
        {
            var context = new DivingCompDbContext();

            context.Database.EnsureCreated();

            //create a new competition object to later add tot the database.
            Competition c = new Competition();

            //add provided info to the competition object.
            c.Name  = CompInfo.Name;
            c.Start = CompInfo.Start;
            c.Jumps = CompInfo.Jumps;

            //add it to the database.
            context.Competitions.Add(c);
            context.SaveChanges();

            List <CompetitionUser> CUIDs = new List <CompetitionUser>();

            //for each jumper in the provided information, add to the database and to the competition
            foreach (User userJumper in CompInfo.Users)
            {
                CompetitionUser temp = new CompetitionUser();
                temp.CID = c.ID;
                temp.UID = userJumper.ID;

                context.CompetitionUsers.Add(temp);
                context.SaveChanges();

                CompetitionUser tmp = new CompetitionUser();
                tmp.ID  = temp.ID;
                tmp.UID = temp.UID;
                CUIDs.Add(tmp);
            }

            //for each judge in the provided information, add judge to the competition.
            foreach (User userJudge in CompInfo.Judges)
            {
                CompetitionJudge temp = new CompetitionJudge();
                temp.CID = c.ID;
                temp.UID = userJudge.ID;
                context.CompetitionJudges.Add(temp);
            }

            //aa...
            List <Jump> orderedJumps = jumps.OrderBy(o => o.CUID).ToList();
            int         n            = 0;

            for (int i = 0; i < orderedJumps.Count; i++)
            {
                if (i % c.Jumps == 0 && i != 0)
                {
                    n++;
                }

                orderedJumps[i].GlobalNumber = orderedJumps[i].Number * (orderedJumps.Count / c.Jumps) + n;
            }

            foreach (Jump j in orderedJumps)
            {
                Jump temp = new Jump();

                for (int i = 0; i < CUIDs.Count; i++) //for all CU stored
                // CUIDs.ID == compuser.ID, CUIDs.UID == user ID
                {
                    // currently, j.CUID == compuser.UID but this will be changed to compuser.ID
                    if (CUIDs[i].UID == j.CUID)    // this means our jump belongs to this compuser
                    {
                        temp.CUID   = CUIDs[i].ID; //here
                        temp.Code   = j.Code;
                        temp.Number = j.Number;
                        temp.Height = j.Height;

                        temp.GlobalNumber = j.GlobalNumber;

                        Jump t = JumpHelper.ParseDifficulty(j.Code, j.Height);
                        temp.Name       = t.Name;
                        temp.Difficulty = t.Difficulty;
                        context.Jumps.Add(temp);
                    }
                }
            }
            context.SaveChanges();
        }
示例#5
0
        public static void CreateTestCompetition()
        {
            CompetitionWithUser c = new CompetitionWithUser();

            c.Name  = "Genererad tävling";
            c.Start = DateTime.Now;
            c.Jumps = 3;

            List <User> judges = new List <User>();
            User        j1     = new User();

            j1.ID = 21;
            judges.Add(j1);
            User j2 = new User();

            j2.ID = 23;
            judges.Add(j2);
            User j3 = new User();

            j3.ID = 24;
            judges.Add(j3);

            c.Judges = judges;

            List <User> jumpers = new List <User>();
            User        u1      = new User();

            u1.ID = 22;
            jumpers.Add(u1);
            User u2 = new User();

            u2.ID = 27;
            jumpers.Add(u2);
            User u3 = new User();

            u3.ID = 28;
            jumpers.Add(u3);

            c.Users = jumpers;

            List <Jump> jumps = new List <Jump>();

            Jump jump1 = new Jump();

            jump1.Code   = "2,0,7,B";
            jump1.Height = 3;
            jump1.CUID   = 22;
            jump1.Number = 0;
            jumps.Add(jump1);

            jump1        = new Jump();
            jump1.Code   = "3,0,5,B";
            jump1.Height = 3;
            jump1.CUID   = 22;
            jump1.Number = 1;
            jumps.Add(jump1);

            jump1        = new Jump();
            jump1.Code   = "4,0,5,C";
            jump1.Height = 3;
            jump1.CUID   = 22;
            jump1.Number = 2;
            jumps.Add(jump1);

            jump1        = new Jump();
            jump1.Code   = "4,0,9,B";
            jump1.Height = 3;
            jump1.CUID   = 27;
            jump1.Number = 0;
            jumps.Add(jump1);

            jump1        = new Jump();
            jump1.Code   = "4,1,3,C";
            jump1.Height = 1;
            jump1.CUID   = 27;
            jump1.Number = 1;
            jumps.Add(jump1);

            jump1        = new Jump();
            jump1.Code   = "2,0,7,B";
            jump1.Height = 3;
            jump1.CUID   = 27;
            jump1.Number = 2;
            jumps.Add(jump1);

            jump1        = new Jump();
            jump1.Code   = "2,0,7,B";
            jump1.Height = 3;
            jump1.CUID   = 28;
            jump1.Number = 0;
            jumps.Add(jump1);

            jump1        = new Jump();
            jump1.Code   = "2,0,7,B";
            jump1.Height = 3;
            jump1.CUID   = 28;
            jump1.Number = 1;
            jumps.Add(jump1);

            jump1        = new Jump();
            jump1.Code   = "2,0,7,B";
            jump1.Height = 3;
            jump1.CUID   = 28;
            jump1.Number = 2;
            jumps.Add(jump1);

            Database db = new Database();

            db.CreateCompetition(c, jumps);
        }