tablib

package module
v0.0.0-...-53e496d Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 25, 2020 License: Unlicense Imports: 17 Imported by: 0

README

Tablib

Tablib is short for "table library". It is a library to execute an 'extensible random table generation engine' for use in tabletop gaming. Tablib is written in Go and is intended to serve as the backend for web services, desktop applications or to be embedded in other applications needing random table support.

Overview

The library offers:

  • An open source, easy-to-use Table definition language
  • A powerful, robust API
  • A Lua script execution engine for more sophisticated needs
Defining Tables

Tables are defined in YAML and look like this (see the Table Reference for the full table syntax):

definition:
  name: Ice_cream_flavors
  type: flat
content:
  - chocolate
  - vanilla
  - strawberry
  - fudge swirl

Tables are loaded into a TableRepository and can be located and accessed with the API:

package main
import (
  "fmt"
  "tablib"
  )

func main() {

  //create a new repository. All tables are stored in a repo
  repo := NewTableRepository()

  //load and validate the YAML file. Provides extensive consistency and
  //validity responses not shown here
  repo.AddTable("path/to/icecream.yml")

  //roll twice on the specified table and return the values as a slice.
  //Provides graceful error handling and a Log that describes how the result
  //was achieved          
  tabData := repo.Roll("Ice_cream_flavors", 2)

  for _, r := range tabData.Result {
    fmt.Printf("A random roll on the ice cream table: %s\n", r)
  }
}

This code will output something like:

A random roll on the ice cream table: chocolate
A random roll on the ice cream table: vanilla

Mechanisms are provided for picking a specified number of unique values from a table, for ranged or weighted tables (e.g. tables that do not have an equal distribution of results), for tables calling other tables to retrieve data and for tables to declare 'inline tables' when a table's contents needs to be flexible but the flexibility does not warrant the creation of a new table in its own right.

Lua Scripting

Tablib also provides a Lua script execution engine that serves as a powerful tool to generate sophisticated results from the tables and stitch together the results of many table rolls. See the Lua Reference section for more details.

local t = require("tables")

results {}
function main()
  results["ice-cream-flavor"] = t.roll("Ice_cream_flavors")
  results["syrup"] = t.roll("sundae-syrup")
  results["toppings"] = t.pick("sundae-toppings", 3)
end

Scripts are loaded into a TableRepository and can be located and accessed with the API:

package main
import (
  "fmt"
  "tablib"
  )

func main() {

  //create a new repository. All scripts are stored in a repo
  repo := NewTableRepository()

  //load and validate the YAML file. Provides extensive consistency and validity
  //responses not shown here
  repo.AddLuaScript(luaCodeAsString)

  //execute the script. Provides graceful error handling and many other features          
  tabMap := repo.Execute("sundae", nil)

  for key, value := range tabMap {
    fmt.Printf("%s: %s\n", key, value)
  }
}

The output of the Lua script is a map (associative array, dictionary table):

ice-cream-flavor: chocolate
syrup: hot fudge
toppings: nuts|whipped cream|sprinkles

The Lua engine supports a wide range of Lua functions and a parameter callback mechanism to enable the script to request data from the library's caller.

Inspiration

The Windows Desktop application Inspiration Pad Pro3 is an excellent random table execution engine. I have made extensive use of this software to create my own tables (see my IPP3 project for the kinds of tables I have created). Over time, I found myself wanting to free myself of the desktop client and access this data over the web. I also wanted to embed random tables in other software I was writing and create sophisticated tables for which the IPP3 product had either no or a very limited syntax. Tablib is my solution to these needs.

Table Reference

TODO

API Reference

TODO

Lua Reference

TODO

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DefaultParamSpecificationCallback

func DefaultParamSpecificationCallback(specs []*ParamSpecification) map[string]string

DefaultParamSpecificationCallback is a convenience function that simply returns the default values in the provided map. This permits users of this lib from providing support for paramaterization of lua scripts at the cost of always having those scripts that do require params to always use their defaul value

Types

type ParamSpecification

type ParamSpecification struct {
	Name    string   `json:"name"`
	Default string   `json:"default"`
	Options []string `json:"options"`
}

ParamSpecification specifies a parameter a lua script requires

func NewParamSpecification

func NewParamSpecification() *ParamSpecification

NewParamSpecification does what it says on the tin

type ParamSpecificationRequestCallback

type ParamSpecificationRequestCallback func([]*ParamSpecification) map[string]string

ParamSpecificationRequestCallback is a function that will be called by tablib to allow the main program to supply params needed for a lua function

type SearchResult

type SearchResult struct {
	Name string
	Type string
	Tags []string
}

SearchResult holds information about each object discovered during a search

type TableRepository

type TableRepository interface {

	//AddLuaScript stores the given lua script in the repository.
	//The script name and the script string itself are mandatory.
	AddLuaScript(scriptName string, luaScript string) error

	//AddTable stores the given yaml representation of a table in the repository.
	//
	//If the presented yaml is not parsable or has other structural issues, an error is raised.
	//Errors and warnings related to the semantics of the table (e.g. internal consistency
	//issues or table syntax errors) are captured in the returned ValidationResult
	AddTable(yamlBytes []byte) (*validate.ValidationResult, error)

	//Execute executes the named Lua script and returns a map of named output keys and their values.
	//
	//The nature of the output is arbitrary and is defined by the Lua script, but typically
	//the returned map contains name/value pairs where the name represents some meaningful
	//table roll and the value is the results from that table roll. The map may contain any other
	//information the script wishes to communicate to the client, like a synthesized or concatenated
	//result of several tables rolls that have been assembled in the script to produce an overall
	//meaningful result. If an error occurs in script execution, error information will be returned
	//in the map in place of the intended result(s).
	//
	//The callback function is optional. If used, it will be called if the Lua script requests
	//parameters from the caller. If set to nil, the Lua script will be returned the default
	//value of each parameter. For more information on the format of Lua scripts and the
	//use of the callback, see the README documentation.
	Execute(scriptName string, callback ParamSpecificationRequestCallback) map[string]string

	//EvaluateDiceExpression revaluates a dice expression and returns the result or
	//an an error f the expression is not valid.
	EvaluateDiceExpression(diceExpr string) (int, error)

	//List provides the raw string listing of the named table or script.
	//
	//An error is returned if the named item does not exist or if itemType is anything
	//other than "table" or "script"
	List(name string, itemType string) (string, error)

	//The table type must be flat; providing the name of a ranged table will generate
	//an error
	Pick(tableName string, count int) *tableresult.TableResult

	//Roll 'rolls' on the named table count times, generating a single result with each roll
	Roll(tableName string, count int) *tableresult.TableResult

	//To obtain the entire contents of the repository, call Search("", nil).
	//To filter by tags only, call Search ("", []string{"tag1", "tag2"}). This will
	//return all items in the repo that have at least one of these tags defined.
	//To filter by name only, call Search("myregex", nil). This will return all items
	//that match the given regex.  Provide both parameters for a narrow search. Note
	//that in this case, the results are filtered first by tag and then by name.
	Search(namePredicate string, tags []string) ([]*SearchResult, error)

	//Tags returns an alphabetized list of all tags used by any table or script
	Tags() []string
}

TableRepository holds the various tables and Lua scripts and manages their execution

func NewTableRepository

func NewTableRepository() TableRepository

NewTableRepository does what it says on the tin

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL