使用PHP在微信公共平台实现24点游戏
海南中立科技 | 2018-01-26 23:30:43 | 阅读:41273
使用PHP在微信公共平台实现24点游戏,搭建服务器为SAE,所以很多语句和MYSQL不太一样,24点的数据库存储在24data里面,先用python计算出结果然后存储在本地的txt文件最后倒入SAE的MYSQL,记录了所有有解的4个数字。可以添加微信号:24点。//defineyourtokendefine("TOKEN","ddwm");define("INPUT_NAME","1");define("START_GAME","2");define("TIMING_START","3");define("TIMING_END","4");$wechatObj=newWhy24Point();if($_GET["echostr"])$wechatObj--->valid();else$wechatObj->responseMsg();classWhy24Point{publicfunctionresponseMsg(){//getpostdata,Maybeduetothedifferentenvironments$postStr=$GLOBALS["HTTP_RAW_POST_DATA"];//extractpostdataif(!empty($postStr)){$postObj=simplexml_load_string($postStr,'SimpleXMLElement',LIBXML_NOCDATA);$fromUsername=$postObj->FromUserName;$toUsername=$postObj->ToUserName;if($postObj->Event=="subscribe"){$time=time();$textTpl="%s";$msgType="text";$contentStr="哟你好亲爱的小伙伴,欢迎来到24点~\n游戏规则:使用+-*/四则运算得出结果为24即可。\n任何时候输入主页返回主菜单,\n输入排名查看当前排名哟~\n如果有情况反馈请发送语音信息!\n请输入任意内容,开始我们的24点职业生涯!";$resultStr=sprintf($textTpl,$fromUsername,$toUsername,$time,$msgType,$contentStr);echo$resultStr;}$keyword=trim($postObj->Content);$time=time();$textTpl="%s";if(!empty($keyword)){$msgType="text";$contentStr=$this->getData($fromUsername,$keyword);$resultStr=sprintf($textTpl,$fromUsername,$toUsername,$time,$msgType,$contentStr);echo$resultStr;}else{echo"输入点东西吧。。";}}else{echo"";exit;}}privatefunctiongetData($fromUsername,$keyword){if($keyword=="主页"){$this->setStep($fromUsername,constant("START_GAME"));return"欢迎来到24点的决战现场。请输入任意内容,开始我们的24点职业生涯!任何时候输入主页返回主菜单,输入排名查看当前排名~";}if($keyword=="排名"){return"您的目前排名是:".$this->getRank($fromUsername);}if($this->isFirst($fromUsername)){return"这是你第一次参加游戏,请输入你的用户昵称:";}if($this->getStep($fromUsername)==constant("INPUT_NAME")){$this->setName($fromUsername,$keyword);$this->setStep($fromUsername,constant("START_GAME"));return"你好,".$keyword."同学。请输入任意内容,开始我们的24点职业生涯!任何时候输入主页返回主菜单,输入排名查看当前排名~";}if($this->getStep($fromUsername)==constant("START_GAME")){$numbers=$this->getNumbers();$this->setStep($fromUsername,constant("TIMING_START"));return"游戏规则:使用+-*/四则运算得出结果为24即可。\n请听题!".$this->getNumbers();}if($this->getStep($fromUsername)==constant("TIMING_START")){if($this->calcStr($keyword)){//$this->setStep($fromUsername,constant("TIMING_END"));$this->setGrade($fromUsername,5);$this->setStep($fromUsername,constant("START_GAME"));return"结果等于24!你现在的分数是:".$this->getGrade($fromUsername);}return"结果错误!骗子,根本就不等于24!";//$this->setStep($fromUsername,constant("TIMING_END"));}return"我擦,系统出问题了。抱了个歉哈!";}//计算排名privatefunctiongetRank($fromUsername){$mysql=newSaeMysql();$grade=$this->getGrade($fromUsername);$sql="SELECTCOUNT(*)FROM`24points`WHERE`grade`>".$grade."";$mysql->closeDb();returnintval($mysql->getVar($sql))+1;}//计算成绩privatefunctionsetGrade($fromUsername,$add){$mysql=newSaeMysql();$sql="SELECTgradeFROM`24points`WHERE`userid`='".$fromUsername."'";$grade=intval($mysql->getVar($sql))+$add;$sql="UPDATE`24points`SET`grade`='".$grade."'WHERE`userid`='".$fromUsername."';";$mysql->runSql($sql);$mysql->closeDb();}//获得成绩privatefunctiongetGrade($fromUsername){$mysql=newSaeMysql();$sql="SELECTgradeFROM`24points`WHERE`userid`='".$fromUsername."'";$mysql->closeDb();returnintval($mysql->getVar($sql));}//计算等式,返回正确或者错误privatefunctioncalcStr($keyword){$keyword=str_replace("(","(",$keyword);$keyword=str_replace(")",")",$keyword);$keyword=str_replace("÷","/",$keyword);$keyword=str_replace("/","/",$keyword);$keyword=str_replace("×","*",$keyword);$atr_arr=str_split($keyword);$new_atr='';for($i=0;$i<strlen($keyword);$i++){$new_atr=$new_atr.$atr_arr[$i];}$s=eval("return$new_atr;");if($s==24){returntrue;}returnfalse;}//是否是第一次privatefunctionisFirst($fromUsername){$mysql=newSaeMysql();$sql="SELECT*FROM`24points`WHERE`userid`='".$fromUsername."'";$data=$mysql->getData($sql);if(count($data)==0){$sql="INSERTINTO`24points`(`userid`,`step`)VALUES('".$fromUsername."','".constant("INPUT_NAME")."');";$mysql->runSql($sql);$mysql->closeDb();returntrue;}returnfalse;}//获取当前的步骤privatefunctiongetStep($fromUsername){$mysql=newSaeMysql();$sql="SELECTstepFROM`24points`WHERE`userid`='".$fromUsername."'";$mysql->closeDb();return$mysql->getVar($sql);}//获取随机的四个数字用空格隔开privatefunctiongetNumbers(){$rand_id=rand(1,1362);$mysql=newSaeMysql();$sql="SELECTnum1FROM`24data`WHERE`id`='".$rand_id."'";$num1=$mysql->getVar($sql);$sql="SELECTnum2FROM`24data`WHERE`id`='".$rand_id."'";$num2=$mysql->getVar($sql);$sql="SELECTnum3FROM`24data`WHERE`id`='".$rand_id."'";$num3=$mysql->getVar($sql);$sql="SELECTnum4FROM`24data`WHERE`id`='".$rand_id."'";$num4=$mysql->getVar($sql);$mysql->closeDb();$numbers=$num1."".$num2."".$num3."".$num4;return$numbers;}//设置当前操作的步数privatefunctionsetStep($fromUsername,$step){$mysql=newSaeMysql();$sql="UPDATE`24points`SET`step`='".$step."'WHERE`userid`='".$fromUsername."';";$mysql->runSql($sql);$mysql->closeDb();}//设置用户昵称privatefunctionsetName($fromUsername,$username){$mysql=newSaeMysql();$sql="UPDATE`24points`SET`username`='".$username."'WHERE`userid`='".$fromUsername."';";$mysql->runSql($sql);$mysql->closeDb();}//验证微信publicfunctionvalid(){$echoStr=$_GET["echostr"];//validsignature,optionif($this->checkSignature()){echo$echoStr;exit;}}//检查签名privatefunctioncheckSignature(){$signature=$_GET["signature"];$timestamp=$_GET["timestamp"];$nonce=$_GET["nonce"];$token=TOKEN;$tmpArr=array($token,$timestamp,$nonce);sort($tmpArr);$tmpStr=implode($tmpArr);$tmpStr=sha1($tmpStr);if($tmpStr==$signature){returntrue;}else{returnfalse;}}}111811111111121111311261127112811291121011211112121121311341135113611371138113911310113111131211313114411451146114711481149114101141211551156115711581166116811691161211710118811913111012111013111111111112111113111212111213111313122412251226122712281229122101221112212122131233123412351236123712381239123101231112312123131244124512461247124812491241012411124121241312551256125712581259125101251212513126612671268126912610126111261212613127712781279127101271112712128812891281012813129111291212913121011121012121013121111121112121113121212121213121313133313341335133613371338133913310133111331213441345134613471348134913410134111341213413135613571358135913510135111351213513136613671368136913610136111361213613137713781379137101371213713138813891381013811138121381313991391013911139121391313101013101113101213111113111213121213121313131314441445144614471448144914410144111441214551456145714581459145101451114512145131466146714681469146101461114612146131477147814791471114712147131488148914811148121481314910149111491214913141010141011141012141212155515561559155101551115512155131566156715681569156101561115612156131578157915710157111571215713158815891581015811158121581315991591015911159121591315101015101115101215101315111115111215121216661668166916610166111661216613167916710167111671216881689168101681116812168131699169101691216913161012161013161112161113161212161213177917710177111771217881789178101781117812179917910179111791217913171012171013171212171213171313188818891881018811188121891118912189131810111810121810131811121811131812121991219101219101319111119111219111319121211010121101112110121211012131111112111111311112121111213111131311212121121213112131322232224222522272228222922210222112221222213223322342235223622372238223922310223112231222313224422452246224722482249224102241122412224132255225622572258225922510225112251222662267226822692261022611226122261322772278227102271222713228822892281022812229102291122912221010221011221013221111221112221113221212221213221313233323352336233723382339233102331123312233132344234523462347234823492341023411234122341323552356235723582359235102351123512235132366236723682369236102361123612236132377237823792371023711237122371323882389238102381123812238132399239102391223913231010231012231013231111231112231113231212231213231313244424452446244724482449244102441124412244132455245624572458245924510245112451224513246624672468246924610246112461224613247724782479247102471124712248824892481024811248122481324992491024912249132410102410112410122410132411112411122412122413132557255825592551025511255122551325662567256825692561025611256122561325772578257925710257112571325882589258102581125812258132591025911259122510102510112510122510132511122512122512132666266726682669266102661126612266132678267926710267112671226713268826892681026811268122681326992691026911269122610102610112610122610132611122611132612122612132778277102771127712277132788278927811278122781327910279112791327101027101127101227111227121227121328882889288102881128812288132899289102891128912289132810102810112810122810132811112811122812122812132813132991129912299132910102910112910122910132911112911132912132913132101011210101221010132101111210111221011132101213211111121111122111113211121221112132111313212121221212132121313213131333333334333533363337333833393331033311333123344334533463347334833493341133412334133355335633573359335103351233513336633673368336933610336113361233613337733783379337113371233713338833893381033812338133399339103391133912339133310133311123311133312123312133444344534463447344834493441034411344123441334553456345734583459345103451134512345133466346834693461034611346123461334773478347934710347113471234893481034811348123481334993491134912349133410103410123410133411123411133412123412133556355735583559355113551235663567356835693561035611356123561335783579357103571135712357133588358935811358123581335993591035912359133510103510113510123510133511113511123512123512133513133666366736683669366103661136612366133677367836793671036712367133688368936810368123681336993691036911369123691336101036101136101236111136111236111336121236121336131337773778377937710377123771337883789378113781237813379937910379113791237913371010371011371013371111371112371212371213371313388838893881038811388123899389103891138912389133810103810113810123811113811123812123812133813133999399103991139912399133910103910113910123910133911113911123911133912123912133913133101012310111231011133111112311121231212123121213312131344444445444644474448444944410444114441244554456445744584451044511445124451344684469446104461144612446134477447844794471044712447134488448944810448114481244813449114491244101044101244101344111244111344121244121345554556455745584559455104566456745684569456104561145612456134577457845794571045711457124571345884589458104581145812458134599459104591245913451010451011451012451013451111451112451113451212451213451313466646674668466946610466124677467846794671046712468846894681046812468134699469104691246913461010461011461012461111461112461212461213461313477747784771147884789478104781147812478134799479104791147912479134710104710114710124711114711124711134712124712134713134888488948810488114881248813489948910489114891248913481010481011481012481111481112481113481212481213481313499104991249101149101249101349111149111249121241010114101012410111241011134101212410121341112134121212555555565559555125566556755685561155775578557105571155885589558105581155812558135599559105591155101055101155101355111155111255111355121255121355131356665667566856695661056612567756785679567125671356885689568105681256813569956910569115691256913561010561011561012561013561111561112561113561212561213561313577957710577115788578957810579105791157912579135710105710115710125710135711115711135712125713135888588958810588135891158912589135810115810125811125811135812125991159912591010591011591013591113591212591213510101151010125101013510111151012135101313511121266666668666966610666116661266796671066711667126688668966810668116681266813669106691166912669136610126610136611126611136612126612136771067711678967810678116781267996791267101067101267101367111167111267111367121267121368886889688106881168812689968910689116891268913681011681012681013681111681112681113681212681313699106991169912691011691012691112691113691212691213610101061010136101112610121261012136111112611121261112136121212612121361213137771277811779107710137711127711137712127712137889788107881178812788137891078912789137810107810117810137811127811137812127812137991379101179101279111179111279121279131371010117101012710111371012127101213712121388810888118881288813889118891288913881012881013881112881113881212881213899128910128910138911118911128911138912128912138101012810111181012128101213810131381112129991299101399111299111399121299121391010139101112910111391012129101213911111191112129111213911131391212121010101210101013101011121010111310101212101012131011111210111113101112121011121310121212101212131012131310131313111111121111111311111212111112131112121211121213111213131113131312121212121212131212131312131313