Skip to content

Commit

Permalink
- Initiate UpdateConfig event
Browse files Browse the repository at this point in the history
- Update RoomData structure
  • Loading branch information
ParticleG committed Aug 12, 2024
1 parent aa69d74 commit 8920228
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 41 deletions.
26 changes: 11 additions & 15 deletions BotrisBattle.NET/BotrisBot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ public class BotrisBot

public event Action GameStart;
public event Action GameReset;

public event Action<RequestMovePayload> RequestMove;
public event Action<UpdateConfigPayload> UpdateConfig;

public void SendMove(Command[] commands)
{
Expand All @@ -27,40 +27,36 @@ public BotrisBot(string token)
{
_websocket = new BotrisWebsocket(token);

_websocket.On<RoomDataPayload>("roomData", (payload) =>
_websocket.On<RoomDataPayload>("room_data", payload =>
{
//Console.WriteLine("房间信息:{0}", payload.roomData.id);
UpdateConfig?.Invoke(
new UpdateConfigPayload
{
Duration = (int)Math.Floor(1000 / payload.roomData.pps)
}
);
});

_websocket.On<AuthenticatedPayload>("authenticated", (payload) =>
{
//Console.WriteLine("认证成功:{0}", payload.SessionId);
});

_websocket.On("game_started", () =>
{
GameStart?.Invoke();
});
_websocket.On("game_started", () => { GameStart?.Invoke(); });

_websocket.On<RequestMovePayload>("request_move", (payload) =>
{
//Console.WriteLine("befory event invoke");
RequestMove?.Invoke(payload);
});

_websocket.On("game_reset", () =>
{
GameReset?.Invoke();
});
_websocket.On("game_reset", () => { GameReset?.Invoke(); });
}



public async void Connect(string room, CancellationToken token)
{
await _websocket.Connect(room, token);
}


}
}
}
21 changes: 11 additions & 10 deletions BotrisBattle.NET/BotrisType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using System.Text.Json;
using System.Threading.Tasks;

// ReSharper disable InconsistentNaming

namespace BotrisBattle.NET
{

Expand All @@ -22,9 +24,9 @@ public class GameEvent: BotrisMessage;

public class PlayerInfo
{
public string userId { get; set; }
public string creator { get; set; }
public string bot { get; set; }
public required string userId { get; set; }
public required string creator { get; set; }
public required string bot { get; set; }
}

public class PlayerData
Expand Down Expand Up @@ -66,22 +68,21 @@ public class GameState

public class RoomData
{
public string id { get; set; }
public PlayerInfo host { get; set; }
public required string id { get; set; }
public required PlayerInfo host { get; set; }
public bool @private { get; set; }
public int ft { get; set; }
public int initialPps { get; set; }
public int finalPps { get; set; }
public double pps { get; set; }
public int startMargin { get; set; }
public int endMargin { get; set; }
public int maxPlayers { get; set; }
public bool gameOngoing { get; set; }
public bool roundOngoing { get; set; }
public long? startedAt { get; set; }
public long? endedAt { get; set; }
public string lastWinner { get; set; } // Assuming SessionId is a string
public List<PlayerData> players { get; set; }
public List<PlayerInfo> banned { get; set; }
public string? lastWinner { get; set; }
public List<PlayerData> players { get; set; } = [];
public List<PlayerInfo> banned { get; set; } = [];
}

// Enum for Piece
Expand Down
23 changes: 7 additions & 16 deletions BotrisBattle.NET/PayloadType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,64 +21,53 @@ public class ErrorPayload
public string Payload { get; set; }
}


public class PlayerJoinedPayload
{
public PlayerData PlayerData { get; set; }
}



public class PlayerLeftPayload
{
public string SessionId { get; set; }
}



public class PlayerBannedPayload
{
public PlayerInfo PlayerInfo { get; set; }
}


public class PlayerUnbannedPayload
{
public PlayerInfo PlayerInfo { get; set; }
}

public class SettingsChangedPayload
{
public RoomData RoomData { get; set; }
}


public class HostChangedPayload
{
public PlayerInfo HostInfo { get; set; }
}



public class RoundStartedPayload
{
public long StartsAt { get; set; }
public RoomData RoomData { get; set; }
}


public class RequestMovePayload
{
public GameState GameState { get; set; }
public List<PlayerData> Players { get; set; }
}


public class ActionPayload
{
public Command[] Commands { get; set; }
}


public class PlayerActionPayload
{
public string SessionId { get; set; }
Expand All @@ -94,25 +83,27 @@ public class PlayerDamageReceivedPayload
public GameState GameState { get; set; }
}


public class RoundOverPayload
{
public string WinnerId { get; set; }
public PlayerInfo WinnerInfo { get; set; }
public RoomData RoomData { get; set; }
}


public class GameOverPayload
{
public string WinnerId { get; set; }
public PlayerInfo WinnerInfo { get; set; }
public RoomData RoomData { get; set; }
}


public class GameResetPayload
{
public RoomData RoomData { get; set; }
}
}

public class UpdateConfigPayload
{
public int Duration = 1;
}
}

0 comments on commit 8920228

Please sign in to comment.