Class: Mjai::TCPActiveGameServer
Defined Under Namespace
Classes: Statistics
Instance Attribute Summary
#num_finished_games, #params, #players
Instance Method Summary
collapse
#port, #print_backtrace, #process_one_game, #run, #send, #server_url, #start_default_players
Constructor Details
Returns a new instance of TCPActiveGameServer.
13
14
15
16
|
# File 'lib/mjai/tcp_active_game_server.rb', line 13
def initialize(params)
super
@name_to_stat = {}
end
|
Instance Method Details
#maybe_open(path, mode, &block) ⇒ Object
89
90
91
92
93
94
95
|
# File 'lib/mjai/tcp_active_game_server.rb', line 89
def maybe_open(path, mode, &block)
if path
open(path, mode, &block)
else
yield(nil)
end
end
|
#num_tcp_players ⇒ Object
18
19
20
|
# File 'lib/mjai/tcp_active_game_server.rb', line 18
def num_tcp_players
return 4
end
|
#on_game_fail(game) ⇒ Object
85
86
87
|
# File 'lib/mjai/tcp_active_game_server.rb', line 85
def on_game_fail(game)
puts("game %d: Ended with error" % self.num_finished_games)
end
|
#on_game_succeed(game) ⇒ Object
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
# File 'lib/mjai/tcp_active_game_server.rb', line 51
def on_game_succeed(game)
puts("game %d: %s" % [
self.num_finished_games,
game.ranked_players.map(){ |pl| "%s:%d" % [pl.name, pl.score] }.join(" "),
])
for player in self.players
@name_to_stat[player.name] ||= Statistics.new(0, 0, 0, [])
@name_to_stat[player.name].num_games += 1
@name_to_stat[player.name].total_score += player.score
@name_to_stat[player.name].total_rank += player.rank
@name_to_stat[player.name].ranks.push(player.rank)
end
names = self.players.map(){ |pl| pl.name }.sort().uniq()
print("Average rank:")
for name in names
stat = @name_to_stat[name]
rank_conf_interval = ConfidenceInterval.calculate(stat.ranks, :min => 1.0, :max => 4.0)
print(" %s:%.3f [%.3f, %.3f]" % [
name,
stat.total_rank.to_f() / stat.num_games,
rank_conf_interval[0],
rank_conf_interval[1],
])
end
puts()
print("Average score:")
for name in names
print(" %s:%d" % [
name,
@name_to_stat[name].total_score.to_f() / @name_to_stat[name].num_games,
])
end
end
|
#play_game(players) ⇒ Object
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
|
# File 'lib/mjai/tcp_active_game_server.rb', line 22
def play_game(players)
if self.params[:log_dir]
mjson_path = "%s/%s.mjson" % [self.params[:log_dir], Time.now.strftime("%Y-%m-%d-%H%M%S")]
else
mjson_path = nil
end
game = nil
success = false
maybe_open(mjson_path, "w") do |mjson_out|
mjson_out.sync = true if mjson_out
game = ActiveGame.new(players)
game.game_type = self.params[:game_type]
game.on_action() do |action|
game.dump_action(action)
end
game.on_responses() do |action, responses|
mjson_out.puts(action.to_json()) if mjson_out
end
success = game.play()
end
FileConverter.new().convert(mjson_path, "#{mjson_path}.html") if mjson_path
return [game, success]
end
|