Sha256: 904478e2a1b7aa8f6b6c673f2bff54843de99830db6f7d8c556870f13414a569

Contents?: true

Size: 1.31 KB

Versions: 2

Compression:

Stored size: 1.31 KB

Contents

require 'sinatra/base'
require 'sinatra/partial'
require 'haml'
require 'json'
require 'woyo/world'

module Woyo

class Server < Sinatra::Application

  def self.load_world glob = 'world/*.rb' 
    world = Woyo::World.new
    Dir[glob].each do |filename|
      eval_world File.read( filename ), filename, world
    end
    world
  end

  def self.eval_world text, filename, world = nil
    world ||= Woyo::World.new
    world.instance_eval text, filename
  end

  configure do
    enable :sessions
    set root: '.'
    set views: Proc.new { File.join(root, "views/server") }
    set public_folder: Proc.new { File.join(root, "public/server") }
    set world: self.load_world
  end

  def world
    settings.world
  end

  get '/' do
    redirect to 'default.html' if world.name.nil? && world.description.nil? && world.start.nil?
    @world = world
    session[:location_id] = world.start
    haml :world
  end

  get '/location' do
    @location = world.locations[session[:location_id]]
    haml :location
  end

  get '/go/*' do |way_id|
    way = world.locations[session[:location_id]].ways[way_id.to_sym]
    session[:location_id] = way.to.id if way.open?
    content_type :json
    way.go.to_json
  end

  get '/do/*/*?/*?' do |item,action,tool|
    # do default or optional action on required item with optional tool
  end
  
end

end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
woyo-server-0.0.7 lib/woyo/server/server.rb
woyo-server-0.0.6 lib/woyo/server/server.rb