module TimelineGame
Constants
- PLUGIN
- STYLE_TITLE
Public Instance Methods
Source
# File lib/timeline_game.rb, line 109 def format_final(game, fake_num:) scores = game["scores"] || {} a = scores[game["challenger"]].to_i b = scores[game["target"]].to_i winner = if a > b game["challenger"] elsif b > a game["target"] else "tie" end lines = [format_header("Final")] lines << "Fake event was ##{fake_num}" lines << "#{game['challenger']}: #{a} ยท #{game['target']}: #{b}" lines << (winner == "tie" ? "Tie!" : "Winner: #{winner}") lines << IrcFormat.report_footer("timeline trap") lines.join("\n") end
Source
# File lib/timeline_game.rb, line 128 def format_header(title) IrcFormat.report_header(tag: "TIME", title: title, style: STYLE_TITLE) end
Source
# File lib/timeline_game.rb, line 132 def format_status(game) return format_header("No game") + "\nNo timeline game active." unless game lines = [format_header("#{game['challenger']} vs #{game['target']}")] lines << "Turn: #{game['turn']}" if game["turn"] lines << IrcFormat.report_footer(game["stage"]) lines.join("\n") end
Source
# File lib/timeline_game.rb, line 44 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 "call", "fake" return { error: usage_message } if parts.length < 2 num = parts[1].to_i return { error: "Pick event 1-4." } unless (1..4).include?(num) { action: :call, choice: num } else { error: usage_message } end end
Source
# File lib/timeline_game.rb, line 62 def start_round(game, rand: ->(max) { Random.rand(0...max) }) round = TimelineEvents.pick(rand: rand) game = game.dup game["stage"] = "active" game["events"] = round[:events] game["calls"] = {} game["turn"] = game["challenger"] lines = round[:events].each_with_index.map { |e, i| "#{i + 1}. #{e[:text]}" } { game: game, message: "#{format_header('Timeline Trap')}\n#{lines.join("\n")}\n#{game['turn']}: !timeline call <1-4> (which is fake?)" } end
Source
# File lib/timeline_game.rb, line 76 def submit_call(game:, user:, choice:) return { error: "No active timeline game." } unless game && game["stage"] == "active" return { error: "Not your turn." } unless GameStore.same_nick?(user, game["turn"]) game = game.dup calls = (game["calls"] || {}).dup key = GameStore.norm_nick(user) calls[key] = choice game["calls"] = calls fake_index = game["events"].each_with_index.find { |e, _| !e[:real] }&.last fake_num = fake_index + 1 correct = choice == fake_num scores = game["scores"] || { game["challenger"] => 0, game["target"] => 0 } if correct scores[key] = scores[key].to_i + 1 else opponent = AsyncDuel.opponent(game, user) scores[opponent] = scores[opponent].to_i + 1 end game["scores"] = scores if calls.size >= 2 game["stage"] = "done" { game: game, message: format_final(game, fake_num: fake_num), done: true } else game["turn"] = AsyncDuel.opponent(game, user) result = correct ? "Correct!" : "Wrong โ opponent gets the point." { game: game, message: "#{result} #{game['turn']}'s turn." } end end
Source
# File lib/timeline_game.rb, line 40 def usage_message "Usage: !timeline challenge <nick> | accept | decline | call <1-4> | status" end