From 8e183256565b08e611ca099f51beb12af5016d28 Mon Sep 17 00:00:00 2001 From: Andrew Tomaka Date: Wed, 15 Apr 2015 11:00:32 -0400 Subject: [PATCH] Setup remote server for storing raids --- .gitignore | 1 + Gemfile | 19 +++++++ Gemfile.lock | 60 +++++++++++++++++++++++ Procfile | 1 + Rakefile | 2 + app.rb | 37 ++++++++++++++ config.ru | 2 + db/migrate/20150415152327_create_raids.rb | 11 +++++ db/schema.rb | 25 ++++++++++ environments.rb | 17 +++++++ bot.js => public/bot.js | 9 ++++ views/index.erb | 24 +++++++++ views/layout.erb | 10 ++++ 13 files changed, 218 insertions(+) create mode 100644 .gitignore create mode 100644 Gemfile create mode 100644 Gemfile.lock create mode 100644 Procfile create mode 100644 Rakefile create mode 100644 app.rb create mode 100644 config.ru create mode 100644 db/migrate/20150415152327_create_raids.rb create mode 100644 db/schema.rb create mode 100644 environments.rb rename bot.js => public/bot.js (96%) create mode 100644 views/index.erb create mode 100644 views/layout.erb diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0df55fc --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +dev.db diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..f259bb4 --- /dev/null +++ b/Gemfile @@ -0,0 +1,19 @@ +source 'https://rubygems.org' +ruby "2.2.0" + +gem 'sinatra' +gem 'activerecord' +gem "sinatra-activerecord" +gem 'sinatra-flash' +gem 'sinatra-redirect-with-flash' +gem 'rake' +gem 'json' + +group :production do + gem 'pg' +end + +group :development do + gem 'sqlite3' + gem 'foreman' +end diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000..bd036c1 --- /dev/null +++ b/Gemfile.lock @@ -0,0 +1,60 @@ +GEM + remote: https://rubygems.org/ + specs: + activemodel (4.2.1) + activesupport (= 4.2.1) + builder (~> 3.1) + activerecord (4.2.1) + activemodel (= 4.2.1) + activesupport (= 4.2.1) + arel (~> 6.0) + activesupport (4.2.1) + i18n (~> 0.7) + json (~> 1.7, >= 1.7.7) + minitest (~> 5.1) + thread_safe (~> 0.3, >= 0.3.4) + tzinfo (~> 1.1) + arel (6.0.0) + builder (3.2.2) + foreman (0.78.0) + thor (~> 0.19.1) + i18n (0.7.0) + json (1.8.2) + minitest (5.6.0) + pg (0.18.1) + rack (1.6.0) + rack-protection (1.5.3) + rack + rake (10.4.2) + sinatra (1.4.6) + rack (~> 1.4) + rack-protection (~> 1.4) + tilt (>= 1.3, < 3) + sinatra-activerecord (2.0.6) + activerecord (>= 3.2) + sinatra (~> 1.0) + sinatra-flash (0.3.0) + sinatra (>= 1.0.0) + sinatra-redirect-with-flash (0.2.1) + sinatra (>= 1.0.0) + sqlite3 (1.3.10) + thor (0.19.1) + thread_safe (0.3.5) + tilt (2.0.1) + tzinfo (1.2.2) + thread_safe (~> 0.1) + +PLATFORMS + ruby + +DEPENDENCIES + activerecord + foreman + json + pg + rake + sinatra + sinatra-activerecord + sinatra-flash + sinatra-redirect-with-flash + sqlite3 diff --git a/Procfile b/Procfile new file mode 100644 index 0000000..8c99558 --- /dev/null +++ b/Procfile @@ -0,0 +1 @@ +web: bundle exec rackup config.ru -p $PORT diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..4e338f0 --- /dev/null +++ b/Rakefile @@ -0,0 +1,2 @@ +require './app' +require 'sinatra/activerecord/rake' diff --git a/app.rb b/app.rb new file mode 100644 index 0000000..a5f63e4 --- /dev/null +++ b/app.rb @@ -0,0 +1,37 @@ +require 'sinatra' +require 'sinatra/activerecord' +require 'json' +require './environments' + +set :public_folder, 'public' + +get '/' do + @raids = Raid.all + erb :index +end + +post '/' do + response['Access-Control-Allow-Origin'] = 'http://trugul.com' + + @raid = Raid.new(params[:raid]) + + if @raid.save + content_type :json + { :message => 'Raid saved' }.to_json + else + content_type :json + { :message => 'Raid failed to save' }.to_json + end +end + +class Raid < ActiveRecord::Base + def money=(value) + value = value.to_s.gsub(/[$,]/, '').to_f + write_attribute(:money, value) + end + + def soldiers=(value) + value = value.to_s.gsub(/[$,]/, '').to_f + write_attribute(:soldiers, value) + end +end diff --git a/config.ru b/config.ru new file mode 100644 index 0000000..76a6edf --- /dev/null +++ b/config.ru @@ -0,0 +1,2 @@ +require './app' +run Sinatra::Application diff --git a/db/migrate/20150415152327_create_raids.rb b/db/migrate/20150415152327_create_raids.rb new file mode 100644 index 0000000..937bb33 --- /dev/null +++ b/db/migrate/20150415152327_create_raids.rb @@ -0,0 +1,11 @@ +class CreateRaids < ActiveRecord::Migration + def change + create_table :raids do |t| + t.string :attacker + t.string :defender + t.float :soldiers + t.float :money + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb new file mode 100644 index 0000000..3796aaf --- /dev/null +++ b/db/schema.rb @@ -0,0 +1,25 @@ +# encoding: UTF-8 +# This file is auto-generated from the current state of the database. Instead +# of editing this file, please use the migrations feature of Active Record to +# incrementally modify your database, and then regenerate this schema definition. +# +# Note that this schema.rb definition is the authoritative source for your +# database schema. If you need to create the application database on another +# system, you should be using db:schema:load, not running all the migrations +# from scratch. The latter is a flawed and unsustainable approach (the more migrations +# you'll amass, the slower it'll run and the greater likelihood for issues). +# +# It's strongly recommended that you check this file into your version control system. + +ActiveRecord::Schema.define(version: 20150415152327) do + + create_table "raids", force: :cascade do |t| + t.string "attacker" + t.string "defender" + t.float "soldiers" + t.float "money" + t.datetime "created_at" + t.datetime "updated_at" + end + +end diff --git a/environments.rb b/environments.rb new file mode 100644 index 0000000..535a00c --- /dev/null +++ b/environments.rb @@ -0,0 +1,17 @@ +configure :development do + set :database, 'sqlite3:dev.db' + set :show_exceptions, true +end + +configure :production do + db = URI.parse(ENV['DATABASE_URL'] || 'postgres:///localhost/mydb') + + ActiveRecord::Base.establish_connection( + :adapter => db.scheme == 'postgres' ? 'postgresql' : db.scheme, + :host => db.host, + :username => db.user, + :password => db.password, + :database => db.path[1..-1], + :encoding => 'utf8' + ) +end diff --git a/bot.js b/public/bot.js similarity index 96% rename from bot.js rename to public/bot.js index 8960377..ab3bf55 100644 --- a/bot.js +++ b/public/bot.js @@ -244,16 +244,25 @@ function haveRaidTarget() { } function botBuildRaidReport(message) { + var botAttacker, botDefender; var botRaidUser; if(/You.? were raided by (.*?).? /.exec(message)) { botRaidUser = /You.? were raided by (.*?).? /.exec(message)[1]; + botAttacker = botRaidUser; + botDefender = botUser; } else { botRaidUser = /You .*? the raid against (.*?)! /.exec(message)[1]; + botAttacker = botUser; + botDefender = botRaidUser; } var botRaidSoldiers = /! .*? lost (.*?) soldiers/.exec(message)[1]; var botRaidResult = (/won/.exec(message) !== null) ? 'won' : 'lost'; var botRaidStolen = (/steal (.*?) from/.exec(message) !== null) ? /steal (.*?) from/.exec(message)[1] : '$0'; + var botStolen = (botRaidResult == 'won') ? botRaidStolen : '-' + botRaidStolen; + + $.post('http://shielded-dusk-2170.herokuapp.com/', { raid: { attacker: botAttacker, defender: botDefender, soldiers: botRaidSoldiers, money: botStolen}}); + return "RAIDED (" + botRaidUser + "): " + botRaidResult + "; soldiers: -" + botRaidSoldiers + "; " + botRaidResult + " money: " + botRaidStolen; } diff --git a/views/index.erb b/views/index.erb new file mode 100644 index 0000000..59ea911 --- /dev/null +++ b/views/index.erb @@ -0,0 +1,24 @@ +

Index

+ + + + + + + + + + + + + <% @raids.each do |raid| %> + + + + + + + + <% end %> + +
AttackerDefenderSoldiersMoneyTime
<%= raid.attacker %><%= raid.defender %><%= raid.soldiers.to_f / 1000000000000.0 %>T<%= raid.money.to_f / 1000000000000.0 %>T<%= raid.created_at %>
diff --git a/views/layout.erb b/views/layout.erb new file mode 100644 index 0000000..5bea7b0 --- /dev/null +++ b/views/layout.erb @@ -0,0 +1,10 @@ + + + Raids + + + +<%= yield %> + + +