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 | * Fabian 'x3n' Landau |
---|
24 | * Co-authors: |
---|
25 | * ... |
---|
26 | * |
---|
27 | */ |
---|
28 | |
---|
29 | #include "OrxonoxStableHeaders.h" |
---|
30 | #include "Gametype.h" |
---|
31 | |
---|
32 | #include "core/CoreIncludes.h" |
---|
33 | #include "core/ConsoleCommand.h" |
---|
34 | #include "objects/infos/PlayerInfo.h" |
---|
35 | |
---|
36 | #include "network/Host.h" |
---|
37 | |
---|
38 | namespace orxonox |
---|
39 | { |
---|
40 | SetConsoleCommand(Gametype, listPlayers, true); |
---|
41 | |
---|
42 | CreateUnloadableFactory(Gametype); |
---|
43 | |
---|
44 | Gametype::Gametype() |
---|
45 | { |
---|
46 | RegisterObject(Gametype); |
---|
47 | |
---|
48 | this->getConnectedClients(); |
---|
49 | |
---|
50 | COUT(0) << "created Gametype" << std::endl; |
---|
51 | } |
---|
52 | |
---|
53 | Gametype* Gametype::getCurrentGametype() |
---|
54 | { |
---|
55 | for (ObjectList<Gametype>::iterator it = ObjectList<Gametype>::begin(); it != ObjectList<Gametype>::end(); ++it) |
---|
56 | return (*it); |
---|
57 | |
---|
58 | return 0; |
---|
59 | } |
---|
60 | |
---|
61 | PlayerInfo* Gametype::getClient(unsigned int clientID) |
---|
62 | { |
---|
63 | Gametype* gametype = Gametype::getCurrentGametype(); |
---|
64 | if (gametype) |
---|
65 | { |
---|
66 | std::map<unsigned int, PlayerInfo*>::const_iterator it = gametype->clients_.find(clientID); |
---|
67 | if (it != gametype->clients_.end()) |
---|
68 | return it->second; |
---|
69 | } |
---|
70 | else |
---|
71 | { |
---|
72 | for (ObjectList<PlayerInfo>::iterator it = ObjectList<PlayerInfo>::begin(); it != ObjectList<PlayerInfo>::end(); ++it) |
---|
73 | if (it->getClientID() == clientID) |
---|
74 | return (*it); |
---|
75 | } |
---|
76 | return 0; |
---|
77 | } |
---|
78 | |
---|
79 | void Gametype::listPlayers() |
---|
80 | { |
---|
81 | Gametype* gametype = Gametype::getCurrentGametype(); |
---|
82 | |
---|
83 | if (gametype) |
---|
84 | { |
---|
85 | for (std::set<PlayerInfo*>::const_iterator it = gametype->players_.begin(); it != gametype->players_.end(); ++it) |
---|
86 | COUT(0) << "ID: " << (*it)->getClientID() << ", Name: " << (*it)->getName() << std::endl; |
---|
87 | } |
---|
88 | else |
---|
89 | { |
---|
90 | for (ObjectList<PlayerInfo>::iterator it = ObjectList<PlayerInfo>::begin(); it != ObjectList<PlayerInfo>::end(); ++it) |
---|
91 | COUT(0) << "ID: " << (*it)->getClientID() << ", Name: " << (*it)->getName() << std::endl; |
---|
92 | } |
---|
93 | } |
---|
94 | |
---|
95 | void Gametype::clientConnected(unsigned int clientID) |
---|
96 | { |
---|
97 | COUT(0) << "client connected" << std::endl; |
---|
98 | |
---|
99 | // create new PlayerInfo instance |
---|
100 | PlayerInfo* player = new PlayerInfo(); |
---|
101 | player->setClientID(clientID); |
---|
102 | |
---|
103 | // add to clients-map |
---|
104 | assert(!this->clients_[clientID]); |
---|
105 | this->clients_[clientID] = player; |
---|
106 | } |
---|
107 | |
---|
108 | void Gametype::clientDisconnected(unsigned int clientID) |
---|
109 | { |
---|
110 | COUT(0) << "client disconnected" << std::endl; |
---|
111 | |
---|
112 | // remove from clients-map |
---|
113 | PlayerInfo* player = this->clients_[clientID]; |
---|
114 | this->clients_.erase(clientID); |
---|
115 | |
---|
116 | // delete PlayerInfo instance |
---|
117 | delete player; |
---|
118 | } |
---|
119 | |
---|
120 | void Gametype::addPlayer(PlayerInfo* player) |
---|
121 | { |
---|
122 | this->players_.insert(player); |
---|
123 | this->playerJoined(player); |
---|
124 | } |
---|
125 | |
---|
126 | void Gametype::removePlayer(PlayerInfo* player) |
---|
127 | { |
---|
128 | this->players_.erase(player); |
---|
129 | this->playerLeft(player); |
---|
130 | } |
---|
131 | |
---|
132 | void Gametype::playerJoined(PlayerInfo* player) |
---|
133 | { |
---|
134 | std::string message = player->getName() + " entered the game"; |
---|
135 | COUT(0) << message << std::endl; |
---|
136 | network::Host::Broadcast(message); |
---|
137 | } |
---|
138 | |
---|
139 | void Gametype::playerLeft(PlayerInfo* player) |
---|
140 | { |
---|
141 | std::string message = player->getName() + " left the game"; |
---|
142 | COUT(0) << message << std::endl; |
---|
143 | network::Host::Broadcast(message); |
---|
144 | } |
---|
145 | |
---|
146 | void Gametype::playerChangedName(PlayerInfo* player) |
---|
147 | { |
---|
148 | if (this->players_.find(player) != this->players_.end()) |
---|
149 | { |
---|
150 | if (player->getName() != player->getOldName()) |
---|
151 | { |
---|
152 | std::string message = player->getOldName() + " changed name to " + player->getName(); |
---|
153 | COUT(0) << message << std::endl; |
---|
154 | network::Host::Broadcast(message); |
---|
155 | } |
---|
156 | } |
---|
157 | } |
---|
158 | } |
---|