controls string on server side
This commit is contained in:
parent
603a53eef7
commit
7bc45d1d58
|
@ -22,14 +22,6 @@ function ScoreRow({name, value}: {
|
|||
</tr>;
|
||||
}
|
||||
|
||||
type Controls = {
|
||||
readonly forward: boolean;
|
||||
readonly backward: boolean;
|
||||
readonly turnLeft: boolean;
|
||||
readonly turnRight: boolean;
|
||||
readonly shoot: boolean;
|
||||
}
|
||||
|
||||
type TankInfo = {
|
||||
readonly explosiveBullets: number;
|
||||
readonly position: { x: number; y: number };
|
||||
|
@ -41,25 +33,10 @@ type TankInfo = {
|
|||
type PlayerInfoMessage = {
|
||||
readonly name: string;
|
||||
readonly scores: Scores;
|
||||
readonly controls: Controls;
|
||||
readonly controls: string;
|
||||
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 }) {
|
||||
const [shouldSendMessage, setShouldSendMessage] = useState(false);
|
||||
|
||||
|
@ -87,7 +64,7 @@ export default function PlayerInfo({playerId}: { playerId: Guid }) {
|
|||
</h3>
|
||||
<table>
|
||||
<tbody>
|
||||
<ScoreRow name="controls" value={controlsString(lastJsonMessage.controls)}/>
|
||||
<ScoreRow name="controls" value={lastJsonMessage.controls}/>
|
||||
<ScoreRow name="kills" value={lastJsonMessage.scores.kills}/>
|
||||
<ScoreRow name="deaths" value={lastJsonMessage.scores.deaths}/>
|
||||
<ScoreRow name="walls" value={lastJsonMessage.scores.wallsDestroyed}/>
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
using System.Net.WebSockets;
|
||||
using System.Text;
|
||||
using System.Text.Json;
|
||||
using TanksServer.GameLogic;
|
||||
|
||||
|
@ -51,7 +52,7 @@ internal sealed class PlayerInfoConnection(
|
|||
var tankInfo = tank != null
|
||||
? new TankInfo(tank.Orientation, tank.ExplosiveBullets, tank.Position.ToPixelPosition(), tank.Moving)
|
||||
: 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);
|
||||
|
||||
if (response.SequenceEqual(_lastMessage))
|
||||
|
@ -60,5 +61,22 @@ internal sealed class PlayerInfoConnection(
|
|||
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();
|
||||
}
|
||||
|
|
|
@ -10,6 +10,6 @@ internal sealed record class TankInfo(
|
|||
internal sealed record class PlayerInfo(
|
||||
string Name,
|
||||
Scores Scores,
|
||||
PlayerControls Controls,
|
||||
string Controls,
|
||||
TankInfo? Tank
|
||||
);
|
||||
|
|
Loading…
Reference in a new issue