From 2368701b05b7d87bd4247f66ddd4609528230b95 Mon Sep 17 00:00:00 2001 From: ParticleG Date: Mon, 12 Aug 2024 12:47:23 +0800 Subject: [PATCH 1/5] - Update external function signature - Remove unused settings --- ZZZTOJ.Botris/ZZZBot.cs | 6 +++--- ZZZTOJ.Botris/ZZZTOJCore.cs | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/ZZZTOJ.Botris/ZZZBot.cs b/ZZZTOJ.Botris/ZZZBot.cs index 569c411..66b7123 100644 --- a/ZZZTOJ.Botris/ZZZBot.cs +++ b/ZZZTOJ.Botris/ZZZBot.cs @@ -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; @@ -133,12 +133,12 @@ public MoveResult GetMove(RequestMovePayload requestMovePayload) //} int[] comboTable = new int[] { 0, 0, 0, 1, 1, 1, 2, 2, 3, 3, 4,4,4,4, -1 }; - var path = ZZZTOJCore.BotrisAI2(field1, 10, 22, requestMovePayload.GameState.b2b ? 1 : 0, + 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], 3, 1, 0, true, false, requestMovePayload.GameState.garbageQueued.Length, comboTable, BotSetting.NextCnt, BotSetting.Level, 0); + requestMovePayload.GameState.canHold, requestMovePayload.GameState.current.piece[0], 3, 1, 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)); diff --git a/ZZZTOJ.Botris/ZZZTOJCore.cs b/ZZZTOJ.Botris/ZZZTOJCore.cs index 219f697..4133e66 100644 --- a/ZZZTOJ.Botris/ZZZTOJCore.cs +++ b/ZZZTOJ.Botris/ZZZTOJCore.cs @@ -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" From aa69d74d5460160e940f7d3a02cd996716cec12b Mon Sep 17 00:00:00 2001 From: ParticleG Date: Mon, 12 Aug 2024 12:47:52 +0800 Subject: [PATCH 2/5] - Move event callback to private scope --- ZZZTOJ.Botris/Program.cs | 37 +++++++++++++++++++++++++------------ 1 file changed, 25 insertions(+), 12 deletions(-) diff --git a/ZZZTOJ.Botris/Program.cs b/ZZZTOJ.Botris/Program.cs index 9b2f219..e7878ca 100644 --- a/ZZZTOJ.Botris/Program.cs +++ b/ZZZTOJ.Botris/Program.cs @@ -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 @@ -53,25 +65,26 @@ 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; + } + 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; From 8920228db517fa9e425b8e84a52b0dcc50db807f Mon Sep 17 00:00:00 2001 From: ParticleG Date: Mon, 12 Aug 2024 12:48:52 +0800 Subject: [PATCH 3/5] - Initiate UpdateConfig event - Update RoomData structure --- BotrisBattle.NET/BotrisBot.cs | 26 +++++++++++--------------- BotrisBattle.NET/BotrisType.cs | 21 +++++++++++---------- BotrisBattle.NET/PayloadType.cs | 23 +++++++---------------- 3 files changed, 29 insertions(+), 41 deletions(-) diff --git a/BotrisBattle.NET/BotrisBot.cs b/BotrisBattle.NET/BotrisBot.cs index 427c179..812ecf0 100644 --- a/BotrisBattle.NET/BotrisBot.cs +++ b/BotrisBattle.NET/BotrisBot.cs @@ -8,8 +8,8 @@ public class BotrisBot public event Action GameStart; public event Action GameReset; - public event Action RequestMove; + public event Action UpdateConfig; public void SendMove(Command[] commands) { @@ -27,9 +27,14 @@ public BotrisBot(string token) { _websocket = new BotrisWebsocket(token); - _websocket.On("roomData", (payload) => + _websocket.On("room_data", payload => { - //Console.WriteLine("房间信息:{0}", payload.roomData.id); + UpdateConfig?.Invoke( + new UpdateConfigPayload + { + Duration = (int)Math.Floor(1000 / payload.roomData.pps) + } + ); }); _websocket.On("authenticated", (payload) => @@ -37,10 +42,7 @@ public BotrisBot(string token) //Console.WriteLine("认证成功:{0}", payload.SessionId); }); - _websocket.On("game_started", () => - { - GameStart?.Invoke(); - }); + _websocket.On("game_started", () => { GameStart?.Invoke(); }); _websocket.On("request_move", (payload) => { @@ -48,19 +50,13 @@ public BotrisBot(string token) 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); } - - } -} +} \ No newline at end of file diff --git a/BotrisBattle.NET/BotrisType.cs b/BotrisBattle.NET/BotrisType.cs index 80f601f..194d271 100644 --- a/BotrisBattle.NET/BotrisType.cs +++ b/BotrisBattle.NET/BotrisType.cs @@ -5,6 +5,8 @@ using System.Text.Json; using System.Threading.Tasks; +// ReSharper disable InconsistentNaming + namespace BotrisBattle.NET { @@ -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 @@ -66,12 +68,11 @@ 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; } @@ -79,9 +80,9 @@ public class RoomData 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 players { get; set; } - public List banned { get; set; } + public string? lastWinner { get; set; } + public List players { get; set; } = []; + public List banned { get; set; } = []; } // Enum for Piece diff --git a/BotrisBattle.NET/PayloadType.cs b/BotrisBattle.NET/PayloadType.cs index 73345b0..7386c2e 100644 --- a/BotrisBattle.NET/PayloadType.cs +++ b/BotrisBattle.NET/PayloadType.cs @@ -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 Players { get; set; } } - public class ActionPayload { public Command[] Commands { get; set; } } - public class PlayerActionPayload { public string SessionId { get; set; } @@ -94,7 +83,6 @@ public class PlayerDamageReceivedPayload public GameState GameState { get; set; } } - public class RoundOverPayload { public string WinnerId { get; set; } @@ -102,7 +90,6 @@ public class RoundOverPayload public RoomData RoomData { get; set; } } - public class GameOverPayload { public string WinnerId { get; set; } @@ -110,9 +97,13 @@ public class GameOverPayload public RoomData RoomData { get; set; } } - public class GameResetPayload { public RoomData RoomData { get; set; } } -} + + public class UpdateConfigPayload + { + public int Duration = 1; + } +} \ No newline at end of file From 2f89bb0e9d22a1eb46b35875357e3e39b77fd9ad Mon Sep 17 00:00:00 2001 From: ParticleG Date: Mon, 12 Aug 2024 12:54:43 +0800 Subject: [PATCH 4/5] - Add log --- ZZZTOJ.Botris/Program.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/ZZZTOJ.Botris/Program.cs b/ZZZTOJ.Botris/Program.cs index e7878ca..4a004ca 100644 --- a/ZZZTOJ.Botris/Program.cs +++ b/ZZZTOJ.Botris/Program.cs @@ -72,6 +72,7 @@ void BotUpdateConfig(UpdateConfigPayload payload) try { bot1.BotSetting.Duration = payload.Duration; + Console.WriteLine("Update config: duration={0}", payload.Duration); } catch (Exception e) { From 105215912b953560e6fa5f932ee2af22634bece7 Mon Sep 17 00:00:00 2001 From: ParticleG Date: Mon, 12 Aug 2024 12:59:14 +0800 Subject: [PATCH 5/5] - Register settings_changed message - Add TODO --- BotrisBattle.NET/BotrisBot.cs | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/BotrisBattle.NET/BotrisBot.cs b/BotrisBattle.NET/BotrisBot.cs index 812ecf0..a585833 100644 --- a/BotrisBattle.NET/BotrisBot.cs +++ b/BotrisBattle.NET/BotrisBot.cs @@ -27,8 +27,14 @@ public BotrisBot(string token) { _websocket = new BotrisWebsocket(token); + _websocket.On("authenticated", (payload) => + { + //Console.WriteLine("认证成功:{0}", payload.SessionId); + }); + _websocket.On("room_data", payload => { + // TODO: Consider network latency UpdateConfig?.Invoke( new UpdateConfigPayload { @@ -37,10 +43,7 @@ public BotrisBot(string token) ); }); - _websocket.On("authenticated", (payload) => - { - //Console.WriteLine("认证成功:{0}", payload.SessionId); - }); + _websocket.On("game_reset", () => { GameReset?.Invoke(); }); _websocket.On("game_started", () => { GameStart?.Invoke(); }); @@ -50,7 +53,16 @@ public BotrisBot(string token) RequestMove?.Invoke(payload); }); - _websocket.On("game_reset", () => { GameReset?.Invoke(); }); + _websocket.On("settings_changed", payload => + { + // TODO: Consider network latency + UpdateConfig?.Invoke( + new UpdateConfigPayload + { + Duration = (int)Math.Floor(1000 / payload.roomData.pps) + } + ); + }); }