diff --git a/public/client.html b/public/client.html index f3e708a..57ef73e 100644 --- a/public/client.html +++ b/public/client.html @@ -1,119 +1,189 @@ - - - - - - 回答者ページ - - - -
-

イントロクイズ

-
-

参加登録

- - - -
-
-

- -

待機中...

-

現在の順番: まだ挙手なし

-
-
- - - + when "correct" + if first_raiser = raised_queue.first + group = first_raiser[:group] + team_points[group] += 5 + puts "[CORRECT] #{group} に 5ポイント加算! 合計: #{team_points[group]}点" + payload = { type: "correct", answer: data["answer"], raiser: first_raiser }.to_json + clients.each { |c| c.send(payload) } + + raised_queue.clear + penalty_box.clear + + broadcast_teams_and_points(clients, participants, team_points) + broadcast_queue(clients, raised_queue) + end + + when "incorrect" + if incorrect_participant = raised_queue.shift + group = incorrect_participant[:group] + penalty_until = Time.now + 8 + penalty_box[group] = penalty_until + + puts "[INCORRECT] #{group} は #{penalty_until.strftime('%H:%M:%S')} までペナルティ。" + broadcast_penalty_info(clients, penalty_box) # ペナルティ情報を即時通知 + + clients.each { |c| c.send({type: "incorrect"}.to_json) } + + EM.add_timer(1) do + puts "[TIMER] 1秒経過..." + if raised_queue.empty? + puts "[AUTO PLAY] 回答者がいないため、再生を再開します。" + clients.each { |c| c.send({ type: "play" }.to_json) } + broadcast_queue(clients, raised_queue) + else + puts "更新されたキューを送信します。" + broadcast_queue(clients, raised_queue) + end + end + end + + when "select" + selected_url = "http://192.168.1.138:8890/#{URI.encode_uri_component(data["file"])}" + penalty_box.clear + clients.each { |c| c.send({ type: "select", url: selected_url }.to_json) } + + # (その他のcaseは変更なし) + when "reset_scores", "adjust_score", "play", "pause", "stop" + # (省略) + when "reset_scores" + team_points.clear + puts "[SCORE RESET] 全チームの得点をリセットしました。" + broadcast_teams_and_points(clients, participants, team_points) + when "adjust_score" + group = data["group"] + score = data["score"].to_i + if group && score + team_points[group] += score + puts "[ADJUST SCORE] #{group} の得点を #{score}点 調整。合計: #{team_points[group]}点" + broadcast_teams_and_points(clients, participants, team_points) + end + when "play" + clients.each { |c| c.send({ type: "play" }.to_json) } if selected_url + when "pause" + clients.each { |c| c.send({ type: "pause" }.to_json) } + when "stop" + clients.each { |c| c.send({ type: "stop" }.to_json) } + else + puts "[WARN] 未知のメッセージタイプを受信: #{data['type']}" + end + rescue JSON::ParserError => e + puts "[ERROR] JSONパースエラー: #{e.message}" + end + end + + ws_conn.onclose do + puts "[CLOSE] 切断: 残り #{clients.size - 1} 名" + clients.delete(ws_conn) + if participant = participants.delete(ws_conn) + puts "[LEAVE] #{participant[:group]} の #{participant[:name]} さんが退出しました。" + raised_queue.delete(participant) + broadcast_teams_and_points(clients, participants, team_points) + broadcast_queue(clients, raised_queue) + end + end + end +end