setClosenessRangeInPercent(15); if($theData->isValid()){ $sms->send($theData->getNumber(),$logic->getMessage($theData->getData()),"GuessGame"); } else { $sms->send($theData->getNumber(),$logic->getFailedMessage(),"GuessGame"); } } } class ExtractData { private $theData; private $theMobileNumber; private $keyword; private $valid=false; function __construct($keyword,$message,$number) { $this->keyword = $keyword; $this->valid = $this->getMessageInfo($message); $this->theMobileNumber = $number; } public function isValid() { return $this->valid; } public function getData() { return $this->theData; } public function getNumber() { return $this->theMobileNumber; } private function getMessageInfo($m) { //remove the keyword and set the result //remove extra spaces $m=trim($m); $m=str_ireplace($this->keyword,"",$m); /// remove keyword $m=ltrim($m); // remove any spaces if(is_numeric($m)) { $this->theData = $m; return true; // is it data? } else return false; } } class MessageLogic { private static $lowGuess ="Bad Luck! There are more sweets in the jar than you think, try again."; private static $highGuess="Bad Luck! There are less sweets in the jar than you think, try again."; private static $correctGuess="Well done! You have guessed the correct amount."; private static $closeGuess="Close Guess! you are within {value}% of the correct answer."; private static $badData="We couldn't understand your answer please text in the word SWEETS and your answer, for example SWEETS 300"; private $numSweets=5678; //default private $closenessRange=5; //5 percent as default public function setNumberOfSweets($value) { $this->numSweets=$value; } public function setClosenessRangeInPercent($value) //in percent { $this->closenessRange=$value; } public function getFailedMessage() { return self::$badData; } public function getMessage($value) { /// some very simple rules if($value==$this->numSweets) return self::$correctGuess; $isClose = round(((abs($this->numSweets - $value)) / $this->numSweets) * 100); // calculate how close the number is if($isClose < $this->closenessRange) return str_replace("{value}",$this->closenessRange,self::$closeGuess); // if with in the closeness range return close messsage else{ if($value > $this->numSweets) return self::$highGuess; else return self::$lowGuess; } } } class SendSMSXML { private static $URL = "http://www.textmarketer.biz/gateway/"; private static $SHORT_CODE="88802"; private $usingShortCode=false; private $my_url; private $error; private $numberOfCreditsRemaining,$creditsUsed; private $transactin_id; function __construct($username,$password) { // Use the same username and password you have for your main account $this->my_url = self::$URL."?username=$username&password=$password&option=xml"; } public function send($number,$message,$originator=null) { // sends an SMS to the gateway, the message length must be between 1 and 640 characters long. $this->error = null; $query_string .="&number=".$number; $query_string .="&message=".urlencode($message); if($this->usingShortCode) $query_string.="&orig=".self::$SHORT_CODE; else $query_string .="&orig=".urlencode($originator); $fp =fopen($this->my_url.$query_string,"r"); $response = fread($fp,1024); return $this->processResponse($response); } public function toggleSortCode() // used for sending from our short code { $this->usingShortCode==false ? $this->usingShortCode=true : $this->usingShortCode=false; } public function getError() { // returns an array of error messages $arr = each($this->error); return $arr['value']; } public function getCreditsRemaining() { // the total of credits you have left in your account return $this->numberOfCreditsRemaining; } public function getCreditsUsed() { // how many credits were used for the send, a message that uses more than 160 characters will use more credits. 1 CR = 160 characters return $this->creditsUsed; } //////// PRIVATE FUNCTIONS private function processResponse($r) { $xml=simplexml_load_string($r); if($xml['status']=="failed"){ foreach($xml->reason as $index => $reason) $this->error[] = $reason; /// parse the errors into an array return false; } else{ $this->transaction_id = $xml['id']; $this->numberOfCreditsRemaining = $xml->credits; $this->creditsUsed = $xml->credits_used; return true; } } } ?>