barker

Barker game show engine.

Installation

Add this line to your application's Gemfile:

gem 'barker'

And then execute:

$ bundle

Or install it yourself as:

$ gem install barker

Usage

Guidelines

Barker let you define project specific guidelines that will be called during the game show life cycle. The default behaviour is implemented in Barker::Guideline::Base.

Example of a project specific guideline.

module Barker
  module Guideline

    class Llmcquiz < Base
      include Errors

      def quizzes
        [Quiz.new(questions.sample(5))]
      end

      def jokers
        [:fifty_fifty]
      end

      def questions
        Barker::Import::Yaml.process(path)
      end

      def round_time
        20
      end

      def path
        File.expand_path "#{File.dirname(__FILE__)}/../../../lib/barker/guideline/llmcquiz/questions.yaml"
      end
    end

  end
end

Api

Only interact with the a api instance. Api calls always return a instance of Barker::Api::Response.

Barker exceptions will not bubble up the stacktrace instead they will be caught in response.error. You can check for errors with the response.ok? method.

class Response < OpenStruct
  def ok?
    !error
  end
end

Create a new game show

The game show will automatically be stored in a memory store.

api = Barker::Api::Base.new
api.create # => <Barker::Api::Response game_show_id="1", candidate_ids=[], started_at=nil, finished_at=nil, aborted_at=nil, started?=false, running?=false, finished?=false, aborted?=false, jokers=[], stage=-1, round=-1>

You can pass a optional guideline

api.create(:guideline => :llmcquiz) # => <Barker::Api::Response game_show_id="1", candidate_ids=[], started_at=nil, finished_at=nil, aborted_at=nil, started?=false, running?=false, finished?=false, aborted?=false, jokers=[:fifty_fifty], stage=-1, round=-1>

Add or remove candidates

api = Barker::Api::Base.new
game_show_id = api.create.game_show_id

