Accessory sorting due to price.
[scorched3d.git] / src / client / dialogs / ConnectDialog.cpp
bloba6ffc1745904de47b04e7f4de18569fca81289e7
1 ////////////////////////////////////////////////////////////////////////////////
2 // Scorched3D (c) 2000-2009
3 //
4 // This file is part of Scorched3D.
5 //
6 // Scorched3D is free software; you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation; either version 2 of the License, or
9 // (at your option) any later version.
11 // Scorched3D is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
16 // You should have received a copy of the GNU General Public License
17 // along with Scorched3D; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 ////////////////////////////////////////////////////////////////////////////////
21 #include <dialogs/ConnectDialog.h>
22 #include <dialogs/PlayerDialog.h>
23 #include <dialogs/MsgBoxDialog.h>
24 #include <dialogs/ProgressDialog.h>
25 #include <client/ScorchedClient.h>
26 #include <client/ClientParams.h>
27 #include <client/ClientState.h>
28 #include <coms/ComsMessageSender.h>
29 #include <coms/ComsConnectMessage.h>
30 #include <net/NetInterface.h>
31 #include <common/Logger.h>
33 ConnectDialog *ConnectDialog::instance_ = 0;
35 ConnectDialog *ConnectDialog::instance()
37 if (!instance_)
39 instance_ = new ConnectDialog;
41 return instance_;
44 ConnectDialog::ConnectDialog() :
45 GLWWindow("Connect", -100.0f, 10.0f, 20.0f, 20.0f, eNoDraw | eNoTitle,
46 "Connection dialog"),
47 connectionState_(eWaiting),
48 tryCount_(0), lastTime_(0), idStore_(0)
50 connectionState_ = eWaiting;
51 tryCount_ = 0;
52 lastTime_ = 0;
55 ConnectDialog::~ConnectDialog()
59 UniqueIdStore &ConnectDialog::getIdStore()
61 if (!idStore_)
63 idStore_ = new UniqueIdStore();
64 // Get the unique id
65 if (!idStore_->loadStore())
67 Logger::log("Failed to load id store");
70 return *idStore_;
73 void ConnectDialog::windowInit(const unsigned state)
78 void ConnectDialog::simulate(float frameTime)
80 time_t currentTime = time(0);
81 if (connectionState_ == eWaiting)
83 if (currentTime - lastTime_ > 3)
85 ScorchedClient::instance()->getNetInterface().stop();
87 if (tryCount_<3)
89 tryCount_++;
90 lastTime_ = currentTime;
91 tryConnection();
93 else
95 connectionState_ = eFinished;
97 LangString msg = LANG_RESOURCE_2("FAILED_TO_CONNECT_TIMEOUT",
98 "Failed to connect to server \"{0}:{1}\", timeout.",
99 host_,
100 port_);
101 MsgBoxDialog::instance()->show(msg);
103 ScorchedClient::instance()->getGameState().stimulate(
104 ClientState::StimOptions);
110 void ConnectDialog::tryConnection()
112 const char *serverName =
113 (ClientParams::instance()->getConnect()[0]?
114 ClientParams::instance()->getConnect():
115 "Localhost");
117 host_ = serverName;
118 port_ = S3D::ScorchedPort;
120 char *colon = strchr((char *)serverName, ':');
121 if (colon)
123 char *stop;
124 *colon = '\0';
125 colon++;
126 port_ = strtol(colon, &stop, 10);
127 host_ = serverName;
128 colon--;
129 *colon = ':';
132 ProgressDialog::instance()->progressChange(
133 LANG_RESOURCE_3("CONNECTING_TO",
134 "Connecting to \"{0}:{1}\" ({3})....",
135 host_,
136 S3D::formatStringBuffer("%i", port_),
137 S3D::formatStringBuffer("%i", tryCount_)), 0);
139 connectionState_ = eTryingConnection;
140 if (ClientParams::instance()->getConnectedToServer())
142 // Do in a thread so connect can block if it wants!
143 SDL_CreateThread(ConnectDialog::tryRemoteConnection, 0);
145 else
147 // Or connect localy
148 tryLocalConnection();
152 int ConnectDialog::tryRemoteConnection(void *)
154 char *host = (char *) instance_->host_.c_str();
155 int port = instance_->port_;
157 // Try to connect to the server
158 ScorchedClient::instance()->getNetInterface().connect(host, port);
160 // Wait for result
161 instance_->connectionState_ = eWaiting;
162 return 0;
165 void ConnectDialog::tryLocalConnection()
167 // Try to connect localy
168 ScorchedClient::instance()->getNetInterface().connect("Local", 0);
170 // Wait for result
171 connectionState_ = eWaiting;;
174 void ConnectDialog::connected()
176 ProgressDialog::instance()->progressChange(LANG_RESOURCE("CONNECTED", "Connected"), 100);
178 // Wait for the coms to start
179 for (int i=0; i<10 && !ScorchedClient::instance()->getNetInterface().started(); i++)
181 SDL_Delay(500);
184 // If we connected then send our details to the server
185 ComsConnectMessage connectMessage;
186 connectMessage.setVersion(S3D::ScorchedVersion.c_str());
187 connectMessage.setProtocolVersion(S3D::ScorchedProtocolVersion.c_str());
189 if (!ComsMessageSender::sendToServer(connectMessage))
191 ScorchedClient::instance()->getNetInterface().stop();
193 LangString msg = LANG_RESOURCE_2("FAILED_TO_CONNECT_SEND",
194 "Failed to connect to server \"{0}:{1}\", send failed.",
195 host_,
196 port_);
197 MsgBoxDialog::instance()->show(msg);
199 ScorchedClient::instance()->getNetInterface().stop();
200 ScorchedClient::instance()->getGameState().stimulate(
201 ClientState::StimOptions);
204 connectionState_ = eFinished;