Skip to content

Commit

Permalink
Add a list of servers to the "Multiplayer" tab
Browse files Browse the repository at this point in the history
If USE_CURL is set, it also downloads a list from a remote server.
The url of this list is configurable in minetest.conf using the setting "serverlist_url"
The local list of favorite servers is saved in client/serverlist/filename
filename is also configureable using the setting "serverlist_file"
  • Loading branch information
Jeija authored and celeron55 committed Jan 21, 2013
1 parent c691d61 commit 6f93c01
Show file tree
Hide file tree
Showing 9 changed files with 431 additions and 16 deletions.
2 changes: 2 additions & 0 deletions client/serverlist/.gitignore
@@ -0,0 +1,2 @@
*
!.gitignore
5 changes: 5 additions & 0 deletions minetest.conf.example
Expand Up @@ -158,6 +158,11 @@
# and only for clients compiled with cURL
#media_fetch_threads = 8

# Url to the server list displayed in the Multiplayer Tab
#serverlist_url = servers.minetest.ru/server.list
# File in client/serverlist/ that contains your favorite servers displayed in the Multiplayer Tab
#serverlist_file = favoriteservers.txt

#
# Server stuff
#
Expand Down
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Expand Up @@ -280,6 +280,7 @@ set(minetest_SRCS
filecache.cpp
tile.cpp
shader.cpp
serverlist.cpp
game.cpp
main.cpp
)
Expand Down
3 changes: 3 additions & 0 deletions src/defaultsettings.cpp
Expand Up @@ -128,6 +128,9 @@ void set_default_settings(Settings *settings)

settings->setDefault("media_fetch_threads", "8");

settings->setDefault("serverlist_url", "servers.minetest.ru/server.list");
settings->setDefault("serverlist_file", "favoriteservers.txt");

// Server stuff
// "map-dir" doesn't exist by default.
settings->setDefault("default_game", "minetest");
Expand Down
177 changes: 164 additions & 13 deletions src/guiMainMenu.cpp
Expand Up @@ -114,6 +114,9 @@ enum
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
Expand Down Expand Up @@ -361,22 +364,22 @@ void GUIMainMenu::regenerateGui(v2u32 screensize)
// 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"")
Environment->setFocus(e);
}
{
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);
Expand All @@ -385,17 +388,29 @@ void GUIMainMenu::regenerateGui(v2u32 screensize)

}
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);
Expand All @@ -404,18 +419,43 @@ void GUIMainMenu::regenerateGui(v2u32 screensize)
}
{
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");
}
Expand Down Expand Up @@ -868,6 +908,12 @@ void GUIMainMenu::readInput(MainMenuData *dst)
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()
Expand Down Expand Up @@ -912,14 +958,20 @@ bool GUIMainMenu::OnEvent(const SEvent& event)
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();
Expand Down Expand Up @@ -987,6 +1039,45 @@ bool GUIMainMenu::OnEvent(const SEvent& event)
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)
Expand All @@ -1009,6 +1100,14 @@ bool GUIMainMenu::OnEvent(const SEvent& event)
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;
}
}
}
}
Expand Down Expand Up @@ -1053,3 +1152,55 @@ void GUIMainMenu::displayMessageMenu(std::wstring msg)
{
(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;
}
16 changes: 14 additions & 2 deletions src/guiMainMenu.h
Expand Up @@ -25,6 +25,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include <string>
#include <list>
#include "subgame.h"
#include "serverlist.h"

class IGameCallback;

struct MainMenuData
Expand All @@ -33,6 +35,8 @@ struct MainMenuData
// Generic
int selected_tab;
// Client options
std::string servername;
std::string serverdescription;
std::wstring address;
std::wstring port;
std::wstring name;
Expand All @@ -58,8 +62,11 @@ struct MainMenuData
std::string create_world_gameid;
bool only_refresh;

bool serverlist_show_available; // if false show local favorites only

std::vector<WorldSpec> worlds;
std::vector<SubgameSpec> games;
std::vector<ServerListSpec> servers;

MainMenuData():
// Generic
Expand All @@ -73,7 +80,9 @@ struct MainMenuData
selected_world(0),
simple_singleplayer_mode(false),
// Actions
only_refresh(false)
only_refresh(false),

serverlist_show_available(false)
{}
};

Expand Down Expand Up @@ -110,12 +119,15 @@ class GUIMainMenu : public GUIModalMenu
gui::IGUIElement* parent;
s32 id;
IMenuManager *menumgr;

bool m_is_regenerating;
v2s32 m_topleft_client;
v2s32 m_size_client;
v2s32 m_topleft_server;
v2s32 m_size_server;
void updateGuiServerList();
void serverListOnSelected();
ServerListSpec getServerListSpec(std::string address, std::string port);
};

#endif
Expand Down
12 changes: 11 additions & 1 deletion src/main.cpp
Expand Up @@ -71,6 +71,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "util/string.h"
#include "subgame.h"
#include "quicktune.h"
#include "serverlist.h"

/*
Settings.
Expand Down Expand Up @@ -1581,7 +1582,7 @@ int main(int argc, char *argv[])
if(menudata.selected_world != -1)
g_settings->set("selected_world_path",
worldspecs[menudata.selected_world].path);

// Break out of menu-game loop to shut down cleanly
if(device->run() == false || kill == true)
break;
Expand All @@ -1598,6 +1599,15 @@ int main(int argc, char *argv[])
current_address = "";
current_port = 30011;
}
else if (address != "")
{
ServerListSpec server;
server.name = menudata.servername;
server.address = wide_to_narrow(menudata.address);
server.port = wide_to_narrow(menudata.port);
server.description = menudata.serverdescription;
ServerList::insert(server);
}

// Set world path to selected one
if(menudata.selected_world != -1){
Expand Down

0 comments on commit 6f93c01

Please sign in to comment.