X-Git-Url: http://git.sourceforge.jp/view?a=blobdiff_plain;f=shogi-server;h=6a77f862ba24fcdb79d17a49774442fb1d99e517;hb=674240ce043b06bb237ded6cbf8864e00e4f0d2a;hp=5135dea7d45a3c06668170b33d2033bacb9b2417;hpb=9309211ca3bee1b2ee04677dd18a65ddd3490910;p=shogi-server%2Fshogi-server.git diff --git a/shogi-server b/shogi-server index 5135dea..6a77f86 100755 --- a/shogi-server +++ b/shogi-server @@ -82,7 +82,7 @@ class League charge rescue Exception => ex # ignore errors - log_error("[in Floodgate's thread] #{ex}") + log_error("[in Floodgate's thread] #{ex} #{ex.backtrace}") end end end @@ -121,6 +121,80 @@ class League end end # class Floodgate + # + # This manages those players who have their player_id. + # Since mk_rate mainly updates the yaml file, basically, + # this only reads data. But this writes some properties. + # TODO Such data should be facoted out to another file + # + class Persistent + def initialize(filename) + @db = YAML::Store.new(filename) + @db.transaction do |pstore| + @db['players'] ||= Hash.new + end + end + + # + # trancaction=true means read only + # + def each_group(transaction=false) + @db.transaction(transaction) do + groups = @db["players"] || Hash.new + groups.each do |group, players| + yield group,players + end + end + end + + def load_player(player) + return unless player.player_id + + hash = nil + each_group(true) do |group, players| + hash = players[player.player_id] + break if hash + end + return unless hash + + # a current user + player.name = hash['name'] + player.rate = hash['rate'] || 0 + player.modified_at = hash['last_modified'] + player.rating_group = hash['rating_group'] + player.win = hash['win'] || 0 + player.loss = hash['loss'] || 0 + player.last_game_win = hash['last_game_win'] || false + end + + def save(player) + return unless player.player_id + + each_group do |group, players| + hash = players[player.player_id] + if hash + # write only this property. + # the others are updated by ./mk_rate + hash['last_game_win'] = player.last_game_win + break + end + end + end + + def get_players + players = [] + each_group(true) do |group, players_hash| + players << players_hash.keys + end + return players.flatten.collect do |player_id| + p = BasicPlayer.new + p.player_id = player_id + load_player(p) + p + end + end + end # class Persistent + def initialize @mutex = Mutex.new # guard @players @games = Hash::new @@ -134,33 +208,38 @@ class League def shutdown @mutex.synchronize do - @players.each {|a| save(a)} + @players.each do |name, player| + @persistent.save(player) + end end @floodgate.shutdown end # this should be called just after instanciating a League object. def setup_players_database - @db = YAML::Store.new(File.join(@dir, "players.yaml")) + filename = File.join(@dir, "players.yaml") + @persistent = Persistent.new(filename) end def add(player) - self.load(player) if player.player_id + @persistent.load_player(player) @mutex.synchronize do @players[player.name] = player end end def delete(player) + @persistent.save(player) @mutex.synchronize do - save(player) @players.delete(player.name) end end def reload @mutex.synchronize do - @players.each {|player| load(player)} + @players.each do |name, player| + @persistent.load_player(player) + end end end @@ -197,59 +276,8 @@ class League return found ? found.last : nil end - def load(player) - hash = search(player.player_id) - return unless hash - - # a current user - player.name = hash['name'] - player.rate = hash['rate'] || 0 - player.modified_at = hash['last_modified'] - player.rating_group = hash['rating_group'] - player.win = hash['win'] || 0 - player.loss = hash['loss'] || 0 - player.last_game_win = hash['last_game_win'] || false - end - - def save(player) - @db.transaction do - break unless @db["players"] - @db["players"].each do |group, players| - hash = players[player.player_id] - if hash - hash['last_game_win'] = player.last_game_win - break - end - end - end - end - - def search(player_id) - hash = nil - @db.transaction(true) do - break unless @db["players"] - @db["players"].each do |group, players| - hash = players[player_id] - break if hash - end - end - hash - end - def rated_players - players = [] - @db.transaction(true) do - break unless @db["players"] - @db["players"].each do |group, players_hash| - players << players_hash.keys - end - end - return players.flatten.collect do |player_id| - p = BasicPlayer.new - p.player_id = player_id - self.load(p) - p - end + return @persistent.get_players end end @@ -380,6 +408,9 @@ class BasicPlayer @player_id = nil @name = nil @password = nil + @rate = 0 + @win = 0 + @loss = 0 @last_game_win = false end @@ -1620,7 +1651,14 @@ class Game @game_id = sprintf("%s+%s+%s+%s+%s", LEAGUE.event, @game_name, @sente.name, @gote.name, issue_current_time) - @logfile = File.join(LEAGUE.dir, @game_id + ".csa") + + now = Time.now + log_dir_name = File.join(LEAGUE.dir, + now.strftime("%Y"), + now.strftime("%m"), + now.strftime("%d")) + FileUtils.mkdir_p(log_dir_name) unless File.exist?(log_dir_name) + @logfile = File.join(log_dir_name, @game_id + ".csa") LEAGUE.games[@game_id] = self @@ -1724,7 +1762,7 @@ class Game move_status = @board.handle_one_move(str, @sente == @current_player) - if [:illegal, :uchifuzme, :oute_kaihimore].include?(move_status) + if [:illegal, :uchifuzume, :oute_kaihimore].include?(move_status) @fh.printf("'ILLEGAL_MOVE(%s)\n", str) else if [:normal, :outori, :sennichite, :oute_sennichite_sente_lose, :oute_sennichite_gote_lose].include?(move_status)