コード例 #1
0
ファイル: ajax.php プロジェクト: awongh/cards
/**
 * getCards 
 * makes a new game and new shuffled deck
 * @param user array
 * @param redis object
 * @return echos json array with user's current total
 * and user's unique game id
**/
function getCards($User, $redis)
{
    $value = getCurrentValue($User['id']);
    if ($value < 50) {
        $return = array('error' => 'not enough credit');
        echo json_encode($return);
        exit;
    }
    //all possible cards
    $suits = array('c', 'd', 'h', 's');
    $values = array(2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 20);
    $deck = array();
    foreach ($suits as $suit) {
        foreach ($values as $value) {
            $deck[] = "{$suit}" . "_" . "{$value}";
        }
    }
    //add jokers
    $deck[52] = "99";
    $deck[53] = "99";
    //make game board here: 9x6
    $x = range(0, 8);
    $y = range(0, 5);
    $board = array();
    foreach ($x as $xpos) {
        foreach ($y as $ypos) {
            $board[] = $xpos . "_" . $ypos;
        }
    }
    //get game id here
    $gameid = setGameId($User['id']);
    //shuffle deck
    shuffle($deck);
    //deal cards: assign a card in the deck for each space on the board
    $allcards = array();
    foreach ($deck as $position => $card) {
        $pos = $board[$position];
        $redis->set("card:{$pos}:gameid:{$gameid}", $card);
        $allcards[$pos] = $card;
    }
    //take away 50 coins from user for this game
    $total = subCurrentValue($User['id'], 50);
    //return their current running total
    $return = array('gameid' => $gameid, 'total' => $total);
    echo json_encode($return);
}
コード例 #2
0
ファイル: index.php プロジェクト: awongh/cards
<?php

require 'redis.php';
if (!isLoggedIn()) {
    header('Location: ' . SITEURL . 'login-register.php');
}
loadUserInfo($User['id']);
$total = getCurrentValue($User['id']);
include 'header.php';
include 'game.php';
include 'footer.php';
コード例 #3
0
ファイル: redis.php プロジェクト: awongh/cards
/**
 * subCurrentValue
 * subtracts a given value from the users 
 * overall total credits
 * @param userid
 * @param the value to be subtracted 
 * @return the new total value
**/
function subCurrentValue($id, $value)
{
    $redis = new Predis\Client();
    $current = getCurrentValue($id);
    $current -= $value;
    $redis->set("uid:{$id}:value", $current);
    return $current;
}