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