1
1
use super :: { AppState , Error , User } ;
2
- use crate :: game:: { GameState , Move , Player } ;
2
+ use crate :: game:: { GameState , Move , Player , TurnStatus } ;
3
3
use async_process:: { Child , ChildStderr , ChildStdin , ChildStdout } ;
4
4
use rocket:: {
5
5
futures:: { io:: BufReader , AsyncBufReadExt , AsyncWriteExt } ,
@@ -24,7 +24,7 @@ fn convert_cell_id(id: &[char]) -> (usize, usize) {
24
24
}
25
25
26
26
impl Game {
27
- pub async fn play_ai ( & mut self ) -> Result < ( ) , Error > {
27
+ pub async fn play_ai ( & mut self ) -> Result < String , Error > {
28
28
self . stdin
29
29
. write_all ( self . checkers . to_csv_string ( ) . as_bytes ( ) )
30
30
. await
@@ -40,7 +40,9 @@ impl Game {
40
40
. apply_move ( convert_cell_id ( & char[ 0 ..=1 ] ) , convert_cell_id ( & char[ 2 ..=3 ] ) ) ?;
41
41
}
42
42
43
- Ok ( ( ) )
43
+ let mut res = vec ! [ ] ;
44
+ self . stderr . read_until ( 1 , & mut res) ;
45
+ Ok ( String :: from_utf8_lossy ( & res) . to_string ( ) )
44
46
}
45
47
46
48
pub async fn play_human ( & mut self , moves : Vec < Move > ) -> Result < ( ) , Error > {
@@ -72,7 +74,7 @@ pub async fn start(
72
74
state : & AppState ,
73
75
user : User ,
74
76
is_first_player : bool ,
75
- ) -> Result < Json < GameState > , Error > {
77
+ ) -> Result < Json < TurnStatus > , Error > {
76
78
let game = {
77
79
let lock = state. lock ( ) . unwrap ( ) ;
78
80
lock. games . get ( & user. name ) . cloned ( )
@@ -91,15 +93,14 @@ pub async fn start(
91
93
. ok_or ( Error :: NotFound ) ?
92
94
. start ( ) ?
93
95
} ;
94
- let mut checkers: GameState = Default :: default ( ) ;
96
+
97
+ let checkers: GameState = Default :: default ( ) ;
95
98
96
99
let mut stderr = BufReader :: new ( child. stderr . take ( ) . unwrap ( ) ) ;
97
100
98
101
let mut output = vec ! [ ] ;
99
102
stderr. read_until ( 1 , & mut output) . await ?;
100
103
101
- checkers. ai_output = String :: from_utf8_lossy ( & output) . to_string ( ) ;
102
-
103
104
let mut lock = state. lock ( ) . unwrap ( ) ;
104
105
lock. games . insert (
105
106
user. name ,
@@ -117,15 +118,18 @@ pub async fn start(
117
118
} ) ) ,
118
119
) ;
119
120
120
- Ok ( Json ( checkers) )
121
+ Ok ( Json ( TurnStatus {
122
+ game : checkers,
123
+ ai_output : String :: from_utf8_lossy ( & output) . to_string ( ) ,
124
+ } ) )
121
125
}
122
126
123
127
#[ post( "/game" , format = "json" , data = "<moves>" ) ]
124
128
pub async fn play (
125
129
state : & AppState ,
126
130
user : User ,
127
131
moves : Json < Vec < Move > > ,
128
- ) -> Result < Json < GameState > , Error > {
132
+ ) -> Result < Json < TurnStatus > , Error > {
129
133
let game = state. lock ( ) . unwrap ( ) . games . get ( & user. name ) . map ( Arc :: clone) ;
130
134
131
135
if game. is_none ( ) {
@@ -136,9 +140,12 @@ pub async fn play(
136
140
let mut lock = game. lock ( ) . await ;
137
141
138
142
lock. play_human ( moves. into_inner ( ) ) . await ?;
139
- lock. play_ai ( ) . await ?;
143
+ let output = lock. play_ai ( ) . await ?;
140
144
141
- Ok ( Json ( lock. checkers . clone ( ) ) )
145
+ Ok ( Json ( TurnStatus {
146
+ game : lock. checkers . clone ( ) ,
147
+ ai_output : output,
148
+ } ) )
142
149
}
143
150
144
151
#[ post( "/game/stop" ) ]
0 commit comments