Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

consider flattening schema definitions in MUD config #2287

Closed
holic opened this issue Feb 21, 2024 · 2 comments
Closed

consider flattening schema definitions in MUD config #2287

holic opened this issue Feb 21, 2024 · 2 comments
Assignees

Comments

@holic
Copy link
Member

holic commented Feb 21, 2024

(related to #1668)

I want to propose moving away from keySchema and valueSchema and into a singular schema definition, e.g.

mudConfig({
  tables: {
    Position: {
      schema: {
        player: 'address',
        x: 'int32',
        y: 'int32',
      },
      key: ['player'],
    },
  },
});

This more cleanly maps to relational concepts (table schema with fields and a primary key) and reduces the number of new concepts we have to introduce about MUD (key schemas vs value schemas). This also maps well to the query API where "subjects" of a query are represented as field tuples.

query({
  from: {
    Position: ['player'],
  },
});

I recognize that this is maybe a little more confusing for folks used to our Solidity table libs, where the input args are keys and output is non-key fields:

(int32 x, int32 y) = Position.get(player);

But I think coming in with fresh eyes, this still makes sense and is a worthwhile trade-off. In Solidity, you don't want to pass around extra data because of gas, so you likely wouldn't return the same key fields you passed in as arguments.

For ECS usages, we'll likely have a plugin or some other wrapper to help you quickly define components as tables and automate setting up keys, etc.

@holic holic changed the title consider flattening schema definitions consider flattening schema definitions in MUD config Feb 21, 2024
@holic holic added this to the MUD v2 stable release milestone Feb 21, 2024
@holic
Copy link
Member Author

holic commented Feb 21, 2024

Using the MUD config in #2272 as an example

Before

const tables = {
  Position: {
    keySchema: {
      player: "address"
    },
    valueSchema: {
      x: "int32",
      y: "int32"
    }
  },
  Health: {
    keySchema: {
      player: "address"
    },
    value: {
      health: "uint256"
    }
  },
  Inventory: {
    keySchema: {
      player: "address",
      item: "uint8"
    },
    valueSchema: {
      amount: "uint32"
    }
  },
  Score: {
    keySchema: {
      player: "address",
      match: "uint256"
    },
    valueSchema: {
      score: "uint256"
    }
  },
  Winner: {
    keySchema: {
      match: "uint256"
    },
    valueSchema: {
      player: "address"
    }
  },
  Terrain: {
    keySchema: {
      x: "int32",
      y: "int32"
    },
    valueSchema: {
      type: "uint8"
    }
  }
}

After

const tables = {
  Position: {
    schema: {
      player: "address",
      x: "int32",
      y: "int32"
    },
    key: ['player']
  },
  Health: {
    schema: {
      player: "address",
      health: "uint256"
    },
    key: ['player']
  },
  Inventory: {
    schema: {
      player: "address",
      item: "uint8",
      amount: "uint32"
    },
    key: ['player', 'item']
  },
  Score: {
    schema: {
      player: "address",
      match: "uint256",
      score: "uint256"
    },
    key: ['player', 'match']
  },
  Winner: {
    schema: {
      match: "uint256",
      player: "address"
    },
    key: ['item']
  },
  Terrain: {
    schema: {
      x: "int32",
      y: "int32",
      type: "uint8"
    },
    key: ['x', 'y']
  }
}

@holic holic moved this to Planned in MUD Feb 23, 2024
@holic
Copy link
Member Author

holic commented Mar 1, 2024

we're doin it

@holic holic closed this as completed Mar 1, 2024
@github-project-automation github-project-automation bot moved this from Planned to Done in MUD Mar 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

No branches or pull requests

2 participants