Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

- auto config thinking time according to pps #2

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 22 additions & 14 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,48 @@ public BotrisBot(string token)
{
_websocket = new BotrisWebsocket(token);

_websocket.On<RoomDataPayload>("roomData", (payload) =>
{
//Console.WriteLine("房间信息:{0}", payload.roomData.id);
});

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

_websocket.On("game_started", () =>
_websocket.On<RoomDataPayload>("room_data", payload =>
{
GameStart?.Invoke();
// TODO: Consider network latency
UpdateConfig?.Invoke(
new UpdateConfigPayload
{
Duration = (int)Math.Floor(1000 / payload.roomData.pps)
}
);
});

_websocket.On("game_reset", () => { GameReset?.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", () =>
_websocket.On<RoomDataPayload>("settings_changed", payload =>
{
GameReset?.Invoke();
// TODO: Consider network latency
UpdateConfig?.Invoke(
new UpdateConfigPayload
{
Duration = (int)Math.Floor(1000 / payload.roomData.pps)
}
);
});
}



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,12 +5,14 @@
using System.Text.Json;
using System.Threading.Tasks;

// ReSharper disable InconsistentNaming

namespace BotrisBattle.NET
{

public class BotrisMessage
{
public string type { get; set; }

Check warning on line 15 in BotrisBattle.NET/BotrisType.cs

View workflow job for this annotation

GitHub Actions / build (windows)

Non-nullable property 'type' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
public JsonElement payload { get; set; } = new JsonElement();
}
public class BotrisMessage1
Expand All @@ -22,9 +24,9 @@

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 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,67 +21,56 @@
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; }

Check warning on line 73 in BotrisBattle.NET/PayloadType.cs

View workflow job for this annotation

GitHub Actions / build (macos)

Non-nullable property 'SessionId' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
public List<Command> Commands { get; set; }
public GameState GameState { get; set; }
public List<GameEvent> Events { get; set; }
Expand All @@ -89,30 +78,32 @@

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

Check warning on line 81 in BotrisBattle.NET/PayloadType.cs

View workflow job for this annotation

GitHub Actions / build (macos)

Non-nullable property 'SessionId' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 81 in BotrisBattle.NET/PayloadType.cs

View workflow job for this annotation

GitHub Actions / build (windows)

Non-nullable property 'SessionId' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 81 in BotrisBattle.NET/PayloadType.cs

View workflow job for this annotation

GitHub Actions / build (windows)

Non-nullable property 'SessionId' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
public int Damage { get; set; }
public GameState GameState { get; set; }

Check warning on line 83 in BotrisBattle.NET/PayloadType.cs

View workflow job for this annotation

GitHub Actions / build (macos)

Non-nullable property 'GameState' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 83 in BotrisBattle.NET/PayloadType.cs

View workflow job for this annotation

GitHub Actions / build (windows)

Non-nullable property 'GameState' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 83 in BotrisBattle.NET/PayloadType.cs

View workflow job for this annotation

GitHub Actions / build (windows)

Non-nullable property 'GameState' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
}


public class RoundOverPayload
{
public string WinnerId { get; set; }

Check warning on line 88 in BotrisBattle.NET/PayloadType.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu)

Non-nullable property 'WinnerId' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 88 in BotrisBattle.NET/PayloadType.cs

View workflow job for this annotation

GitHub Actions / build (macos)

Non-nullable property 'WinnerId' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 88 in BotrisBattle.NET/PayloadType.cs

View workflow job for this annotation

GitHub Actions / build (windows)

Non-nullable property 'WinnerId' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 88 in BotrisBattle.NET/PayloadType.cs

View workflow job for this annotation

GitHub Actions / build (windows)

Non-nullable property 'WinnerId' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
public PlayerInfo WinnerInfo { get; set; }

Check warning on line 89 in BotrisBattle.NET/PayloadType.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu)

Non-nullable property 'WinnerInfo' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 89 in BotrisBattle.NET/PayloadType.cs

View workflow job for this annotation

GitHub Actions / build (macos)

Non-nullable property 'WinnerInfo' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 89 in BotrisBattle.NET/PayloadType.cs

View workflow job for this annotation

GitHub Actions / build (windows)

Non-nullable property 'WinnerInfo' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 89 in BotrisBattle.NET/PayloadType.cs

View workflow job for this annotation

GitHub Actions / build (windows)

Non-nullable property 'WinnerInfo' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
public RoomData RoomData { get; set; }

Check warning on line 90 in BotrisBattle.NET/PayloadType.cs

View workflow job for this annotation

GitHub Actions / build (macos)

Non-nullable property 'RoomData' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 90 in BotrisBattle.NET/PayloadType.cs

View workflow job for this annotation

GitHub Actions / build (windows)

Non-nullable property 'RoomData' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 90 in BotrisBattle.NET/PayloadType.cs

View workflow job for this annotation

GitHub Actions / build (windows)

Non-nullable property 'RoomData' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
}


