-
Notifications
You must be signed in to change notification settings - Fork 1
/
pokemaker.rb
319 lines (274 loc) · 7.07 KB
/
pokemaker.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
#!/usr/bin/env ruby
require "rubygems"
require "sinatra"
require "dm-core"
require "dm-migrations"
require "dm-types"
require "dm-serializer"
require "extlib"
require "digest/sha1"
require "haml"
require "sass"
require "base64"
require "permutation"
require "rio"
require "ostruct"
require "./lib/gtscrypt.rb"
require "./lib/models.rb"
require "./lib/poketext.rb"
require "./lib/pokedex.rb"
INITIAL_USER_ID=123456
INITIAL_ADMIN_USER="admin"
INITIAL_ADMIN_PASSWORD="pokemaker"
DataMapper.setup(:default, "sqlite:./pokegts.db")
DataMapper.setup(:pokedex, "sqlite:./pokedex.db")
set :port, 80
enable :sessions
DataMapper.auto_upgrade!
helpers do
def pidgenhash(pid)
return Digest::SHA1.hexdigest(pid)
end
def saverawpkmr(pkm,b64=false)
pkm = Base64.decode64(pkm.gsub("-","+").gsub("_","/")) if b64 == true
decrypt = GTSCrypt.new pkm
data = decrypt.decrypt
return data[4..data.size].to_s
end
def auth
redirect "/trainer/login" if @trainer.nil?
end
def auth?
return true unless @trainer.nil?
return false
end
end
before do
if request.path_info =~ %r{/pokemondpds/}
session[:pid] = params[:pid] unless params[:pid].nil?
@trainer = Trainer.first_or_create(:tid=>session[:pid])
if @trainer.monsters.empty?
welpkm = @trainer.monsters.new
welpkm.blob = Trainer.first(:name=>INITIAL_ADMIN_USER).monsters.first.blob
welpkm.extra = Trainer.first(:name=>INITIAL_ADMIN_USER).monsters.first.extra
welpkm.queue = true
welpkm.save
end
halt pidgenhash(session[:pid])[0,32] unless params[:hash]
end
unless params[:t].nil?
@trainer = Trainer.first(:tid=>params[:t]) if @trainer.nil?
else
@trainer = Trainer.first(:tid=>session[:pid])
end
end
get "/system/initialize" do
promo = User.first_or_create(:id=>1, :fbid => INITIAL_USER_ID, :name=>INITIAL_ADMIN_USER, :pass => INITIAL_ADMIN_PASSWORD)
promo.save
trainer = promo.trainers.first_or_create(:id=>1, :tid => INITIAL_USER_ID, :name=>INITIAL_ADMIN_USER, :pass => INITIAL_ADMIN_PASSWORD)
trainer.reg = true
trainer.save
rio('pkm').files('*.pkm') do |data|
pkm = "".force_encoding('UTF-8')
monster = trainer.monsters.new
data >> pkm
pkm << open("./extra").read().force_encoding('UTF-8') if pkm.size == 236
monster.blob = pkm
monster.save
end
redirect "/"
end
get "/css/:css" do
sass params[:css].to_sym
end
get "/" do
haml :index
end
get '/new' do
haml :newindex
end
get "/system/search/:model" do
Kernel.const_get(params[:model].classify).all(:name.like=>"%#{params[:term]}%").to_json
end
get "/trainer/:t/profile" do
@trainer = Trainer.first(:id=>params[:t])
return haml :profile unless @trainer.name.nil?
haml :fpupdate
end
get "/trainer/:t/profile/update" do
auth
haml :fpupdate
end
post "/trainer/:t/profile" do
auth
@trainer.name = params[:name]
@trainer.pass = params[:pass] unless params[:pass].nil?
@trainer.save
redirect "/trainer/#{@trainer.id}/profile"
end
get "/trainer/:t/pokemon/:p" do
@pkm = Trainer.first(:id=>params[:t]).monsters.first(:id=>params[:p])
if @pkm.trainer.tid == session[:pid]
haml :pokeedit
else
haml :pokemon
end
end
get "/trainer/:t/pokemon/:p/delete" do
auth
@pkm = @trainer.monsters.first(:id=>params[:p])
if @trainer.id == @pkm.trainer.id
@pkm.destroy
end
redirect "/trainer/#{@trainer.id}/profile"
end
post "/system/pokemon/:p/ev" do
auth
monster = Monster.get(params[:p])
halt unless @trainer.id == monster.trainer.id
poke = monster.structure
evs = poke.evs
evs[params[:n].to_i] = params[:v].to_i unless params[:v].to_i > 255 or params[:v].to_i < 0
poke.evs = evs
monster.structure = poke
monster.save
total = 0
evs.each do |ev|
total += ev
end
total.to_s
end
post "/system/pokemon/:p/iv" do
auth
monster = Monster.get(params[:p])
halt unless @trainer.id == monster.trainer.id
poke = monster.structure
ivs = poke.ivs
ivs[params[:n].to_i] = params[:v].to_i unless params[:v].to_i > 31 or params[:v].to_i < 0
poke.ivs = ivs
monster.structure = poke
monster.save
total = 0
ivs.each do |iv|
total += iv
end
total.to_s
end
post "/system/pokemon/:p/move" do
auth
monster = Monster.get(params[:p])
halt unless @trainer.id == monster.trainer.id
poke = monster.structure
moves = poke.moves
moves[params[:n].to_i] = params[:v].to_i
poke.moves = moves
monster.structure = poke
monster.save
end
post "/system/pokemon/:p/edit" do
monster = Monster.get(params[:p])
halt unless @trainer.id == monster.trainer.id
poke = monster.structure
begin
m = poke.method(params[:m]+"=")
rescue NameError
halt "no such method called #{params[:m]}"
end
m.call(params[:v])
monster.structure = poke
monster.save
"success"
end
get "/system/ui/auto/pokemon" do
haml :pokenew
end
get "/test" do
m = Monster.get(11)
p = m.structure
p.nickname=("Ame")
m.structure = p
m.save
end
get "/trainer/:t/pokemon/:p/send" do
auth
@pkm = @trainer.monsters.first(:id=>params[:p])
redirect "/trainer/#{@trainer.id}/profile" if @pkm.nil?
@pkm.queue = true
@pkm.save
redirect "/trainer/#{@pkm.trainer.id}/pokemon/#{@pkm.id}"
end
get "/trainer/:t/pokemon/:p/give/:n" do
auth
@pkm = @trainer.monsters.first(:id=>params[:p])
halt unless @trainer.id == @pkm.trainer.id
redirect "/trainer/#{@trainer.id}/profile" if @pkm.nil?
@pkm.trainer = Trainer.first(:tid=>params[:n])
@pkm.save
redirect "/trainer/#{@pkm.trainer.id}/pokemon/#{@pkm.id}"
end
get "/trainer/login" do
haml :flogin
end
get "/trainer/logout" do
session[:pid] = nil
redirect "/"
end
post "/trainer/login" do
@trainer = Trainer.first(:tid=>params[:login])
session[:pid] = @trainer.tid if @trainer.user.pass == params[:pass] unless @trainer.nil?
redirect "/trainer/login" if session[:pid].nil?
redirect "/trainer/#{@trainer.id}/profile"
end
get "/trainer/pokemon/upload" do
haml :fpkmupload
end
post "/trainer/pokemon/upload" do
auth
monster = @trainer.monsters.new
pkm = params[:file][:tempfile].read()
pkm << open("./extra").read().force_encoding('UTF-8') if pkm.size == 236
monster.blob = pkm
monster.save
redirect "/trainer/#{@trainer.id}/pokemon/#{monster.id}"
end
get "/pokemondpds/common/setProfile.asp" do
return ("\x00"*8).to_s
end
get "/pokemondpds/worldexchange/info.asp" do
return "\x01\x00"
end
get "/pokemondpds/worldexchange/result.asp" do
data = "\x05\x00"
if @trainer && @trainer.monsters && @trainer.monsters.first(:queue=>true)
data = @trainer.monsters.first(:queue=>true).blobe
end
return data
end
get "/pokemondpds/worldexchange/get.asp" do
return @trainer.monsters.first(:queue=>true).blobe unless @trainer.monsters.first(:queue=>true).nil?
end
get "/pokemondpds/worldexchange/delete.asp" do
monster = @trainer && @trainer.monsters && @trainer.monsters.first(:queue=>true)
if @trainer && monster
monster.queue = false
monster.save
end
return "\x01\x00"
end
get "/pokemondpds/worldexchange/post.asp" do
pkm = params[:data]
monster = @trainer.monsters.new
monster.blobe = saverawpkmr(pkm,true)
if @trainer.reg == false
@trainer.tid = monster.structure.otid
@trainer.name = monster.structure.trainer
@trainer.reg = true
@trainer.save
end
monster.queue = true
monster.save
return "\x01\x00"
end
get "/pokemondpds/worldexchange/post_finish.asp" do
return "\x01\x00"
end