Skip to content

Instantly share code, notes, and snippets.

@sudojunior
Created May 23, 2019 20:08
Show Gist options
  • Save sudojunior/80599b8c3b200e41dd06a1da20552bb5 to your computer and use it in GitHub Desktop.
Save sudojunior/80599b8c3b200e41dd06a1da20552bb5 to your computer and use it in GitHub Desktop.
const Admin = require('./UserAdmin');
const Inventory = require('./UserInventory');
class User {
/**
* @param {string} id Primary key of record
*/
constructor(id) {
this.id = id;
this.data = {
admin: {},
inventory: {}
}
}
/**
* @param {User.default} data
*/
_patch(data) {
data.admin && this.admin._patch(data.admin);
data.inventory && this.inventory._patch(data.inventory);
}
get admin() {
return new Admin(this);
}
get inventory() {
return new Inventory(this);
}
static get default() {
return {
id: "", // UUID - database won't allow this if it already exists
admin: Admin.default,
inventory: Inventory.default
}
}
}
module.exports = User;
class UserAdmin {
/**
* @param {import('./User')}
*/
constructor(user) {
this.user = user;
}
get data() {
return this.user.data.admin;
}
static get default() {
return {};
}
}
module.exports = UserAdmin;
class UserInventory {
/**
* @param {import('./User')}
*/
constructor(user) {
this.user = user;
}
get data() {
this.user.data.inventory;
}
/**
* @param {UserInventory.default}
* @returns {void}
*/
_patch(data) {
Object.entries(data).forEach(([id, amount]) => {
this._patchItem(id, amount);
});
}
/**
* @param {string} id
* @param {number} amount
* @returns {void}
*/
_patchItem(id, amount) {
}
get(item) {
}
has(item, amount = 1) {
}
async add(id, amount) {
}
async remove(id, amount) {
}
async set(id, amount) {
}
async clear(id) {
}
static get default() {
return [];
// unknown item format (object item id mapping or array iterable to Map class)
// further unknown with item meta (store as:
// a. [UUID, number] where 'UUID' is an item id and 'number' is the amount held in the inventory
// b. [UUID, {}]
// c. InventoryItem[] where 'InventoryItem' is an interface holding various meta data about the rest of the item (amount, effects, custom name, etc.)
// ... and the list goes on ... it could exist as another class altogether.
// )
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment