-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathelements.rb
More file actions
153 lines (122 loc) · 3.7 KB
/
elements.rb
File metadata and controls
153 lines (122 loc) · 3.7 KB
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
require 'logger'
require 'httpclient'
module Elements
module Network
DOMAIN = 'http://www.elementsthegame.com'
def logger
@@logger ||= Logger.new($stdout)
end
def login_url; "#{DOMAIN}/testo5.php"; end
def sync_url; "#{DOMAIN}/dev7.php"; end
def swf_url; "#{DOMAIN}/elt133rt.swf"; end
def default_request_headers
{
'Connection' => 'keep-alive',
'Origin' => DOMAIN,
'User-Agent' => 'Mozilla/5.0',
'Content-Type' => 'application/x-www-form-urlencoded',
'Accept' => '*/*',
'DNT' => 1,
'Referer' => swf_url,
'Accept-Language' => 'en-US,en;q=0.8'
}
end
def url_encode_to_hash(string)
string.split('&').inject({}) {|h, pair| parts = pair.split('='); h[parts[0]] = parts[1]; h }
end
def request(url, params, headers = {})
resp = HTTPClient.new.post(url, params, default_request_headers.merge(headers))
url_encode_to_hash(resp.content)
end
def self.included(base)
base.send :extend, self
end
end
end
module Elements
class Player
include Network
attr_accessor :attributes
def initialize(attributes)
self.attributes = attributes
end
def sync_attributes
{
'errorcode' => -1,
'deck2n' => 0, #wtf?
'deck3' => nil, #wtf?
'newemail' => nil,
'newpsw' => nil,
'cardo' => 0
}
end
def compute_fhcv_checksum
fhcv = 0
deckall = attributes['decka']
deckalln = (deckall.size / 4.0).floor
deckalln.times do |i|
cardnum = deckall[4*i..4*i+3].to_i
fhcv = fhcv + 10 + (cardnum / 2000).floor * 1500
if cardnum - (cardnum / 100.0).floor * 100 == 20
fhcv = fhcv + 1000
end
end
logger.debug "computed fhcv: #{fhcv}"
attributes['fhcv'] = fhcv
end
def compute_fh_checksum
electrum = attributes['electrum'].to_i
attributes['deck2'] = "fhh" if electrum > 4
ranrr = 100.0 + rand * 600.0;
fhcv = compute_fhcv_checksum.to_i
score = attributes['score'].to_i
lonelyt = attributes['lonelyt'].to_i
fhrr = -(Math.sin(ranrr / 314.0) * Math.sin(ranrr / 314.0) + Math.cos(ranrr / 314.0) *
Math.cos(ranrr / 314.0)).round * (10 ** (ranrr / 100.0).floor)
fhrr += fhcv + electrum * 3
fh = fhrr + fhcv + electrum + score + lonelyt/2
logger.debug "computed fh: #{fh}"
attributes['fh'] = fh
end
def valid?
if attributes['decka'].size > 16000
logger.error 'A maximum of 4000 cards can be saved'
return false
end
true
end
def sync
return unless valid?
compute_fh_checksum
logger.info "Syncing player data"
params = attributes.merge(sync_attributes)
result = request(sync_url, params)
logger.info "Sync result: #{result['errorcode']}"
end
def self.login username, password
params = {user: username, psw: password, errorcode: -1}
logger.info "Logging in as #{username}"
attributes = request(login_url, params)
if attributes['errorcode'] != '0'
logger.error "Auth failed"
exit(1)
end
logger.info "Logged in!"
new(attributes.merge('user' => username, 'psw' => password))
end
end
end
def usage
puts "Usage: ruby elements.rb user psw [[electrum=XXX] [won=XXX] [lost=XXX] [score=XXX]]"
exit(0)
end
usage if ARGV.size < 3
whitelist = %w[electrum won lost score]
attributes = ARGV[2..-1].inject({}) do |h, str|
key, value = str.split('=').map(&:downcase)
h[key] = whitelist.include?(key) && value.to_i || usage
h
end
player = Elements::Player.login ARGV[0], ARGV[1]
player.attributes.merge!(attributes)
player.sync