|
|
@@ -11,24 +11,24 @@ namespace App\Module\Game\Logics;
|
|
|
class RewardCollectorLogic
|
|
|
{
|
|
|
/**
|
|
|
- * 物品奖励数据
|
|
|
- * 格式:[item_id => ['item_id' => int, 'instance_id' => int, 'quantity' => int]]
|
|
|
+ * 物品奖励数据,按用户分组
|
|
|
+ * 格式:[user_id => [item_key => ['user_id' => int, 'item_id' => int, 'instance_id' => int, 'quantity' => int]]]
|
|
|
*
|
|
|
* @var array
|
|
|
*/
|
|
|
private static array $itemRewards = [];
|
|
|
|
|
|
/**
|
|
|
- * 代币奖励数据
|
|
|
- * 格式:[coin_type => ['type' => int, 'quantity' => int]]
|
|
|
+ * 代币奖励数据,按用户分组
|
|
|
+ * 格式:[user_id => [coin_type => ['user_id' => int, 'type' => int, 'quantity' => int]]]
|
|
|
*
|
|
|
* @var array
|
|
|
*/
|
|
|
private static array $coinRewards = [];
|
|
|
|
|
|
/**
|
|
|
- * 神像奖励数据
|
|
|
- * 格式:[god_type => ['type' => int, 'diff' => int, 'quantity' => int]]
|
|
|
+ * 神像奖励数据,按用户分组
|
|
|
+ * 格式:[user_id => [god_type => ['user_id' => int, 'type' => int, 'diff' => int, 'quantity' => int]]]
|
|
|
*
|
|
|
* @var array
|
|
|
*/
|
|
|
@@ -37,21 +37,27 @@ class RewardCollectorLogic
|
|
|
/**
|
|
|
* 添加物品奖励
|
|
|
*
|
|
|
+ * @param int $userId 用户ID
|
|
|
* @param int $itemId 物品ID
|
|
|
* @param int $instanceId 物品实例ID
|
|
|
* @param int $quantity 奖励数量
|
|
|
* @return void
|
|
|
*/
|
|
|
- public static function addItemReward(int $itemId, int $instanceId, int $quantity): void
|
|
|
+ public static function addItemReward(int $userId, int $itemId, int $instanceId, int $quantity): void
|
|
|
{
|
|
|
- $key = $itemId . '_' . $instanceId;
|
|
|
-
|
|
|
- if (isset(self::$itemRewards[$key])) {
|
|
|
+ $key = "{$itemId}_{$instanceId}";
|
|
|
+
|
|
|
+ if (!isset(self::$itemRewards[$userId])) {
|
|
|
+ self::$itemRewards[$userId] = [];
|
|
|
+ }
|
|
|
+
|
|
|
+ if (isset(self::$itemRewards[$userId][$key])) {
|
|
|
// 如果已存在,累加数量
|
|
|
- self::$itemRewards[$key]['quantity'] += $quantity;
|
|
|
+ self::$itemRewards[$userId][$key]['quantity'] += $quantity;
|
|
|
} else {
|
|
|
// 新增奖励记录
|
|
|
- self::$itemRewards[$key] = [
|
|
|
+ self::$itemRewards[$userId][$key] = [
|
|
|
+ 'user_id' => $userId,
|
|
|
'item_id' => $itemId,
|
|
|
'instance_id' => $instanceId,
|
|
|
'quantity' => $quantity,
|
|
|
@@ -62,18 +68,24 @@ class RewardCollectorLogic
|
|
|
/**
|
|
|
* 添加代币奖励
|
|
|
*
|
|
|
+ * @param int $userId 用户ID
|
|
|
* @param int $coinType 代币类型
|
|
|
* @param int $quantity 奖励数量
|
|
|
* @return void
|
|
|
*/
|
|
|
- public static function addCoinReward(int $coinType, int $quantity): void
|
|
|
+ public static function addCoinReward(int $userId, int $coinType, int $quantity): void
|
|
|
{
|
|
|
- if (isset(self::$coinRewards[$coinType])) {
|
|
|
+ if (!isset(self::$coinRewards[$userId])) {
|
|
|
+ self::$coinRewards[$userId] = [];
|
|
|
+ }
|
|
|
+
|
|
|
+ if (isset(self::$coinRewards[$userId][$coinType])) {
|
|
|
// 如果已存在,累加数量
|
|
|
- self::$coinRewards[$coinType]['quantity'] += $quantity;
|
|
|
+ self::$coinRewards[$userId][$coinType]['quantity'] += $quantity;
|
|
|
} else {
|
|
|
// 新增奖励记录
|
|
|
- self::$coinRewards[$coinType] = [
|
|
|
+ self::$coinRewards[$userId][$coinType] = [
|
|
|
+ 'user_id' => $userId,
|
|
|
'type' => $coinType,
|
|
|
'quantity' => $quantity,
|
|
|
];
|
|
|
@@ -83,20 +95,26 @@ class RewardCollectorLogic
|
|
|
/**
|
|
|
* 添加神像奖励
|
|
|
*
|
|
|
+ * @param int $userId 用户ID
|
|
|
* @param int $godType 神像类型
|
|
|
* @param int $diff 时间差值(秒)
|
|
|
* @param int $quantity 奖励数量
|
|
|
* @return void
|
|
|
*/
|
|
|
- public static function addGodReward(int $godType, int $diff, int $quantity): void
|
|
|
+ public static function addGodReward(int $userId, int $godType, int $diff, int $quantity): void
|
|
|
{
|
|
|
- if (isset(self::$godRewards[$godType])) {
|
|
|
+ if (!isset(self::$godRewards[$userId])) {
|
|
|
+ self::$godRewards[$userId] = [];
|
|
|
+ }
|
|
|
+
|
|
|
+ if (isset(self::$godRewards[$userId][$godType])) {
|
|
|
// 如果已存在,累加数量和时间
|
|
|
- self::$godRewards[$godType]['quantity'] += $quantity;
|
|
|
- self::$godRewards[$godType]['diff'] += $diff;
|
|
|
+ self::$godRewards[$userId][$godType]['quantity'] += $quantity;
|
|
|
+ self::$godRewards[$userId][$godType]['diff'] += $diff;
|
|
|
} else {
|
|
|
// 新增奖励记录
|
|
|
- self::$godRewards[$godType] = [
|
|
|
+ self::$godRewards[$userId][$godType] = [
|
|
|
+ 'user_id' => $userId,
|
|
|
'type' => $godType,
|
|
|
'diff' => $diff,
|
|
|
'quantity' => $quantity,
|
|
|
@@ -107,14 +125,41 @@ class RewardCollectorLogic
|
|
|
/**
|
|
|
* 获取本次请求的所有奖励数据
|
|
|
*
|
|
|
+ * @param int|null $userId 用户ID,如果为null则返回所有用户的数据
|
|
|
* @return array 奖励数据数组,包含items、coins和gods
|
|
|
*/
|
|
|
- public static function getRewards(): array
|
|
|
+ public static function getRewards(?int $userId = null): array
|
|
|
{
|
|
|
+ if ($userId !== null) {
|
|
|
+ // 返回指定用户的奖励数据
|
|
|
+ return [
|
|
|
+ 'items' => array_values(self::$itemRewards[$userId] ?? []),
|
|
|
+ 'coins' => array_values(self::$coinRewards[$userId] ?? []),
|
|
|
+ 'gods' => array_values(self::$godRewards[$userId] ?? []),
|
|
|
+ ];
|
|
|
+ }
|
|
|
+
|
|
|
+ // 返回所有用户的奖励数据,合并为一个数组
|
|
|
+ $allItems = [];
|
|
|
+ $allCoins = [];
|
|
|
+ $allGods = [];
|
|
|
+
|
|
|
+ foreach (self::$itemRewards as $userItems) {
|
|
|
+ $allItems = array_merge($allItems, array_values($userItems));
|
|
|
+ }
|
|
|
+
|
|
|
+ foreach (self::$coinRewards as $userCoins) {
|
|
|
+ $allCoins = array_merge($allCoins, array_values($userCoins));
|
|
|
+ }
|
|
|
+
|
|
|
+ foreach (self::$godRewards as $userGods) {
|
|
|
+ $allGods = array_merge($allGods, array_values($userGods));
|
|
|
+ }
|
|
|
+
|
|
|
return [
|
|
|
- 'items' => array_values(self::$itemRewards),
|
|
|
- 'coins' => array_values(self::$coinRewards),
|
|
|
- 'gods' => array_values(self::$godRewards),
|
|
|
+ 'items' => $allItems,
|
|
|
+ 'coins' => $allCoins,
|
|
|
+ 'gods' => $allGods,
|
|
|
];
|
|
|
}
|
|
|
|
|
|
@@ -133,40 +178,72 @@ class RewardCollectorLogic
|
|
|
/**
|
|
|
* 检查是否有奖励数据
|
|
|
*
|
|
|
+ * @param int|null $userId 用户ID,如果为null则检查所有用户
|
|
|
* @return bool
|
|
|
*/
|
|
|
- public static function hasRewards(): bool
|
|
|
+ public static function hasRewards(?int $userId = null): bool
|
|
|
{
|
|
|
+ if ($userId !== null) {
|
|
|
+ return !empty(self::$itemRewards[$userId]) || !empty(self::$coinRewards[$userId]) || !empty(self::$godRewards[$userId]);
|
|
|
+ }
|
|
|
+
|
|
|
return !empty(self::$itemRewards) || !empty(self::$coinRewards) || !empty(self::$godRewards);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取物品奖励数据
|
|
|
*
|
|
|
+ * @param int|null $userId 用户ID,如果为null则返回所有用户的数据
|
|
|
* @return array
|
|
|
*/
|
|
|
- public static function getItemRewards(): array
|
|
|
+ public static function getItemRewards(?int $userId = null): array
|
|
|
{
|
|
|
- return array_values(self::$itemRewards);
|
|
|
+ if ($userId !== null) {
|
|
|
+ return array_values(self::$itemRewards[$userId] ?? []);
|
|
|
+ }
|
|
|
+
|
|
|
+ $allItems = [];
|
|
|
+ foreach (self::$itemRewards as $userItems) {
|
|
|
+ $allItems = array_merge($allItems, array_values($userItems));
|
|
|
+ }
|
|
|
+ return $allItems;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取代币奖励数据
|
|
|
*
|
|
|
+ * @param int|null $userId 用户ID,如果为null则返回所有用户的数据
|
|
|
* @return array
|
|
|
*/
|
|
|
- public static function getCoinRewards(): array
|
|
|
+ public static function getCoinRewards(?int $userId = null): array
|
|
|
{
|
|
|
- return array_values(self::$coinRewards);
|
|
|
+ if ($userId !== null) {
|
|
|
+ return array_values(self::$coinRewards[$userId] ?? []);
|
|
|
+ }
|
|
|
+
|
|
|
+ $allCoins = [];
|
|
|
+ foreach (self::$coinRewards as $userCoins) {
|
|
|
+ $allCoins = array_merge($allCoins, array_values($userCoins));
|
|
|
+ }
|
|
|
+ return $allCoins;
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 获取神像奖励数据
|
|
|
*
|
|
|
+ * @param int|null $userId 用户ID,如果为null则返回所有用户的数据
|
|
|
* @return array
|
|
|
*/
|
|
|
- public static function getGodRewards(): array
|
|
|
+ public static function getGodRewards(?int $userId = null): array
|
|
|
{
|
|
|
- return array_values(self::$godRewards);
|
|
|
+ if ($userId !== null) {
|
|
|
+ return array_values(self::$godRewards[$userId] ?? []);
|
|
|
+ }
|
|
|
+
|
|
|
+ $allGods = [];
|
|
|
+ foreach (self::$godRewards as $userGods) {
|
|
|
+ $allGods = array_merge($allGods, array_values($userGods));
|
|
|
+ }
|
|
|
+ return $allGods;
|
|
|
}
|
|
|
}
|