-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathws-dobon.rb
418 lines (337 loc) · 10.4 KB
/
ws-dobon.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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
# -*- coding: utf-8 -*-
require 'rubygems'
require 'sequel'
require 'sinatra'
require 'haml'
require 'sass'
require 'digest/sha2'
require 'time'
require 'enumerator'
require 'json'
require 'pusher'
$:.unshift(File.expand_path(File.dirname(__FILE__)))
require 'lib/dobon'
###
### Configs
###
def app_root
"#{env['rack.url_scheme']}://#{env['HTTP_HOST']}#{env['SCRIPT_NAME']}"
end
use Rack::Session::Cookie,
:expire_after => 2592000,
:secret => ENV['SESSION_COOKIE_SECRET'] || 'CHANGE ME!!'
Pusher.app_id = ENV['PUSHER_APP_ID'] || 'CHANGE ME!!'
Pusher.key = ENV['PUSHER_KEY'] || 'CHANGE ME!!'
Pusher.secret = ENV['PUSHER_SECRET'] || 'CHANGE ME!!'
###
### Models
###
require 'models'
###
### Helpers
###
require 'helpers'
###
### Routes
###
include Models
### APIs
## ログイン状態であるかのフィルタ
before '/*' do
@player = Player.find(:sessionkey => session[:sessionkey])
## sessionkeyが有効でない or 既に部屋が閉じている
if session[:sessionkey].nil? or @player.nil? or @player.room.is_closed
@player = nil
session[:sessionkey] = ''
else
@room = @player.room
end
end
## 部屋の作成
get '/room/create' do
player_not_registered
halt_ng "部屋の名前を入力して下さい。" if params[:name].nil? or params[:name].empty?
DB.transaction do
room = Room.create(:name => params[:name], :is_closed => false)
end
return_ok "部屋を作成しました"
end
## 部屋への参加
get '/player/join' do
player_not_registered
halt_ng "プレイヤーの名前を入力して下さい。" if params[:name].nil? or params[:name].empty?
halt_ng "部屋を指定して下さい。" unless params[:room_id]
@room = Room.find(:id => params[:room_id])
halt_ng "存在しない部屋IDです" if @room.nil?
halt_ng "部屋が既に閉じています" if @room.is_closed
game_not_started
puts "#{@room.players.size}"
halt_ng "部屋が満員です" if @room.players.size == 4
player = Player.find(:sessionkey => session[:sessionkey])
DB.transaction do
player = Player.create(
:room_id => params[:room_id],
:name => params[:name],
:hand => ""
)
PlayerState.find(:label => 'not-ready').add_player(player)
player.update(:sessionkey => Digest::SHA256.hexdigest(Time.now.to_s + player.id.to_s))
session[:sessionkey] = player.sessionkey
end
#return_ok "#{@room.name}に参加しました。"
{:room_id => @room.id}.to_json
end
## 部屋からの退出
get '/player/quit' do
player_registered
### 工大祭用 コメントアウト
#game_not_started
### プレイヤーをinactiveに
DB.transaction do
PlayerState.find(:label => 'inactive').add_player(@player)
end
session[:sessionkey] = ''
### プレイヤー数が0であれば部屋を閉じる
if @room.players.all?{|player| player.player_state.label == 'inactive'}
DB.transaction do
@room.update(:is_closed => true)
end
end
return_ok ""
end
## 準備完了
get '/player/ready' do
player_registered
game_not_started
DB.transaction do
PlayerState.find(:label => 'ready').add_player(@player)
end
if @room.players.size > 1 and # 4人以上で
@room.players.all?{|player| player.player_state.label == 'ready' }
## ゲーム開始
table = nil
DB.transaction do
## ゲームをテーブルに追加
game = @room.add_game({})
## プレイ順の決定
players = @room.players.sort_by{rand}
orders = players.map.with_index do |player, idx|
order = PlayingOrder.create(:order => idx)
player.add_playing_order(order)
@room.games.last.add_playing_order(order)
order
end
current_player_id = orders.sort_by{|e|e.order}.first.player.id
### ラウンド, テーブルの生成
table = start_new_round(game, players, current_player_id)
end
## ゲーム開始をpush
Pusher[@room.id].trigger('deal', deal_response(table).to_json)
return_ok "all-players-ready"
else
return_ok "#{@player.player_state.label}"
end
end
### 準備未完了
get '/player/not-ready' do
player_registered
game_not_started
DB.transaction do
PlayerState.find(:label => 'not-ready').add_player(@player)
end
return_ok "#{@player.player_state.label}"
end
### Action API フィルタ
before '/player/action/*' do
player_registered
game_started
@game = @room.games.last
@round = @game.rounds.last
@table = @round.tables.first
@current_player = @table.current_player
@hand = Playingcard::Deck.new(@player.hand)
end
### カードを場に出す
get '/player/action/play' do
## エラーチェック
your_turn
not_agari(@table.last_played) unless @table.last_played.nil?
phase_not_wait_to_dobon
halt_ng 'カードを指定して下さい。' unless params[:card]
card = Playingcard::Card.new(params[:card])
halt_ng '不正なカードの値です。' unless card.valid?
unless @hand.include?(card)
halt_ng '指定されたカードが手札にありません。'
end
@hand.delete(card)
table = table_model_to_logic(@table)
res = table.put(card, params[:specify])
if res.nil?
halt_ng 'このカードは場に出すことが出来ません。'
end
DB.transaction do
@player.update(:hand => @hand.to_s)
@table.update(
:deck => table.deck.to_s,
:discards => table.discards.to_s,
:specify => table.specify,
:restriction => table.restriction,
:reverse => table.reverse,
:passed => table.passed,
:attack => table.attack.to_s,
:current_player_id => next_player(res ? 2 : 1).id,
:last_played_id => @player.id,
:last_played_time => Time.now.to_s
)
#RoundState.find(:label => 'wait-to-dobon')
# .add_round(@round)
end
## カード出した
Pusher[@room.id].trigger('play', deal_response(@table).to_json)
return_ok ''
end
### パスする
get '/player/action/pass' do
your_turn
not_agari(@table.last_played) unless @table.last_played.nil?
phase_not_wait_to_dobon
table = table_model_to_logic(@table)
draw, next_turn = table.pass
draw.times{ @hand.push(table.draw) }
DB.transaction do
@player.update(:hand => @hand.to_s)
@table.update(
:deck => table.deck.to_s,
:discards => table.discards.to_s,
:specify => table.specify,
:restriction => table.restriction,
:reverse => table.reverse,
:passed => table.passed,
:attack => table.attack.to_s,
:last_played_time => Time.now.to_s
)
if next_turn
@table.update(:current_player_id =>
next_player(1).id)
end
end
## パスした
Pusher[@room.id].trigger('pass', deal_response(@table).to_json)
return_ok ''
end
### ドボンする
get '/player/action/dobon' do
not_passed
halt_ng '自分の出したカードに対してドボンは出来ません。' if @player.id == @table.last_played.id
table = table_model_to_logic(@table)
res = table.dobon(@hand)
winner_label = ''
loser_label = ''
point = Playingcard::Deck.new(@table.last_played.hand).sum
winner_id = ''
loser_id = ''
case res
when 'dobon'
rate = 1
winner_label = 'dobon'
loser_label = 'make'
winner_id = @player.id
loser_id = @table.last_played.id
when 'joker'
rate = 14
winner_label = 'dobon'
loser_label = 'make'
winner_id = @player.id
loser_id = @table.last_played.id
when 'miss-dobon'
## 工大祭用:ミスドボンは認めないことに!!
halt_ng "手札の合計と等しくありません"
rate = 1
winner_label = 'make'
loser_label = 'miss-dobon'
winner_id = @table.last_played.id
loser_id = @player.id
end
table = nil
DB.transaction do
### 勝ちプレイヤー
r = RoundResult.create(:round_id => @round.id,
:player_id => winner_id,
:point => point * rate)
FinishType.find(:label => winner_label).add_round_result(r)
### 負けプレイヤー
rr = RoundResult.create(:round_id => @round.id,
:player_id => loser_id,
:point => point * rate * -1)
FinishType.find(:label => loser_label).add_round_result(rr)
Round.update(:winner_id => winner_id, :loser_id => loser_id)
### ラウンド, テーブルの生成
table = start_new_round(@game, @room.players, loser_id)
end
Pusher[@room.id].trigger('dobon',{
:table => deal_response(table),
:winner_id => winner_id,
:loser_id => loser_id
}.to_json)
return_ok ''
end
### 上がり
get '/player/action/agari' do
halt_ng '上がり状態ではありません。' if @table.last_played.nil? or not agari?(@table.last_played)
halt_ng 'ドボン待ち時間です。' if Time.now - @table.last_played_time <= 5
### 点数計算
points = @room.players.map{ |player|
if player.id != @table.last_played.id
[
player,
(Playingcard::Deck.new(player.hand).sum / 2.0).ceil
]
end
}.compact
### 上がり処理
table = nil
loser_id = nil
DB.transaction do
points.each do |player, point|
rr = RoundResult.create(:round_id => @round.id,
:player_id => player.id,
:point => -1 * point)
FinishType.find(:label => 'make').add_round_result(rr)
end
r = RoundResult.create(:round_id => @round.id,
:player_id => @table.last_played.id,
:point => points.map{|player, point| point}.inject(0){|sum, point| sum += point})
FinishType.find(:label => 'agari').add_round_result(r)
loser_id = points.sort_by{|player, point| point}.last[0].id
Round.update(:winner_id => @table.last_played.id, :loser_id => loser_id)
table = start_new_round(@game, @room.players, loser_id)
end
Pusher[@room.id].trigger('agari',{
:table => deal_response(table),
:winner_id => @table.last_played.id,
:loser_id => loser_id
}.to_json)
return_ok ''
end
### プレイデータ取得用API
get '/player/self' do
player_response().to_json
end
get '/player/action/hand' do
Playingcard::Deck.new(@player.hand).to_a.map{|c|
"#{c.to_s}"
}.to_s ## !TODO to_json
end
### Views
## index.haml
get '/' do
@rooms = Room.filter(:is_closed => false).reject{|room|
not room.games.nil? and not room.games.empty?
}
haml :index
end
## stylesheets
get '/styles/*.css' do |filename|
content_type 'text/css', :charset => 'utf-8'
sass filename.to_sym
end