Sha256: 909ac318e50a35c3ddf962b7f9108f36a9e7f76b4e8b0d11685684853bc77dfa

Contents?: true

Size: 1.68 KB

Versions: 1

Compression:

Stored size: 1.68 KB

Contents

require 'sinatra/base'
require 'yaml'

module Gamefic
  module Sdk

    class Server < Sinatra::Base
      set :port, 4342

      get '/' do
        config = Gamefic::Sdk::Config.load(settings.source_dir)
        paths = [config.script_path, config.import_path, Gamefic::Sdk::GLOBAL_SCRIPT_PATH]
        @@plot = Gamefic::Sdk::Debug::Plot.new Source::File.new(*paths)
        @@plot.script 'main'
        #@@plot.script 'debug'
        sinatra = Gamefic::Sdk::Platform::Sinatra.new(config: config)
        sinatra.build
        File.read File.join(sinatra.release_target, 'index.html')
      end

      post '/start' do
        content_type :json
        @@character = @@plot.player_class.new(name: 'player', synonyms: 'me myself self you yourself', description: 'As good-looking as ever.')
        @@plot.introduce @@character
        @@plot.ready
        @@character.state.to_json
      end

      post '/update' do
        content_type :json
        @@character.queue.push params['command']
        @@plot.update
        @@plot.ready
        @@character.state.merge(input: params['command']).to_json
      end

      post '/restore' do
        content_type :json
        snapshot = JSON.parse(params['snapshot'], symbolize_names: true)
        @@plot.restore snapshot
        @@character.cue @@plot.default_scene
        @@plot.update
        @@plot.ready
        @@character.state.to_json
      end

      class << self

        def run!
          start_browser if settings.browser
          super
        end
        
        def start_browser
          Thread.new {
            sleep 1 until Server.running?
            `start http://localhost:#{settings.port}`
          }
        end

      end
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
gamefic-sdk-1.7.0 lib/gamefic-sdk/server.rb