module PitchGame
Constants
- MAX_PITCHES
- PLUGIN
- STYLE_TITLE
Public Instance Methods
Source
# File lib/pitch_game.rb, line 126 def format_header(title) IrcFormat.report_header(tag: "PITCH", title: title, style: STYLE_TITLE) end
Source
# File lib/pitch_game.rb, line 96 def format_pitch_summary(game) lines = [format_header("Final pitches")] [game["challenger"], game["target"]].each_with_index do |player, index| lines << "#{index + 1}. #{player}" Array(game["pitches"][player]).each { |p| lines << " #{p}" } end lines.join("\n") end
Source
# File lib/pitch_game.rb, line 130 def format_status(game) return format_header("No game") + "\nNo pitch war active." unless game lines = [format_header("#{game['challenger']} vs #{game['target']}")] lines << "Product: #{game['product']}" if game["product"] lines << "Stage: #{game['stage']}" if game["stage"] == "vote" t = tally(game) lines << "Votes: #{game['challenger']}=#{t[1]} ยท #{game['target']}=#{t[2]}" end lines << IrcFormat.report_footer("pitch wars") lines.join("\n") end
Source
# File lib/pitch_game.rb, line 37 def parse_command(text) base = AsyncDuel.parse_base(text, usage: usage_message) return base unless base[:action] == :unknown parts = base[:tail] case parts[0].downcase when "vote" return { error: usage_message } if parts.length < 2 num = parts[1].to_i return { error: "Vote 1 or 2." } unless [1, 2].include?(num) { action: :vote, choice: num } else { action: :pitch, text: parts.join(" ") } end end
Source
# File lib/pitch_game.rb, line 55 def start_game(game, rand: ->(max) { Random.rand(0...max) }) product = PitchProducts.pick(rand: rand) game = game.dup game["stage"] = "pitch" game["product"] = product game["pitches"] = { game["challenger"] => [], game["target"] => [] } game["turn"] = game["challenger"] { game: game, message: "#{format_header('Pitch Wars')}\nProduct: #{product}\n#{game['challenger']}: pitch with !pitch <text> (up to #{MAX_PITCHES} lines each)" } end
Source
# File lib/pitch_game.rb, line 68 def submit_pitch(game:, user:, text:) return { error: "Not in pitching phase." } unless game && game["stage"] == "pitch" return { error: "You are not in this game." } unless AsyncDuel.player?(game, user) pitch = text.to_s.strip return { error: "Pitch cannot be empty." } if pitch.empty? game = game.dup pitches = game["pitches"].transform_values(&:dup) key = GameStore.norm_nick(user) list = pitches[key] || [] return { error: "You used all #{MAX_PITCHES} pitches." } if list.length >= MAX_PITCHES list << pitch pitches[key] = list game["pitches"] = pitches if pitches.values.all? { |arr| arr.length >= MAX_PITCHES } game["stage"] = "vote" game.delete("turn") summary = format_pitch_summary(game) { game: game, message: "#{summary}\nChannel: !pitch vote 1 (#{game['challenger']}) or 2 (#{game['target']})" } else game["turn"] = AsyncDuel.opponent(game, user) { game: game, message: "Pitch #{list.length}/#{MAX_PITCHES} recorded. #{game['turn']}'s turn." } end end
Source
# File lib/pitch_game.rb, line 105 def submit_vote(game:, user:, choice:) return { error: "Not in voting phase." } unless game && game["stage"] == "vote" return { error: "Players cannot vote โ ask the channel." } if AsyncDuel.player?(game, user) game = game.dup votes = (game["votes"] || {}).dup key = GameStore.norm_nick(user) return { error: "You already voted." } if votes[key] votes[key] = choice game["votes"] = votes { game: game, message: "Vote recorded for player #{choice}." } end
Source
# File lib/pitch_game.rb, line 119 def tally(game) votes = game["votes"] || {} counts = { 1 => 0, 2 => 0 } votes.each_value { |v| counts[v] = counts[v].to_i + 1 } counts end
Source
# File lib/pitch_game.rb, line 33 def usage_message "Usage: !pitch challenge <nick> | accept | decline | <pitch text> | vote <1|2> | status" end