-
Notifications
You must be signed in to change notification settings - Fork 18
Memory Database Select Queries v7
- status: complete
- version: 7.x
- follows from: Memory Database
Here are a few examples of database queries.
First of all, let's store a convenient reference to the memory database to be used in all examples.
// Store reference.
let memory = node.game.memory;
Fetch all items that contain a property called 'value'.
memory.select('value').fetch();
// [
// {
// value: 1,
// player: "A",
// stage: { stage: 2, step: 1, round : 1 },
// timestamp: 1477.13729710
// },
// {
// value: 2,
// player: "A",
// stage: { stage: 2, step: 1, round : 1 },
// timestamp: 17.147.33111
// },
// {
// value: 3,
// player: "B",
// stage: { stage: 2, step: 1, round : 1 },
// timestamp: 177.47899812
// },
// ]
All items that contain a property named 'foo' equal to 'bar' from player 'A'.
memory
.select('foo', '=', 'bar')
.and('player', '=', 'A')
.fetch();
// [ {
// foo: "bar",
// player: "A",
// stage: { stage: 1, step: 1, round : 1 },
// timestamp: 1477.13729710
// } ]
// All items with a property 'value' between 0 and 5.
memory
.select('value', '>', 0)
.and('value', '<', 5)
// Consider also the '<>' operator for the same result.
.fetch();
// [
// {
// value: 1,
// player: "A",
// stage: { stage: 2, step: 1, round : 1 },
// timestamp: 1477.13729710
// },
// {
// value: 2,
// player: "A",
// stage: { stage: 2, step: 1, round : 1 },
// timestamp: 17.147.33111
// },
// {
// value: 3,
// player: "B",
// stage: { stage: 2, step: 1, round : 1 },
// timestamp: 177.47899812
// }
// ]
Sometimes you need fast access to an item just added.
// Creates a direct link to the last item added onto the database.
memory.tag("last_decision");
// Retrieves tag.
memory.resolveTag("last_decision");
// {
// value: 3,
// player: "B",
// stage: { stage: 2, step: 1, round : 1 },
// timestamp: 177.47899812
// }
Hashes and views provide convenient access to collections of items already inserted in the memory database.
-
View: A sub-database which contains semantically coherent items. Example: a database with all items from the player with id "A".
-
Hash: An automatic way to dynamically create views based on incoming data, when the criteria for subsetting the database are not known in advance. A hash function takes an item from the database and returns an alphanumeric id, that is a "hash". Example: you normally do not know the id of players in advance (it is randomly assigned), but you want to create a new view for every new id.
There are three predefined sub-databases inside the memory database:
-
memory.player
: hashes items by player id, -
memory.stage
: hashes items by the stage of creation, -
memory.done
: a view containing all items sent along vianode.done
.
Here are a few examples about how to use hashes and views:
// Store reference.
let memory = node.game.memory;
// Items indexed by the player id.
memory.player.A.size(); // 3
memory.player.B.size(); // 2
// Get all items from previous step.
memory.stage[node.game.getPreviousStep()].size(); // 2;
// Get items indexed from given stages.
memory.stage['1.1.1'].size(); // 2
memory.stage['2.1.1'].size(); // 3
// This is equivalent to using a GameStage object
// (first import the GameStage class).
const ngc = require('nodegame-client');
const GameStage = ngc.GameStage;
memory.stage[new GameStage('1.1.1')].size(); // 2
It is often convenient to define a custom hash database.
// In the init function of logic.
// A hash function containing all items from player A that have a property
// 'foo' with value 'bar'.
memory.hash('bar_A', function(item) {
if (item.player === 'A' && item.foo ==='bar') {
return item;
}
});
// Needed if the hash is created after items have been already inserted.
// memory.rebuildIndexes();
memory.bar_A.size(); // 1
Go back to the wiki Home.
Copyright (C) 2021 Stefano Balietti
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.