• R/O
  • HTTP
  • SSH
  • HTTPS

Commit

Tags
No Tags

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

shogi-server source


Commit MetaInfo

Revisión0a1c409fedbbc7061dc32114740ab32d5f76735d (tree)
Tiempo2010-09-05 16:56:27
Autordaigo <beatles@user...>
CommiterDaigo Moriwaki

Log Message

Enhanced the Buoy feature: Players are allowed to start buoy games with specific turns.

ex. %%GAME buoy_foo-1500-0 +

Cambiar Resumen

Diferencia incremental

--- a/changelog
+++ b/changelog
@@ -6,6 +6,8 @@
66 + Starting a buoy game, players are notified a starting game
77 position with the initial position and moves, instread of a
88 targeting position.
9+ + Players are allowed to start buoy games with specific turns.
10+ ex. %%GAME buoy_foo-1500-0 +
911
1012 2010-08-05 Daigo Moriwaki <daigo at debian dot org>
1113
@@ -272,8 +274,8 @@
272274 New commands:
273275 + %%SETBUOY <game_name> <moves> [count]
274276 Set a new buoy game.
275- ex. %%SETBUOYGAME buoy_foo-900-0 +7776FU 10
276- ex. %%SETBUOYGAME buoy_foo-1500-0 +7776FU-3334FU
277+ ex. %%SETBUOY buoy_foo-900-0 +7776FU 10
278+ ex. %%SETBUOY buoy_foo-1500-0 +7776FU-3334FU
277279 - game_name is a valid game name with a prefix "buoy_".
278280 ex. buoy_foo-900-0
279281 - moves are initial moves from the Hirate position to a
--- a/shogi_server/command.rb
+++ b/shogi_server/command.rb
@@ -476,6 +476,7 @@ module ShogiServer
476476 @command_name = command_name
477477 @game_name = game_name
478478 @my_sente_str = my_sente_str
479+ player.set_sente_from_str(@my_sente_str)
479480 end
480481
481482 def call
@@ -497,7 +498,7 @@ module ShogiServer
497498 end
498499 @player.sente = nil
499500 else
500- rival = $league.find_rival(@player, @my_sente_str, @game_name)
501+ rival = $league.find_rival(@player, @game_name)
501502 if rival.instance_of?(Symbol)
502503 # An error happened. rival is not a player instance, but an error
503504 # symobl that must be returned to the main routine immediately.
@@ -543,7 +544,6 @@ module ShogiServer
543544 if (@command_name == "GAME")
544545 @player.status = "game_waiting"
545546 @player.game_name = @game_name
546- @player.set_sente_from_str(@my_sente_str)
547547 else # challenge
548548 @player.write_safe(sprintf("##[ERROR] can't find rival for %s\n", @game_name))
549549 @player.status = "connected"
@@ -726,15 +726,32 @@ module ShogiServer
726726 @player.write_safe(sprintf("##[SETBUOY] +OK\n"))
727727 log_info("A buoy game was created: %s by %s" % [@game_name, @player.name])
728728
729- # if two players, who are not @player, are waiting for a new game, start it
730- p1 = $league.get_player("game_waiting", @game_name, true, @player)
731- return :continue unless p1
732- p2 = $league.get_player("game_waiting", @game_name, false, @player)
733- return :continue unless p2
734-
729+ # if two players are waiting for this buoy game, start it
730+ candidates = $league.find_all_players do |player|
731+ player.status == "game_waiting" &&
732+ player.game_name == @game_name &&
733+ player.name != @player.name
734+ end
735+ if candidates.empty?
736+ log_info("No players found for a buoy game. Wait for players: %s" % [@game_name])
737+ return :continue
738+ end
739+ p1 = candidates.first
740+ p2 = $league.find_rival(p1, @game_name)
741+ if p2.nil?
742+ log_info("No opponent found for a buoy game. Wait for the opponent: %s by %s" % [@game_name, p1.name])
743+ return :continue
744+ elsif p2.instance_of?(Symbol)
745+ # An error happened. rival is not a player instance, but an error
746+ # symobl that must be returned to the main routine immediately.
747+ return p2
748+ end
749+ # found two players: p1 and p2
750+ log_info("Starting a buoy game: %s with %s and %s" % [@game_name, p1.name, p2.name])
735751 buoy.decrement_count(buoy_game)
736752 game = Game::new(@game_name, p1, p2, board)
737753 return :continue
754+
738755 rescue WrongMoves => e
739756 @player.write_safe(sprintf("##[ERROR] wrong moves: %s\n", @moves))
740757 log_error "Received wrong moves: %s from %s. [%s]" % [@moves, @player.name, e.message]
--- a/shogi_server/game.rb
+++ b/shogi_server/game.rb
@@ -32,9 +32,11 @@ class Game
3232 @@mutex = Mutex.new
3333 @@time = 0
3434
35- # Decide turns of players according to their turn preferences.
35+ # Decide an actual turn of each player according to their turn preferences.
3636 # p2 is a rival player of the p1 player.
3737 # p1_sente_string must be "*", "+" or "-".
38+ # After this call, the sente value of each player is always true or false, not
39+ # nil.
3840 #
3941 def Game.decide_turns(p1, p1_sente_string, p2)
4042 if ((p1_sente_string == "*") && (p2.sente == nil))
--- a/shogi_server/league.rb
+++ b/shogi_server/league.rb
@@ -105,17 +105,17 @@ class League
105105 # 2. a rival player instance found
106106 # 3. nil if rival not found
107107 #
108- def find_rival(player, my_sente_string, game_name)
109- case my_sente_string
110- when "*" # no preference
108+ def find_rival(player, game_name)
109+ case player.sente
110+ when nil # no preference
111111 if Login.handicapped_game_name?(game_name)
112112 player.write_safe("##[ERROR] Random turn preference is not allowed for handicapped games\n")
113113 return :continue
114114 end
115115 return get_player("game_waiting", game_name, nil, player)
116- when "+" # rival must be gote
116+ when true # rival must be gote
117117 return get_player("game_waiting", game_name, false, player)
118- when "-" # rival must be sente
118+ when false # rival must be sente
119119 return get_player("game_waiting", game_name, true, player)
120120 else
121121 write_safe("##[ERROR] bad game option: %s\n" % [my_sente_string])
--- a/test/TC_command.rb
+++ b/test/TC_command.rb
@@ -14,6 +14,19 @@ class MockLeague
1414 def initialize
1515 @games = {}
1616 @games["dummy_game_id"] = MockGame.new
17+
18+ reset_players
19+ end
20+
21+ def reset_players
22+ $p1 = MockPlayer.new
23+ $p1.name = "p1"
24+ $p1.status = "game_waiting"
25+ $p1.sente = true
26+ $p2 = MockPlayer.new
27+ $p2.name = "p2"
28+ $p2.status = "game_waiting"
29+ $p2.sente = false
1730 end
1831
1932 def games
@@ -38,12 +51,8 @@ class MockLeague
3851
3952 def get_player(status, game_id, sente, searcher)
4053 if sente == true
41- $p1 = MockPlayer.new
42- $p1.name = "p1"
4354 return $p1
4455 elsif sente == false
45- $p2 = MockPlayer.new
46- $p2.name = "p2"
4756 return $p2
4857 elsif sente == nil
4958 return nil
@@ -51,6 +60,23 @@ class MockLeague
5160 return nil
5261 end
5362 end
63+
64+ def find_all_players
65+ [$p1,$p2].each {|pp| yield pp}
66+ end
67+
68+ def find_rival(player, game_name)
69+ case player.sente
70+ when nil # no preference
71+ return get_player("game_waiting", game_name, nil, player)
72+ when true # rival must be gote
73+ return get_player("game_waiting", game_name, false, player)
74+ when false # rival must be sente
75+ return get_player("game_waiting", game_name, true, player)
76+ else
77+ return :continue
78+ end
79+ end
5480 end
5581
5682
@@ -772,8 +798,7 @@ end
772798 class BaseTestBuoyCommand < Test::Unit::TestCase
773799 def setup
774800 @p = MockPlayer.new
775- $p1 = nil
776- $p2 = nil
801+ $league = MockLeague.new
777802
778803 delete_buoy_yaml
779804 @buoy = ShogiServer::Buoy.new
@@ -830,8 +855,8 @@ class TestSetBuoyCommand < BaseTestBuoyCommand
830855 cmd = ShogiServer::SetBuoyCommand.new "%%SETBUOY", @p, "buoyhoge-1500-0", "+7776FU", 1
831856 rt = cmd.call
832857 assert :continue, rt
833- assert !$p1
834- assert !$p2
858+ assert $p1.out.empty?
859+ assert $p2.out.empty?
835860 assert @buoy.is_new_game?("buoy_hoge-1500-0")
836861 end
837862
@@ -844,8 +869,8 @@ class TestSetBuoyCommand < BaseTestBuoyCommand
844869 cmd = ShogiServer::SetBuoyCommand.new "%%SETBUOY", @p, "buoy_duplicated-1500-0", "+7776FU", 1
845870 rt = cmd.call
846871 assert :continue, rt
847- assert !$p1
848- assert !$p2
872+ assert $p1.out.empty?
873+ assert $p2.out.empty?
849874 assert !@buoy.is_new_game?("buoy_duplicated-1500-0")
850875 end
851876
@@ -854,8 +879,8 @@ class TestSetBuoyCommand < BaseTestBuoyCommand
854879 cmd = ShogiServer::SetBuoyCommand.new "%%SETBUOY", @p, "buoy_badmoves-1500-0", "+7776FU+8786FU", 1
855880 rt = cmd.call
856881 assert :continue, rt
857- assert !$p1
858- assert !$p2
882+ assert $p1.out.empty?
883+ assert $p2.out.empty?
859884 assert @buoy.is_new_game?("buoy_badmoves-1500-0")
860885 end
861886
@@ -864,8 +889,8 @@ class TestSetBuoyCommand < BaseTestBuoyCommand
864889 cmd = ShogiServer::SetBuoyCommand.new "%%SETBUOY", @p, "buoy_badcounter-1500-0", "+7776FU", 0
865890 rt = cmd.call
866891 assert :continue, rt
867- assert !$p1
868- assert !$p2
892+ assert $p1.out.empty?
893+ assert $p2.out.empty?
869894 assert @buoy.is_new_game?("buoy_badcounter-1500-0")
870895 end
871896 end
@@ -882,8 +907,8 @@ class TestDeleteBuoyCommand < BaseTestBuoyCommand
882907 cmd = ShogiServer::DeleteBuoyCommand.new "%%DELETEBUOY", @p, buoy_game.game_name
883908 rt = cmd.call
884909 assert :continue, rt
885- assert !$p1
886- assert !$p2
910+ assert $p1.out.empty?
911+ assert $p2.out.empty?
887912 assert @buoy.is_new_game?(buoy_game.game_name)
888913 end
889914
@@ -893,8 +918,8 @@ class TestDeleteBuoyCommand < BaseTestBuoyCommand
893918 cmd = ShogiServer::DeleteBuoyCommand.new "%%DELETEBUOY", @p, buoy_game.game_name
894919 rt = cmd.call
895920 assert :continue, rt
896- assert !$p1
897- assert !$p2
921+ assert $p1.out.empty?
922+ assert $p2.out.empty?
898923 assert @buoy.is_new_game?(buoy_game.game_name)
899924 end
900925