Thanks tarini ^^
Daikinee I am a BGA developer just like you and I do have partial access to the PHP source code of the framework, but I think you're looking for a BGA admin.
The reason I never used the Deck class is that the PHP library does all I ever needed, transparently.
pickCard( $location, $player_id ) =>
$player['hand'][] = array_pop($deck)
pickCards( $nbr, $location, $player_id ) =>
$player['hand'] = array_merge($player['hand'], array_splice($deck, -$nbr))
Now to emulate the features around $location_arg, I use array_column. It's not yet available on BGA (requires PHP >= 5.5) so I use a version attached at the end of this post.
How does it work? Imagine you have this deck of cards:
Code: Select all
$deck = Array(
Array('id' => 2, 'type' => 'weapon'),
Array('id' => 5, 'type' => 'spell'),
Array('id' => 3, 'type' => 'weapon'));
array_column($deck, 'type') returns Array('weapon', 'spell', 'weapon')
And you can do whatever you want with that:
Code: Select all
$weaponCardKeys = array_intersect(array_column($deck, 'type'), Array('weapon'));
$weaponCards = array_intersect_key($deck, $weaponCardKeys);
$deckWithoutWeapons = array_values(array_diff_key($deck, $weaponCardKeys));
I'm not saying you should avoid the Deck component, but if you want to use array manipulation instead, these hacks could help ^^
Here is the array_column function:
Code: Select all
function array_column($input = null, $columnKey = null, $indexKey = null) {
$params = func_get_args();
if (!is_array($params[0])) {
trigger_error('array_column() expects parameter 1 to be array, ' . gettype($params[0]) . ' given', E_USER_WARNING);
return null;
}
if (!is_int($params[1]) && !is_float($params[1]) && !is_string($params[1]) && $params[1] !== null && !(is_object($params[1]) && method_exists($params[1], '__toString'))
) {
trigger_error('array_column(): The column key should be either a string or an integer', E_USER_WARNING);
return false;
}
if (isset($params[2]) && !is_int($params[2]) && !is_float($params[2]) && !is_string($params[2]) && !(is_object($params[2]) && method_exists($params[2], '__toString'))
) {
trigger_error('array_column(): The index key should be either a string or an integer', E_USER_WARNING);
return false;
}
$paramsInput = $params[0];
$paramsColumnKey = ($params[1] !== null) ? (string) $params[1] : null;
$paramsIndexKey = null;
if (isset($params[2])) {
if (is_float($params[2]) || is_int($params[2])) {
$paramsIndexKey = (int) $params[2];
} else {
$paramsIndexKey = (string) $params[2];
}
}
$resultArray = array();
foreach ($paramsInput as $row) {
$key = $value = null;
$keySet = $valueSet = false;
if ($paramsIndexKey !== null && array_key_exists($paramsIndexKey, $row)) {
$keySet = true;
$key = (string) $row[$paramsIndexKey];
}
if ($paramsColumnKey === null) {
$valueSet = true;
$value = $row;
} elseif (is_array($row) && array_key_exists($paramsColumnKey, $row)) {
$valueSet = true;
$value = $row[$paramsColumnKey];
}
if ($valueSet) {
if ($keySet) {
$resultArray[$key] = $value;
} else {
$resultArray[] = $value;
}
}
}
return $resultArray;
}