Improvements to matchmaking system

This commit is contained in:
omer
2024-04-15 15:51:19 +02:00
parent 79ca60d136
commit a55d3c4b9b
+39 -32
View File
@@ -15,11 +15,11 @@
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdarg.h>
#include <memory.h>
#include <malloc.h>
#include <ev.h>
#include <malloc.h>
#include <memory.h>
#include <stdarg.h>
#include <stdio.h>
#include <hmbase.h>
@@ -30,51 +30,58 @@ struct playerlist_s {
struct playerlist_s *next;
};
static struct playerlist_s *queue = NULL;
static int gamehandle = 0;
struct playerlist_s *queue = NULL;
struct playerlist_s *tail = NULL; // Pointer to the last element
void mm_push(struct conn_client_s *c)
{
struct playerlist_s *p;
p = malloc(sizeof(*p));
// Add a player to the matchmaking queue
void mm_push(struct conn_client_s *c) {
struct playerlist_s *p = malloc(sizeof(*p));
p->player = c;
p->next = queue;
p->next = NULL;
if (queue == NULL) {
queue = p;
} else {
tail->next = p;
}
struct conn_client_s *mm_pop()
{
struct conn_client_s *c;
struct playerlist_s *p;
tail = p; // Update the tail pointer
}
p = queue;
c = queue->player;
// Remove a player from the matchmaking queue
struct conn_client_s *mm_pop() {
if (queue == NULL)
return NULL;
struct playerlist_s *temp = queue;
struct conn_client_s *c = temp->player;
queue = queue->next;
free(p);
if (queue == NULL) {
tail = NULL; // If the queue is empty after pop, reset the tail pointer
}
free(temp);
return c;
}
int matchmaking(struct conn_client_s *player1)
{
struct conn_client_s *player2;
// Matchmaking
int matchmaking(struct conn_client_s *player) {
// Add the player to the queue
mm_push(player);
// first player
if(queue == NULL) {
mm_push(player1);
return 0;
}
player2 = mm_pop();
while (queue != NULL && queue->next != NULL) {
struct conn_client_s *player1 = mm_pop();
struct conn_client_s *player2 = mm_pop();
if (player1 && player2) {
start_game(player1, player2, gamehandle++);
return 0;
}
} else {
// This block should never be reached in this setup
return -1;
}
}
return 0;
}