Sha256: 150922048a289f28d31a55452feaa275f4a896c29f31c0f39b32835f397fdeda

Contents?: true

Size: 1.82 KB

Versions: 1

Compression:

Stored size: 1.82 KB

Contents

require 'sinatra/base'
require 'yaml'

module Gamefic
  module Sdk
    class Server < Sinatra::Base
      set :port, 4342
      set :server, :webrick

      get '/' do
        File.read File.join(settings.public_folder, 'index.html')
      end

      post '/start' do
        content_type :json
        start_plot
        @@character.output.to_json
      end

      post '/receive' do
        content_type :json
        @@character.queue.push params['command']
        {}.to_json
      end

      post '/update' do
        content_type :json
        @@plot.update
        @@plot.ready
        @@character.output.to_json
      end

      get '/snapshot' do
        content_type :json
        @@plot.save.to_json
      end

      post '/restore' do
        content_type :json
        start_plot
        # The snapshot needs to be received as a JSON string because of issues
        # with IndifferentHash malforming arrays.
        snapshot = JSON.parse(params['snapshot'])
        @@plot.restore snapshot
        @@character.cue @@plot.default_scene
        @@plot.update
        @@plot.ready
        @@character.output.to_json
      end

      def start_plot
        reset_features
        load File.join(settings.source_dir, 'main.rb')
        @@plot = Gamefic::Plot.new
        @@character = @@plot.get_player_character
        @@plot.introduce @@character
        @@plot.ready
      end

      def reset_features
        @@old_features ||= $LOADED_FEATURES.clone
        @@old_constants ||= Object.constants(false)
        $LOADED_FEATURES.keep_if { |e| @@old_features.include?(e) }
        Object.constants(false).each do |const|
          Object.send(:remove_const, const) unless @@old_constants.include?(const)
        end
        Gamefic::Plot.blocks.clear
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

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