5
5
import com .bernd .model .AcceptRequest ;
6
6
import com .bernd .model .ActiveGame ;
7
7
import com .bernd .model .Game ;
8
- import com .bernd .model .GameMove ;
9
8
import com .bernd .model .Move ;
10
9
import com .bernd .model .OpenGame ;
11
10
import com .bernd .model .ViewGame ;
12
11
import com .bernd .util .Auth ;
13
12
import com .bernd .util .RandomString ;
14
- import java .security .Principal ;
15
- import java .util .Objects ;
13
+ import org .springframework .http .HttpStatus ;
16
14
import org .springframework .http .ResponseEntity ;
17
15
import org .springframework .messaging .core .MessageSendingOperations ;
18
16
import org .springframework .messaging .handler .annotation .MessageMapping ;
22
20
import org .springframework .web .bind .annotation .PostMapping ;
23
21
import org .springframework .web .bind .annotation .RequestBody ;
24
22
import org .springframework .web .bind .annotation .ResponseBody ;
23
+ import org .springframework .web .server .ResponseStatusException ;
24
+
25
+ import java .security .Principal ;
26
+ import java .util .Objects ;
27
+
28
+ import static com .bernd .util .Util .COLORS ;
25
29
26
30
@ Controller
27
31
public class GameController {
@@ -47,7 +51,7 @@ public class GameController {
47
51
public ViewGame getGame (@ PathVariable String id ) {
48
52
Game game = games .get (id );
49
53
if (game == null ) {
50
- return null ;
54
+ throw new ResponseStatusException ( HttpStatus . NOT_FOUND , "no such game" ) ;
51
55
}
52
56
return game .toView ();
53
57
}
@@ -58,10 +62,8 @@ public void action(Move move, Principal p) {
58
62
if (p == null || game == null ) {
59
63
return ;
60
64
}
61
- String principal = Objects .toString (p .getName (), "" );
62
- int color = principal .equals (game .black ().name ()) ? Board .B :
63
- principal .equals (game .white ().name ()) ? Board .W : 0 ;
64
- if (!canMove (game , color )) {
65
+ int color = getCurrentColor (game , Objects .toString (p .getName (), "" ));
66
+ if (color == 0 ) {
65
67
return ;
66
68
}
67
69
Move updatedMove = move .withColor (color ).withMoveNumber (game .moves ().size ());
@@ -74,25 +76,33 @@ public void action(Move move, Principal p) {
74
76
}
75
77
}
76
78
77
- private boolean canMove (Game game , int color ) {
79
+ private int getCurrentColor (Game game , String principal ) {
78
80
if (game .gameHasEnded ()) {
79
- return false ;
81
+ return 0 ;
80
82
}
83
+ if (game .handicap () != 0 ) {
84
+ return Board .B ;
85
+ }
86
+ int color = getColor (game , principal );
81
87
if (color == 0 ) {
82
- return false ;
88
+ return 0 ;
83
89
}
84
90
MoveList moves = game .moves ();
85
- if (game . counting ()) {
86
- return true ;
91
+ if (moves . isEmpty ()) {
92
+ return Board . B ;
87
93
}
88
- if (game .handicap () != 0 ) {
89
- return color == Board .B ;
94
+ return moves .get (moves .size () - 1 ).color () ^ COLORS ;
95
+ }
96
+
97
+ private static int getColor (Game game , String principal ) {
98
+ if (!(game .isBlack (principal ) || game .isWhite (principal ))) {
99
+ return 0 ;
90
100
}
91
- if (moves .isEmpty ()) {
92
- return color == Board .B ;
101
+ if (game .isSelfPlay ()) {
102
+ return game .moves ().size () + game .handicap () % 2 == 0 ?
103
+ Board .B : Board .W ;
93
104
}
94
- GameMove lastMove = moves .get (moves .size () - 1 );
95
- return color != lastMove .color ();
105
+ return game .isBlack (principal ) ? Board .B : Board .W ;
96
106
}
97
107
98
108
@ ResponseBody
0 commit comments