Skip to content

Commit b22e48b

Browse files
committed
Use the linked list for list of the guild players instead of the dynamic array
1 parent 4ce68d1 commit b22e48b

File tree

2 files changed

+12
-25
lines changed

2 files changed

+12
-25
lines changed

src/player/guild_player.cpp

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -144,34 +144,23 @@ void GuildPlayer::HandleReadyState() {
144144
}
145145

146146
GuildPlayer* GuildPlayer::Get(const dpp::snowflake &guild_id) {
147-
/* Try to get the guild in the array */
148-
for (unsigned int i = 0; i < _players_count; i++)
149-
if (_players[i]->guild_id == guild_id) return _players[i];
147+
/* Try to get the guild in the list */
148+
for (GuildPlayer* player: _players)
149+
if (player->guild_id == guild_id) return player;
150150

151-
/* If there is not a such guild we need to add it to the array */
151+
/* If there is not a such guild player we need to add it to the array */
152152
return Add(guild_id);
153153
}
154154

155155
bool GuildPlayer::IsPlayerReady() { return _voiceconn != nullptr && _voiceconn->voiceclient != nullptr && _voiceconn->voiceclient->is_ready(); }
156156

157157
GuildPlayer* GuildPlayer::Add(const dpp::snowflake &guild_id) {
158-
/* Increase the number of guilds */
159-
_players_count++;
160-
161-
/* Try to get the empty place for the pointer */
162-
for (unsigned int i = _max_players_count - PLAYERS_DELTA; i < _max_players_count; i++) {
163-
if (_players[i] != nullptr) continue;
164-
_players[i] = new GuildPlayer(guild_id);
165-
return _players[i];
166-
}
158+
/* Create a new guild player */
159+
GuildPlayer* player = new GuildPlayer(guild_id);
167160

168-
/* If the array with pointer is full we need to increase it */
169-
auto** new_guilds = new GuildPlayer* [_max_players_count + PLAYERS_DELTA];
170-
for (unsigned int i = 0; i < _max_players_count; i++) new_guilds[i] = _players[i];
171-
delete[] _players;
172-
_players = new_guilds;
161+
/* Add it to the list */
162+
_players.Append(player);
173163

174-
/* Add the new guild to the array */
175-
_players[_max_players_count] = new GuildPlayer(guild_id);
176-
return _players[_max_players_count += PLAYERS_DELTA];
164+
/* Return a new player */
165+
return player;
177166
}

src/player/guild_player.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#define BRAGI_GUILD_PLAYER_H
33

44
#include "player/track/track.h"
5+
#include "util/linked_list.h"
56
#include "playlist.h"
67

78
#include <dpp/dpp.h>
@@ -42,10 +43,7 @@ class GuildPlayer final {
4243
bool IsPlayerReady();
4344

4445
private:
45-
static inline constexpr unsigned short PLAYERS_DELTA = 4;
46-
static inline unsigned int _max_players_count = PLAYERS_DELTA;
47-
static inline unsigned int _players_count = 0;
48-
static inline GuildPlayer** _players = new GuildPlayer* [PLAYERS_DELTA];
46+
static inline LinkedList<GuildPlayer*> _players;
4947
static inline LoopType _loop_type = DISABLED;
5048

5149
static GuildPlayer* Add(const dpp::snowflake &guild_id);

0 commit comments

Comments
 (0)