使用PHP在微信公共平台实现24点游戏[导读]使用PHP在微信公共平台实现24点游戏,搭建服务器为SAE,所以很多语句和MYSQL不太一样,24点的数据库存储在24data里面,先用python计算出结果然后存储在本地的txt文件最后倒入SAE的MYSQL,记录了所有有解的4个数字001.//defineyourtoken002.define(TOKEN,ddwm);003.define(INPUT_NAME,1);004.define(START_GAME,2);005.define(TIMING_START,3);006.define(TIMING_END,4);007.008.009.$wechatObj=newWhy24Point();010.011.if($_GET[echostr])012.$wechatObj---valid();013.else014.$wechatObj-responseMsg();015.016.classWhy24Point{017.018.publicfunctionresponseMsg(){019.//getpostdata,Maybeduetothedifferentenvironments020.$postStr=$GLOBALS[HTTP_RAW_POST_DATA];021.022.//extractpostdata023.if(!emptyempty($postStr)){024.$postObj=simplexml_load_string($postStr,'SimpleXMLElement',LIBXML_NOCDATA);025.$fromUsername=$postObj-FromUserName;026.$toUsername=$postObj-ToUserName;027.if($postObj-Event==subscribe){028.$time=time();029.$textTpl=xml030.tousername!--[CDATA[%s]]--/tousername031.fromusername!--[CDATA[%s]]--/fromusername032.createtime%s/createtime033.msgtype!--[CDATA[%s]]--/msgtype034.content!--[CDATA[%s]]--/content035./xml;036.$msgType=text;037.$contentStr=哟你好亲爱的小伙伴,欢迎来到24点~\n游戏规则:使用+-*/四则运算得出结果为24即可。\n任何时候输入主页返回主菜单,\n输入排名查看当前排名哟~\n如果有情况反馈请发送语音信息!\n请输入任意内容,开始我们的24点职业生涯!;038.$resultStr=sprintf($textTpl,$fromUsername,$toUsername,$time,$msgType,$contentStr);039.echo$resultStr;040.}041.$keyword=trim($postObj-Content);042.$time=time();043.$textTpl=xml044.tousername!--[CDATA[%s]]--/tousername045.fromusername!--[CDATA[%s]]--/fromusername046.createtime%s/createtime047.msgtype!--[CDATA[%s]]--/msgtype048.content!--[CDATA[%s]]--/content049./xml;050.if(!emptyempty($keyword)){051.$msgType=text;052.$contentStr=$this-getData($fromUsername,$keyword);053.$resultStr=sprintf($textTpl,$fromUsername,$toUsername,$time,$msgType,$contentStr);054.echo$resultStr;055.}else{056.echo输入点东西吧。。;057.}058.}else{059.echo;060.exit;061.}062.}063.064.privatefunctiongetData($fromUsername,$keyword){065.if($keyword==主页){066.$this-setStep($fromUsername,constant(START_GAME));067.return欢迎来到24点的决战现场。请输入任意内容,开始我们的24点职业生涯!任何时候输入主页返回主菜单,输入排名查看当前排名~;068.}069.070.if($keyword==排名){071.return您的目前排名是:.$this-getRank($fromUsername);072.}073.074.075.if($this-isFirst($fromUsername)){076.return这是你第一次参加游戏,请输入你的用户昵称:;077.}078.079.if($this-getStep($fromUsername)==constant(INPUT_NAME)){080.$this-setName($fromUsername,$keyword);081.$this-setStep($fromUsername,constant(START_GAME));082.return你好,.$keyword.同学。请输入任意内容,开始我们的24点职业生涯!任何时候输入主页返回主菜单,输入排名查看当前排名~;083.}084.085.if($this-getStep($fromUsername)==constant(START_GAME)){086.$numbers=$this-getNumbers();087.$this-setStep($fromUsername,constant(TIMING_START));088.return游戏规则:使用+-*/四则运算得出结果为24即可。\n请听题!.$this-getNumbers();089.}090.091.if($this-getStep($fromUsername)==constant(TIMING_START)){092.if($this-calcStr($keyword)){093.//$this-setStep($fromUsername,constant(TIMING_END));094.$this-setGrade($fromUsername,5);095.$this-setStep($fromUsername,constant(START_GAME));096.return结果等于24!你现在的分数是:.$this-getGrade($fromUsername);097.}098.099.return结果错误!骗子,根本就不等于24!;100.101.//$this-setStep($fromUsername,constant(TIMING_END));102.}103.104.return我擦,系统出问题了。抱了个歉哈!;105.}106.107.//计算排名108.privatefunctiongetRank($fromUsername){109.$mysql=newSaeMysql();110.$grade=$this-getGrade($fromUsername);111.$sql=SELECTCOUNT(*)FROM`24points`WHERE`grade`.$grade.;112.$mysql-closeDb();113.returnintval($mysql-getVar($sql))+1;114.}115.116.//计算成绩117.privatefunctionsetGrade($fromUsername,$add){118.$mysql=newSaeMysql();119.$sql=SELECTgradeFROM`24points`WHERE`userid`='.$fromUsername.';120.$grade=intval($mysql-getVar($sql))+$add;121.$sql=UPDATE`24points`SET`grade`='.$grade.'WHERE`userid`='.$fromUsername.';;122.$mysql-runSql($sql);123.$mysql-closeDb();124.}125.126.//获得成绩127.privatefunctiongetGrade($fromUsername){128.$mysql=newSaeMysql();129.$sql=SELECTgradeFROM`24points`WHERE`userid`='.$fromUsername.';130.$mysql-closeDb();131.returnintval($mysql-getVar($sql));132.}133.134.135.//计算等式,返回正确或者错误136.privatefunctioncalcStr($keyword){137.$keyword=str_replace((,(,$keyword);138.$keyword=str_replace(),),$keyword);139.$keyword=str_replace(÷,/,$keyword);140.$keyword=str_replace(/,/,$keyword);141.$keyword=str_replace(×,*,$keyword);142.$atr_arr=str_split($keyword);143.$new_atr='';144.for($i=0;$istrlen($keyword);$i++){145.$new_atr=$new_atr.$atr_arr[$i];146.}147.$s=eval(return$new_atr;);148.if($s==24){149.returntrue;150.}151.152.returnfalse;153.}154.155.//是否是第一次156.privatefunctionisFirst($fromUsername){157.$mysql=newSaeMysql();158.$sql=SELECT*FROM`24points`WHERE`userid`='.$fromUsername.';159.$data=$mysql-getData($sql);160.if(count($data)==0){161.$sql=INSERTINTO`24points`(`userid`,`step`)VALUES('.$fromUsername.','.constant(INPUT_NAME).');;162.$mysql-runSql($sql);163.$mysql-closeDb();164.returntrue;165.}166.returnfalse;167.}168.169.//获取当前的步骤170.privatefunctiongetStep($fromUsername){171.$mysql=newSaeMysql();172.$sql=SELECTstepFROM`24points`WHERE`userid`='.$fromUsername.';173.$mysql-clos