Skip to content

Commit

Permalink
Merge pull request #9 from insthync/1.23
Browse files Browse the repository at this point in the history
1.23
  • Loading branch information
insthync authored Dec 25, 2022
2 parents b946bd2 + d5378e1 commit 2fc9597
Show file tree
Hide file tree
Showing 7 changed files with 384 additions and 239 deletions.
2 changes: 1 addition & 1 deletion GameData.json

Large diffs are not rendered by default.

137 changes: 137 additions & 0 deletions functions/FortuneWheel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
<?php
function RandomFortuneWheelReward($fortuneWheel)
{
$rewards = $fortuneWheel['rewards'];
$generatedResult = array();
$generatedWeight = array();
$countRewards = count($rewards);
for ($i = 0; $i < $countRewards; ++$i)
{
$reward = $rewards[$i];
$id = '_' . $i;
$generatedResult[$id] = $reward;
$generatedResult[$id]['rewardIndex'] = $i;
$generatedWeight[$id] = $reward['randomWeight'];
}

$takenId = WeightedRandom($generatedWeight, 0);
if ($takenId) {
return $generatedResult[$takenId];
}
return NULL;
}

function GetAvailableFortuneWheelList()
{
$list = array();
$gameData = \Base::instance()->get('GameData');
$fortuneWheels = $gameData['fortuneWheels'];
foreach ($fortuneWheels as $key => $value) {
$list[] = $key;
}
$output = array('error' => '');
$output['list'] = $list;
echo json_encode($output);
}

function SpinFortuneWheel($fortuneWheelDataId)
{
$gameData = \Base::instance()->get('GameData');
$output = array('error' => '');
$player = GetPlayer();
$playerId = $player->id;

$fortuneWheel = $gameData['fortuneWheels'][$fortuneWheelDataId];
if (!$fortuneWheel) {
$output['error'] = 'ERROR_INVALID_FORTUNE_WHEEL_DATA';
} else {
$softCurrency = GetCurrency($playerId, $gameData['softCurrencyId']);
$hardCurrency = GetCurrency($playerId, $gameData['hardCurrencyId']);

$rewardIndex = 0;
$rewardItems = array();
$createItems = array();
$updateItems = array();
$deleteItemIds = array();
$updateCurrencies = array();
$requirementType = $fortuneWheel['requirementType'];
$price = $fortuneWheel['price'];
if ($requirementType == ELootboxRequirementType::SoftCurrency && $price > $softCurrency->amount) {
$output['error'] = 'ERROR_NOT_ENOUGH_SOFT_CURRENCY';
} else if ($requirementType == ELootboxRequirementType::HardCurrency && $price > $hardCurrency->amount) {
$output['error'] = 'ERROR_NOT_ENOUGH_HARD_CURRENCY';
} else {
switch ($requirementType)
{
case ELootboxRequirementType::SoftCurrency:
$softCurrency->amount -= $price;
$softCurrency->update();
$updateCurrencies[] = $softCurrency;
break;
case ELootboxRequirementType::HardCurrency:
$hardCurrency->amount -= $price;
$hardCurrency->update();
$updateCurrencies[] = $hardCurrency;
break;
}

$reward = RandomFortuneWheelReward($fortuneWheel);
if ($reward)
{
$rewardIndex = $reward['rewardIndex'];

// Soft currency
$rewardSoftCurrency = $reward['rewardSoftCurrency'];
$softCurrency = GetCurrency($playerId, $gameData['softCurrencyId']);
$softCurrency->amount += $rewardSoftCurrency;
$softCurrency->update();
$updateCurrencies[] = $softCurrency;

// Hard currency
$rewardHardCurrency = $reward['rewardHardCurrency'];
$hardCurrency = GetCurrency($playerId, $gameData['hardCurrencyId']);
$hardCurrency->amount += $rewardHardCurrency;
$hardCurrency->update();
$updateCurrencies[] = $hardCurrency;

$wheelRewardItems = $reward['rewardItems'];
$countRewardItems = count($wheelRewardItems);
for ($i = 0; $i < $countRewardItems; ++$i)
{
$rewardItem = $wheelRewardItems[$i];
$addItemsResult = AddItems($playerId, $rewardItem['id'], $rewardItem['amount']);
if ($addItemsResult['success'])
{
$rewards[] = CreateEmptyItem($i, $playerId, $rewardItem['id'], $rewardItem['amount']);

$resultCreateItems = $addItemsResult['createItems'];
$resultUpdateItems = $addItemsResult['updateItems'];
$countCreateItems = count($resultCreateItems);
$countUpdateItems = count($resultUpdateItems);
for ($j = 0; $j < $countCreateItems; ++$j)
{
$createItem = $resultCreateItems[$j];
$createItem->save();
HelperUnlockItem($playerId, $createItem->dataId);
$createItems[] = $createItem;
}
for ($j = 0; $j < $countUpdateItems; ++$j)
{
$updateItem = $resultUpdateItems[$j];
$updateItem->update();
$updateItems[] = $updateItem;
}
}
}
}
}
$output['rewardItems'] = ItemCursorsToArray($rewardItems);
$output['createItems'] = ItemCursorsToArray($createItems);
$output['updateItems'] = ItemCursorsToArray($updateItems);
$output['deleteItemIds'] = $deleteItemIds;
$output['updateCurrencies'] = CursorsToArray($updateCurrencies);
$output['rewardIndex'] = $rewardIndex;
}
echo json_encode($output);
}
?>
21 changes: 0 additions & 21 deletions functions/Helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,27 +157,6 @@ function WeightedRandom($weights, $noResultWeight)
return $selected;
}

