Skip to main content

Player state

Set and save player progress. Player progress is represented by fields that are explicitly set in the control panel.

Player properties

Player properties FREE:

// ID
gp.player.id;
// Score
gp.player.score;
// Name
gp.player.name;
// Avatar link
gp.player.avatar;
// Stub – is the player default or the data in its model differs from the default
gp.player.isStub;
// Player fields
gp.player.fields;
Methods for work with the player state FREE:
// Get the value of the key field
gp.player.get('gold');
// Set the value of the key field to value, the value is cast to the type
gp.player.set('class', 'warrior');
// Add the value to the key field
gp.player.add('gold', 50);
// Toggle the value of the key field
gp.player.toggle('vip');
// Check if the key field is present and not empty (not 0, '', false, null, undefined)
gp.player.has('vip');

// Return the player state as an object
gp.player.toJSON();
// Set the state from the object
gp.player.fromJSON({
name: 'test player',
score: 4522,
gold: 100,
vip: false,
class: 'warrior',
});

// Reset the player state to default
gp.player.reset();
// Remove the player – reset fields and clear ID
gp.player.remove();

Player events:

// The player state has changed
gp.player.on('change', () => {});

Field Range Limits

Methods for working with player field boundary values, only for fields with specified restrictions (more details) FREE:

// Minimum value of the field key:min
gp.player.getMinValue('energy');
gp.player.get('energy:min'); // equivalent
// Set value
gp.player.set('energy:min', 0);

// Maximum value of the field key:max
gp.player.getMaxValue('energy');
gp.player.get('energy:max'); // equivalent
// Set value
gp.player.set('energy:max', 100);

Events for boundary values:

// Subscribe to the event of reaching the maximum value
gp.player.on('field:maximum', ({ field }) => {});

// Subscribe to the event of reaching the minimum value
gp.player.on('field:minimum', ({ field }) => {});

Auto-Increment Values

Methods for working with auto-increment variables, only for fields with specified auto-update interval (more details) FREE:

// Value of the field auto-update interval in seconds, key:incrementInterval
gp.player.get('energy:incrementInterval');
// Set value
gp.player.set('energy:incrementInterval', 5 * 60);

// Amount by which the field increases during auto-update, key:incrementValue
gp.player.get('energy:incrementValue');
// Set value
gp.player.set('energy:incrementValue', 5);

// Timestamp of the last auto-update of the field, ISO 8601 string, key:timestamp
gp.player.get('energy:timestamp');
// Set value
gp.player.set('energy:timestamp', '2021-04-27T17:13:02.815Z');

// Remaining time until the next auto-update in seconds, key:secondsLeft
// Read only
gp.player.get('energy:secondsLeft');

// Remaining time until the field reaches its limit in seconds, key:secondsLeftTotal
// Read only
gp.player.get('energy:secondsLeftTotal');

Events for auto-updates:

// Subscribe to the event of incrementing the variable
gp.player.on('field:increment', ({ field, oldValue, newValue }) => {});

Player Statistics

FREE

You can retrieve information about the number of days and time spent in the game.

// Number of days in the game
gp.player.stats.activeDays;

// Number of consecutive days in the game
gp.player.stats.activeDaysConsecutive;

// Number of seconds spent in the game today
gp.player.stats.playtimeToday;

// Number of seconds spent in the game overall
gp.player.stats.playtimeAll;

Player fields

Player fields are set in the panel, they describe the player state and store the following properties:

/**
* Field name translated into current language
* @type {string}
*/
field.name;

/**
* Unique field key
* @type {string}
*/
field.key;

/**
* Field type
* @type {'stats' | 'data' | 'flag' | 'service' | 'accounts'}
* stats — numeric fields
* data — string fields
* flags — boolean fields
* service – ID, test, active, remote, etc.
* accounts — vkId, yandexId, okId, etc.
*/
field.type;

/**
* Important field for the player
* If you need to decide who will be left, you can display these fields
* @type {boolean}
*/
field.important;

/**
* Player's public field
* will be visible to all players when searching for players
* @type {boolean}
*/
field.public;

/**
* Default field value
* @type {string | number | boolean}
*/
field.default;

/**
* Possible variants of values for the fields
* If they are not specified, then the field takes any value
* @type {ModelFieldVariant[]}
*/
field.variants;

/**
* Limits for the field,
* Can be empty
* @type {FieldLimits | null}
*/
field.limits;

/**
* Auto-increment interval,
* Can be empty
* @type {IntervalIncrement | null}
*/
field.intervalIncrement;

Field variants can also be set in the panel. Variant properties:

/**
* Variant name translated into current language
* @type {string}
*/
fieldVariant.name;

/**
* Unique value of the variant
* @type {string | number | boolean}
*/
fieldVariant.value;

Field limits can be set in the panel. Field limit properties:

/**
* Lower limit of the field
* @type {number}
*/
limit.min;

/**
* Upper limit of the field
* @type {number}
*/
limit.max;

/**
* Can the field be manually set above the maximum limit
* @type {boolean}
*/
limit.couldGoOverLimit;

Auto-increment interval of the field can be set in the panel. Properties of the auto-increment interval of the field:

/**
* Increment interval, seconds
* @type {number}
*/
intervalIncrement.interval;

/**
* Value to increment
* @type {number}
*/
intervalIncrement.increment;

The list of fields is available directly in the player model FREE:

gp.player.fields;

Methods for work with fields FREE:

// Get the field by key
gp.player.getField('gold');
// Get the translated field name by key
gp.player.getFieldName('score');
// Get the translated name of the field variant by the key and its value
gp.player.getFieldVariantName('rank', 2);

Stay in Touch

Other documents of this chapter available Here. To get started, welcome to the Tutorials chapter.

GamePush Community Telegram: @gs_community.

For your suggestions e-mail: [email protected]

We Wish you Success!