controls string on server side
This commit is contained in:
		
							parent
							
								
									603a53eef7
								
							
						
					
					
						commit
						7bc45d1d58
					
				
					 3 changed files with 22 additions and 27 deletions
				
			
		|  | @ -22,14 +22,6 @@ function ScoreRow({name, value}: { | ||||||
|     </tr>; |     </tr>; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| type Controls = { |  | ||||||
|     readonly forward: boolean; |  | ||||||
|     readonly backward: boolean; |  | ||||||
|     readonly turnLeft: boolean; |  | ||||||
|     readonly turnRight: boolean; |  | ||||||
|     readonly shoot: boolean; |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| type TankInfo = { | type TankInfo = { | ||||||
|     readonly explosiveBullets: number; |     readonly explosiveBullets: number; | ||||||
|     readonly position: { x: number; y: number }; |     readonly position: { x: number; y: number }; | ||||||
|  | @ -41,25 +33,10 @@ type TankInfo = { | ||||||
| type PlayerInfoMessage = { | type PlayerInfoMessage = { | ||||||
|     readonly name: string; |     readonly name: string; | ||||||
|     readonly scores: Scores; |     readonly scores: Scores; | ||||||
|     readonly controls: Controls; |     readonly controls: string; | ||||||
|     readonly tank?: TankInfo; |     readonly tank?: TankInfo; | ||||||
| } | } | ||||||
| 
 | 
 | ||||||
| function controlsString(controls: Controls) { |  | ||||||
|     let str = ''; |  | ||||||
|     if (controls.forward) |  | ||||||
|         str += '▲'; |  | ||||||
|     if (controls.backward) |  | ||||||
|         str += '▼'; |  | ||||||
|     if (controls.turnLeft) |  | ||||||
|         str += '◄'; |  | ||||||
|     if (controls.turnRight) |  | ||||||
|         str += '►'; |  | ||||||
|     if (controls.shoot) |  | ||||||
|         str += '•'; |  | ||||||
|     return str; |  | ||||||
| } |  | ||||||
| 
 |  | ||||||
| export default function PlayerInfo({playerId}: { playerId: Guid }) { | export default function PlayerInfo({playerId}: { playerId: Guid }) { | ||||||
|     const [shouldSendMessage, setShouldSendMessage] = useState(false); |     const [shouldSendMessage, setShouldSendMessage] = useState(false); | ||||||
| 
 | 
 | ||||||
|  | @ -87,7 +64,7 @@ export default function PlayerInfo({playerId}: { playerId: Guid }) { | ||||||
|         </h3> |         </h3> | ||||||
|         <table> |         <table> | ||||||
|             <tbody> |             <tbody> | ||||||
|             <ScoreRow name="controls" value={controlsString(lastJsonMessage.controls)}/> |             <ScoreRow name="controls" value={lastJsonMessage.controls}/> | ||||||
|             <ScoreRow name="kills" value={lastJsonMessage.scores.kills}/> |             <ScoreRow name="kills" value={lastJsonMessage.scores.kills}/> | ||||||
|             <ScoreRow name="deaths" value={lastJsonMessage.scores.deaths}/> |             <ScoreRow name="deaths" value={lastJsonMessage.scores.deaths}/> | ||||||
|             <ScoreRow name="walls" value={lastJsonMessage.scores.wallsDestroyed}/> |             <ScoreRow name="walls" value={lastJsonMessage.scores.wallsDestroyed}/> | ||||||
|  |  | ||||||
|  | @ -1,4 +1,5 @@ | ||||||
| using System.Net.WebSockets; | using System.Net.WebSockets; | ||||||
|  | using System.Text; | ||||||
| using System.Text.Json; | using System.Text.Json; | ||||||
| using TanksServer.GameLogic; | using TanksServer.GameLogic; | ||||||
| 
 | 
 | ||||||
|  | @ -51,7 +52,7 @@ internal sealed class PlayerInfoConnection( | ||||||
|         var tankInfo = tank != null |         var tankInfo = tank != null | ||||||
|             ? new TankInfo(tank.Orientation, tank.ExplosiveBullets, tank.Position.ToPixelPosition(), tank.Moving) |             ? new TankInfo(tank.Orientation, tank.ExplosiveBullets, tank.Position.ToPixelPosition(), tank.Moving) | ||||||
|             : null; |             : null; | ||||||
|         var info = new PlayerInfo(player.Name, player.Scores, player.Controls, tankInfo); |         var info = new PlayerInfo(player.Name, player.Scores, ControlsToString(player.Controls), tankInfo); | ||||||
|         var response = JsonSerializer.SerializeToUtf8Bytes(info, _context.PlayerInfo); |         var response = JsonSerializer.SerializeToUtf8Bytes(info, _context.PlayerInfo); | ||||||
| 
 | 
 | ||||||
|         if (response.SequenceEqual(_lastMessage)) |         if (response.SequenceEqual(_lastMessage)) | ||||||
|  | @ -60,5 +61,22 @@ internal sealed class PlayerInfoConnection( | ||||||
|         return _lastMessage = response; |         return _lastMessage = response; | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  |     private static string ControlsToString(PlayerControls controls) | ||||||
|  |     { | ||||||
|  |         var str = new StringBuilder("[ "); | ||||||
|  |         if (controls.Forward) | ||||||
|  |             str.Append("▲ "); | ||||||
|  |         if (controls.Backward) | ||||||
|  |             str.Append("▼ "); | ||||||
|  |         if (controls.TurnLeft) | ||||||
|  |             str.Append("◄ "); | ||||||
|  |         if (controls.TurnRight) | ||||||
|  |             str.Append("► "); | ||||||
|  |         if (controls.Shoot) | ||||||
|  |             str.Append("• "); | ||||||
|  |         str.Append(']'); | ||||||
|  |         return str.ToString(); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|     public void Dispose() => _wantedFrames.Dispose(); |     public void Dispose() => _wantedFrames.Dispose(); | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -10,6 +10,6 @@ internal sealed record class TankInfo( | ||||||
| internal sealed record class PlayerInfo( | internal sealed record class PlayerInfo( | ||||||
|     string Name, |     string Name, | ||||||
|     Scores Scores, |     Scores Scores, | ||||||
|     PlayerControls Controls, |     string Controls, | ||||||
|     TankInfo? Tank |     TankInfo? Tank | ||||||
| ); | ); | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue
	
	 Vinzenz Schroeter
						Vinzenz Schroeter