module ChainGame
Constants
- MAX_ROUNDS
- PLUGIN
- STYLE_TITLE
- WORDS_PER_TURN
Public Instance Methods
Source
# File lib/chain_game.rb, line 42 def add_words(game:, user:, text:) return { error: "No active chain." } unless game && game["stage"] == "active" return { error: "Not your turn." } unless GameStore.same_nick?(user, game["turn"]) words = count_words(text) return { error: "Add exactly #{WORDS_PER_TURN} words (got #{words.length})." } unless words.length == WORDS_PER_TURN game = game.dup line = words.join(" ") game["lines"] = Array(game["lines"]) + [line] game["round"] = game["round"].to_i + 1 if game["round"] >= MAX_ROUNDS game["stage"] = "done" { game: game, message: format_story(game), done: true } else game["turn"] = AsyncDuel.opponent(game, user) { game: game, message: "Line #{game['round']} added. #{game['turn']}: your #{WORDS_PER_TURN} words." } end end
Source
# File lib/chain_game.rb, line 26 def count_words(text) text.to_s.strip.split(/\s+/).reject(&:empty?) end
Source
# File lib/chain_game.rb, line 72 def format_header(title) IrcFormat.report_header(tag: "CHAIN", title: title, style: STYLE_TITLE) end
Source
# File lib/chain_game.rb, line 76 def format_status(game) return format_header("No game") + "\nNo echo chain active." unless game lines = [format_header("#{game['challenger']} vs #{game['target']}")] lines << "Round #{game['round']}/#{MAX_ROUNDS} ยท turn: #{game['turn']}" Array(game["lines"]).each_with_index { |l, i| lines << "#{i + 1}. #{l}" } lines << IrcFormat.report_footer("echo chain") lines.join("\n") end
Source
# File lib/chain_game.rb, line 63 def format_story(game) story = Array(game["lines"]).join(" ") word_count = story.split.length lines = [format_header("Echo Chain complete")] lines << story lines << IrcFormat.report_footer("#{word_count} words", "#{game['round']} rounds") lines.join("\n") end
Source
# File lib/chain_game.rb, line 19 def parse_command(text) base = AsyncDuel.parse_base(text, usage: usage_message) return base unless base[:action] == :unknown { action: :add, text: base[:tail].join(" ") } end
Source
# File lib/chain_game.rb, line 30 def start_game(game, rand: ->(max) { Random.rand(0...max) }) game = game.dup game["stage"] = "active" game["lines"] = [] game["round"] = 0 game["turn"] = game["challenger"] { game: game, message: "#{format_header('Echo Chain')}\n#{game['challenger']}: add exactly #{WORDS_PER_TURN} words with !chain <words>" } end
Source
# File lib/chain_game.rb, line 15 def usage_message "Usage: !chain challenge <nick> | accept | decline | <six words> | status" end