api.candidates(game_show_id) # => []
api.join(game_show_id, 42, "de") # => <Barker::Api::Response candidate_id=42, locale="de", joined_at=2013-06-11 18:44:23 +0200, leaved_at=nil, aborted?=false, joined?=true, leaved?=false, jokers=[]>
api.candidates(game_show_id) # => [<Barker::Api::Response candidate_id=42, locale="de", joined_at=2013-06-11 18:44:51 +0200, leaved_at=nil, aborted?=false, joined?=true, leaved?=false, jokers=[]>]
api.leave(game_show_id, candidate_id) => #<Barker::Api::Response candidate_id=42, locale="de", joined_at=2013-06-11 18:45:46 +0200, leaved_at=2013-06-11 18:45:46 +0200, aborted?=false, joined?=true, leaved?=true, jokers=[]>
api.candidates(game_show_id, candidate_id) => [#<Barker::Api::Response candidate_id=42, locale="de", joined_at=2013-06-11 18:45:46 +0200, leaved_at=2013-06-11 18:45:46 +0200, aborted?=false, joined?=true, leaved?=true, jokers=[]>]

Control

Start
api = Barker::Api::Base.new
game_show_id = api.create.game_show_id

api.start(game_show_id) # => <Barker::Api::Response game_show_id="1", candidate_ids=[], started_at=2013-06-11 18:51:41 +0200, finished_at=nil, aborted_at=nil, started?=true, running?=true, finished?=false, aborted?=false, jokers=[:fifty_fifty], stage=-1, round=-1>
api.abort(game_show_id) # => <Barker::Api::Response game_show_id="1", candidate_ids=[], started_at=nil, finished_at=nil, aborted_at=2013-06-11 18:51:41 +0200, started?=false, running?=false, finished?=false, aborted?=true, jokers=[:fifty_fifty], stage=-1, round=-1>
api.finish(game_show_id) # => <Barker::Api::Response game_show_id="1", candidate_ids=[], started_at=nil, finished_at=2013-06-11 18:51:41 +0200, aborted_at=nil, started?=false, running?=false, finished?=true, aborted?=false, jokers=[:fifty_fifty], stage=-1, round=-1>
Abort
api = Barker::Api::Base.new
game_show_id = api.create.game_show_id

api.abort(game_show_id) # => <Barker::Api::Response game_show_id="1", candidate_ids=[], started_at=nil, finished_at=nil, aborted_at=2013-06-11 18:51:41 +0200, started?=false, running?=false, finished?=false, aborted?=true, jokers=[:fifty_fifty], stage=-1, round=-1>
Finish
api = Barker::Api::Base.new
game_show_id = api.create.game_show_id

api.finish(game_show_id) # => <Barker::Api::Response game_show_id="1", candidate_ids=[], started_at=nil, finished_at=2013-06-11 18:51:41 +0200, aborted_at=nil, started?=false, running?=false, finished?=true, aborted?=false, jokers=[:fifty_fifty], stage=-1, round=-1>
Ask
api = Barker::Api::Base.new
game_show_id = api.create.game_show_id

api.join(game_show_id, 42, "de")

api.start(game_show_id)
api.next_stage(game_show_id) # => <Barker::Api::Response game_show_id="1", candidate_ids=[42], started_at=2013-06-11 19:00:20 +0200, finished_at=nil, aborted_at=nil, started?=true, running?=true, finished?=false, aborted?=false, jokers=[:fifty_fifty], stage=0, round=-1>}
api.next_round(game_show_id) # => <Barker::Api::Response game_show_id="1", candidate_ids=[42], started_at=2013-06-11 19:00:20 +0200, finished_at=nil, aborted_at=nil, started?=true, running?=true, finished?=false, aborted?=false, jokers=[:fifty_fifty], stage=0, round=0>}

api.ask(game_show_id, 42) # => <Barker::Api::Response question_id=9, candidate_id=42, label="frage", state=:asked, asked_at=2013-06-11 19:02:52 +0200, answered_at=nil, asked?=true, answered?=false, open?=false, answers=[#<Barker::Api::Response answer_id=21, label="antwort", correct?=false, revealed?=false>, #<Barker::Api::Response answer_id=22, label="antwort", correct?=true, revealed?=false>
Answer
api = Barker::Api::Base.new
game_show_id = api.create.game_show_id

api.join(game_show_id, 42, "de")

api.start(game_show_id)
api.next_stage(game_show_id)
api.next_round(game_show_id)
api.ask(game_show_id, 42)

api.answer(game_show_id, 42, 25) # => <Barker::Api::Response answer_id=25, label="antwort", correct?=false, revealed?=false>
api.given_answer(game_show_id, 25) # => <Barker::Api::Response answer_id=25, label="antwort", correct?=false, revealed?=false>
Joker
api = Barker::Api::Base.new
game_show_id = api.create.game_show_id

api.join(game_show_id, 42, "de")

api.start(game_show_id)
api.next_stage(game_show_id)
api.next_round(game_show_id)
api.ask(game_show_id, 42)

api.candidates(game_show_id) # => [<Barker::Api::Response candidate_id=42, locale="de", joined_at=2013-06-11 19:08:44 +0200, leaved_at=nil, aborted?=false, joined?=true, leaved?=false, jokers=[]>]
api.joker(game_show_id) # => <Barker::Api::Response joker_id="fifty_fifty">
api.candidates(game_show_id) # => [<Barker::Api::Response candidate_id=42, locale="de", joined_at=2013-06-11 19:08:44 +0200, leaved_at=nil, aborted?=false, joined?=true, leaved?=false, jokers=[:fifty_fifty]>]

Store

For now barker only support a memory store / repo. ActiveRecord will be supported sooner or later.

How to use in Rails

Here is a very early concept how it could be used in a rails app.

http://gitlab.neopoly.de/jo/barker/issues/19#note_23

TODO: Add example with real api.

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Added some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request