Class: KAG::Gather::Match

Inherits:
SymbolTable
  • Object
show all
Includes:
Cinch::Helpers
Defined in:
lib/kag/gather/match.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.type_as_stringObject



14
15
16
17
18
# File 'lib/kag/gather/match.rb', line 14

def self.type_as_string
  ms = KAG::Config.instance[:match_size].to_i
  ts = (ms / 2).ceil
  "#{ts.to_s}v#{ts.to_s} #{KAG::Config.instance[:match_type]}"
end

Instance Method Details

#add_end_voteObject



96
97
98
99
# File 'lib/kag/gather/match.rb', line 96

def add_end_vote
  self[:end_votes] = 0 unless self[:end_votes] > 0
  self[:end_votes] = self[:end_votes] + 1
end

#archiveObject



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
208
209
210
# File 'lib/kag/gather/match.rb', line 181

def archive
  match = self.dup
  match[:server] = self.server[:key]
  ts = []
  if match.teams
    match.teams.each do |team|
      ts << {:players => team.teammates,:color => team[:color],:name => team[:name]}
    end
    match[:teams] = ts
  end
  match.delete(:players)
  match.delete(:bot) if match.key?(:bot)

  unless File.exists?("data/matches.json")
    File.open("data/matches.json","w") {|f| f.write("{}") }
  end

  data = Hash.new
  d = ::IO.read("data/matches.json")
  if d and !d.empty?
    data = Hash.new
    data.merge!(JSON.parse(d))
  end

  data[Time.now.to_s] = match
  File.open("data/matches.json","w") do |f|
    f.write(data.to_json)
  end
  true
end

#ceaseObject



120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/kag/gather/match.rb', line 120

def cease
  if self.server
    self.archive
    KAG::Stats::Main.add_stat(:matches_completed)
    if self.server.has_rcon?
      self.teams.each do |team|
        team.kick_all
      end
    else
      debug "NO RCON, so could not kick!"
    end
    self.server.disconnect
    self.server.delete(:match)
    true
  else
    debug "No server for match defined!"
    false
  end
end

#debug(msg) ⇒ Object



175
176
177
178
179
# File 'lib/kag/gather/match.rb', line 175

def debug(msg)
  if KAG::Config.instance[:debug]
    puts msg
  end
end

#get_needed_end_votes_leftObject



107
108
109
110
# File 'lib/kag/gather/match.rb', line 107

def get_needed_end_votes_left
  evt = KAG::Config.instance[:end_vote_threshold].to_i
  evt - self[:end_votes]
end

#has_player?(user) ⇒ Boolean

Returns:

  • (Boolean)


112
113
114
115
116
117
118
# File 'lib/kag/gather/match.rb', line 112

def has_player?(user)
  playing = false
  self.teams.each do |team|
    playing = true if team.has_player?(user)
  end
  playing
end

#needs_sub?Boolean

Returns:

  • (Boolean)


161
162
163
# File 'lib/kag/gather/match.rb', line 161

def needs_sub?
  self[:subs_needed].length > 0
end

#notify_teams_of_match_startObject



88
89
90
91
92
93
94
# File 'lib/kag/gather/match.rb', line 88

def notify_teams_of_match_start
  messages = {}
  self.teams.each do |t|
    messages.merge!(t.notify_of_match_start)
  end
  messages
end

#remove_player(user) ⇒ Object



148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/kag/gather/match.rb', line 148

def remove_player(user)
  sub = false
  self[:teams].each do |team|
    if team.has_player?(user)
      sub = team.remove_player(user)
    end
  end
  if sub
    self[:subs_needed] << sub
  end
  sub
end

#restart_mapObject



140
141
142
143
144
145
146
# File 'lib/kag/gather/match.rb', line 140

def restart_map
  if self.server.has_rcon?
    self.server.restart_map
  else
    debug "Cannot restart map, no RCON defined"
  end
end

#setup_teamsObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/kag/gather/match.rb', line 20

def setup_teams
  self.players.shuffle!
  match_size = KAG::Config.instance[:match_size].to_i
  match_size = 2 if match_size < 2

  team_list = [{
    :color => "\x0312",
    :name => "Blue"
  },{
     :color => "\x0304",
     :name => "Red"
  }]
  players_per_team = (match_size / 2).floor.to_i

  debug "MATCH SIZE #{match_size.to_s}"
  debug "Players Per Team: #{players_per_team.to_s}"
  debug "PLAYERS: #{self.players.keys.join(",")}"

  self.players.each do |authname,user|
    if user
      KAG::User::User.add_stat(user,:matches)
    else
      self.players.delete(authname.to_sym)
    end
  end

  self.teams = []
  lb = 0
  team_list.each do |ts|
    eb = lb+players_per_team-1
    eb = self.players.length if eb > self.players.length-1
    x = 0
    ps = {}
    self.players.each do |authname,user|
      if x >= lb and x <= eb
        ps[authname.to_sym] = user
      end
      x = x + 1
    end
    #ps = Hash[self.players.sort_by{|k,v| v ? v.to_s : ''}[lb..(eb)]]
    lb = players_per_team

    self.teams << KAG::Gather::Team.new({
      :players => ps,
      :match => self,
      :color => ts[:color],
      :name => ts[:name]
    }).setup
  end
  self.teams
end

#startObject



72
73
74
75
76
77
78
# File 'lib/kag/gather/match.rb', line 72

def start
  self[:end_votes] = 0 unless self[:end_votes]
  self[:subs_needed] = []
  setup_teams
  restart_map
  KAG::Stats::Main.add_stat(:matches_started)
end

#sub_in(user) ⇒ Object



165
166
167
168
169
170
171
172
173
# File 'lib/kag/gather/match.rb', line 165

def sub_in(user)
  placement = false
  if needs_sub?
    placement = self[:subs_needed].shift
    KAG::User::User.add_stat(user,:substitutions)
    KAG::Stats::Main.add_stat(:substitutions_done)
  end
  placement
end

#text_for_match_startObject



80
81
82
83
84
85
86
# File 'lib/kag/gather/match.rb', line 80

def text_for_match_start
  msg = "MATCH: #{KAG::Gather::Match.type_as_string} - "
  self.teams.each do |team|
    msg = msg+" "+team.text_for_match_start
  end
  msg+" \x0301(!end when done)"
end

#voted_to_end?Boolean

Returns:

  • (Boolean)


101
102
103
104
105
# File 'lib/kag/gather/match.rb', line 101

def voted_to_end?
  evt = KAG::Config.instance[:end_vote_threshold].to_i
  evt = 3 if evt < 1
  self[:end_votes] >= evt
end