function RandomLootBoxReward($lootBox)
{
$lootboxRewards = $lootBox['lootboxRewards'];
$generatedResult = array();
$generatedWeight = array();
$countLootboxRewards = count($lootboxRewards);
for ($i = 0; $i < $countLootboxRewards; ++$i)
{
$lootboxReward = $lootboxRewards[$i];
$id = '_' . $i;
$generatedResult[$id] = $lootboxReward;
$generatedWeight[$id] = $lootboxReward['randomWeight'];
}

$takenId = WeightedRandom($generatedWeight, 0);
if ($takenId) {
return $generatedResult[$takenId];
}
return NULL;
}

function RandomRandomStoreItems($store)
{
$items = $store['items'];
Expand Down
117 changes: 117 additions & 0 deletions functions/IAP.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
<?php
function GetAvailableIapPackageList()
{
$list = array();
$gameData = \Base::instance()->get('GameData');
$iapPackages = $gameData['iapPackages'];
foreach ($iapPackages as $key => $value) {
$list[] = $key;
}
$output = array('error' => '');
$output['list'] = $list;
echo json_encode($output);
}

function GetAvailableInGamePackageList()
{
$list = array();
$gameData = \Base::instance()->get('GameData');
$inGamePackages = $gameData['inGamePackages'];
foreach ($inGamePackages as $key => $value) {
$list[] = $key;
}
$output = array('error' => '');
$output['list'] = $list;
echo json_encode($output);
}

function OpenInGamePackage($inGamePackageDataId)
{
$gameData = \Base::instance()->get('GameData');
$output = array('error' => '');
$player = GetPlayer();
$playerId = $player->id;

$inGamePackage = $gameData['inGamePackages'][$inGamePackageDataId];
if (!$inGamePackage) {
$output['error'] = 'ERROR_INVALID_IN_GAME_PACKAGE_DATA';
} else {
$softCurrency = GetCurrency($playerId, $gameData['softCurrencyId']);
$hardCurrency = GetCurrency($playerId, $gameData['hardCurrencyId']);

$rewardItems = array();
$createItems = array();
$updateItems = array();
$deleteItemIds = array();
$updateCurrencies = array();
$requirementType = $inGamePackage['requirementType'];
$price = $inGamePackage['price'];
$rewardSoftCurrency = $inGamePackage['rewardSoftCurrency'];
$rewardHardCurrency = $inGamePackage['rewardHardCurrency'];
if ($requirementType == EInGamePackageRequirementType::SoftCurrency && $price > $softCurrency->amount) {
$output['error'] = 'ERROR_NOT_ENOUGH_SOFT_CURRENCY';
} else if ($requirementType == EInGamePackageRequirementType::HardCurrency && $price > $hardCurrency->amount) {
$output['error'] = 'ERROR_NOT_ENOUGH_HARD_CURRENCY';
} else {
switch ($requirementType)
{
case EInGamePackageRequirementType::SoftCurrency:
$softCurrency->amount -= $price;
break;
case EInGamePackageRequirementType::HardCurrency:
$hardCurrency->amount -= $price;
break;
}
// Increase soft currency
$softCurrency->amount += $rewardSoftCurrency;
$softCurrency->update();
$updateCurrencies[] = $softCurrency;
// Increase hard currency
$hardCurrency->amount += $rewardHardCurrency;
$hardCurrency->update();
$updateCurrencies[] = $hardCurrency;

$countRewardItems = count($inGamePackage['rewardItems']);
for ($i = 0; $i < $countRewardItems; ++$i)
{
$rewardItem = $inGamePackage['rewardItems'][$i];
if (empty($rewardItem) || empty($rewardItem['id'])) {
continue;
}

$addItemsResult = AddItems($playerId, $rewardItem['id'], $rewardItem['amount']);
if ($addItemsResult['success'])
{
$rewardItems[] = CreateEmptyItem($i, $playerId, $rewardItem['id'], $rewardItem['amount']);

$resultCreateItems = $addItemsResult['createItems'];
$resultUpdateItems = $addItemsResult['updateItems'];
$countCreateItems = count($resultCreateItems);
$countUpdateItems = count($resultUpdateItems);
for ($j = 0; $j < $countCreateItems; ++$j)
{
$createItem = $resultCreateItems[$j];
$createItem->save();
HelperUnlockItem($playerId, $createItem->dataId);
$createItems[] = $createItem;
}
for ($j = 0; $j < $countUpdateItems; ++$j)
{
$updateItem = $resultUpdateItems[$j];
$updateItem->update();
$updateItems[] = $updateItem;
}
}
}
$output['rewardItems'] = ItemCursorsToArray($rewardItems);
$output['createItems'] = ItemCursorsToArray($createItems);
$output['updateItems'] =ItemCursorsToArray($updateItems);
$output['deleteItemIds'] = $deleteItemIds;
$output['updateCurrencies'] = CursorsToArray($updateCurrencies);
$output['rewardSoftCurrency'] = $rewardSoftCurrency;
$output['rewardHardCurrency'] = $rewardHardCurrency;
}
}
echo json_encode($output);
}
?>
Loading

0 comments on commit 2fc9597

Please sign in to comment.