127.0.0.1:6379> zrange g_1 -10 -1 withscores 1) "tom5" 2) "506" 3) "tom6" 4) "507" 5) "hug" 6) "600" 7) "tom" 8) "5000" 9) "tom8" 10) "5088" 11) "tom9" 12) "5089" 13) "tom10" 14) "5090" 15) "tom11" 16) "5091" 17) "tom12" 18) "5092" 19) "tom13" 20) "5093"
<?php require './predis/autoload.php'; $redis = new Predis\Client( array( 'scheme'=>'tcp', 'host'=>'127.0.0.1', 'port'=>6379, )); //header("content-type:text/html;charset=utf-8"); if(trim($_POST['game_id']) == '' || trim($_POST['user_name']) == '' || trim($_POST['score']) == ''){ echo 'need more information!'; exit; }else{ // echo 'OK'; // echo $_POST['game_id']; // echo $_POST['user_name']; // echo $_POST['score']; $game_id = "g_" . $_POST['game_id']; $name = $_POST['user_name']; $score = $_POST['score']; if($score > 0){ $beforeScore = $redis->zscore($game_id , $name); if($score > $beforeScore){ $itemScore = array($name => $score); $redis->zadd($game_id, $itemScore); } } }
<?php require './predis/autoload.php'; $redis = new Predis\Client( array( 'scheme'=>'tcp', 'host'=>'127.0.0.1', 'port'=>6379, )); if(trim($_GET['game_id']) == ''){ echo 'need game_id'; }else{ $game_id = "g_" . $_GET['game_id']; $board_score = $redis->zrange($game_id, -10, -1, 'withscores'); //print_r($board_score); echo json_encode($board_score); //echo $board_score; }
<?php require './predis/autoload.php'; $redis = new Predis\Client( array( 'scheme'=>'tcp', 'host'=>'127.0.0.1', 'port'=>6379, )); if(trim($_GET['game_id']) == '' || trim($_GET['user_name']) == ''){ echo 'need game_id and name'; }else{ $game_id = "g_" . $_GET['game_id']; $user_name = $_GET['user_name']; $userCurrentRange = $redis->zrevrank($game_id, $user_name); //echo 'current range:' . $userCurrentRange; $result = array(); if(true){ $halfCount = 4; $leftRank = $userCurrentRange - $halfCount; $rightRank = $userCurrentRange + $halfCount; if($leftRank < 0){ $leftRank = 0; } $board_score = $redis->zrevrange($game_id, $leftRank, $rightRank, 'withscores'); $index = 0; foreach($board_score as $name => $score){ if($name == $user_name){ break; }else{ $index = $index + 1; } } $firstRank = $userCurrentRange - $index + 1; foreach($board_score as $name => $score){ $eachItem = array(); $eachItem['name'] = $name; $eachItem['score'] = $score; $eachItem['rank'] = $firstRank; $result[] = $eachItem; $firstRank = $firstRank + 1; } echo json_encode($result); }else{ echo json_encode($result); } }
getJsonFromUrl:function(){ var xhr = cc.loader.getXMLHttpRequest(); var args = "?game_id=" + gameId; xhr.open("GET", rankBoardURL + args, true); xhr.setRequestHeader("Content-Type","application/x-www-form-urlencoded"); xhr.onreadystatechange = function () { if (xhr.readyState == 4 && (xhr.status >= 200 && xhr.status <= 207)) { var response = xhr.responseText; cc.log("res:" + response); cc.log("send success"); var obj = JSON.parse(response); this.initContent(obj); } }.bind(this); xhr.send(); }, initContent:function(jsonValue){ var size = cc.director.getWinSize(); var fontDefBlueStroke = new cc.FontDefinition(); fontDefBlueStroke.fontName = "Arial"; fontDefBlueStroke.fontSize = 50; fontDefBlueStroke.textAlign = cc.TEXT_ALIGNMENT_LEFT; var jsonLength = 0; for(var name in jsonValue){ jsonLength++; } cc.log("jsonlength:" + jsonLength); var nameMargin = 50; var nameStartPositionY = 800 - nameMargin * jsonLength; var lastRank = jsonLength; for(var name in jsonValue){ cc.log(name); cc.log(jsonValue[name]); this.rankLabel = new cc.LabelTTF(lastRank + ':', fontDefBlueStroke); this.addChild(this.rankLabel); this.rankLabel.x = 100; this.rankLabel.y = nameStartPositionY; this.rankLabel.setAnchorPoint(cc.p(0, 0.5)); this.nameLabel = new cc.LabelTTF(name, fontDefBlueStroke); this.addChild(this.nameLabel); this.nameLabel.x = size.width * 0.5 - 100; this.nameLabel.y = nameStartPositionY; this.nameLabel.setAnchorPoint(cc.p(0, 0.5)); this.scoreLabel = new cc.LabelTTF(parseFloat(jsonValue[name]).toFixed(2), fontDefBlueStroke); this.addChild(this.scoreLabel); this.scoreLabel.x = size.width * 0.5; this.scoreLabel.y = nameStartPositionY; this.scoreLabel.setAnchorPoint(cc.p(0, 0.5)); nameStartPositionY = nameStartPositionY + nameMargin; lastRank = lastRank - 1; } }
http://www.waitingfy.com/archives/1420