gamequeue.c
上传用户:tany51
上传日期:2013-06-12
资源大小:1397k
文件大小:5k
源码类别:

MySQL数据库

开发平台:

Visual C++

  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. #ifdef STDC_HEADERS
  21. # include <stdlib.h>
  22. #else
  23. # ifdef HAVE_MALLOC_H
  24. #  include <malloc.h>
  25. # endif
  26. #endif
  27. #ifdef HAVE_STRING_H
  28. # include <string.h>
  29. #else
  30. # ifdef HAVE_STRINGS_H
  31. #  include <strings.h>
  32. # endif
  33. # ifdef HAVE_MEMORY_H
  34. #  include <memory.h>
  35. # endif
  36. #endif
  37. #include "compat/strcasecmp.h"
  38. #include "connection.h"
  39. #include "gamequeue.h"
  40. #include "handle_d2cs.h"
  41. #include "common/packet.h"
  42. #include "common/list.h"
  43. #include "common/eventlog.h"
  44. #include "common/setup_after.h"
  45. static t_list * gqlist_head=NULL;
  46. static unsigned int gqlist_seqno=0;
  47. extern t_list * gqlist(void)
  48. {
  49. return gqlist_head;
  50. }
  51. extern int gqlist_create(void)
  52. {
  53. if (!(gqlist_head=list_create())) return -1;
  54. return 0;
  55. }
  56. extern int gqlist_destroy(void)
  57. {
  58. t_gq * gq;
  59. BEGIN_LIST_TRAVERSE_DATA(gqlist_head,gq)
  60. {
  61. gq_destroy(gq);
  62. }
  63. END_LIST_TRAVERSE_DATA()
  64. if (list_destroy(gqlist_head)<0) {
  65. eventlog(eventlog_level_error,__FUNCTION__,"error destroy game queue list");
  66. return -1;
  67. }
  68. gqlist_head=NULL;
  69. return 0;
  70. }
  71. extern t_gq * gq_create(unsigned int clientid, t_packet * packet, char const * gamename)
  72. {
  73. t_gq * gq;
  74. if (!(gq=malloc(sizeof(t_gq)))) return NULL;
  75. gq->seqno=++gqlist_seqno;
  76. gq->clientid=clientid;
  77. gq->packet=packet;
  78. strncpy(gq->gamename, gamename, MAX_GAMENAME_LEN);
  79. if (packet) packet_add_ref(packet);
  80. if (list_append_data(gqlist_head,gq)<0) {
  81. eventlog(eventlog_level_error,__FUNCTION__,"error add game queue to list");
  82. if (packet) packet_del_ref(packet);
  83. free(gq);
  84. return NULL;
  85. }
  86. return gq;
  87. }
  88. extern int gq_destroy(t_gq * gq)
  89. {
  90. ASSERT(gq,-1);
  91. if (list_remove_data(gqlist_head,gq)<0) {
  92. eventlog(eventlog_level_error,__FUNCTION__,"error remove game queue from list");
  93. return -1;
  94. }
  95. if (gq->packet) packet_del_ref(gq->packet);
  96. free(gq);
  97. return 0;
  98. }
  99. extern unsigned int gq_get_clientid(t_gq const * gq)
  100. {
  101. ASSERT(gq,0);
  102. return gq->clientid;
  103. }
  104. extern int gqlist_check_creategame(int number)
  105. {
  106. t_connection * c;
  107. t_gq * gq;
  108. int i=0;
  109. if (number <= 0) return -1;
  110. BEGIN_LIST_TRAVERSE_DATA(gqlist_head,gq)
  111. {
  112. c=d2cs_connlist_find_connection_by_sessionnum(gq->clientid);
  113. if (!c) {
  114. eventlog(eventlog_level_error,__FUNCTION__,"client %d not found (gamename: %s)",gq->clientid,gq->gamename);
  115. gq_destroy(gq);
  116. continue;
  117. } else if (!conn_get_gamequeue(c)) {
  118. eventlog(eventlog_level_error,__FUNCTION__,"got NULL game queue for client %s",d2cs_conn_get_account(c));
  119. gq_destroy(gq);
  120. continue;
  121. } else {
  122. eventlog(eventlog_level_info,__FUNCTION__,"try create game %s for account %s",gq->gamename,d2cs_conn_get_account(c));
  123. d2cs_handle_client_creategame(c,gq->packet);
  124. conn_set_gamequeue(c,NULL);
  125. gq_destroy(gq);
  126. i++;
  127. if(i >= number) break;
  128. }
  129. }
  130. END_LIST_TRAVERSE_DATA()
  131. return 0;
  132. }
  133. extern int gqlist_update_all_clients(void)
  134. {
  135. t_connection * c;
  136. t_gq * gq;
  137. unsigned int n;
  138. n=0;
  139. BEGIN_LIST_TRAVERSE_DATA(gqlist_head,gq)
  140. {
  141. c=d2cs_connlist_find_connection_by_sessionnum(gq->clientid);
  142. if (!c) {
  143. eventlog(eventlog_level_error,__FUNCTION__,"client %d not found (gamename: %s)",gq->clientid,gq->gamename);
  144. gq_destroy(gq);
  145. continue;
  146. } else {
  147. n++;
  148. eventlog(eventlog_level_debug,__FUNCTION__,"update client %s position to %d",d2cs_conn_get_account(c),n);
  149. d2cs_send_client_creategamewait(c,n);
  150. }
  151. }
  152. END_LIST_TRAVERSE_DATA()
  153. if (n) eventlog(eventlog_level_info,__FUNCTION__,"total %d game queues",n);
  154. return 0;
  155. }
  156. extern unsigned int gqlist_get_gq_position(t_gq * gq)
  157. {
  158. t_gq * tmp;
  159. unsigned int pos;
  160. pos=0;
  161. BEGIN_LIST_TRAVERSE_DATA(gqlist_head,tmp)
  162. {
  163. pos++;
  164. if (tmp==gq) return pos;
  165. }
  166. END_LIST_TRAVERSE_DATA()
  167. return 0;
  168. }
  169. extern unsigned int gqlist_get_length(void)
  170. {
  171. return list_get_length(gqlist_head);
  172. }
  173. extern t_gq * gqlist_find_game(char const * gamename)
  174. {
  175. t_gq * gq;
  176. BEGIN_LIST_TRAVERSE_DATA(gqlist_head,gq)
  177. {
  178. if (!strcasecmp(gq->gamename,gamename)) return gq;
  179. }
  180. END_LIST_TRAVERSE_DATA()
  181. return NULL;
  182. }