| 1 |
/*
|
| 2 |
* Copyright (C) 2000,2001 Onlyer (onlyer@263.net)
|
| 3 |
*
|
| 4 |
* This program is free software; you can redistribute it and/or
|
| 5 |
* modify it under the terms of the GNU General Public License
|
| 6 |
* as published by the Free Software Foundation; either version 2
|
| 7 |
* of the License, or (at your option) any later version.
|
| 8 |
*
|
| 9 |
* This program is distributed in the hope that it will be useful,
|
| 10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
| 11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
| 12 |
* GNU General Public License for more details.
|
| 13 |
*
|
| 14 |
* You should have received a copy of the GNU General Public License
|
| 15 |
* along with this program; if not, write to the Free Software
|
| 16 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
| 17 |
*/
|
| 18 |
#include "common/setup_before.h"
|
| 19 |
#include "setup.h"
|
| 20 |
|
| 21 |
#ifdef STDC_HEADERS
|
| 22 |
# include <stdlib.h>
|
| 23 |
#else
|
| 24 |
# ifdef HAVE_MALLOC_H
|
| 25 |
# include <malloc.h>
|
| 26 |
# endif
|
| 27 |
#endif
|
| 28 |
#ifdef HAVE_STRING_H
|
| 29 |
# include <string.h>
|
| 30 |
#else
|
| 31 |
# ifdef HAVE_STRINGS_H
|
| 32 |
# include <strings.h>
|
| 33 |
# endif
|
| 34 |
# ifdef HAVE_MEMORY_H
|
| 35 |
# include <memory.h>
|
| 36 |
# endif
|
| 37 |
#endif
|
| 38 |
#include "compat/strcasecmp.h"
|
| 39 |
|
| 40 |
#include "connection.h"
|
| 41 |
#include "gamequeue.h"
|
| 42 |
#include "handle_d2cs.h"
|
| 43 |
#include "common/packet.h"
|
| 44 |
#include "common/list.h"
|
| 45 |
#include "common/eventlog.h"
|
| 46 |
#include "common/xalloc.h"
|
| 47 |
#include "common/setup_after.h"
|
| 48 |
|
| 49 |
static t_list * gqlist_head=NULL;
|
| 50 |
static unsigned int gqlist_seqno=0;
|
| 51 |
|
| 52 |
extern t_list * gqlist(void)
|
| 53 |
{
|
| 54 |
return gqlist_head;
|
| 55 |
}
|
| 56 |
|
| 57 |
extern int gqlist_create(void)
|
| 58 |
{
|
| 59 |
gqlist_head=list_create();
|
| 60 |
return 0;
|
| 61 |
}
|
| 62 |
|
| 63 |
extern int gqlist_destroy(void)
|
| 64 |
{
|
| 65 |
t_gq * gq;
|
| 66 |
|
| 67 |
BEGIN_LIST_TRAVERSE_DATA(gqlist_head,gq)
|
| 68 |
{
|
| 69 |
gq_destroy(gq,(t_elem **)curr_elem_);
|
| 70 |
}
|
| 71 |
END_LIST_TRAVERSE_DATA()
|
| 72 |
|
| 73 |
if (list_destroy(gqlist_head)<0) {
|
| 74 |
eventlog(eventlog_level_error,__FUNCTION__,"error destroy game queue list");
|
| 75 |
return -1;
|
| 76 |
}
|
| 77 |
gqlist_head=NULL;
|
| 78 |
return 0;
|
| 79 |
}
|
| 80 |
|
| 81 |
extern t_gq * gq_create(unsigned int clientid, t_packet * packet, char const * gamename)
|
| 82 |
{
|
| 83 |
t_gq * gq;
|
| 84 |
|
| 85 |
gq=xmalloc(sizeof(t_gq));
|
| 86 |
gq->seqno=++gqlist_seqno;
|
| 87 |
gq->clientid=clientid;
|
| 88 |
gq->packet=packet;
|
| 89 |
strncpy(gq->gamename, gamename, MAX_GAMENAME_LEN);
|
| 90 |
if (packet) packet_add_ref(packet);
|
| 91 |
list_append_data(gqlist_head,gq);
|
| 92 |
return gq;
|
| 93 |
}
|
| 94 |
|
| 95 |
extern int gq_destroy(t_gq * gq, t_elem ** elem)
|
| 96 |
{
|
| 97 |
ASSERT(gq,-1);
|
| 98 |
if (list_remove_data(gqlist_head,gq,elem)<0) {
|
| 99 |
eventlog(eventlog_level_error,__FUNCTION__,"error remove game queue from list");
|
| 100 |
return -1;
|
| 101 |
}
|
| 102 |
if (gq->packet) packet_del_ref(gq->packet);
|
| 103 |
xfree(gq);
|
| 104 |
return 0;
|
| 105 |
}
|
| 106 |
|
| 107 |
extern unsigned int gq_get_clientid(t_gq const * gq)
|
| 108 |
{
|
| 109 |
ASSERT(gq,0);
|
| 110 |
return gq->clientid;
|
| 111 |
}
|
| 112 |
|
| 113 |
extern int gqlist_check_creategame(int number)
|
| 114 |
{
|
| 115 |
t_connection * c;
|
| 116 |
t_gq * gq;
|
| 117 |
int i=0;
|
| 118 |
|
| 119 |
if (number <= 0) return -1;
|
| 120 |
|
| 121 |
BEGIN_LIST_TRAVERSE_DATA(gqlist_head,gq)
|
| 122 |
{
|
| 123 |
c=d2cs_connlist_find_connection_by_sessionnum(gq->clientid);
|
| 124 |
if (!c) {
|
| 125 |
eventlog(eventlog_level_error,__FUNCTION__,"client %d not found (gamename: %s)",gq->clientid,gq->gamename);
|
| 126 |
gq_destroy(gq,&curr_elem_);
|
| 127 |
continue;
|
| 128 |
} else if (!conn_get_gamequeue(c)) {
|
| 129 |
eventlog(eventlog_level_error,__FUNCTION__,"got NULL game queue for client %s",d2cs_conn_get_account(c));
|
| 130 |
gq_destroy(gq,&curr_elem_);
|
| 131 |
continue;
|
| 132 |
} else {
|
| 133 |
eventlog(eventlog_level_info,__FUNCTION__,"try create game %s for account %s",gq->gamename,d2cs_conn_get_account(c));
|
| 134 |
d2cs_handle_client_creategame(c,gq->packet);
|
| 135 |
conn_set_gamequeue(c,NULL);
|
| 136 |
gq_destroy(gq,&curr_elem_);
|
| 137 |
i++;
|
| 138 |
if(i >= number) break;
|
| 139 |
}
|
| 140 |
}
|
| 141 |
END_LIST_TRAVERSE_DATA()
|
| 142 |
return 0;
|
| 143 |
}
|
| 144 |
|
| 145 |
extern int gqlist_update_all_clients(void)
|
| 146 |
{
|
| 147 |
t_connection * c;
|
| 148 |
t_gq * gq;
|
| 149 |
unsigned int n;
|
| 150 |
|
| 151 |
n=0;
|
| 152 |
BEGIN_LIST_TRAVERSE_DATA(gqlist_head,gq)
|
| 153 |
{
|
| 154 |
c=d2cs_connlist_find_connection_by_sessionnum(gq->clientid);
|
| 155 |
if (!c) {
|
| 156 |
eventlog(eventlog_level_error,__FUNCTION__,"client %d not found (gamename: %s)",gq->clientid,gq->gamename);
|
| 157 |
gq_destroy(gq,&curr_elem_);
|
| 158 |
continue;
|
| 159 |
} else {
|
| 160 |
n++;
|
| 161 |
eventlog(eventlog_level_debug,__FUNCTION__,"update client %s position to %d",d2cs_conn_get_account(c),n);
|
| 162 |
d2cs_send_client_creategamewait(c,n);
|
| 163 |
}
|
| 164 |
}
|
| 165 |
END_LIST_TRAVERSE_DATA()
|
| 166 |
if (n) eventlog(eventlog_level_info,__FUNCTION__,"total %d game queues",n);
|
| 167 |
return 0;
|
| 168 |
}
|
| 169 |
|
| 170 |
extern unsigned int gqlist_get_gq_position(t_gq * gq)
|
| 171 |
{
|
| 172 |
t_gq * tmp;
|
| 173 |
unsigned int pos;
|
| 174 |
|
| 175 |
pos=0;
|
| 176 |
BEGIN_LIST_TRAVERSE_DATA(gqlist_head,tmp)
|
| 177 |
{
|
| 178 |
pos++;
|
| 179 |
if (tmp==gq) return pos;
|
| 180 |
}
|
| 181 |
END_LIST_TRAVERSE_DATA()
|
| 182 |
return 0;
|
| 183 |
}
|
| 184 |
|
| 185 |
extern unsigned int gqlist_get_length(void)
|
| 186 |
{
|
| 187 |
return list_get_length(gqlist_head);
|
| 188 |
}
|
| 189 |
|
| 190 |
extern t_gq * gqlist_find_game(char const * gamename)
|
| 191 |
{
|
| 192 |
t_gq * gq;
|
| 193 |
|
| 194 |
BEGIN_LIST_TRAVERSE_DATA(gqlist_head,gq)
|
| 195 |
{
|
| 196 |
if (!strcasecmp(gq->gamename,gamename)) return gq;
|
| 197 |
}
|
| 198 |
END_LIST_TRAVERSE_DATA()
|
| 199 |
return NULL;
|
| 200 |
}
|