public class GameOverPayload
{
public string WinnerId { get; set; }

Check warning on line 95 in BotrisBattle.NET/PayloadType.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu)

Non-nullable property 'WinnerId' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 95 in BotrisBattle.NET/PayloadType.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu)

Non-nullable property 'WinnerId' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 95 in BotrisBattle.NET/PayloadType.cs

View workflow job for this annotation

GitHub Actions / build (macos)

Non-nullable property 'WinnerId' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 95 in BotrisBattle.NET/PayloadType.cs

View workflow job for this annotation

GitHub Actions / build (macos)

Non-nullable property 'WinnerId' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 95 in BotrisBattle.NET/PayloadType.cs

View workflow job for this annotation

GitHub Actions / build (windows)

Non-nullable property 'WinnerId' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 95 in BotrisBattle.NET/PayloadType.cs

View workflow job for this annotation

GitHub Actions / build (windows)

Non-nullable property 'WinnerId' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
public PlayerInfo WinnerInfo { get; set; }

Check warning on line 96 in BotrisBattle.NET/PayloadType.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu)

Non-nullable property 'WinnerInfo' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 96 in BotrisBattle.NET/PayloadType.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu)

Non-nullable property 'WinnerInfo' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 96 in BotrisBattle.NET/PayloadType.cs

View workflow job for this annotation

GitHub Actions / build (macos)

Non-nullable property 'WinnerInfo' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 96 in BotrisBattle.NET/PayloadType.cs

View workflow job for this annotation

GitHub Actions / build (macos)

Non-nullable property 'WinnerInfo' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 96 in BotrisBattle.NET/PayloadType.cs

View workflow job for this annotation

GitHub Actions / build (windows)

Non-nullable property 'WinnerInfo' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 96 in BotrisBattle.NET/PayloadType.cs

View workflow job for this annotation

GitHub Actions / build (windows)

Non-nullable property 'WinnerInfo' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
public RoomData RoomData { get; set; }

Check warning on line 97 in BotrisBattle.NET/PayloadType.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu)

Non-nullable property 'RoomData' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 97 in BotrisBattle.NET/PayloadType.cs

View workflow job for this annotation

GitHub Actions / build (macos)

Non-nullable property 'RoomData' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 97 in BotrisBattle.NET/PayloadType.cs

View workflow job for this annotation

GitHub Actions / build (windows)

Non-nullable property 'RoomData' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 97 in BotrisBattle.NET/PayloadType.cs

View workflow job for this annotation

GitHub Actions / build (windows)

Non-nullable property 'RoomData' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
}


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

Check warning on line 102 in BotrisBattle.NET/PayloadType.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu)

Non-nullable property 'RoomData' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 102 in BotrisBattle.NET/PayloadType.cs

View workflow job for this annotation

GitHub Actions / build (ubuntu)

Non-nullable property 'RoomData' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 102 in BotrisBattle.NET/PayloadType.cs

View workflow job for this annotation

GitHub Actions / build (macos)

Non-nullable property 'RoomData' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 102 in BotrisBattle.NET/PayloadType.cs

View workflow job for this annotation

GitHub Actions / build (macos)

Non-nullable property 'RoomData' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 102 in BotrisBattle.NET/PayloadType.cs

View workflow job for this annotation

GitHub Actions / build (windows)

Non-nullable property 'RoomData' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.

Check warning on line 102 in BotrisBattle.NET/PayloadType.cs

View workflow job for this annotation

GitHub Actions / build (windows)

Non-nullable property 'RoomData' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
}
}

public class UpdateConfigPayload
{
public int Duration = 1;
}
}
38 changes: 26 additions & 12 deletions ZZZTOJ.Botris/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,22 @@

BotrisBot bot = new(botSetting.Token);
ZZZBot bot1 = new() { BotSetting = botSetting };
bot.RequestMove += Bot_RequestMove;
async void Bot_RequestMove(RequestMovePayload obj)

bot.RequestMove += BotRequestMove;
bot.UpdateConfig += BotUpdateConfig;

bot.Connect(botSetting.RoomKey, CancellationToken.None);

while (true)
{
var input = Console.ReadLine();
if (input == "q") break;
}

return;

void BotRequestMove(RequestMovePayload obj)
{
//await Task.Delay(1);
//Console.Clear();
try
Expand All @@ -53,25 +65,27 @@ async void Bot_RequestMove(RequestMovePayload obj)
Console.WriteLine(ex.Message);
Console.WriteLine(ex.StackTrace);
}


}

bot.Connect(botSetting.RoomKey, CancellationToken.None);

