Sphynx.Riddle (Sphynx v0.1.1) View Source

Module Riddle

defmodule NumberOfExploitsOfHercules do
  use Sphynx.Riddle
end

All functions, which implemented in our example (NumberOfExploitsOfHercules) is a part of game lifecycle.

Roadmap of riddle looks like:

  1. Creating the riddle. Calling create/2 function with context (map) and options (keyword). The context and options params passing through context/1 and init/1 functions (can be overriden).

  2. Send riddle to game. When riddle were sent to game, riddle are makes. In this case, a call make/1 occurs.

  3. Processing the answer of user. In this point system calls answer/1 and sends result of this call into the check/3.

  4. Result of check/3 redirects to verdict/2. If result have specific pattern - next riddle will be maken, otherwise - result will be returned to guessing man.

Generally: Need to define your riddle module (lets name it NumberOfExploitsOfHercules). Riddle module have to use Sphynx.Riddle and implement his required functions.

defmodule NumberOfExploitsOfHercules do
  use Sphynx.Riddle
end

What is inside: Your module have to implement collbacks from Sphynx.Riddle. Some of collbacks are required, some - not. The some function overridable and have default definition. There functions gonna be called by Sphynx.Clash in game progress.

Requireds:

  • answer/1 - function for returning correct answer to your question. And it's not static value necessarily. This function receives the current riddle implementation as argument and you can use it for making a different answers depending on situation.

  • check/3 - Function for checking and returnning result. It receives arguments:

    1. Current module implementation.
    2. Correct answer. This value - result of calling answer/1.
    3. Answer for checking. It's data with user answer, accepts any type.
  • verdict/2 - Function for delivery of a verdict. Receives 2 argument:

    1. Current module implementation.
    2. Result of checking. That is data, which been returned by check/3

Optional:

Link to this section Summary

Types

t()

A struct for user module which implement Sphynx.Riddle

Functions

Function for putting custom context data

Function for putting options of riddle instance

Link to this section Types

Specs

t() :: %{options: Keyword.t(), register: list(), context: any()}

A struct for user module which implement Sphynx.Riddle

Link to this section Functions

Link to this function

put_context(riddle, context)

View Source

Specs

put_context(t(), any()) :: t()

Function for putting custom context data

This function calls context/1 of received riddle and pass exist context to it. Result are the data, which will been putted into :context field of current riddle.

Arguments

  1. User module with implementation of Sphynx.Riddle

  2. Data, which will been sent as argument into context/1 function of riddle

Link to this function

put_options(riddle, options)

View Source

Specs

put_options(t(), map()) :: t()

Function for putting options of riddle instance

This function calls init/1 of received riddle and pass exist options to it. Result are the data, which will been putted into :options field of current riddle.

Arguments

  1. User module with implementation of Sphynx.Riddle

  2. Data, which will been sent as argument into init/1 function of riddle