Class: Artaius::Plugins::Mixer

Inherits:
Object
  • Object
show all
Includes:
Cinch::Plugin
Defined in:
lib/artaius/plugins/mixer.rb

Overview

The plugins allows to create game mixes in IRC.

Defined Under Namespace

Classes: Game, Gamer

Constant Summary collapse

DEFAULT_LIMIT =

Internal: Limit to be used, when the op didn’t specify the number of players in the mix.

10
MIN_SLOTS =

Internal: Minimal number of slots needed to be able to play the game.

2
PENDING_DELAY =

Internal: Delay between the beginning of game and its ending (in seconds).

300

Instance Method Summary collapse

Instance Method Details

#add_player(m) ⇒ Object

Internal: Add a new player to the game.

m - The recieved Message.

Returns nothing.



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
# File 'lib/artaius/plugins/mixer.rb', line 74

def add_player(m)
  return if !@game or !already_exists?(m.user.authname)

  unless @game.players.map(&:irc_authname).include?(m.user.authname)
    @game.players << create_gamer(m)

    need_players = @limit - @game.players.size

    if ready_to_begin?
      each_team { |blue, red| begin_game!(m, blue, red) }
    else
      m.reply I18n.mixer.players(show_players)
      m.reply I18n.mixer.need_players(need_players)
    end
  end

  @timer.stop if @timer

  @timer ||= Timer(PENDING_DELAY, shots: 1, start_automatically: false) do
    @game = nil
    Channel(m.channel.name).send I18n.mixer.game_cancelled
  end

  @timer.start
end

#cancel(m) ⇒ Object

Internal: Remove the user from the game. If the user is the last user in the game, automatically revoke that game.

m - The recieved Message.

Returns nothing.



110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/artaius/plugins/mixer.rb', line 110

def cancel(m)
  player = @game.players.find { |p| p.irc_authname == m.user.authname}
  @game.players.delete(player)

  @initiator = @game.players[0]
  m.reply I18n.mixer.cancel(m.user.nick)

  unless @initiator
    @timer.stop
    @game = nil
    m.reply I18n.mixer.last_left
  else
    m.reply I18n.mixer.new_initiator(@initiator.irc_nick)
    m.reply show_players
  end
end

#force_start(m) ⇒ Object

Internal: Start the game by all means.

m - The recieved Message.

Returns nothing.



151
152
153
154
155
# File 'lib/artaius/plugins/mixer.rb', line 151

def force_start(m)
  return unless @game && m.user.authname == @initiator.irc_authname

  each_team { |blue, red| begin_game!(m, blue, red) }
end

#roster(m) ⇒ Object

Internal: Send message about current roster in the mix, if there is any.

m - The recieved Message.

Returns nothing.



136
137
138
139
140
# File 'lib/artaius/plugins/mixer.rb', line 136

def roster(m)
  return unless @game

  m.reply I18n.mixer.roster(show_players)
end

#slot_dispatcher(m, sign, slots) ⇒ Object

Internal: Add or remove slot from the current game.

m - The recieved Message. sign - The + or - sign, indicates adding on removing a slot

respectively.

slots - The Integer, reperesenting number of slots to be added or

removed from the game.


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
# File 'lib/artaius/plugins/mixer.rb', line 169

def slot_dispatcher(m, sign, slots)
  return unless @game && m.user.authname == @initiator.irc_authname

  case sign
  when '+'

    if slots
      slots.to_i.times { add_slot }
      m.reply I18n.mixer.n_slots_added(slots, slots_message)
    else
      if @limit >= MIN_SLOTS
        add_slot
        m.reply I18n.mixer.slot_added(slots_message)
      end
    end

  when '-'

    if slots
      removed_slots = 0
      slots.to_i.times { |i|
        remove_slot
        removed_slots = i+1
        break unless @limit > MIN_SLOTS
      }
      m.reply I18n.mixer.n_slots_removed(removed_slots, slots_message)
    else
      if @limit > MIN_SLOTS
        remove_slot
        m.reply I18n.mixer.slot_removed(slots_message)
      end
    end

  end

  if ready_to_begin?
    each_team { |blue, red| begin_game!(m, blue, red) }
  end
end

#slots(m) ⇒ Object

Internal: Display information about slots of the current game.

m - The recieved Message.

Returns nothing.



218
219
220
221
222
# File 'lib/artaius/plugins/mixer.rb', line 218

def slots(m)
  return unless @game

  m.reply I18n.mixer.slot_stats(@game.players.size, @limit)
end

#start_game(m, limit) ⇒ Object

Internal: Create new game and add the creator to that game, so he/she will be the first player in that game.

m - The recieved Message. limit - The Integer, which sets the limit of the players in the mix.

Returns nothing.



51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'lib/artaius/plugins/mixer.rb', line 51

def start_game(m, limit)
  return if @game or !already_exists?(m.user.authname)

  @limit = if limit && !limit.empty?
    limit.to_i
  else
    DEFAULT_LIMIT
  end

  @game = Game.new([], limit, Time.now)
  @initiator = create_gamer(m)
  add_player(m)
end