while (true)
void BotUpdateConfig(UpdateConfigPayload payload)
{
var input = Console.ReadLine();
if (input == "q") break;
try
{
bot1.BotSetting.Duration = payload.Duration;
Console.WriteLine("Update config: duration={0}", payload.Duration);
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
}


public class BotSetting
{
public int NextCnt { get; set; } = 6;
public int Level { get; set; } = 8;
public int BPM { get; set; } = 200;

public int Duration { get; set; } = 100;
public string Token { get; set; } = string.Empty;
public string RoomKey { get; set; } = string.Empty;
public bool Quiet {get;set;} = false;
Expand Down
12 changes: 7 additions & 5 deletions ZZZTOJ.Botris/ZZZBot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class ZZZBot
static int _garbage = 0;
static object _lockQueue = new();
static object _lockBoard = new();
public BotSetting BotSetting = new BotSetting() { BPM = 200, Level = 8, NextCnt = 6 };
public BotSetting BotSetting = new();

static DateTime _startTime;
static int _nowIdx = 0;
Expand Down Expand Up @@ -133,15 +133,17 @@ public MoveResult GetMove(RequestMovePayload requestMovePayload)

//}
int[] comboTable = new int[] { 0, 0, 0, 1, 1, 1, 2, 2, 3, 3, 4,4,4,4, -1 };
Console.WriteLine("T={0},X={1},Y={2},R={3}",requestMovePayload.GameState.current.piece[0],requestMovePayload.GameState.current.x, requestMovePayload.GameState.current.y, requestMovePayload.GameState.current.rotation);
var path = ZZZTOJCore.BotrisAI2(field1, 10, 22, requestMovePayload.GameState.b2b ? 1 : 0,

if (!BotSetting.Quiet) {
Console.WriteLine("T={0},X={1},Y={2},R={3}",requestMovePayload.GameState.current.piece[0],requestMovePayload.GameState.current.x, requestMovePayload.GameState.current.y, requestMovePayload.GameState.current.rotation);
}
var path = ZZZTOJCore.BotrisAI3(field1, 10, 22, requestMovePayload.GameState.b2b ? 1 : 0,
requestMovePayload.GameState.combo,
requestMovePayload.GameState.queue.Select(s => s[0]).ToArray(),

requestMovePayload.GameState.held == null ? ' ': requestMovePayload.GameState.held[0],
requestMovePayload.GameState.canHold, requestMovePayload.GameState.current.piece[0],
requestMovePayload.GameState.current.x, 20 - requestMovePayload.GameState.current.y, requestMovePayload.GameState.current.rotation,
true, false, requestMovePayload.GameState.garbageQueued.Length, comboTable, BotSetting.NextCnt, BotSetting.Level, 0);
true, false, requestMovePayload.GameState.garbageQueued.Length, comboTable, BotSetting.NextCnt, BotSetting.Duration);
string resultpath = Marshal.PtrToStringAnsi(path);
_IOBoard.NextQueue.Enqueue(TetrisMino.Z);
Console.WriteLine(resultpath.PadRight(50));
Expand Down
6 changes: 3 additions & 3 deletions ZZZTOJ.Botris/ZZZTOJCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ public static class ZZZTOJCore
)]
public static extern nint BotrisAI(int[] overfield, int[] field, int field_w, int field_h, int b2b, int combo,
char[] next, char hold, [MarshalAs(UnmanagedType.I1)] bool curCanHold, char active, int x, int y, int spin,
[MarshalAs(UnmanagedType.I1)] bool canhold, [MarshalAs(UnmanagedType.I1)] bool can180spin, int upcomeAtt, int[] comboTable, int maxDepth, int level, int player);
[MarshalAs(UnmanagedType.I1)] bool canhold, [MarshalAs(UnmanagedType.I1)] bool can180spin, int upcomeAtt, int[] comboTable, int maxDepth, int level);
[DllImport("zzz_botris"
//, EntryPoint = "TetrisAI"
)]
public static extern nint BotrisAI2(int[] field, int field_w, int field_h, int b2b, int combo,
public static extern nint BotrisAI3(int[] field, int field_w, int field_h, int b2b, int combo,
char[] next, char hold, [MarshalAs(UnmanagedType.I1)] bool curCanHold, char active, int x, int y, int spin,
[MarshalAs(UnmanagedType.I1)] bool canhold, [MarshalAs(UnmanagedType.I1)] bool can180spin, int upcomeAtt, int[] comboTable, int maxDepth, int level, int player);
[MarshalAs(UnmanagedType.I1)] bool canhold, [MarshalAs(UnmanagedType.I1)] bool can180spin, int upcomeAtt, int[] comboTable, int maxDepth, int duration);

[DllImport("zzz_botris"
//, EntryPoint = "TetrisAI"
Expand Down
Loading