shogi-server source
Révision | fd33baf3cba2786e693ae8d7b93d7b13636a6070 (tree) |
---|---|
l'heure | 2010-09-05 23:27:23 |
Auteur | daigo <beatles@user...> |
Commiter | Daigo Moriwaki |
Enhanced the CSA Login mode to accept a turn preference.
Logging in the server with the CSA mode, users are now allowed to
specify a turn preference in an enhanced gamename string which
looks like <gamename>-<time>-<time>-<turn>. The first three
parts are a regular game definition. The last "-<turn>"
part is optional.
+ Without -<turn> (i.e. same as the previous behavior), there
+ <turn> is either "B" for black or "W" for white.
@@ -1,3 +1,16 @@ | ||
1 | +2010-09-05 Daigo Moriwaki <daigo at debian dot org> | |
2 | + | |
3 | + * [shogi-server] | |
4 | + - shogi_server/login.rb: Enhanced the CSA Login mode. | |
5 | + Logging in the server with the CSA mode, users are now allowed to | |
6 | + specify a turn preference in an enhanced gamename string which | |
7 | + looks like <gamename>-<time>-<time>-<turn>. The first three | |
8 | + parts are a regular game definition. The last "-<turn>" | |
9 | + part is optional. | |
10 | + + Without -<turn> (i.e. same as the previous behavior), there | |
11 | + is no turn preference. A user's turn will be defined randomly. | |
12 | + + <turn> is either "B" for black or "W" for white. | |
13 | + | |
1 | 14 | 2010-09-04 Daigo Moriwaki <daigo at debian dot org> |
2 | 15 | |
3 | 16 | * [shogi-server] |
@@ -127,14 +127,40 @@ end | ||
127 | 127 | class LoginCSA < Login |
128 | 128 | PROTOCOL = "CSA" |
129 | 129 | |
130 | + attr_reader :gamename | |
131 | + | |
132 | + # A turn preference string: "+", "-" or default "*" | |
133 | + attr_reader :turn_preference | |
134 | + | |
130 | 135 | def initialize(player, password) |
131 | 136 | @gamename = nil |
137 | + @turn_preference = "*" | |
132 | 138 | super |
133 | 139 | @player.protocol = PROTOCOL |
134 | 140 | end |
135 | 141 | |
142 | + # Parse a gamename str and see if it includes an optional turn | |
143 | + # preference. | |
144 | + # ex. foo-1500-0-B for black | |
145 | + # ex. foo-1500-0-W for white | |
146 | + # | |
147 | + # Return an array of a valid gamename without an turn preference and a | |
148 | + # turn character "+" or "-"; false otherwise | |
149 | + # | |
150 | + def parse_gamename_turn(str) | |
151 | + if str =~ /^(.+)-\d+-\d+-(\w)$/ | |
152 | + case $2 | |
153 | + when "b","B" | |
154 | + return [str[0, str.length-2], "+"] | |
155 | + when "w","W" | |
156 | + return [str[0, str.length-2], "-"] | |
157 | + end | |
158 | + end | |
159 | + return false | |
160 | + end | |
161 | + | |
136 | 162 | def parse_password(password) |
137 | - if Login.good_game_name?(password) | |
163 | + if Login.good_game_name?(password) || parse_gamename_turn(password) | |
138 | 164 | @gamename = password |
139 | 165 | @player.set_password(nil) |
140 | 166 | elsif password.split(",").size > 1 |
@@ -144,12 +170,17 @@ class LoginCSA < Login | ||
144 | 170 | @player.set_password(password) |
145 | 171 | @gamename = Default_Game_Name |
146 | 172 | end |
147 | - @gamename = self.class.good_game_name?(@gamename) ? @gamename : Default_Game_Name | |
173 | + array = parse_gamename_turn(@gamename) | |
174 | + if array | |
175 | + @gamename = array.first | |
176 | + @turn_preference = array.last | |
177 | + end | |
178 | + @gamename = Login.good_game_name?(@gamename) ? @gamename : Default_Game_Name | |
148 | 179 | end |
149 | 180 | |
150 | 181 | def process |
151 | 182 | super |
152 | - @csa_1st_str = "%%GAME #{@gamename} *" | |
183 | + @csa_1st_str = "%%GAME #{@gamename} #{@turn_preference}" | |
153 | 184 | end |
154 | 185 | end |
155 | 186 |
@@ -40,15 +40,73 @@ class TestLogin < Test::Unit::TestCase | ||
40 | 40 | assert_instance_of(ShogiServer::LoginCSA, login) |
41 | 41 | assert_equal("xyz", player.password) |
42 | 42 | assert_equal(@p_csa.player_id, player.player_id) |
43 | + assert_equal("*", login.turn_preference) | |
44 | + end | |
45 | + | |
46 | + def test_login_factory_csa_no_gamename | |
47 | + player = ShogiServer::BasicPlayer.new | |
48 | + player.name = "hoge" | |
49 | + login = ShogiServer::Login::factory("LOGIN hoge xyz", player) | |
50 | + assert_instance_of(ShogiServer::LoginCSA, login) | |
51 | + assert_equal("xyz", player.password) | |
52 | + assert_equal(@p_csa.player_id, player.player_id) | |
53 | + assert_equal("*", login.turn_preference) | |
54 | + assert_equal(ShogiServer::Default_Game_Name, login.gamename) | |
55 | + end | |
56 | + | |
57 | + def test_login_factory_csa_with_black | |
58 | + player = ShogiServer::BasicPlayer.new | |
59 | + player.name = "hoge" | |
60 | + login = ShogiServer::Login::factory("LOGIN hoge floodgate-900-0-B,xyz", player) | |
61 | + assert_instance_of(ShogiServer::LoginCSA, login) | |
62 | + assert_equal("xyz", player.password) | |
63 | + assert_equal(@p_csa.player_id, player.player_id) | |
64 | + assert_equal("+", login.turn_preference) | |
65 | + assert_equal("floodgate-900-0", login.gamename) | |
66 | + end | |
67 | + | |
68 | + def test_login_factory_csa_with_white | |
69 | + player = ShogiServer::BasicPlayer.new | |
70 | + player.name = "hoge" | |
71 | + login = ShogiServer::Login::factory("LOGIN hoge floodgate-900-0-W,xyz", player) | |
72 | + assert_instance_of(ShogiServer::LoginCSA, login) | |
73 | + assert_equal("xyz", player.password) | |
74 | + assert_equal(@p_csa.player_id, player.player_id) | |
75 | + assert_equal("-", login.turn_preference) | |
76 | + assert_equal("floodgate-900-0", login.gamename) | |
43 | 77 | end |
44 | 78 | |
45 | 79 | def test_login_factory_csa_without_trip |
46 | 80 | player = ShogiServer::BasicPlayer.new |
47 | 81 | player.name = "hoge" |
48 | - login = ShogiServer::Login::factory("LOGIN hoge floodagate-900-0", player) | |
82 | + login = ShogiServer::Login::factory("LOGIN hoge floodgate-900-0", player) | |
83 | + assert_instance_of(ShogiServer::LoginCSA, login) | |
84 | + assert_nil(player.password) | |
85 | + assert_equal(nil, player.player_id) | |
86 | + assert_equal("*", login.turn_preference) | |
87 | + assert_equal("floodgate-900-0", login.gamename) | |
88 | + end | |
89 | + | |
90 | + def test_login_factory_csa_without_trip_with_black | |
91 | + player = ShogiServer::BasicPlayer.new | |
92 | + player.name = "hoge" | |
93 | + login = ShogiServer::Login::factory("LOGIN hoge floodgate-900-0-B", player) | |
94 | + assert_instance_of(ShogiServer::LoginCSA, login) | |
95 | + assert_nil(player.password) | |
96 | + assert_equal(nil, player.player_id) | |
97 | + assert_equal("+", login.turn_preference) | |
98 | + assert_equal("floodgate-900-0", login.gamename) | |
99 | + end | |
100 | + | |
101 | + def test_login_factory_csa_without_trip_with_white | |
102 | + player = ShogiServer::BasicPlayer.new | |
103 | + player.name = "hoge" | |
104 | + login = ShogiServer::Login::factory("LOGIN hoge floodgate-900-0-W", player) | |
49 | 105 | assert_instance_of(ShogiServer::LoginCSA, login) |
50 | 106 | assert_nil(player.password) |
51 | 107 | assert_equal(nil, player.player_id) |
108 | + assert_equal("-", login.turn_preference) | |
109 | + assert_equal("floodgate-900-0", login.gamename) | |
52 | 110 | end |
53 | 111 | end |
54 | 112 |