[7565] | 1 | /* |
---|
| 2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
| 3 | * > www.orxonox.net < |
---|
| 4 | * |
---|
| 5 | * |
---|
| 6 | * License notice: |
---|
| 7 | * |
---|
| 8 | * This program is free software; you can redistribute it and/or |
---|
| 9 | * modify it under the terms of the GNU General Public License |
---|
| 10 | * as published by the Free Software Foundation; either version 2 |
---|
| 11 | * of the License, or (at your option) any later version. |
---|
| 12 | * |
---|
| 13 | * This program is distributed in the hope that it will be useful, |
---|
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 16 | * GNU General Public License for more details. |
---|
| 17 | * |
---|
| 18 | * You should have received a copy of the GNU General Public License |
---|
| 19 | * along with this program; if not, write to the Free Software |
---|
| 20 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
---|
| 21 | * |
---|
| 22 | * Author: |
---|
| 23 | * Sandro 'smerkli' Merkli |
---|
| 24 | * Co-authors: |
---|
| 25 | * ... |
---|
| 26 | * |
---|
| 27 | */ |
---|
| 28 | |
---|
[7589] | 29 | #include "ServerList.h" |
---|
[7565] | 30 | |
---|
| 31 | namespace orxonox |
---|
[10622] | 32 | { |
---|
[7565] | 33 | ServerList::ServerList() |
---|
| 34 | { /* create a new list */ } |
---|
| 35 | |
---|
| 36 | ServerList::~ServerList() |
---|
| 37 | { /* delete the list */ |
---|
[7589] | 38 | serverlist.clear(); |
---|
[7565] | 39 | } |
---|
| 40 | |
---|
[10622] | 41 | int |
---|
[8937] | 42 | ServerList::addServer( packet::ServerInformation toadd, |
---|
| 43 | ENetPeer *peer ) |
---|
[10622] | 44 | { |
---|
[8937] | 45 | ServerListElem toAdd; |
---|
| 46 | toAdd.ServerInfo = toadd; |
---|
| 47 | toAdd.peer = peer; |
---|
| 48 | |
---|
[10622] | 49 | this->serverlist.push_back( toAdd ); |
---|
[7565] | 50 | return 0; |
---|
| 51 | } |
---|
| 52 | |
---|
[10622] | 53 | bool |
---|
[7589] | 54 | ServerList::delServerByName( std::string name ) |
---|
[10622] | 55 | { |
---|
[7565] | 56 | /* get an iterator */ |
---|
[8937] | 57 | std::list<ServerListElem>::iterator i; |
---|
[7565] | 58 | |
---|
| 59 | /* loop through list elements */ |
---|
[10622] | 60 | for( i = serverlist.begin(); i != serverlist.end(); ++i ) |
---|
[8937] | 61 | if( (*i).ServerInfo.getServerName() == name ) |
---|
[7565] | 62 | { /* found this name, remove and quit */ |
---|
[7657] | 63 | this->serverlist.erase( i ); |
---|
[7565] | 64 | return true; |
---|
| 65 | } |
---|
| 66 | return false; |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | bool ServerList::delServerByAddress( std::string address ) |
---|
[10622] | 70 | { |
---|
[7565] | 71 | /* get an iterator */ |
---|
[8937] | 72 | std::list<ServerListElem>::iterator i; |
---|
[7565] | 73 | |
---|
| 74 | /* loop through list elements */ |
---|
[10622] | 75 | for( i = serverlist.begin(); i != serverlist.end(); ++i ) |
---|
[8937] | 76 | if( (*i).ServerInfo.getServerIP() == address ) |
---|
[7565] | 77 | { /* found this name, remove and quit */ |
---|
[7657] | 78 | this->serverlist.erase( i ); |
---|
[7565] | 79 | return true; |
---|
| 80 | } |
---|
| 81 | return false; |
---|
| 82 | } |
---|
| 83 | |
---|
[8937] | 84 | /* SEARCHING */ |
---|
| 85 | ServerListSearchResult |
---|
| 86 | ServerList::findServerByAddress( std::string address ) |
---|
| 87 | { |
---|
| 88 | /* get an iterator */ |
---|
| 89 | std::list<ServerListElem>::iterator i; |
---|
[7565] | 90 | |
---|
[8937] | 91 | /* loop through list elements */ |
---|
[10622] | 92 | for( i = serverlist.begin(); i != serverlist.end(); ++i ) |
---|
[8937] | 93 | if( (*i).ServerInfo.getServerIP() == address ) |
---|
| 94 | { /* found the target, return it */ |
---|
| 95 | ServerListSearchResult res = { (*i), true }; |
---|
| 96 | return res; |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | /* no success */ |
---|
| 100 | ServerListSearchResult res = { (*i), false }; |
---|
| 101 | return res; |
---|
| 102 | } |
---|
| 103 | |
---|
| 104 | ServerListSearchResult |
---|
| 105 | ServerList::findServerByName( std::string name ) |
---|
| 106 | { |
---|
| 107 | /* get an iterator */ |
---|
| 108 | std::list<ServerListElem>::iterator i; |
---|
| 109 | |
---|
| 110 | /* iterate, return when name found */ |
---|
| 111 | /* loop through list elements */ |
---|
[10622] | 112 | for( i = serverlist.begin(); i != serverlist.end(); ++i ) |
---|
[8937] | 113 | if( (*i).ServerInfo.getServerName() == name ) |
---|
[10622] | 114 | { |
---|
[8937] | 115 | ServerListSearchResult res = { (*i), true }; |
---|
| 116 | return res; |
---|
| 117 | } |
---|
| 118 | |
---|
| 119 | /* no luck, return a struct that tells the caller so */ |
---|
| 120 | ServerListSearchResult res = { (*i), false }; |
---|
| 121 | return res; |
---|
| 122 | } |
---|
| 123 | |
---|
| 124 | /* SORTING */ |
---|
[7565] | 125 | /* sort by name */ |
---|
[10622] | 126 | bool sub_compare_names( ServerListElem no1, |
---|
[8937] | 127 | ServerListElem no2 ) |
---|
| 128 | { return no1.ServerInfo.getServerName() > no2.ServerInfo.getServerName(); } |
---|
[7565] | 129 | |
---|
| 130 | void ServerList::sortByName() |
---|
[10622] | 131 | { |
---|
| 132 | this->serverlist.sort( sub_compare_names ); |
---|
[7565] | 133 | } |
---|
[10622] | 134 | |
---|
[7565] | 135 | /* sort by ping */ |
---|
[10622] | 136 | bool sub_compare_pings( ServerListElem no1, |
---|
[8937] | 137 | ServerListElem no2 ) |
---|
[10622] | 138 | { |
---|
| 139 | return no1.ServerInfo.getServerRTT() > no2.ServerInfo.getServerRTT(); |
---|
[7589] | 140 | } |
---|
[7565] | 141 | |
---|
| 142 | void ServerList::sortByPing() |
---|
| 143 | { |
---|
[10622] | 144 | this->serverlist.sort( sub_compare_pings ); |
---|
[7565] | 145 | } |
---|
| 146 | |
---|
[10622] | 147 | bool ServerList::setNameByAddress( std::string address, std::string name ){ |
---|
| 148 | /* get an iterator */ |
---|
| 149 | std::list<ServerListElem>::iterator i; |
---|
| 150 | |
---|
| 151 | /* loop through list elements */ |
---|
| 152 | for( i = serverlist.begin(); i != serverlist.end(); ++i ) |
---|
| 153 | if( (*i).ServerInfo.getServerIP() == address ) |
---|
| 154 | { /* found this adress, rename and quit */ |
---|
| 155 | (*i).ServerInfo.setServerName( name ); |
---|
| 156 | return true; |
---|
| 157 | } |
---|
| 158 | return false; |
---|
| 159 | }; |
---|
| 160 | |
---|
| 161 | bool ServerList::setClientsByAddress( std::string address, int clientNumber ){ |
---|
| 162 | /* get an iterator */ |
---|
| 163 | std::list<ServerListElem>::iterator i; |
---|
| 164 | |
---|
| 165 | /* loop through list elements */ |
---|
| 166 | for( i = serverlist.begin(); i != serverlist.end(); ++i ) |
---|
| 167 | if( (*i).ServerInfo.getServerIP() == address ) |
---|
| 168 | { /* found this adress, rename and quit */ |
---|
| 169 | (*i).ServerInfo.setClientNumber( clientNumber ); |
---|
| 170 | return true; |
---|
| 171 | } |
---|
| 172 | return false; |
---|
| 173 | }; |
---|
| 174 | |
---|
[7565] | 175 | } |
---|