for social games
Oh, you are kidding. You already know what energy is. But, just to make it clear, energy is a concept that is consumable and recoverable in social games. It limits how far players can advance in each session.
Players use energy to perform actions such as farming, housing, or social interactions. Then consumed energy will be recovered after certain amount of time designed by the developer. Recovery is the essence of energy system. It will make players to come back to the game periodically.
Popular social games such as FarmVille , Zoo Invasion or The Sims Social are benefited from the system in high retention rate.
Install via PyPI first:
$ easy_install energy
Or check out developement version:
$ git clone git://github.com/sublee/energy.git
The only thing you need to implement energy system is Energy object. Maximum energy and recovery interval have to be set in seconds before use:
from energy import Energy
energy = Energy(max=10, recovery_interval=300)
The example Energy object has 10 of maximum and will recover in every 5 minutes. When a player performs a action that requires energy just call Energy.use() method:
>>> print energy
<Energy 10/10>
>>> energy.use()
>>> print energy
<Energy 9/10 recover in 05:00>
If the player has not enough energy, it throws ValueError:
>>> print energy
<Energy 9/10 recover in 04:12>
>>> energy.use(10)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "energy.py", line 104, in use
raise ValueError('Not enough energy')
ValueError: Not enough energy
You may want to save Energy object within a specific player’s data in database.
An Energy object is serializable by Pickle. If you have a key-value storage, you can save an Energy object with a player easily. Or, you should prepare some columns for Energy.used and Energy.used_at in your database to save them. Here’s an example of save/load an Energy object:
>>> MAX_ENERGY, ENERGY_RECOVERY_INTERVAL = 10, 300
>>> energy = Energy(MAX_ENERGY, ENERGY_RECOVERY_INTERVAL)
>>> saved_used, saved_used_at = energy.used, energy.used_at
>>> loaded_energy = Energy(MAX_ENERGY, ENERGY_RECOVERY_INTERVAL,
... used=saved_used, used_at=saved_used_at)
>>> loaded_energy == energy
True
A consumable and recoverable stuff in social gamers. Think over reasonable energy parameters for your own game. Energy may decide return period of your players.
Parameters: |
|
---|---|
Raises TypeError: | |
some argument isn’t valid type |
Updates max or recovery_interval.
Parameters: |
|
---|
Calculates the current presentative energy. This equivalents to casting to int but can work with specified time.
>>> energy = Energy(10, 300)
>>> energy.use()
>>> energy.current()
9
>>> int(energy)
9
Parameters: | time – the time when checking the energy. Defaults to the present time in UTC. |
---|
Calculates the current energy debt.
>>> energy = Energy(10, 300)
>>> energy.debt()
>>> energy.use(11, force=True)
>>> energy.debt()
1
>>> energy.use(2, force=True)
3
Parameters: | time – the time when checking the energy. Defaults to the present time in UTC. |
---|
The near seconds to ignore exception when used at the future.
New in version 0.1.3.
The maximum energy.
Calculates the seconds passed from using the energy first.
Parameters: | time – the time when checking the energy. Defaults to the present time in UTC. |
---|---|
Raises ValueError: | |
used at the future |
Calculates seconds to be recovered fully. If the energy is full or over the maximum, this returns None.
Parameters: | time – the time when checking the energy. Defaults to the present time in UTC. |
---|
New in version 0.1.5.
Calculates seconds to the next energy recovery. If the energy is full or over the maximum, this returns None.
Parameters: | time – the time when checking the energy. Defaults to the present time in UTC. |
---|
Calculates the recovered energy from the player used energy first.
Parameters: | time – the time when checking the energy. Defaults to the present time in UTC. |
---|
The interval in seconds to recover energy.
The quantity of once energy recovery.
Makes the energy to be full. Most social games reset energy when the player reaches higher level.
Parameters: | time – the time when setting the energy. Defaults to the present time in UTC. |
---|
Sets the energy to the fixed quantity.
>>> energy = Energy(10, 300)
>>> print energy
<Energy 10/10>
>>> energy.set(3)
>>> print energy
<Energy 3/10 recover in 05:00>
You can also set over the maximum when give bonus energy.
>>> energy.set(15)
>>> print energy
<Energy 15/10>
Parameters: |
|
---|
Consumes the energy.
Parameters: |
|
---|---|
Raises ValueError: | |
not enough energy |
Quantity of used energy.
A time when using the energy first.
Released on June 24th 2014.
Fixes a bug on restored extra energy. This issue was reported by @hest.
Released on May 13th 2014.
(by @youknowone)
Released on Nov 5th 2013.
recovery_quantity works. Thanks to Donggeun Lee. (commit c98f348)
Released on Jan 21st 2013.
used_at parameter is allowed to be a datetime object or timestamp number.
Released on Jan 11th 2013.
Released on Dec 17th 2012.
Fixes a bug about the timezone of the default time argument. The documentation describes the timezone as UTC but it was a local timezone.
If your server is already using this project and setted with non-UTC timezone, you should override timestamp() to use local timezone.
Released on Dec 11th 2012.
Released on Oct 12th 2012.
Released on Sep 25th 2012.
First public preview release.
This project is licensed under BSD. See LICENSE for the details.
I’m Heungsub Lee, a game developer. Any regarding questions or patches are welcomed.