Improvements to matchmaking system

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