-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.rb
76 lines (62 loc) · 1.37 KB
/
main.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
require 'rubygems'
require 'twilio-ruby'
require 'sinatra'
require 'mongo'
require 'uri'
require_relative 'blackjack'
def get_connection
return @db_connection if @db_connection
db = URI.parse(ENV['MONGOHQ_URL'])
db_name = db.path.gsub(/^\//, '')
@db_connection = Mongo::Connection.new(db.host, db.port).db(db_name)
@db_connection.authenticate(db.user, db.password) unless (db.user.nil? || db.user.nil?)
@db_connection
end
def updateDb(playerHand, dealerHand, deck, user)
db = get_connection
coll = db['test']
coll.insert(
"playerHand" => playerHand,
"dealerHand" => dealerHand,
"deck" => deck,
"user" => user
)
end
def getGameState(user)
db = get_connection
coll = db['test']
user = coll.find("user" => user).to_a
return user
end
def clearUser(user)
db = get_connection
coll = db['test']
coll.remove("user" => user)
end
def newGame(user)
clearUser(user)
bj = Blackjack.new
updateDb(bj.playerHand, bj.dealerHand, bj.deck, user)
end
def hit
end
get '/' do
message = params[:Body]
user = params[:From]
if message == "DEAL"
newGame(user)
test = getGameState(user)
twiml = Twilio::TwiML::Response.new do |r|
r.Message test
end
twiml.text
end
end
get '/mongoTest' do
bj = Blackjack.new
db = get_connection
collections = db.collection_names
"collections = #{collections}"
coll = db['test']
coll.insert({"playerHand" => bj.playerHand})
end