Esempio n. 1
0
        //return a competition with results from the user ID that is provided.
        public CompetitionWithResult GetCompetitionWithResultFromID(int ID)
        {
            using (var context = new DivingCompDbContext())
            {
                CompetitionWithResult cwr = new CompetitionWithResult();
                cwr.Comp = GetCompetitionWithUserFromID(ID);

                //cwr.Results = new List<Result>();

                //users = context.Users.Where(u => context.CompetitionUsers.Any(cu => u.ID == cu.UID & cu.CID == comp.ID)).ToList();
                //users where id = ( in competition users where cu.UID <-- and cu.cid == comp.id)
                cwr.Jumps = context.Jumps.Where(jump =>
                                                context.CompetitionUsers.Any(cu => jump.CUID == cu.ID && cu.CID == cwr.Comp.ID)).Include(j => j.Results).ToList();


                foreach (Jump j in cwr.Jumps)
                {
                    CompetitionUser tmp = context.CompetitionUsers.FirstOrDefault(u => u.ID == j.CUID);
                    j.CUID = tmp.UID;

                    foreach (Result r in j.Results)
                    {
                        r.Jump = null;
                    }
                }



                return(cwr);
            }
        }
Esempio n. 2
0
        public CompetitionWithResult GetActiveCompetitions() //CompetitionWithUser innan
        {
            using (var context = new DivingCompDbContext())
            {
                //create two lists, one to get all active competitions, and one for all users in those competitions
                List <CompetitionWithResult> result = new List <CompetitionWithResult>();                                             //War CompetitionWithUser här förut
                Competition c = context.Competitions.OrderBy(x => x.Start <= DateTime.Now && !helper.IsFinished(x.Finished)).First(); //ändrare to orderby och tar första istället för att ta alla osm inte är klara
                //.OrderBy(a => a.dStart).First();
                //for each active competition, add everything needed to the first list (result)
                CompetitionWithResult cwr = GetCompetitionWithResultFromID(c.ID);

                //CompetitionWithResult temp = new CompetitionWithResult(); //Här var förut en CompetitionWithUser, underliggande tilldelningar hade ej.Comp innan ändring
                //temp.Comp = new CompetitionWithUser();
                //temp.Comp.ID = c.ID;
                //temp.Comp.Name = c.Name;
                //temp.Comp.Start = c.Start;
                //temp.Comp.Finished = c.Finished;
                //temp.Comp.Jumps = c.Jumps;

                //List<User> users = context.Users.Where(u => context.CompetitionUsers.Any(cu => u.ID == cu.UID & cu.CID == c.ID)).ToList();
                //List<User> judges = context.Users.Where(j => context.CompetitionJudges.Any(cj => j.ID == cj.UID & cj.CID == c.ID)).ToList();
                //List<Jump> jumps = cwr.Jumps = context.Jumps.Where(jump =>
                //    context.CompetitionUsers.Any(cu => jump.CUID == cu.ID && cu.CID == cwr.Comp.ID)).ToList();

                //temp.Comp.Users = users;
                //temp.Comp.Judges = judges;
                //temp.Jumps =
                //temp.Results = new List<Result>();
                //result.Add(temp);


                return(cwr);
            }
        }
Esempio n. 3
0
 public static void CompetitionData(CompetitionWithResult compdata)
 {
     App.Current.Dispatcher.Invoke((Action) delegate
     {
         JudgePage currentPage = App.MainWindowRef.Main.Content as JudgePage;
         currentPage.compdata  = compdata;
     });
 }
        private void Listen(object obj)
        {
            var address = Client.Client.RemoteEndPoint.ToString().Split(':');

            Console.WriteLine(String.Format("Client {0} is connected from {1}", this.ID, address[0]));

            CancellationToken ct = (CancellationToken)obj;

            while (!ct.IsCancellationRequested)
            {
                Byte[] bytes     = new byte[0];
                string handshake = "";


                do
                {
                    bytes = new Byte[Client.Available];
                    try
                    {
                        Stream.Read(bytes, 0, bytes.Length);
                        handshake += Encoding.UTF8.GetString(bytes);
                    }
                    catch (System.IO.IOException)
                    {
                        //IOException should only be when client disconnect during message transit
                        //Removes all trace of client
                        Disconnect();
                        break;
                    }
                } while (Stream.DataAvailable);

                if (handshake != "")
                {
                    //this is the first difference, a handshake!
                    if (new Regex("^GET").IsMatch(handshake))
                    {
                        Byte[] response = Encoding.UTF8.GetBytes(
                            "HTTP/1.1 101 Switching Protocols" + Environment.NewLine
                            + "Connection: Upgrade" +
                            Environment.NewLine
                            + "Upgrade: websocket" +
                            Environment.NewLine
                            + "Sec-WebSocket-Accept: " +
                            Convert.ToBase64String(
                                SHA1.Create()
                                .ComputeHash(
                                    Encoding
                                    .UTF8
                                    .GetBytes(
                                        new
                                        Regex(
                                            "Sec-WebSocket-Key: (.*)")
                                        .Match(
                                            handshake)
                                        .Groups
                                        [1]
                                        .Value
                                        .Trim() +
                                        "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
                                        )
                                    )
                                ) + Environment.NewLine
                            + Environment.NewLine);
                        Stream.Write(response, 0, response.Length);
                    }
                    else
                    {
                        handshake = Encoding.UTF8.GetString(javaScriptUser(bytes));
                        string[] splitMsg = handshake.Split("\r\n");
                        Database db       = new Database();

                        switch (splitMsg[0])
                        {
                        case "GET ALL COMPETITIONS":

                            List <CompetitionWithUser> comp = db.GetAllCompetitions();
                            int i = 0;
                            foreach (CompetitionWithUser c in comp)
                            {
                                i++;
                            }

                            string json = JsonConvert.SerializeObject(comp);
                            Send("{\"Type\":\"CompetitionWithUser\",\"Num\":" + i + ",\"Data\":" + json + '}');

                            break;

                        case "GET COMPETITION":
                            CompetitionWithResult temp =
                                db.GetCompetitionWithResultFromID(Int32.Parse(splitMsg[1]));

                            Send("{\"Type\":\"SingleCompetition\",\"Data\":" + JsonConvert.SerializeObject(temp) + '}');
                            break;

                        case "Exit<00>":
                            Disconnect();
                            break;

                        default:
                            break;
                        }
                    }
                }
                else
                {
                    Disconnect();
                }
            }
        }