Skip to content

Commit 1a77080

Browse files
author
Matthew Bate
committed
Fix Randomization of maps on remote
1 parent 4f412d2 commit 1a77080

File tree

3 files changed

+59
-47
lines changed

3 files changed

+59
-47
lines changed

BHD-ServerManager/API/Services/InstanceMapper.cs

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -76,24 +76,6 @@ private static BanInstanceDTO MapBanInstance(banInstance bans)
7676
};
7777
}
7878

79-
private static MapInstanceDTO MapMapInstance(mapInstance maps)
80-
{
81-
var playlists = maps.Playlists.ToDictionary(
82-
kvp => kvp.Key,
83-
kvp => kvp.Value.Select(m =>
84-
new MapDTO(m.MapID, m.MapName, m.MapFile, m.MapType)).ToList()
85-
);
86-
87-
return new MapInstanceDTO
88-
{
89-
Playlists = playlists,
90-
ActivePlaylist = maps.ActivePlaylist,
91-
CurrentMap = maps.CurrentMapName,
92-
CurrentMapIndex = maps.CurrentMapIndex,
93-
NextMap = maps.NextMapName
94-
};
95-
}
96-
9779
private static string DecodePlayerName(string? base64)
9880
{
9981
if (string.IsNullOrEmpty(base64)) return "Unknown";

HawkSyncShared/DTOs/MapPlaylistDTO.cs

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,6 @@ public class MapDTO
1212
public string MapFile { get; set; } = string.Empty;
1313
public int ModType { get; set; }
1414
public int MapType { get; set; }
15-
16-
// Add this constructor:
17-
public MapDTO(int mapID, string mapName, string mapFile, int mapType)
18-
{
19-
MapID = mapID;
20-
MapName = mapName;
21-
MapFile = mapFile;
22-
MapType = mapType;
23-
}
24-
25-
// Optionally, add a full constructor if you want to set ModType too:
26-
public MapDTO(int mapID, string mapName, string mapFile, int modType, int mapType)
27-
{
28-
MapID = mapID;
29-
MapName = mapName;
30-
MapFile = mapFile;
31-
ModType = modType;
32-
MapType = mapType;
33-
}
34-
35-
// Parameterless constructor for serialization
36-
public MapDTO() { }
3715
}
3816

3917
/// <summary>

RemoteClient/Forms/Panels/tabMaps.cs

Lines changed: 59 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,11 @@ private void WireUpEvents()
7777
btn_mapTypeFB.Click += (s, e) => FilterAvailableMaps(8);
7878

7979
// Playlist selection
80-
btn_loadPlaylist1.Click += (s, e) => LoadPlaylist(1);
81-
btn_loadPlaylist2.Click += (s, e) => LoadPlaylist(2);
82-
btn_loadPlaylist3.Click += (s, e) => LoadPlaylist(3);
83-
btn_loadPlaylist4.Click += (s, e) => LoadPlaylist(4);
84-
btn_loadPlaylist5.Click += (s, e) => LoadPlaylist(5);
80+
btn_loadPlaylist1.Click += (s, e) => actionClick_loadMapPlaylist1();
81+
btn_loadPlaylist2.Click += (s, e) => actionClick_loadMapPlaylist2();
82+
btn_loadPlaylist3.Click += (s, e) => actionClick_loadMapPlaylist3();
83+
btn_loadPlaylist4.Click += (s, e) => actionClick_loadMapPlaylist4();
84+
btn_loadPlaylist5.Click += (s, e) => actionClick_loadMapPlaylist5();
8585

8686
// Playlist editing
8787
dataGridView_availableMaps.CellDoubleClick += (s, e) => AddMapToPlaylist(e.RowIndex);
@@ -184,8 +184,23 @@ private void RandomizePlaylist()
184184
MessageBox.Show("Cannot randomize an empty playlist.", "Empty Playlist", MessageBoxButtons.OK, MessageBoxIcon.Warning);
185185
return;
186186
}
187-
var rnd = new Random();
188-
_currentPlaylist = _currentPlaylist.OrderBy(x => rnd.Next()).ToList();
187+
188+
// Fisher-Yates shuffle
189+
var random = new Random();
190+
for (int i = _currentPlaylist.Count - 1; i > 0; i--)
191+
{
192+
int j = random.Next(i + 1);
193+
var temp = _currentPlaylist[i];
194+
_currentPlaylist[i] = _currentPlaylist[j];
195+
_currentPlaylist[j] = temp;
196+
}
197+
198+
// Renumber MapIDs sequentially (1-based)
199+
for (int i = 0; i < _currentPlaylist.Count; i++)
200+
{
201+
_currentPlaylist[i].MapID = i + 1;
202+
}
203+
189204
RefreshCurrentPlaylistGrid();
190205
MessageBox.Show("Playlist randomized. Remember to save to apply changes.", "Randomized", MessageBoxButtons.OK, MessageBoxIcon.Information);
191206
}
@@ -200,6 +215,43 @@ private void RefreshCurrentPlaylistGrid()
200215
id++, map.MapName, map.MapFile, map.ModType, map.MapType, GetShortType(map.MapType));
201216
}
202217
}
218+
219+
/// <summary>
220+
/// Playlist selection button handlers
221+
/// </summary>
222+
private void actionClick_loadMapPlaylist1() => methodFunction_selectPlaylist(1);
223+
private void actionClick_loadMapPlaylist2() => methodFunction_selectPlaylist(2);
224+
private void actionClick_loadMapPlaylist3() => methodFunction_selectPlaylist(3);
225+
private void actionClick_loadMapPlaylist4() => methodFunction_selectPlaylist(4);
226+
private void actionClick_loadMapPlaylist5() => methodFunction_selectPlaylist(5);
227+
228+
/// <summary>
229+
/// Select and load a specific playlist
230+
/// </summary>
231+
private void methodFunction_selectPlaylist(int playlistID)
232+
{
233+
Color selected = SystemColors.ActiveBorder;
234+
Color notSelected = Button.DefaultBackColor;
235+
236+
// Set selected playlist
237+
mapInstance!.SelectedPlaylist = playlistID;
238+
239+
// Update button colors
240+
btn_loadPlaylist1.BackColor = playlistID == 1 ? selected : notSelected;
241+
btn_loadPlaylist2.BackColor = playlistID == 2 ? selected : notSelected;
242+
btn_loadPlaylist3.BackColor = playlistID == 3 ? selected : notSelected;
243+
btn_loadPlaylist4.BackColor = playlistID == 4 ? selected : notSelected;
244+
btn_loadPlaylist5.BackColor = playlistID == 5 ? selected : notSelected;
245+
246+
// Update active playlist display
247+
btn_activePlaylist.Text = $"P{mapInstance.ActivePlaylist}";
248+
249+
// Update map controls
250+
methodFunction_UpdateMapControls();
251+
252+
// Load playlist
253+
_ = LoadPlaylist(playlistID);
254+
}
203255

204256
private string GetShortType(int mapType)
205257
{

0 commit comments

Comments
 (0)