GUI_ID_CONFIGURE_WORLD_BUTTON,
GUI_ID_WORLD_LISTBOX,
GUI_ID_TAB_CONTROL,
+ GUI_ID_SERVERLIST,
+ GUI_ID_SERVERLIST_TOGGLE,
+ GUI_ID_SERVERLIST_DELETE,
};
enum
// Nickname + password
{
core::rect<s32> rect(0, 0, 110, 20);
- rect += m_topleft_client + v2s32(35+30, 50+6);
+ rect += m_topleft_client + v2s32(m_size_client.X-60-100, 10+6);
Environment->addStaticText(wgettext("Name/Password"),
rect, false, true, this, -1);
}
changeCtype("C");
{
- core::rect<s32> rect(0, 0, 230, 30);
- rect += m_topleft_client + v2s32(160+30, 50);
+ core::rect<s32> rect(0, 0, 120, 30);
+ rect += m_topleft_client + v2s32(m_size_client.X-60-100, 50);
gui::IGUIElement *e =
Environment->addEditBox(m_data->name.c_str(), rect, true, this, GUI_ID_NAME_INPUT);
if(m_data->name == L"")
}
{
core::rect<s32> rect(0, 0, 120, 30);
- rect += m_topleft_client + v2s32(m_size_client.X-60-100, 50);
+ rect += m_topleft_client + v2s32(m_size_client.X-60-100, 90);
gui::IGUIEditBox *e =
Environment->addEditBox(L"", rect, true, this, 264);
e->setPasswordBox(true);
}
changeCtype("");
+ // Server List
+ {
+ core::rect<s32> rect(0, 0, 390, 160);
+ rect += m_topleft_client + v2s32(50, 10);
+ gui::IGUIListBox *e = Environment->addListBox(rect, this,
+ GUI_ID_SERVERLIST);
+ e->setDrawBackground(true);
+ if (m_data->serverlist_show_available == false)
+ m_data->servers = ServerList::getLocal();
+ updateGuiServerList();
+ e->setSelected(0);
+ }
// Address + port
{
core::rect<s32> rect(0, 0, 110, 20);
- rect += m_topleft_client + v2s32(35+30, 100+6);
+ rect += m_topleft_client + v2s32(50, m_size_client.Y-50-15+6);
Environment->addStaticText(wgettext("Address/Port"),
rect, false, true, this, -1);
}
changeCtype("C");
{
- core::rect<s32> rect(0, 0, 230, 30);
- rect += m_topleft_client + v2s32(160+30, 100);
+ core::rect<s32> rect(0, 0, 260, 30);
+ rect += m_topleft_client + v2s32(50, m_size_client.Y-25-15);
gui::IGUIElement *e =
Environment->addEditBox(m_data->address.c_str(), rect, true,
this, GUI_ID_ADDRESS_INPUT);
}
{
core::rect<s32> rect(0, 0, 120, 30);
- rect += m_topleft_client + v2s32(m_size_client.X-60-100, 100);
+ rect += m_topleft_client + v2s32(50+260+10, m_size_client.Y-25-15);
Environment->addEditBox(m_data->port.c_str(), rect, true,
this, GUI_ID_PORT_INPUT);
}
changeCtype("");
+ #if USE_CURL
+ // Toggle Serverlist (Favorites/Online)
+ {
+ core::rect<s32> rect(0, 0, 260, 30);
+ rect += m_topleft_client + v2s32(50,
+ 180);
+ gui::IGUIButton *e = Environment->addButton(rect, this, GUI_ID_SERVERLIST_TOGGLE,
+ wgettext("Show Public"));
+ e->setIsPushButton(true);
+ if (m_data->serverlist_show_available)
+ {
+ e->setText(wgettext("Show Favorites"));
+ e->setPressed();
+ }
+ }
+ #endif
+ // Delete Local Favorite
+ {
+ core::rect<s32> rect(0, 0, 120, 30);
+ rect += m_topleft_client + v2s32(50+260+10, 180);
+ gui::IGUIButton *e = Environment->addButton(rect, this, GUI_ID_SERVERLIST_DELETE,
+ wgettext("Delete"));
+ if (m_data->serverlist_show_available) // Hidden on Show-Online mode
+ e->setVisible(false);
+ }
// Start game button
{
- core::rect<s32> rect(0, 0, 180, 30);
- rect += m_topleft_client + v2s32(m_size_client.X-180-30,
- m_size_client.Y-30-15);
+ core::rect<s32> rect(0, 0, 120, 30);
+ rect += m_topleft_client + v2s32(m_size_client.X-130-30,
+ m_size_client.Y-25-15);
Environment->addButton(rect, this, GUI_ID_JOIN_GAME_BUTTON,
- wgettext("Start Game / Connect"));
+ wgettext("Connect"));
}
changeCtype("C");
}
if(e != NULL && e->getType() == gui::EGUIET_LIST_BOX)
dst->selected_world = ((gui::IGUIListBox*)e)->getSelected();
}
+ {
+ ServerListSpec server =
+ getServerListSpec(wide_to_narrow(dst->address), wide_to_narrow(dst->port));
+ dst->servername = server.name;
+ dst->serverdescription = server.description;
+ }
}
void GUIMainMenu::acceptInput()
regenerateGui(m_screensize_old);
return true;
}
+ if(event.GUIEvent.EventType==gui::EGET_LISTBOX_CHANGED && event.GUIEvent.Caller->getID() == GUI_ID_SERVERLIST)
+ {
+ serverListOnSelected();
+ return true;
+ }
if(event.GUIEvent.EventType==gui::EGET_BUTTON_CLICKED)
{
switch(event.GUIEvent.Caller->getID())
case GUI_ID_JOIN_GAME_BUTTON: {
MainMenuData cur;
readInput(&cur);
- if(cur.address == L"" && getTab() == TAB_MULTIPLAYER){
+ if (getTab() == TAB_MULTIPLAYER && cur.address == L"")
+ {
(new GUIMessageMenu(env, parent, -1, menumgr,
wgettext("Address required."))
)->drop();
menu->drop();
return true;
}
+ case GUI_ID_SERVERLIST_DELETE: {
+ gui::IGUIListBox *serverlist = (gui::IGUIListBox*)getElementFromId(GUI_ID_SERVERLIST);
+ u16 selected = ((gui::IGUIListBox*)serverlist)->getSelected();
+ if (selected == -1) return true;
+ ServerList::deleteEntry(m_data->servers[selected]);
+ m_data->servers = ServerList::getLocal();
+ updateGuiServerList();
+ if (selected > 0)
+ selected -= 1;
+ serverlist->setSelected(selected);
+ serverListOnSelected();
+ return true;
+ }
+ #if USE_CURL
+ case GUI_ID_SERVERLIST_TOGGLE: {
+ gui::IGUIElement *togglebutton = getElementFromId(GUI_ID_SERVERLIST_TOGGLE);
+ gui::IGUIElement *deletebutton = getElementFromId(GUI_ID_SERVERLIST_DELETE);
+ gui::IGUIListBox *serverlist = (gui::IGUIListBox*)getElementFromId(GUI_ID_SERVERLIST);
+ if (m_data->serverlist_show_available) // switch to favorite list
+ {
+ m_data->servers = ServerList::getLocal();
+ togglebutton->setText(wgettext("Show Public"));
+ deletebutton->setVisible(true);
+ updateGuiServerList();
+ serverlist->setSelected(0);
+ }
+ else // switch to online list
+ {
+ m_data->servers = ServerList::getOnline();
+ togglebutton->setText(wgettext("Show Favorites"));
+ deletebutton->setVisible(false);
+ updateGuiServerList();
+ serverlist->setSelected(0);
+ }
+ serverListOnSelected();
+
+ m_data->serverlist_show_available = !m_data->serverlist_show_available;
+ }
+ #endif
}
}
if(event.GUIEvent.EventType==gui::EGET_EDITBOX_ENTER)
m_data->address = L""; // Force local game
quitMenu();
return true;
+ case GUI_ID_SERVERLIST:
+ gui::IGUIListBox *serverlist = (gui::IGUIListBox*)getElementFromId(GUI_ID_SERVERLIST);
+ if (serverlist->getSelected() > -1)
+ {
+ acceptInput();
+ quitMenu();
+ return true;
+ }
}
}
}
{
(new GUIMessageMenu(env, parent, -1, menumgr, msg))->drop();
}
+
+void GUIMainMenu::updateGuiServerList()
+{
+ gui::IGUIListBox *serverlist = (gui::IGUIListBox *)getElementFromId(GUI_ID_SERVERLIST);
+ serverlist->clear();
+
+ for(std::vector<ServerListSpec>::iterator i = m_data->servers.begin();
+ i != m_data->servers.end(); i++)
+ {
+ std::string text;
+ if (i->name != "" && i->description != "")
+ text = i->name + " (" + i->description + ")";
+ else if (i->name !="")
+ text = i->name;
+ else
+ text = i->address + ":" + i->port;
+
+ serverlist->addItem(narrow_to_wide(text).c_str());
+ }
+}
+
+void GUIMainMenu::serverListOnSelected()
+{
+ if (!m_data->servers.empty())
+ {
+ gui::IGUIListBox *serverlist = (gui::IGUIListBox*)getElementFromId(GUI_ID_SERVERLIST);
+ u16 id = serverlist->getSelected();
+ if (id < 0) return;
+ ((gui::IGUIEditBox*)getElementFromId(GUI_ID_ADDRESS_INPUT))
+ ->setText(narrow_to_wide(m_data->servers[id].address).c_str());
+ ((gui::IGUIEditBox*)getElementFromId(GUI_ID_PORT_INPUT))
+ ->setText(narrow_to_wide(m_data->servers[id].port).c_str());
+ }
+}
+
+ServerListSpec GUIMainMenu::getServerListSpec(std::string address, std::string port)
+{
+ ServerListSpec server;
+ server.address = address;
+ server.port = port;
+ for(std::vector<ServerListSpec>::iterator i = m_data->servers.begin();
+ i != m_data->servers.end(); i++)
+ {
+ if (i->address == address && i->port == port)
+ {
+ server.description = i->description;
+ server.name = i->name;
+ break;
+ }
+ }
+ return server;
+}
--- /dev/null
+/*
+Minetest-c55
+Copyright (C) 2011 celeron55, Perttu Ahola <celeron55@gmail.com>
+
+This program is free software; you can redistribute it and/or modify
+it under the terms of the GNU Lesser General Public License as published by
+the Free Software Foundation; either version 2.1 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU Lesser General Public License for more details.
+
+You should have received a copy of the GNU Lesser General Public License along
+with this program; if not, write to the Free Software Foundation, Inc.,
+51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+*/
+
+#include <iostream>
+#include <sstream>
+#include <algorithm>
+
+#include "main.h" // for g_settings
+#include "settings.h"
+#include "serverlist.h"
+#include "filesys.h"
+#include "porting.h"
+#include "log.h"
+#if USE_CURL
+#include <curl/curl.h>
+#endif
+
+namespace ServerList
+{
+std::string getFilePath()
+{
+ std::string serverlist_file = g_settings->get("serverlist_file");
+
+ std::string rel_path = std::string("client") + DIR_DELIM
+ + "serverlist" + DIR_DELIM
+ + serverlist_file;
+ std::string path = porting::path_share + DIR_DELIM + rel_path;
+ return path;
+}
+
+std::vector<ServerListSpec> getLocal()
+{
+ std::string path = ServerList::getFilePath();
+ std::string liststring;
+ if(fs::PathExists(path))
+ {
+ std::ifstream istream(path.c_str(), std::ios::binary);
+ if(istream.is_open())
+ {
+ std::ostringstream ostream;
+ ostream << istream.rdbuf();
+ liststring = ostream.str();
+ istream.close();
+ }
+ }
+
+ return ServerList::deSerialize(liststring);
+}
+
+
+#if USE_CURL
+
+static size_t WriteCallback(void *contents, size_t size, size_t nmemb, void *userp)
+{
+ ((std::string*)userp)->append((char*)contents, size * nmemb);
+ return size * nmemb;
+}
+
+
+std::vector<ServerListSpec> getOnline()
+{
+ std::string liststring;
+ CURL *curl;
+
+ curl = curl_easy_init();
+ if (curl)
+ {
+ CURLcode res;
+
+ curl_easy_setopt(curl, CURLOPT_URL, g_settings->get("serverlist_url").c_str());
+ curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, ServerList::WriteCallback);
+ curl_easy_setopt(curl, CURLOPT_WRITEDATA, &liststring);
+
+ res = curl_easy_perform(curl);
+ if (res != CURLE_OK)
+ errorstream<<"Serverlist at url "<<g_settings->get("serverlist_url")<<" not found (internet connection?)"<<std::endl;
+ curl_easy_cleanup(curl);
+ }
+
+ return ServerList::deSerialize(liststring);
+}
+
+#endif
+
+/*
+ Delete a server fromt he local favorites list
+*/
+bool deleteEntry (ServerListSpec server)
+{
+ std::vector<ServerListSpec> serverlist = ServerList::getLocal();
+ for(unsigned i = 0; i < serverlist.size(); i++)
+ {
+ if (serverlist[i].address == server.address
+ && serverlist[i].port == server.port)
+ {
+ serverlist.erase(serverlist.begin() + i);
+ }
+ }
+
+ std::string path = ServerList::getFilePath();
+ std::ofstream stream (path.c_str());
+ if (stream.is_open())
+ {
+ stream<<ServerList::serialize(serverlist);
+ return true;
+ }
+ return false;
+}
+
+/*
+ Insert a server to the local favorites list
+*/
+bool insert (ServerListSpec server)
+{
+ // Remove duplicates
+ ServerList::deleteEntry(server);
+
+ std::vector<ServerListSpec> serverlist = ServerList::getLocal();
+
+ // Insert new server at the top of the list
+ serverlist.insert(serverlist.begin(), server);
+
+ std::string path = ServerList::getFilePath();
+ std::ofstream stream (path.c_str());
+ if (stream.is_open())
+ {
+ stream<<ServerList::serialize(serverlist);
+ }
+
+ return false;
+}
+
+std::vector<ServerListSpec> deSerialize(std::string liststring)
+{
+ std::vector<ServerListSpec> serverlist;
+ std::istringstream stream(liststring);
+ std::string line;
+ while (std::getline(stream, line))
+ {
+ std::transform(line.begin(), line.end(),line.begin(), ::toupper);
+ if (line == "[SERVER]")
+ {
+ ServerListSpec thisserver;
+ std::getline(stream, thisserver.name);
+ std::getline(stream, thisserver.address);
+ std::getline(stream, thisserver.port);
+ std::getline(stream, thisserver.description);
+ serverlist.push_back(thisserver);
+ }
+ }
+ return serverlist;
+}
+
+std::string serialize(std::vector<ServerListSpec> serverlist)
+{
+ std::string liststring;
+ for(std::vector<ServerListSpec>::iterator i = serverlist.begin(); i != serverlist.end(); i++)
+ {
+ liststring += "[server]\n";
+ liststring += i->name + "\n";
+ liststring += i->address + "\n";
+ liststring += i->port + "\n";
+ liststring += i->description + "\n";
+ liststring += "\n";
+ }
+ return liststring;
+}
+
+} //namespace ServerList