Improvements to matchmaking system
This commit is contained in:
+39
-32
@@ -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;
|
||||
}
|
||||
|
||||
tail = p; // Update the tail pointer
|
||||
}
|
||||
|
||||
struct conn_client_s *mm_pop()
|
||||
{
|
||||
struct conn_client_s *c;
|
||||
struct playerlist_s *p;
|
||||
// Remove a player from the matchmaking queue
|
||||
struct conn_client_s *mm_pop() {
|
||||
if (queue == NULL)
|
||||
return NULL;
|
||||
|
||||
p = queue;
|
||||
c = queue->player;
|
||||
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;
|
||||
}
|
||||
while (queue != NULL && queue->next != NULL) {
|
||||
struct conn_client_s *player1 = mm_pop();
|
||||
struct conn_client_s *player2 = mm_pop();
|
||||
|
||||
player2 = mm_pop();
|
||||
|
||||
if(player1 && player2) {
|
||||
if (player1 && player2) {
|
||||
start_game(player1, player2, gamehandle++);
|
||||
return 0;
|
||||
} else {
|
||||
// This block should never be reached in this setup
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
return -1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user