Skip to content

Instantly share code, notes, and snippets.

@kimoto
Created August 23, 2010 15:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kimoto/545673 to your computer and use it in GitHub Desktop.
Save kimoto/545673 to your computer and use it in GitHub Desktop.
# name:
# auto-team-balance(all shuffle mode) concept code
# spec:
# spectators is need more +1 slot
# if under 2 player then not enable auto balanced function, why? should be server shutting down
# known bugs:
# *fixed: unbalance size teams so error
# history:
# bug fixed
# cleanup code
# author:
# kimoto
MIN_PLAYER_SIZE = 2
TEAM_SPECTATORS = 1
TEAM_SURVIVORS = 2
TEAM_INFECTED = 3
def guess_auto_team_balance(survivors, infected, spectators)
return nil if (survivors + infected).size <= MIN_PLAYER_SIZE
players = (survivors + infected).shuffle
total_size = players.size
half_size = total_size / 2
n_survivors = players[0...half_size]
n_infected = players[half_size...total_size]
[n_survivors, n_infected]
end
def guess_move_players(orig_survivors, survivors)
need = survivors - orig_survivors
not_need = orig_survivors - survivors
[need,not_need]
end
def change_client_team(client, type)
# nop
end
def auto_team_balance(orig_survivors, orig_infected, spectators)
balanced_teams = guess_auto_team_balance(orig_survivors, orig_infected, spectators)
(survivors, infected) = balanced_teams
if balanced_teams.nil?
puts "cant auto team balance"
exit
end
(need, not_need) = guess_move_players(orig_survivors, survivors)
puts "ORIG_survivors: #{orig_survivors.inspect}"
puts "ORIG_infected: #{orig_infected.inspect}"
puts "balanced_survivors: #{survivors.inspect}"
puts "balanced_infected: #{infected.inspect}"
puts "need move to survivors: #{need.inspect}"
puts "need out going from survivors: #{not_need.inspect}"
# first step
# move not need player
while true
break if not_need.empty? and need.empty?
# not need player going spectator
if not_need_player = not_need.shift()
orig_survivors.delete(not_need_player)
change_client_team(not_need_player, TEAM_SPECTATORS)
spectators << not_need_player
end
# need player come here
if need_player = need.shift()
orig_survivors << need_player
orig_infected.delete(need_player)
change_client_team(need_player, TEAM_SURVIVORS)
end
if not_need_player
orig_infected << not_need_player
spectators.delete(not_need_player)
change_client_team(not_need_player, TEAM_INFECTED)
end
end
[orig_survivors, orig_infected, spectators]
end
#orig_survivors=[1,2,3,4]
#orig_infected=[5,6,7,8]
orig_survivors=[1]
orig_infected=[2,3,4]
#orig_survivors=[1,2,3]
#orig_infected=[4,5,6]
#orig_survivors=[1,2]
#orig_infected=[3,4]
#orig_survivors=[1]
#orig_infected=[2]
#orig_survivors=[]
#orig_infected=[]
spectators=["o", "x"]
#(survivors, infected) = auto_team_balance(orig_survivors, orig_infected, spectators)
(survivors, infected, spectators) = auto_team_balance(orig_survivors, orig_infected, spectators)
puts "*** RESULT ***"
puts "survivors: #{survivors.inspect}"
puts "infected: #{infected.inspect}"
puts "spectators: #{spectators.inspect}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment