Exemplo n.º 1
0
        private static void BotLoop(object _statusUpdate)
            {
            StatusUpdateHandler statusUpdate = (StatusUpdateHandler) _statusUpdate;
            Freerice_Site_Interaction freeRiceInteraction = new Freerice_Site_Interaction();

            while(true)
                {
                FreericeSiteData data = new FreericeSiteData();
                try
                    {
                    data = freeRiceInteraction.GetSiteData();
                    statusUpdate(data.Word);
                    }
                catch(ErrorLoadingFreerice s)
                    {
                    freeRiceInteraction.ResetSite();
                    statusUpdate("Error loading site " + s);
                    Thread.Sleep(5000);
                    continue;
                    }

                int answer = SearchThesaurusForAnswer(data.Word, data.Answers);
                if(answer == -1)
                    {
                    statusUpdate("Unable to find correct word so we guess");
                    Random rand = new Random();
                    answer = rand.Next(0, 3);
                    }
                else
                    {
                        statusUpdate("Found answer: " + data.Answers[answer]);
                    }
                

                FreericeAnswerData answerData = new FreericeAnswerData();
                while (true)
                    {
                    try
                        {
                        answerData = freeRiceInteraction.SubmitAnswer(data, answer);
                        statusUpdate(answerData.IsAnswerCorrect + " " + data.riceDonated);
                        break;
                        }
                    catch (ErrorLoadingFreerice s)
                        {
                        statusUpdate("Error sending answer " + s);
                        }
                    }
                }  
            }
        public FreericeAnswerData SubmitAnswer(FreericeSiteData data, int choice)
            {
            if (choice < 0 || choice > 3)
            {
                throw new ConstraintException("Invalid range of " + choice);
            }

            siteData = webRequest.GetStringRepresentationOfSite(FreericeUrl, "POST", "SELECTED=" + (choice + 1) + data.answerPostData);

            FreericeSiteStatus siteStatus = GetSiteStatus();
            if (siteStatus == FreericeSiteStatus.UnknownLocation || siteStatus == FreericeSiteStatus.AtSiteToSlowPage)
            {
                throw new ErrorLoadingFreerice(siteStatus);
            }

            FreericeAnswerData answerData = new FreericeAnswerData();
            answerData.IsAnswerCorrect = IsAnswerCorrect();
            answerData.word = data.Word;
            answerData.Synonym = GetCorrectWordSynonym();

            return answerData;
            }