@@ -167,14 +167,29 @@ pub const Client = struct {
167167 if (std .mem .eql (u8 , playerName , & self .name )) {
168168 try std .io .getStdOut ().writer ().print ("lost all units\n rejoining...\n " , .{});
169169 self .game .? .clear ();
170- self .state = State .Connected ;
170+ self .state = State .Ready ;
171171 try self .send ("j\n " );
172172 } else {
173173 print ("{s} left\n " , .{playerName });
174174 try self .game .? .deletePlayer (playerName );
175175 }
176176 self .game .? .board .printUnits (& self .game .? );
177177 },
178+ @intFromEnum (Message .Type .lost ) = > {
179+ const playerName = buff [1.. ]; // player name
180+ try std .io .getStdOut ().writer ().print ("lost the game to {s}\n rejoining...\n " , .{playerName });
181+ self .game .? .clear ();
182+ self .state = State .Ready ;
183+ try self .send ("j\n " );
184+ },
185+ @intFromEnum (Message .Type .win ) = > {
186+ const playerName = buff [1.. ]; // player name
187+ std .debug .assert (std .mem .eql (u8 , playerName , & self .name ));
188+ try std .io .getStdOut ().writer ().print ("won the game\n rejoining...\n " , .{});
189+ self .game .? .clear ();
190+ self .state = State .Ready ;
191+ try self .send ("j\n " );
192+ },
178193 @intFromEnum (Message .Type .move ) = > {
179194 var it = std .mem .tokenizeAny (u8 , buff [1.. ], " \n " );
180195
@@ -223,14 +238,19 @@ pub const Client = struct {
223238 const cds = coordOps .towards (unit .* .x , unit .* .y , fc .x , fc .y );
224239 const max_len = 10 ; // TO DO figure out max_len from config sent by server
225240 var buf : [max_len ]u8 = undefined ;
226- try self .send ("m" );
227- try self .send (try std .fmt .bufPrint (& buf , "{}" , .{unit .* .id }));
228- try self .send (" " );
229- try self .send (try std .fmt .bufPrint (& buf , "{}" , .{cds .x }));
230- try self .send (" " );
231- try self .send (try std .fmt .bufPrint (& buf , "{}" , .{cds .y }));
232- try self .send ("\n " );
233- print ("move from {d} {d}\n " , .{ unit .* .x , unit .* .y });
241+ if (cds .x == unit .* .x and cds .y == unit .* .y ) {
242+ try self .send ("d" );
243+ try self .send (try std .fmt .bufPrint (& buf , "{}" , .{unit .* .id }));
244+ try self .send ("\n " );
245+ } else {
246+ try self .send ("m" );
247+ try self .send (try std .fmt .bufPrint (& buf , "{}" , .{unit .* .id }));
248+ try self .send (" " );
249+ try self .send (try std .fmt .bufPrint (& buf , "{}" , .{cds .x }));
250+ try self .send (" " );
251+ try self .send (try std .fmt .bufPrint (& buf , "{}" , .{cds .y }));
252+ try self .send ("\n " );
253+ }
234254 }
235255 }
236256 },
0 commit comments