Skip to content

Commit

Permalink
update xml files, add update stamina, fix cache
Browse files Browse the repository at this point in the history
  • Loading branch information
unknown committed Aug 23, 2011
1 parent 777f06d commit c9a9b30
Show file tree
Hide file tree
Showing 12 changed files with 526 additions and 88 deletions.
38 changes: 33 additions & 5 deletions do.php
Original file line number Original file line Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php <?php


$player_id = $_GET['player_id']; $playerId = $_GET['playerId'];
$session_id = $_GET['session_id']; $sessionId = $_GET['sessionId'];


//required parameter validations. //required parameter validations.


Expand All @@ -13,16 +13,44 @@
$data = null; $data = null;
switch($method) switch($method)
{ {
case 'add_to_deck': case 'addToDeck':
$data = add_to_deck($_POST['player_id'], $_POST['deck_id'], $_POST['card_id']); $data = add_to_deck($_POST['playerId'], $_POST['deckId'], $_POST['cardId']);
break; break;
case 'remove_from_deck': case 'remove_from_deck':
$data = remove_from_deck($_POST['player_id'], $_POST['deck_id'], $_POST['card_order']); $data = remove_from_deck($_POST['player_id'], $_POST['deckId'], $_POST['cardOrder']);
break; break;
case 'test': case 'test':
require_once('inc/DataCache.php'); require_once('inc/DataCache.php');
$data = json_encode(get_all_cards()); $data = json_encode(get_all_cards());
break; break;
case 'testCard':
require_once('inc/DataCache.php');
$data = json_encode(get_all_cards());
break;

case 'testStamina':
require_once('inc/Common.php');
require_once('inc/DataCache.php');
$playerId = $_GET['playerId'];
$dec_sta = $_GET['useStamina'];
$player = getPlayerInfo($playerId);
debug_print($player);
$ret = updateStamina($player, $dec_sta);
break;

case 'startMission':
$missionId = $_GET['missionId'];
require_once('inc/BattleManager.php');
startMission($playerId, $missionId);
break;

case 'clearCache':
require_once('inc/CacheManager.php');
CacheManager::clearCache();
break;

default:

} }


print_r($data); print_r($data);
Expand Down
58 changes: 56 additions & 2 deletions inc/BattleManager.php
Original file line number Original file line Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php <?php




function initMission($mid) function initMission($mid)
{ {
Expand All @@ -14,8 +16,60 @@ function initMission($mid)
} }
} }


function start_mission($player_id, $mession_id) function startMission($playerId, $missionId)
{ {
require_once('inc/Common.php');
require_once('inc/DataCache.php');
require_once('inc/Battle.class.php');
//check if player is already engaged in battle
$player = getPlayerInfo($playerId);
$battle = $player->current_battle;
if ($battle) { //not null
//remove curent battle
}

$mission = getMissionInfo($missionId);

//init
$battle = new Battle();

$battle->id = $missionId;

$battle->start_time = time();
/*
public $end_time;
//????
public $version;
//????,??vs???
public $player_role;
public $attacker_name;
public $defender_name;
public $attacher_deck;
public $defender_deck;
public $attacker_shuffle;
public $defender_shuffle;
//????
public $win_condition;
public $win_condition_param;
public $lose_condition;
public $lose_condition_param;
public $current_round;
public $current_player;
//??????????
public $actions;
//????
public $battle_result;
*/

//save to cache
$player['current_battle'] = $battle;
updatePlayerInfo($player);


} }


Expand Down
21 changes: 19 additions & 2 deletions inc/CacheManager.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ public static function setValue($key, $group = null, $value, $duration)
{ {
$cacheOption = array( $cacheOption = array(
'cacheDir' => 'tmp/', 'cacheDir' => 'tmp/',
'lifeTime' => $duration 'lifeTime' => $duration,
'automaticSerialization' => true
); );


$cache = new Cache_Lite($cacheOption); $cache = new Cache_Lite($cacheOption);
Expand All @@ -26,14 +27,30 @@ public static function setValue($key, $group = null, $value, $duration)
public static function getValue($key, $group = null) public static function getValue($key, $group = null)
{ {
$cacheOption = array( $cacheOption = array(
'cacheDir' => 'tmp/' 'cacheDir' => 'tmp/',
'automaticSerialization' => true
); );


$cache = new Cache_Lite($cacheOption); $cache = new Cache_Lite($cacheOption);


return $cache->get($key, $group); return $cache->get($key, $group);
} }


/**
*
* Clear Cache
*/
public static function clearCache()
{
$cacheOption = array(
'cacheDir' => 'tmp/'
);

$cache = new Cache_Lite($cacheOption);

return $cache->clean();
}

} }


?> ?>
82 changes: 75 additions & 7 deletions inc/Common.php
Original file line number Original file line Diff line number Diff line change
@@ -1,5 +1,18 @@
<?php <?php



//print an object or array for debug information
function debug_print($obj) {
echo '<pre>';
print_r($obj);
echo '</pre>';
}


//********************************
// XML functions
//********************************

