代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
PHP
】
PHP在微信公共平台实现24点游戏
作者:
汪海洋
/ 发布于
2013/9/2
/
612
<?php //define your token define("TOKEN", "ddwm"); define("INPUT_NAME", "1"); define("START_GAME", "2"); define("TIMING_START", "3"); define("TIMING_END", "4"); $wechatObj = new Why24Point(); if ($_GET["echostr"]) $wechatObj->valid(); else $wechatObj->responseMsg(); class Why24Point { public function responseMsg() { //get post data, May be due to the different environments $postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; //extract post data if (!empty($postStr)) { $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); $fromUsername = $postObj->FromUserName; $toUsername = $postObj->ToUserName; if($postObj->Event == "subscribe"){ $time = time(); $textTpl = " <xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[%s]]></MsgType> <Content><![CDATA[%s]]></Content> </xml>"; $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 = " <xml> <ToUserName><![CDATA[%s]]></ToUserName> <FromUserName><![CDATA[%s]]></FromUserName> <CreateTime>%s</CreateTime> <MsgType><![CDATA[%s]]></MsgType> <Content><![CDATA[%s]]></Content> </xml>"; 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; } } private function getData($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 "我擦,系统出问题了。抱了个歉哈!"; } //计算排名 private function getRank($fromUsername){ $mysql = new SaeMysql(); $grade = $this->getGrade($fromUsername); $sql = "SELECT COUNT( * ) FROM `24points` WHERE `grade` > ".$grade.""; $mysql->closeDb(); return intval($mysql->getVar($sql))+1; } //计算成绩 private function setGrade($fromUsername, $add){ $mysql = new SaeMysql(); $sql = "SELECT grade FROM `24points` WHERE `userid` = '" . $fromUsername . "'"; $grade = intval($mysql->getVar($sql)) + $add; $sql = "UPDATE `24points` SET `grade` = '" . $grade . "' WHERE `userid` ='" . $fromUsername . "';"; $mysql->runSql($sql); $mysql->closeDb(); } //获得成绩 private function getGrade($fromUsername){ $mysql = new SaeMysql(); $sql = "SELECT grade FROM `24points` WHERE `userid` = '" . $fromUsername . "'"; $mysql->closeDb(); return intval($mysql->getVar($sql)); } //计算等式,返回正确或者错误 private function calcStr($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) { return true; } return false; } //是否是第一次 private function isFirst($fromUsername) { $mysql = new SaeMysql(); $sql = "SELECT * FROM `24points` WHERE `userid` = '" . $fromUsername . "'"; $data = $mysql->getData($sql); if (count($data) == 0) { $sql = "INSERT INTO `24points` (`userid`, `step`) VALUES ('" . $fromUsername . "', '" . constant("INPUT_NAME") . "');"; $mysql->runSql($sql); $mysql->closeDb(); return true; } return false; } //获取当前的步骤 private function getStep($fromUsername) { $mysql = new SaeMysql(); $sql = "SELECT step FROM `24points` WHERE `userid` = '" . $fromUsername . "'"; $mysql->closeDb(); return $mysql->getVar($sql); } //获取随机的四个数字用空格隔开 private function getNumbers() { $rand_id = rand(1, 1362); $mysql = new SaeMysql(); $sql = "SELECT num1 FROM `24data` WHERE `id` = '" . $rand_id . "'"; $num1 = $mysql->getVar($sql); $sql = "SELECT num2 FROM `24data` WHERE `id` = '" . $rand_id . "'"; $num2 = $mysql->getVar($sql); $sql = "SELECT num3 FROM `24data` WHERE `id` = '" . $rand_id . "'"; $num3 = $mysql->getVar($sql); $sql = "SELECT num4 FROM `24data` WHERE `id` = '" . $rand_id . "'"; $num4 = $mysql->getVar($sql); $mysql->closeDb(); $numbers = $num1 . " " . $num2 . " " . $num3 . " " . $num4; return $numbers; } //设置当前操作的步数 private function setStep($fromUsername, $step) { $mysql = new SaeMysql(); $sql = "UPDATE `24points` SET `step` = '" . $step . "' WHERE `userid` ='" . $fromUsername . "';"; $mysql->runSql($sql); $mysql->closeDb(); } //设置用户昵称 private function setName($fromUsername, $username) { $mysql = new SaeMysql(); $sql = "UPDATE `24points` SET `username` = '" . $username . "' WHERE `userid` ='" . $fromUsername . "';"; $mysql->runSql($sql); $mysql->closeDb(); } //验证微信 public function valid() { $echoStr = $_GET["echostr"]; //valid signature , option if ($this->checkSignature()) { echo $echoStr; exit; } } //检查签名 private function checkSignature() { $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) { return true; } else { return false; } } } ?>
试试其它关键字
24点游戏
同语言下
.
用net匹配并替换iOS标准的emoji表情符号
.
处理带Emoji表情的的字符串
.
获取微信昵称时 过滤特殊字符
.
通过判断上传文件的头字符来判断文件的类型
.
模拟百度URL加密解密算法
.
以太坊检查地址是否合法
.
实现crontab解析类
.
获取每个月的开始和结束时间
.
图片上传工具类
.
APP手机应用信息采集
可能有用的
.
C#实现的html内容截取
.
List 切割成几份 工具类
.
SQL查询 多列合并成一行用逗号隔开
.
一行一行读取txt的内容
.
C#动态修改文件夹名称(FSO实现,不移动文件)
.
c# 移动文件或文件夹
.
c#图片添加水印
.
Java PDF转换成图片并输出给前台展示
.
网站后台修改图片尺寸代码
.
处理大图片在缩略图时的展示
汪海洋
贡献的其它代码
(
1
)
.
PHP在微信公共平台实现24点游戏
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3