Class: GameMachine::DefaultHandlers::TeamHandler

Inherits:
Object
  • Object
show all
Includes:
Models
Defined in:
server/lib/game_machine/default_handlers/team_handler.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeTeamHandler

Returns a new instance of TeamHandler


18
19
20
# File 'server/lib/game_machine/default_handlers/team_handler.rb', line 18

def initialize
  @matched = self.class.matched
end

Instance Attribute Details

#matchedObject (readonly)

Returns the value of attribute matched


8
9
10
# File 'server/lib/game_machine/default_handlers/team_handler.rb', line 8

def matched
  @matched
end

#presetskillsObject (readonly)

Returns the value of attribute presetskills


8
9
10
# File 'server/lib/game_machine/default_handlers/team_handler.rb', line 8

def presetskills
  @presetskills
end

#requirementsObject (readonly)

Returns the value of attribute requirements


8
9
10
# File 'server/lib/game_machine/default_handlers/team_handler.rb', line 8

def requirements
  @requirements
end

Class Method Details

.matchedObject


10
11
12
13
14
15
16
# File 'server/lib/game_machine/default_handlers/team_handler.rb', line 10

def self.matched
  if @matched
    @matched
  else
    @matched = java.util.concurrent.ConcurrentHashMap.new
  end
end

Instance Method Details

#can_add_member?(team_name, member) ⇒ Boolean

return true if member has rights to join team

Returns:

  • (Boolean)

146
147
148
# File 'server/lib/game_machine/default_handlers/team_handler.rb', line 146

def can_add_member?(team_name,member)
  true
end

#can_create_team?(team_name, member) ⇒ Boolean

return true if member has rights to create team

Returns:

  • (Boolean)

141
142
143
# File 'server/lib/game_machine/default_handlers/team_handler.rb', line 141

def can_create_team?(team_name,member)
  true
end

#destroy_on_owner_leave?Boolean

Returns:

  • (Boolean)

150
151
152
# File 'server/lib/game_machine/default_handlers/team_handler.rb', line 150

def destroy_on_owner_leave?
  true
end

#game_id(player_id) ⇒ Object


30
31
32
33
# File 'server/lib/game_machine/default_handlers/team_handler.rb', line 30

def game_id(player_id)
  player_service = GameMachine::JavaLib::PlayerService.get_instance
  player_service.get_game_id(player_id)
end

#match!(teams, team) ⇒ Object

Should return a Match object if a match is found, otherwise nil


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
# File 'server/lib/game_machine/default_handlers/team_handler.rb', line 43

def match!(teams,team)
  my_rating = team_rating(team)
  best = nil
  best_team = nil
  teams.teams.each do |other|

    if other.members.size < other.min_for_match
      next
    end

    if rating = team_rating(other)
      diff = my_rating > rating ? my_rating - rating : rating - my_rating
      if best.nil? || diff < best
        best = diff
        best_team = other
      end
    end
  end

  if best_team
    GameMachine::Models::StartMatch.new(:team_names => [team.name,best_team.name])
  else
    nil
  end
end

#match_started(match) ⇒ Object

Called when Game Machine has started the match


70
71
72
# File 'server/lib/game_machine/default_handlers/team_handler.rb', line 70

def match_started(match)

end

#pass_skilltest?(player_id, skills, req) ⇒ Boolean

Returns:

  • (Boolean)

128
129
130
131
132
133
134
135
136
137
138
# File 'server/lib/game_machine/default_handlers/team_handler.rb', line 128

def pass_skilltest?(player_id,skills,req)
  c = Dentaku::Calculator.new
  c.store(skills)
  #puts "Skills=#{skills.inspect} requirements=#{req.inspect}"
  req.each do |expr|
    unless c.evaluate(expr)
      return false
    end
  end
  true
end

#player_can_set_skills?Boolean

Returns:

  • (Boolean)

22
23
24
# File 'server/lib/game_machine/default_handlers/team_handler.rb', line 22

def player_can_set_skills?
  true
end

#scope_key(str, player_id) ⇒ Object


26
27
28
# File 'server/lib/game_machine/default_handlers/team_handler.rb', line 26

def scope_key(str,player_id)
  "#{game_id(player_id)}^^#{str}"
end

#skills_for_player(player_id) ⇒ Object


86
87
88
89
90
91
92
93
94
# File 'server/lib/game_machine/default_handlers/team_handler.rb', line 86

def skills_for_player(player_id)
  player_skills_id = "player_skills_#{player_id}"
  if player_skills = PlayerSkills.find(scope_key(player_skills_id,player_id))
    skills = player_skills.skills.marshal_dump
  else
    skills = nil
  end
  skills
end

#team_created(team, create_team_message) ⇒ Object


38
39
40
# File 'server/lib/game_machine/default_handlers/team_handler.rb', line 38

def team_created(team,create_team_message)
  matched[team.name] = {}
end

#team_rating(team) ⇒ Object


74
75
76
77
78
79
80
81
82
83
84
# File 'server/lib/game_machine/default_handlers/team_handler.rb', line 74

def team_rating(team)
  rating = 0
  team.members.each do |member|
    if skills = skills_for_player(member)
      if skills[:rating] && skills[:rating] > 0
        rating += skills[:rating]
      end
    end
  end
  rating
end

#teams_filter(teams, teams_request) ⇒ Object

Filter you can apply to the teams list sent to clients You can add whatever extra fields you want to the TeamsRequest class on the client and they will show up here.


99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'server/lib/game_machine/default_handlers/team_handler.rb', line 99

def teams_filter(teams,teams_request)
  skills = skills_for_player(teams_request.player_id)
  matching = []
  teams.teams.each do |team|
    if team.owner == teams_request.player_id
      matching << team
      next
    end

    if team.requirements
      if skills
        if matched[team.name] && matched[team.name][teams_request.player_id]
          matching << team
        elsif pass_skilltest?(teams_request.player_id,skills,team.requirements)
          matching << team
           matched[team.name] ||= {}
          matched[team.name][teams_request.player_id] = true
        end
      end
    else
      matching << team
      next
    end
  end

  teams.teams = matching
  teams
end

#update_teams(key) ⇒ Object


35
36
# File 'server/lib/game_machine/default_handlers/team_handler.rb', line 35

def update_teams(key)
end