-
Notifications
You must be signed in to change notification settings - Fork 0
/
salty.rb
136 lines (107 loc) · 3 KB
/
salty.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
require 'bundler'
require 'rubygems'
require 'multi_json'
require 'pry'
require 'net/http'
# Control your Saltybet account from Ruby
# http://www.saltybet.com
# https://github.com/justjake/salty.rb
module Salty
# How long the user has to place a bet before a match begins
BET_WINDOW_DURATION = 70 * 60
API_ENDPOINT = 'www.saltybet.com'
# salty bet web client
class Client
def initialize(username, password)
@username = username
@password = password
end
# fetch saltybet.com/state.json as a dict
def get_state_hash
json = Net::HTTP.get(API_ENDPOINT, '/state.json')
MultiJson.load(json)
end
# fetch saltybet.com/zdata.json as a dict
def get_zdata_hash
json = Net::HTTP.get(API_ENDPOINT, '/zdata.json')
MultiJson.load(json)
end
# create a new state snapshot of saltybet and return it
def get_state!
@latest_state = State.new(get_state_hash(), get_zdata_hash())
end
def state
@latest_state or current_state()
end
end
# snapsot of saltybet state at a moment in time
# constructed from state.json + zdata.json
class State
attr_reader :p1, :p2,
:alert,
:betstate,
:bettors
def betting?
@betting
end
def initialize(state, zdata)
update(state, zdata)
end
def int(num_string)
num_string.split(',').join().to_i
end
def update(state, zdata)
@state = state
@zdata = zdata
@p1 = Character.new(state['p1name'], int(state['p1total']))
@p2 = Character.new(state['p2name'], int(state['p2total']))
@alert = state['alert']
@betting = state['status'] == 'open'
@bettors = []
zdata.each do |key, value|
if key.to_i > 0
b = User.from_json_data(value)
@bettors << b
end
end
return self
end
end
# a castmember.
# Represents someone you can bet on
# TODO: implement stats DB accessor
class Character
attr_accessor :name, :total
def initialize(name, total = nil)
@name = name
@total = total
end
end
# a user on satlybet.com
# places bets on Characters
# TODO: figure out what all the properties in the user JSON are
# {"n"=>"Cillu", "b"=>"2817", "p"=>"2", "w"=>"27", "r"=>"11", "g"=>"0"}
# n: username
# b: total saltybucks
# g: true if this user is "gold" aka Illuminati
# w: wager placed on this fight
# r: bettor level
# p: player this user last betted on
class User
attr_accessor :name, :cash, :wager, :illuminati, :level, :bet_on
# create a user from JSON data
def self.from_json_data(data)
u = User.new()
u.name = data['n']
u.cash = data['b'].to_i
u.wager = data['w'].to_i unless data['w'].nil?
u.level = data['r'].to_i unless data['r'].nil?
u.bet_on = data['p'].to_i unless data['p'].nil?
u.illuminati = data['g'] == '1' unless data['g'].nil?
return u
end
end
end
# dev console
c = Salty::Client.new('u', 'p')
binding.pry