当前位置: 面试刷题>> 询问冷却时间 (经典算法题500道)


题目描述补充

"码小课网站正在设计一个在线游戏系统,其中每个玩家在使用特定技能后需要等待一段时间(即冷却时间)才能再次使用该技能。系统需要记录每个玩家每个技能的最后使用时间,并在玩家尝试再次使用该技能时检查是否已经过了冷却时间。如果过了冷却时间,则允许使用并更新该技能的最后使用时间;如果未过冷却时间,则返回错误信息提示玩家还需等待多久。

请设计并实现一个函数,该函数接受玩家ID、技能ID和当前时间戳作为参数,返回是否允许使用技能以及如果不允许,还需要等待多久(以秒为单位)的信息。假设每个技能的冷却时间是固定的,且所有时间戳都是UNIX时间戳(整数类型)。"

示例代码实现

PHP 示例

<?php

class SkillCooldownManager {
    private $cooldowns = [];

    public function canUseSkill($playerId, $skillId, $currentTime, $cooldownSeconds) {
        $key = "$playerId-$skillId";
        if (!isset($this->cooldowns[$key])) {
            // 如果没有记录,说明技能刚被使用或从未使用,允许使用并更新时间
            $this->cooldowns[$key] = $currentTime;
            return ['canUse' => true, 'waitTime' => 0];
        }

        $lastUsedTime = $this->cooldowns[$key];
        $elapsedTime = $currentTime - $lastUsedTime;

        if ($elapsedTime >= $cooldownSeconds) {
            // 如果已过冷却时间,允许使用并更新时间
            $this->cooldowns[$key] = $currentTime;
            return ['canUse' => true, 'waitTime' => 0];
        } else {
            // 如果未过冷却时间,返回还需等待的时间
            return ['canUse' => false, 'waitTime' => $cooldownSeconds - $elapsedTime];
        }
    }
}

// 使用示例
$manager = new SkillCooldownManager();
$result = $manager->canUseSkill(1, 1, time(), 10); // 假设当前时间是UNIX时间戳,技能冷却时间为10秒
print_r($result);

?>

Python 示例

class SkillCooldownManager:
    def __init__(self):
        self.cooldowns = {}

    def can_use_skill(self, player_id, skill_id, current_time, cooldown_seconds):
        key = f"{player_id}-{skill_id}"
        if key not in self.cooldowns:
            # 如果没有记录,则允许使用并更新时间
            self.cooldowns[key] = current_time
            return True, 0

        last_used_time = self.cooldowns[key]
        elapsed_time = current_time - last_used_time

        if elapsed_time >= cooldown_seconds:
            # 如果已过冷却时间,允许使用并更新时间
            self.cooldowns[key] = current_time
            return True, 0
        else:
            # 如果未过冷却时间,返回还需等待的时间
            return False, cooldown_seconds - elapsed_time

# 使用示例
manager = SkillCooldownManager()
result = manager.can_use_skill(1, 1, int(time.time()), 10)  # 假设当前时间是UNIX时间戳,技能冷却时间为10秒
print(result)

注意: Python 示例中使用了 time.time() 来获取当前时间戳,并需要导入 time 模块(import time)。

JavaScript 示例

class SkillCooldownManager {
    constructor() {
        this.cooldowns = {};
    }

    canUseSkill(playerId, skillId, currentTime, cooldownSeconds) {
        const key = `${playerId}-${skillId}`;
        if (!(key in this.cooldowns)) {
            // 如果没有记录,则允许使用并更新时间
            this.cooldowns[key] = currentTime;
            return { canUse: true, waitTime: 0 };
        }

        const lastUsedTime = this.cooldowns[key];
        const elapsedTime = currentTime - lastUsedTime;

        if (elapsedTime >= cooldownSeconds) {
            // 如果已过冷却时间,允许使用并更新时间
            this.cooldowns[key] = currentTime;
            return { canUse: true, waitTime: 0 };
        } else {
            // 如果未过冷却时间,返回还需等待的时间
            return { canUse: false, waitTime: cooldownSeconds - elapsedTime };
        }
    }
}

// 使用示例
const manager = new SkillCooldownManager();
const currentTime = Date.now() / 1000; // JavaScript中的Date.now()返回毫秒,需要转换为秒
const result = manager.canUseSkill(1, 1, currentTime, 10); // 假设当前时间是UNIX时间戳,技能冷却时间为10秒
console.log(result);

以上代码示例均实现了题目要求的功能,并给出了如何使用这些类的示例。

推荐面试题