Skip to content

Commit

Permalink
Use the linked list for list of the guild players instead of the dyna…
Browse files Browse the repository at this point in the history
…mic array
  • Loading branch information
Evilur committed Dec 28, 2024
1 parent 4ce68d1 commit b22e48b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 25 deletions.
31 changes: 10 additions & 21 deletions src/player/guild_player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,34 +144,23 @@ void GuildPlayer::HandleReadyState() {
}

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

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

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

GuildPlayer* GuildPlayer::Add(const dpp::snowflake &guild_id) {
/* Increase the number of guilds */
_players_count++;

/* Try to get the empty place for the pointer */
for (unsigned int i = _max_players_count - PLAYERS_DELTA; i < _max_players_count; i++) {
if (_players[i] != nullptr) continue;
_players[i] = new GuildPlayer(guild_id);
return _players[i];
}
/* Create a new guild player */
GuildPlayer* player = new GuildPlayer(guild_id);

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

/* Add the new guild to the array */
_players[_max_players_count] = new GuildPlayer(guild_id);
return _players[_max_players_count += PLAYERS_DELTA];
/* Return a new player */
return player;
}
6 changes: 2 additions & 4 deletions src/player/guild_player.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define BRAGI_GUILD_PLAYER_H

#include "player/track/track.h"
#include "util/linked_list.h"
#include "playlist.h"

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

private:
static inline constexpr unsigned short PLAYERS_DELTA = 4;
static inline unsigned int _max_players_count = PLAYERS_DELTA;
static inline unsigned int _players_count = 0;
static inline GuildPlayer** _players = new GuildPlayer* [PLAYERS_DELTA];
static inline LinkedList<GuildPlayer*> _players;
static inline LoopType _loop_type = DISABLED;

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

0 comments on commit b22e48b

Please sign in to comment.