Setup remote server for storing raids

This commit is contained in:
Andrew Tomaka 2015-04-15 11:00:32 -04:00
parent e238c25762
commit 8e18325656
13 changed files with 218 additions and 0 deletions

1
.gitignore vendored Normal file
View file

@ -0,0 +1 @@
dev.db

19
Gemfile Normal file
View file

@ -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

60
Gemfile.lock Normal file
View file

@ -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

1
Procfile Normal file
View file

@ -0,0 +1 @@
web: bundle exec rackup config.ru -p $PORT

2
Rakefile Normal file
View file

@ -0,0 +1,2 @@
require './app'
require 'sinatra/activerecord/rake'

37
app.rb Normal file
View file

@ -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

2
config.ru Normal file
View file

@ -0,0 +1,2 @@
require './app'
run Sinatra::Application

View file

@ -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

25
db/schema.rb Normal file
View file

@ -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

17
environments.rb Normal file
View file

@ -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

View file

@ -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;
}

24
views/index.erb Normal file
View file

@ -0,0 +1,24 @@
<h2>Index</h2>
<table border="1">
<thead>
<tr>
<td>Attacker</td>
<td>Defender</td>
<td>Soldiers</td>
<td>Money</td>
<td>Time</td>
</tr>
</thead>
<tbody>
<% @raids.each do |raid| %>
<tr>
<td><%= raid.attacker %></td>
<td><%= raid.defender %></td>
<td><%= raid.soldiers.to_f / 1000000000000.0 %>T</td>
<td><%= raid.money.to_f / 1000000000000.0 %>T</td>
<td><%= raid.created_at %></td>
</tr>
<% end %>
</tbody>
</table>

10
views/layout.erb Normal file
View file

@ -0,0 +1,10 @@
<html>
<head>
<title>Raids</title>
</head>
<body>
<%= yield %>
</body>
</html>