Sub topic 14
Exploits, Ledgers, and Safeguards
What can go wrong
Assume you understand basic game loops, rewards, shops, and percentages. You should also be comfortable reading short Python code and reasoning about rates.
An economy exploit is a way to create, duplicate, or spend resources outside the intended rules. Common causes include:
- A client sends a reward request more than once.
- A trade completes twice after a network retry.
- A negative amount increases a balance.
- A player spends the same balance in two requests at nearly the same time.
- A reward has no daily or mission limit.
- A purchase is granted, but the payment is never recorded.
Treat every currency change as a financial transaction. The server should decide whether the change is valid. The client can display a result, but it should not be trusted to calculate or authorize currency.
Use a ledger for every change
A ledger is a record of currency transactions. Each entry should include the player, currency, signed amount, reason, timestamp, and unique transaction ID. A balance can then be checked against the ledger or rebuilt from it.
Keep earning and spending behind a small interface. This prevents unrelated game systems from changing balances directly.
from collections import defaultdict
class Ledger:
def __init__(self):
self.balance = 0
self.earned = defaultdict(int)
self.spent = defaultdict(int)
def earn(self, reason, amount):
if amount <= 0:
raise ValueError("amount must be positive")
self.balance += amount
self.earned[reason] += amount
def spend(self, reason, amount):
if amount <= 0:
raise ValueError("amount must be positive")
if amount > self.balance:
raise ValueError("insufficient funds")
self.balance -= amount
self.spent[reason] += amount
wallet = Ledger()
wallet.earn("quest_reward", 500)
wallet.spend("weapon_repair", 120)
print(wallet.balance, dict(wallet.earned), dict(wallet.spent))In production, store each accepted transaction durably. Use an idempotency key, which is a unique request ID. If the same request arrives twice, return the original result instead of applying it twice.
Use an atomic operation for spending. “Atomic” means the balance check and deduction happen as one indivisible operation. Otherwise, two simultaneous purchases might both observe the same balance and create an overdraft.
Add limits and validation
Validation should happen on the server at the trust boundary. Check that:
- Amounts are positive integers or fixed-precision values.
- The player owns the item or reward being used.
- The request is authorized for that player.
- A reward has not already been claimed.
- Daily, match, or account caps have not been exceeded.
- A trade gives and removes exactly the intended items and currency.
Caps reduce the damage from both bugs and abuse. They should be visible when they affect normal play, and monitored so that a cap does not silently frustrate legitimate players.
Detect and recover
Track rejected requests, duplicate request IDs, unusual earning rates, rapid balance growth, and repeated cap hits. Alerts should identify the source and reason, not only the final balance.
When an exploit is found, preserve the ledger first. Then disable the affected source if necessary, identify impacted accounts, and apply reversible corrections where possible. Avoid silently deleting balances without an audit trail.
Exercise
Why should a retrying purchase use an idempotency key?
Show answer
The key lets the server recognize that the request was already processed, so a network retry does not grant the item or remove currency twice.