//parse xml to array //parse xml to array
//parameters: //parameters:
// file_name: the name of the xml file // file_name: the name of the xml file
Expand Down Expand Up @@ -117,13 +130,6 @@ function xml2array($file_name, $index_base=0, $value_key=null) {
} }




//print an object or array for debug information
function debug_print($obj) {
echo '<pre>';
print_r($obj);
echo '</pre>';
}



function getMissionsFromXML() { function getMissionsFromXML() {
$obj = xml2array('xml/missions.xml', 1); $obj = xml2array('xml/missions.xml', 1);
Expand All @@ -148,4 +154,66 @@ function getBuffsFromXML() {










//********************************
// Stamina calculation
//********************************


//return:
//>=0 : successfully consumed
//<0 : not enough stamina
function updateStamina(&$player, $stamina_consume=0) {
debug_print('Player had '.$player['stamina'].' stamina by '.$player['stamina_last_update']);
debug_print('Player uses '.$stamina_consume.' points of stamina');

$current_time = time();
$new_stamina = $player['stamina'];
$prev_min = floor($player['stamina_last_update'] / 60);
$current_min = floor($current_time / 60);
require_once('inc/Const.php');
if ($current_min > $prev_min) {
$new_stamina += ($current_min - $prev_min) * constant('STAMINA_PER_MIN');
if ($new_stamina > constant('STAMINA_MAX')) $new_stamina = constant('STAMINA_MAX');
}
$result = $new_stamina;
if ($stamina_consume > 0 ) {
if ($new_stamina > $stamina_consume) {
$new_stamina -= $stamina_consume;
$player['stamina'] = $new_stamina;
$player['stamina_last_update'] = $current_time;
$result = $new_stamina;
} else {
$result = -1;
debug_print('Not enough stamina!');
}
}
updatePlayerInfo($player);
debug_print('Now player has '.$player['stamina'].' stamina by '.$player['stamina_last_update']);
return $result;
}



//********************************
// time related
//********************************





//********************************
//
//********************************







?> ?>
40 changes: 33 additions & 7 deletions inc/Const.php
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -2,32 +2,58 @@


define('DECK_CARDS_MAX',20); define('DECK_CARDS_MAX',20);


define('STAMINA_MAX',100);
define('STAMINA_PER_MIN',1);

define('CACHE_GROUP_SYSTEM', 'SYSTEM');
define('CACHE_GROUP_PLAYER', 'PLAYER');
define('CACHE_GROUP_MISSION', 'MISSION');
define('CACHE_GROUP_CARD', 'CARD');
define('CACHE_GROUP_BATTLE', 'BATTLE');



//卡牌类型:CARD_TYPE_ //卡牌类型:CARD_TYPE_
//主公:lord //主公:lord
//建筑:building //建筑:building
//武将:hero
//士兵:unit //士兵:unit
// 武将和士兵统称army
//计策:spell //计策:spell
//装备:artifact //装备:artifact
define('CARD_TYPE_LORD',0); define('CARD_TYPE_LORD',0);
define('CARD_TYPE_BUILDING',1); define('CARD_TYPE_BUILDING',1);
define('CARD_TYPE_HERO',2); define('CARD_TYPE_UNIT',2);
define('CARD_TYPE_UNIT',3); define('CARD_TYPE_SPELL',3);
define('CARD_TYPE_SPELL',4); define('CARD_TYPE_ARTIFACT',4);
define('CARD_TYPE_ARTIFACT',5);


//卡牌在牌组中允许出现次数:CARD_OCCUR_
//唯一:UNIQUE
//可重复:MULTI
define('CARD_OCCUR_MULTI',0);
define('CARD_OCCUR_UNIQUE',1);

//卡牌势力或者阵营:CARD_FACTION_ //卡牌势力或者阵营:CARD_FACTION_
//魏、蜀、吴、群 //魏、蜀、吴、群
define('CARD_FACTION_WEI',0); define('CARD_FACTION_WEI',0);
define('CARD_FACTION_SHU',1); define('CARD_FACTION_SHU',1);
define('CARD_FACTION_WU',2); define('CARD_FACTION_WU',2);
define('CARD_FACTION_QUN',3); define('CARD_FACTION_QUN',3);


//卡包: CARD_PACKAGE_
//非玩家:npc
//奖励:reward
//黄巾之乱:HUANGJIN
//官渡之战:GUANDU
//赤壁之战:CHIBI
//三国鼎立:SANGUO
define('CARD_PACKAGE_NPC',0);
define('CARD_PACKAGE_REWARD',1);
define('CARD_PACKAGE_HUANGJIN',2);
define('CARD_PACKAGE_GUANDU',3);
define('CARD_PACKAGE_CHIBI',4);
define('CARD_PACKAGE_SANGUO',5);

//卡牌稀有度: CARD_RARITY_ //卡牌稀有度: CARD_RARITY_
//普通common: black //普通common: black
//特殊uncommon: silver //罕见uncommon: silver
//稀有rare: gold //稀有rare: gold
//史诗epic: purple //史诗epic: purple
define('CARD_RARITY_COMMON',0); define('CARD_RARITY_COMMON',0);
Expand Down
Loading

0 comments on commit c9a9b30

Please sign in to comment.