public ServerCommManager()
 {
     isAuthorized = false;
     ascEncoding = new ASCIIEncoding();
     db = new PlayerDBContext();
     HaveResponse = false;
 }
        public int CheckScheduleCount()
        {
            if (CheckAuthInfo())
            {

                int index = OperationContext.Current.IncomingMessageHeaders.FindHeader("PLAYER", "http://my");
                if (index != -1)
                {
                    string PlayerName = OperationContext.Current.IncomingMessageHeaders.GetHeader<string>(index);
                    PlayerDBContext db = new PlayerDBContext();
                    var players = from s in db.Players
                                  select s;

                    var player = players.First(s => s.Name.Equals(PlayerName));
                    return player.Schedules.Count();
                }
            }
            return -1;
        }
        public string DownloadSchedule(int i)
        {
            if (CheckAuthInfo())
            {

                int index = OperationContext.Current.IncomingMessageHeaders.FindHeader("PLAYER", "http://my");
                if (index != -1)
                {
                    string PlayerName = OperationContext.Current.IncomingMessageHeaders.GetHeader<string>(index);
                    PlayerDBContext db = new PlayerDBContext();
                    var players = from s in db.Players
                                  select s;

                    var player = players.First(s => s.Name.Equals(PlayerName));
                    if (player.Schedules.Count > i)
                    {
                        Schedule sche = player.Schedules.ElementAt(i).schedule;
                        string xml = ContentHelper.ScheduleToXMLString(sche);
                        return xml;
                    }
                }
            }
            return null;
        }
        private bool CheckAuthInfo()
        {
            try
            {
                string PlayerName = string.Empty, Date = string.Empty, HASH = string.Empty;

                int index = OperationContext.Current.IncomingMessageHeaders.FindHeader("PLAYER", "http://my");
                if (index != -1)
                {
                    PlayerName = OperationContext.Current.IncomingMessageHeaders.GetHeader<string>(index);
                }
                index = OperationContext.Current.IncomingMessageHeaders.FindHeader("TIME", "http://my");
                if (index != -1)
                {
                    Date = OperationContext.Current.IncomingMessageHeaders.GetHeader<string>(index);
                }
                index = OperationContext.Current.IncomingMessageHeaders.FindHeader("TOKEN", "http://my");
                if (index != -1)
                {
                    HASH = OperationContext.Current.IncomingMessageHeaders.GetHeader<string>(index);
                }
                if (PlayerName != string.Empty&&Date != string.Empty && HASH!=string.Empty)
                {
                    string pwd = string.Empty;
                    PlayerDBContext db = new PlayerDBContext();
                    var players = from s in db.Players
                                  select s;

                    var player = players.First(s => s.Name.Equals(PlayerName));
                    pwd = player.Password;
                    if (pwd != string.Empty)
                    {
                        string authToken = CalculateMD5Hash(PlayerName + ":" + pwd + "@" + Date);
                        if (string.Compare(authToken, HASH) == 0)
                            return true;
                    }
                }
            }
            catch (Exception exp)
            { }
            return false;
        }