Skip to content
Browse files

WIP

  • Loading branch information...
1 parent 0dac9f1 commit 8a4a42e0ceb5c29362977c99c8c3dae107a3a666 @jackquack committed
View
25 app/controllers/predictions_controller.rb
@@ -3,4 +3,29 @@ def create
params[:prediction][:user_id] = current_user.id
create!
end
+
+ def yes
+ @prediction = Prediction.find(params[:id])
+ @prediction.outcome = 'yes'
+
+ #cancel incomplete transaction
+ @prediction.buys.incomplete.destroy_all
+ @prediction.sells.incomplete.destroy_all
+
+ @prediction.transactions.each{|transaction| transaction.execute('yes') }
+ @prediction.save
+ redirect_to prediction_path(@prediction)
+ end
+ def no
+ @prediction = Prediction.find(params[:id])
+ @prediction.outcome = 'no'
+
+ #cancel incomplete transaction
+ @prediction.buys.incomplete.destroy_all
+ @prediction.sells.incomplete.destroy_all
+
+ @prediction.transactions.each{|transaction| transaction.execute('no') }
+ @prediction.save
+ redirect_to prediction_path(@prediction)
+ end
end
View
4 app/models/hold.rb
@@ -0,0 +1,4 @@
+class Hold < ActiveRecord::Base
+ attr_accessible :transaction_id, :transaction_type, :user_id, :value
+ belongs_to :holdable, :polymorphic => true
+end
View
4 app/models/holds.rb
@@ -1,4 +0,0 @@
-class Holds < ActiveRecord::Base
- attr_accessible :transaction_id, :transaction_type, :user_id, :value
- belongs_to :buy, :sell, :polymorphic => true
-end
View
4 app/models/prediction.rb
@@ -4,4 +4,8 @@ class Prediction < ActiveRecord::Base
has_many :buys
has_many :transactions
belongs_to :user
+
+ def volume
+ self.transactions.map{|transaction| transaction.number_of_shares * transaction.price}.sum
+ end
end
View
14 app/models/transaction.rb
@@ -3,4 +3,18 @@ class Transaction < ActiveRecord::Base
belongs_to :prediction
belongs_to :buy
belongs_to :sell
+
+ def execute(outcome)
+ self.buy.user.subtract_amount(self.buy.number_of_shares * self.buy.price)
+ if outcome == 'yes'
+ #buyer is winner
+ self.buy.user.add_amount(self.buy.number_of_shares * configatron.one_btc)
+ self.sell.user.subtract_amount(self.sell.number_of_shares * configatron.one_btc) #seller is loser
+ elsif outcome == 'no'
+ # self.sell.user.subtract_amount(self.buy.number_of_shares * 0) #seller is winner
+ self.sell.user.add_amount(self.sell.number_of_shares * self.sell.price) #seller is winner
+ else
+ throw 'Outcome Invalid'
+ end
+ end
end
View
29 app/models/user.rb
@@ -29,4 +29,33 @@ def generate_bitcoin_account
self.public_key = Guid.new.to_s
self.private_key = Guid.new.to_s
end
+
+ def bitcoin_available
+ self.account_balance - self.holds
+ end
+
+ def account_balance
+ configatron.one_btc*2
+ end
+
+ def holds
+ self.outstanding_buys + self.outstanding_sells
+ end
+
+ def outstanding_sells
+ self.sells.incomplete.map{|sell| sell.price * configatron.one_btc}.sum
+ end
+ def outstanding_buys
+ self.buys.incomplete.map{|buy| buy.price * buy.number_of_shares}.sum
+ end
+
+ def transfer
+
+ end
+
+ def add_amount(number_of_bitcoin)
+ end
+
+ def subtract_amount(number_of_bitcoin)
+ end
end
View
4 app/views/layouts/application.html.haml
@@ -13,4 +13,8 @@
= link_to "Sign in with Facebook", user_omniauth_authorize_path(:facebook)
- else
Hi, #{current_user.name}
+ %br
+ %span Account Available: BTC #{current_user.bitcoin_available}
+ %br
+ %span Account Balance: BTC #{current_user.account_balance}
= yield
View
10 app/views/predictions/index.html.haml
@@ -4,9 +4,7 @@
%tr
%th Name
%th End date
- %th Prediction
- %th Source
- %th Verified by
+ %th Outcome?
%th
%th
%th
@@ -14,10 +12,8 @@
- @predictions.each do |prediction|
%tr
%td= prediction.name
- %td= prediction.end_date
- %td= prediction.prediction
- %td= prediction.source
- %td= prediction.verified_by
+ %td= time_ago_in_words(prediction.end_date)
+ %td= prediction.outcome
%td= link_to 'Show', prediction
%td= link_to 'Edit', edit_prediction_path(prediction)
%td= link_to 'Destroy', prediction, :method => :delete, :data => { :confirm => 'Are you sure?' }
View
20 app/views/predictions/show.html.haml
@@ -14,16 +14,16 @@
%table
%tr
+ %th Number Of Shares
%th Price
- %th Quantity
%th
%th
%th
- @prediction.transactions.each do |transaction|
%tr
- %td= transaction.price
%td= transaction.number_of_shares
+ %td= transaction.price
%td= link_to 'Show', transaction
%h1 Place Your Bits
@@ -31,8 +31,8 @@
%table
%tr
- %th NumberOfShares
- %th User
+ %th Number Of Shares
+ %th Price
%th
%th
%th
@@ -58,7 +58,7 @@
%table
%tr
- %th NumberOfShares
+ %th Number Of Shares
%th User
%th Prediction
%th
@@ -80,8 +80,16 @@
%td= link_to 'Destroy', buy, :method => :delete, :data => { :confirm => 'Are you sure?' }
%br
-= link_to 'New Buy', new_prediction_buy_path(@prediction.id)
+= form_for @prediction, :url => yes_prediction_path(@prediction), :method => :put do |ff|
+ = ff.submit 'YES'
+%br
+= form_for @prediction, :url => yes_prediction_path(@prediction), :method => :put do |ff|
+ = ff.submit 'NO'
+
+%br
+= link_to 'New Buy', new_prediction_buy_path(@prediction.id)
+\|
= link_to 'Edit', edit_prediction_path(@prediction)
\|
= link_to 'Back', predictions_path
View
4 config/database.yml
@@ -21,8 +21,8 @@ test:
reconnect: false
database: bitpredictor_test
pool: 5
- username: root
- password: root
+ username: postgres
+ password: asdcVFGH
production:
adapter: postgresql
View
3 config/initializers/configatron.rb
@@ -3,4 +3,5 @@
configatron.production.facebook.app_id = 152613691570503
configatron.production.facebook.app_secret = '623644277ea911b9dbc695f2c9937932'
configatron.development.facebook.app_id = 527188943990546
-configatron.development.facebook.app_secret = 'a90cb7138481f1540fad9e3d8ba5b0c1'
+configatron.development.facebook.app_secret = 'a90cb7138481f1540fad9e3d8ba5b0c1'
+configatron.one_btc = 0.00013333333333
View
4 config/routes.rb
@@ -15,6 +15,10 @@
resources :predictions do
resources :buys
resources :sells
+ member do
+ put 'yes'
+ put 'no'
+ end
end
devise_for :users, :controllers => { :omniauth_callbacks => "omniauth_callbacks"}
View
5 db/migrate/20130323203319_add_outcome_to_predictions.rb
@@ -0,0 +1,5 @@
+class AddOutcomeToPredictions < ActiveRecord::Migration
+ def change
+ add_column :predictions, :outcome, :string
+ end
+end
View
11 db/migrate/20130323210728_rename_hold_transaction_to_holdable.rb
@@ -0,0 +1,11 @@
+class RenameHoldTransactionToHoldable < ActiveRecord::Migration
+ def up
+ rename_column :holds, :transaction_id, :holdable_id
+ rename_column :holds, :transaction_type, :holdable_type
+ end
+
+ def down
+ rename_column :holds, :holdable_id, :transaction_id
+ rename_column :holds, :holdable_type, :transaction_type
+ end
+end
View
11 db/schema.rb
@@ -11,7 +11,7 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20130323200813) do
+ActiveRecord::Schema.define(:version => 20130323210728) do
create_table "buys", :force => true do |t|
t.decimal "number_of_shares"
@@ -26,10 +26,10 @@
create_table "holds", :force => true do |t|
t.integer "user_id"
t.float "value"
- t.string "transaction_type"
- t.integer "transaction_id"
- t.datetime "created_at", :null => false
- t.datetime "updated_at", :null => false
+ t.string "holdable_type"
+ t.integer "holdable_id"
+ t.datetime "created_at", :null => false
+ t.datetime "updated_at", :null => false
end
create_table "predictions", :force => true do |t|
@@ -41,6 +41,7 @@
t.datetime "created_at", :null => false
t.datetime "updated_at", :null => false
t.integer "user_id"
+ t.string "outcome"
end
create_table "sells", :force => true do |t|

0 comments on commit 8a4a42e

Please sign in to comment.
Something went wrong with that request. Please try again.