Changeset View
Changeset View
Standalone View
Standalone View
source/lobby/XmppClient.cpp
Show All 21 Lines | |||||
#ifdef WIN32 | #ifdef WIN32 | ||||
# include <winsock2.h> | # include <winsock2.h> | ||||
#endif | #endif | ||||
#include "i18n/L10n.h" | #include "i18n/L10n.h" | ||||
#include "lib/external_libraries/enet.h" | #include "lib/external_libraries/enet.h" | ||||
#include "lib/utf8.h" | #include "lib/utf8.h" | ||||
#include "lobby/LobbyScriptConversions.cpp" | |||||
#include "network/NetServer.h" | #include "network/NetServer.h" | ||||
#include "network/StunClient.h" | #include "network/StunClient.h" | ||||
#include "ps/CLogger.h" | #include "ps/CLogger.h" | ||||
#include "ps/ConfigDB.h" | #include "ps/ConfigDB.h" | ||||
#include "ps/Pyrogenesis.h" | #include "ps/Pyrogenesis.h" | ||||
#include "scriptinterface/ScriptInterface.h" | #include "scriptinterface/ScriptInterface.h" | ||||
#include <gloox/gloox.h> | #include <gloox/gloox.h> | ||||
▲ Show 20 Lines • Show All 454 Lines • ▼ Show 20 Lines | |||||
* Requests from GUI * | * Requests from GUI * | ||||
*****************************************************/ | *****************************************************/ | ||||
/** | /** | ||||
* Handle requests from the GUI for the list of players. | * Handle requests from the GUI for the list of players. | ||||
* | * | ||||
* @return A JS array containing all known players and their presences | * @return A JS array containing all known players and their presences | ||||
*/ | */ | ||||
void XmppClient::GUIGetPlayerList(const ScriptInterface& scriptInterface, JS::MutableHandleValue ret) | JS::Value XmppClient::GUIGetPlayerList(const ScriptInterface& scriptInterface) | ||||
{ | { | ||||
JSContext* cx = scriptInterface.GetContext(); | JSContext* cx = scriptInterface.GetContext(); | ||||
JSAutoRequest rq(cx); | JSAutoRequest rq(cx); | ||||
scriptInterface.Eval("([])", ret); | |||||
// Convert the internal data structure to a Javascript object. | // Convert the internal data structure to a Javascript object. | ||||
JS::RootedObject playerList(cx, JS_NewArrayObject(cx, 0)); | |||||
int i = 0; | |||||
vladislavbelov: `u32`. | |||||
for (const std::pair<std::string, std::vector<std::string> >& p : m_PlayerMap) | for (const std::pair<std::string, std::vector<std::string> >& p : m_PlayerMap) | ||||
{ | { | ||||
JS::RootedValue player(cx); | JS::RootedValue player(cx, JS::ObjectValue(*JS_NewObject(cx, nullptr))); | ||||
scriptInterface.Eval("({})", &player); | |||||
scriptInterface.SetProperty(player, "name", wstring_from_utf8(p.first)); | scriptInterface.SetProperty(player, "name", wstring_from_utf8(p.first)); | ||||
scriptInterface.SetProperty(player, "presence", wstring_from_utf8(p.second[0])); | scriptInterface.SetProperty(player, "presence", wstring_from_utf8(p.second[0])); | ||||
scriptInterface.SetProperty(player, "rating", wstring_from_utf8(p.second[1])); | scriptInterface.SetProperty(player, "rating", wstring_from_utf8(p.second[1])); | ||||
scriptInterface.SetProperty(player, "role", wstring_from_utf8(p.second[2])); | scriptInterface.SetProperty(player, "role", wstring_from_utf8(p.second[2])); | ||||
scriptInterface.CallFunctionVoid(ret, "push", player); | JS_SetElement(cx, playerList, i++, player); | ||||
} | } | ||||
return JS::ObjectValue(*playerList); | |||||
} | } | ||||
/** | JS::Value XmppClient::GUIGetGameList(const ScriptInterface& scriptInterface) | ||||
* Handle requests from the GUI for the list of all active games. | |||||
* | |||||
* @return A JS array containing all known games | |||||
*/ | |||||
void XmppClient::GUIGetGameList(const ScriptInterface& scriptInterface, JS::MutableHandleValue ret) | |||||
{ | { | ||||
JSContext* cx = scriptInterface.GetContext(); | JSContext* cx = scriptInterface.GetContext(); | ||||
JSAutoRequest rq(cx); | JSAutoRequest rq(cx); | ||||
JS::RootedValue value(cx); | |||||
scriptInterface.Eval("([])", ret); | ScriptInterface::ToJSVal(cx, &value, m_GameList); | ||||
const char* stats[] = { "name", "ip", "port", "stunIP", "stunPort", "hostUsername", "state", | return value; | ||||
"nbp", "maxnbp", "players", "mapName", "niceMapName", "mapSize", "mapType", | |||||
"victoryCondition", "startTime", "mods" }; | |||||
for(const glooxwrapper::Tag* const& t : m_GameList) | |||||
{ | |||||
JS::RootedValue game(cx); | |||||
scriptInterface.Eval("({})", &game); | |||||
for (size_t i = 0; i < ARRAY_SIZE(stats); ++i) | |||||
scriptInterface.SetProperty(game, stats[i], wstring_from_utf8(t->findAttribute(stats[i]).to_string())); | |||||
scriptInterface.CallFunctionVoid(ret, "push", game); | |||||
} | |||||
} | } | ||||
/** | JS::Value XmppClient::GUIGetBoardList(const ScriptInterface& scriptInterface) | ||||
* Handle requests from the GUI for leaderboard data. | |||||
* | |||||
* @return A JS array containing all known leaderboard data | |||||
*/ | |||||
void XmppClient::GUIGetBoardList(const ScriptInterface& scriptInterface, JS::MutableHandleValue ret) | |||||
{ | { | ||||
JSContext* cx = scriptInterface.GetContext(); | JSContext* cx = scriptInterface.GetContext(); | ||||
JSAutoRequest rq(cx); | JSAutoRequest rq(cx); | ||||
JS::RootedValue value(cx); | |||||
scriptInterface.Eval("([])", ret); | ScriptInterface::ToJSVal(cx, &value, m_BoardList); | ||||
const char* attributes[] = { "name", "rank", "rating" }; | return value; | ||||
for(const glooxwrapper::Tag* const& t : m_BoardList) | |||||
{ | |||||
JS::RootedValue board(cx); | |||||
scriptInterface.Eval("({})", &board); | |||||
for (size_t i = 0; i < ARRAY_SIZE(attributes); ++i) | |||||
scriptInterface.SetProperty(board, attributes[i], wstring_from_utf8(t->findAttribute(attributes[i]).to_string())); | |||||
scriptInterface.CallFunctionVoid(ret, "push", board); | |||||
} | |||||
} | } | ||||
/** | JS::Value XmppClient::GUIGetProfile(const ScriptInterface& scriptInterface) | ||||
* Handle requests from the GUI for profile data. | |||||
* | |||||
* @return A JS array containing the specific user's profile data | |||||
*/ | |||||
void XmppClient::GUIGetProfile(const ScriptInterface& scriptInterface, JS::MutableHandleValue ret) | |||||
{ | { | ||||
JSContext* cx = scriptInterface.GetContext(); | JSContext* cx = scriptInterface.GetContext(); | ||||
JSAutoRequest rq(cx); | JSAutoRequest rq(cx); | ||||
JS::RootedValue value(cx); | |||||
scriptInterface.Eval("([])", ret); | ScriptInterface::ToJSVal(cx, &value, m_Profile); | ||||
const char* stats[] = { "player", "rating", "totalGamesPlayed", "highestRating", "wins", "losses", "rank" }; | return value; | ||||
Done Inline ActionsNotice that the entire function could be removed if we would allow ourselves to bind JSInterface_ to XmppClient class instead of the IXmppClient interface. It would mean that people who want to implement IXmppClient with a different backend to gloox / glooxwrapper would have to provide their own interface as well. The functions would then be registered in the implementation class of the interface class, so that would be easy to forget or get out of sync as it would be removed from the interface IXmppClass. Shame, silly proxy getter. elexis: Notice that the entire function could be removed if we would allow ourselves to bind… | |||||
for (const glooxwrapper::Tag* const& t : m_Profile) | |||||
{ | |||||
JS::RootedValue profile(cx); | |||||
scriptInterface.Eval("({})", &profile); | |||||
for (size_t i = 0; i < ARRAY_SIZE(stats); ++i) | |||||
scriptInterface.SetProperty(profile, stats[i], wstring_from_utf8(t->findAttribute(stats[i]).to_string())); | |||||
scriptInterface.CallFunctionVoid(ret, "push", profile); | |||||
} | |||||
} | } | ||||
/***************************************************** | /***************************************************** | ||||
* Message interfaces * | * Message interfaces * | ||||
*****************************************************/ | *****************************************************/ | ||||
void XmppClient::CreateGUIMessage( | void XmppClient::CreateGUIMessage( | ||||
const std::string& type, | const std::string& type, | ||||
▲ Show 20 Lines • Show All 637 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator
u32.