Design a gameplay ability system with cooldowns, resource costs and timed buffs
Company: Epic Games
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: medium
Interview Round: Other
Design a gameplay ability system where players can register and trigger different abilities, such as firing a gun or casting a spell.
The system must handle cooldowns between ability uses, resource costs (such as mana or ammo), and effects, including damage accumulation and timed buffs.
Each ability has a cooldown period, a resource cost of a specific type, a damage value, and an optional buff with a duration. To use an ability, a player must have enough of its resource and the ability must not be on cooldown for that player. Buffs remain active until their duration expires.
Implement a class `GameplayAbilitySystem` with these methods:
- `register_ability(name: str, cooldown: int, cost: int, resource_type: str, damage: int, buff_name: str, buff_duration: int) -> None` registers an ability with its cooldown time, resource cost, resource type, damage value, and optional buff (an empty string means no buff) with its duration.
- `set_resource(player_id: str, resource_type: str, amount: int) -> None` sets a player's resource (for example mana or ammo) to a specific amount.
- `use_ability(player_id: str, ability_name: str, timestamp: int) -> str` attempts to use an ability at the given timestamp and returns `"SUCCESS"`, `"ABILITY_NOT_FOUND"`, `"ON_COOLDOWN"` or `"NOT_ENOUGH_RESOURCE"`.
- `get_resource(player_id: str, resource_type: str) -> int` returns the player's current amount of that resource.
- `get_damage_dealt(player_id: str) -> int` returns the total damage the player has dealt across all successful ability uses.
- `get_active_buffs(player_id: str, timestamp: int) -> List[str]` returns the names of the player's active buffs at the given timestamp, sorted ascending. A buff expires when the timestamp equals or exceeds its expiry time.
- `get_status(player_id: str, timestamp: int) -> List[str]` returns a summary of the player's state: resource amounts, total damage dealt, and active buffs.
These rules fix the expected output:
- `use_ability` checks, in this order, that the ability exists, that it is off cooldown for this player, and that the player holds at least `cost` of its resource type. The first failed check decides the return value, and a failed use changes no state.
- A successful use at time `t` deducts the cost, keeps the ability on cooldown for that player until `t + cooldown` (it can be used again at that timestamp), adds its damage to the player's total, and, when the ability has a buff, makes the buff active until `t + buff_duration`.
- A resource the player was never given counts as 0.
- `get_status` returns one `"type:amount"` entry for each resource type set for the player with `set_resource`, in ascending order of type name, then `"damage_dealt:<total>"`, then `"buffs:<names>"` with the active buff names sorted and joined by commas, or `"buffs:none"` when no buff is active.
For example, one session:
```text
register_ability("fireball", 3, 10, "mana", 50, "burning", 2)
register_ability("shoot", 0, 1, "ammo", 5, "", 0)
set_resource("p1", "mana", 25)
set_resource("p1", "ammo", 2)
use_ability("p1", "fireball", 0) -> "SUCCESS"
get_active_buffs("p1", 1) -> ["burning"]
use_ability("p1", "fireball", 2) -> "ON_COOLDOWN"
use_ability("p1", "shoot", 2) -> "SUCCESS"
get_active_buffs("p1", 2) -> []
use_ability("p1", "fireball", 3) -> "SUCCESS"
use_ability("p1", "fireball", 4) -> "ON_COOLDOWN"
get_status("p1", 4) -> ["ammo:1", "mana:5", "damage_dealt:105", "buffs:burning"]
use_ability("p1", "fireball", 6) -> "NOT_ENOUGH_RESOURCE"
use_ability("p1", "heal", 6) -> "ABILITY_NOT_FOUND"
get_status("p2", 6) -> ["damage_dealt:0", "buffs:none"]
```
The first fireball leaves 15 mana and keeps `burning` active until timestamp 2, so the buff is gone by the query at 2. At timestamp 4 the fireball is on cooldown until 6 and the player also has only 5 mana; the cooldown check runs first, so the result is `"ON_COOLDOWN"`. At timestamp 6 the cooldown is over and the mana shortage decides the result. Player `"p2"` has no recorded state.
```hint What to store
Keep ability definitions apart from per-player state. For cooldowns and buffs, think about which single timestamp you can store so that every later check is one comparison.
```
```hint Watch the reads
Decide what a query about an unknown player or resource returns, and make sure answering it does not add entries that later show up in get_status.
```
### Constraints & Assumptions
- `1 <= ability name length <= 20`
- `0 <= cooldown <= 1000`
- `0 <= cost <= 1000`
- `0 <= damage <= 10000`
- `0 <= buff_duration <= 1000`
- `0 <= resource amount <= 10^6`
- Timestamps are non-negative integers and non-decreasing per player.
- At most `10^4` total operations.
### Clarifying Questions to Ask
- If a player gains a buff that is still active, does its expiry reset, extend, or stack?
- Can two different abilities grant the same buff name, and if so, which expiry wins?
- If an ability is registered again under the same name, does the new definition replace the old one, and do running cooldowns carry over?
- Will the methods be called from several threads, or does one game loop own all state?
### What a Strong Answer Covers
- A split between shared ability definitions and per-player state, with a concrete structure for resources, cooldowns, damage and buffs.
- The order of checks in `use_ability` and how a failed use is kept free of side effects.
- Exact handling of the expiry boundary for both cooldowns and buffs.
- What the queries return for unknown players and resources, and whether reads modify stored state.
- How expired buffs are cleaned up and what each method costs.
### Follow-up Questions
- How would you support buffs that modify later casts, such as a damage multiplier or a shorter cooldown?
- How would you add resource regeneration over time without running a timer for every player?
- What changes if `use_ability` can be called for the same player from several threads at once?
- What breaks if timestamps can arrive out of order, and how would you handle it?
Overview: Implement a gameplay ability system that registers abilities and lets players use them while enforcing per-player cooldowns, resource costs such as mana or ammo, accumulated damage and timed buffs. It tests low-level design, state modeling, validating before applying changes, and careful handling of expiry boundaries.