Changeset View
Changeset View
Standalone View
Standalone View
ps/trunk/source/lobby/XmppClient.cpp
Show First 20 Lines • Show All 497 Lines • ▼ Show 20 Lines | |||||
* | * | ||||
* @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) | void XmppClient::GUIGetPlayerList(const ScriptInterface& scriptInterface, JS::MutableHandleValue ret) | ||||
{ | { | ||||
JSContext* cx = scriptInterface.GetContext(); | JSContext* cx = scriptInterface.GetContext(); | ||||
JSAutoRequest rq(cx); | JSAutoRequest rq(cx); | ||||
scriptInterface.Eval("([])", ret); | scriptInterface.CreateArray(ret); | ||||
int j = 0; | |||||
// Convert the internal data structure to a Javascript object. | |||||
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); | ||||
scriptInterface.Eval("({})", &player); | |||||
scriptInterface.SetProperty(player, "name", wstring_from_utf8(p.first)); | scriptInterface.CreateObject( | ||||
scriptInterface.SetProperty(player, "presence", wstring_from_utf8(p.second[0])); | &player, | ||||
scriptInterface.SetProperty(player, "rating", wstring_from_utf8(p.second[1])); | "name", wstring_from_utf8(p.first), | ||||
scriptInterface.SetProperty(player, "role", wstring_from_utf8(p.second[2])); | "presence", wstring_from_utf8(p.second[0]), | ||||
scriptInterface.CallFunctionVoid(ret, "push", player); | "rating", wstring_from_utf8(p.second[1]), | ||||
"role", wstring_from_utf8(p.second[2])); | |||||
scriptInterface.SetPropertyInt(ret, j++, player); | |||||
} | } | ||||
} | } | ||||
/** | /** | ||||
* Handle requests from the GUI for the list of all active games. | * Handle requests from the GUI for the list of all active games. | ||||
* | * | ||||
* @return A JS array containing all known games | * @return A JS array containing all known games | ||||
*/ | */ | ||||
void XmppClient::GUIGetGameList(const ScriptInterface& scriptInterface, JS::MutableHandleValue ret) | void XmppClient::GUIGetGameList(const ScriptInterface& scriptInterface, JS::MutableHandleValue ret) | ||||
{ | { | ||||
JSContext* cx = scriptInterface.GetContext(); | JSContext* cx = scriptInterface.GetContext(); | ||||
JSAutoRequest rq(cx); | JSAutoRequest rq(cx); | ||||
scriptInterface.Eval("([])", ret); | scriptInterface.CreateArray(ret); | ||||
int j = 0; | |||||
const char* stats[] = { "name", "ip", "port", "stunIP", "stunPort", "hostUsername", "state", | const char* stats[] = { "name", "ip", "port", "stunIP", "stunPort", "hostUsername", "state", | ||||
"nbp", "maxnbp", "players", "mapName", "niceMapName", "mapSize", "mapType", | "nbp", "maxnbp", "players", "mapName", "niceMapName", "mapSize", "mapType", | ||||
"victoryCondition", "startTime", "mods" }; | "victoryCondition", "startTime", "mods" }; | ||||
for(const glooxwrapper::Tag* const& t : m_GameList) | for(const glooxwrapper::Tag* const& t : m_GameList) | ||||
{ | { | ||||
JS::RootedValue game(cx); | JS::RootedValue game(cx); | ||||
scriptInterface.Eval("({})", &game); | scriptInterface.CreateObject(&game); | ||||
for (size_t i = 0; i < ARRAY_SIZE(stats); ++i) | 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.SetProperty(game, stats[i], wstring_from_utf8(t->findAttribute(stats[i]).to_string())); | ||||
scriptInterface.CallFunctionVoid(ret, "push", game); | scriptInterface.SetPropertyInt(ret, j++, game); | ||||
} | } | ||||
} | } | ||||
/** | /** | ||||
* Handle requests from the GUI for leaderboard data. | * Handle requests from the GUI for leaderboard data. | ||||
* | * | ||||
* @return A JS array containing all known leaderboard data | * @return A JS array containing all known leaderboard data | ||||
*/ | */ | ||||
void XmppClient::GUIGetBoardList(const ScriptInterface& scriptInterface, JS::MutableHandleValue ret) | void XmppClient::GUIGetBoardList(const ScriptInterface& scriptInterface, JS::MutableHandleValue ret) | ||||
{ | { | ||||
JSContext* cx = scriptInterface.GetContext(); | JSContext* cx = scriptInterface.GetContext(); | ||||
JSAutoRequest rq(cx); | JSAutoRequest rq(cx); | ||||
scriptInterface.Eval("([])", ret); | scriptInterface.CreateArray(ret); | ||||
int j = 0; | |||||
const char* attributes[] = { "name", "rank", "rating" }; | const char* attributes[] = { "name", "rank", "rating" }; | ||||
for(const glooxwrapper::Tag* const& t : m_BoardList) | for(const glooxwrapper::Tag* const& t : m_BoardList) | ||||
{ | { | ||||
JS::RootedValue board(cx); | JS::RootedValue board(cx); | ||||
scriptInterface.Eval("({})", &board); | scriptInterface.CreateObject(&board); | ||||
for (size_t i = 0; i < ARRAY_SIZE(attributes); ++i) | 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.SetProperty(board, attributes[i], wstring_from_utf8(t->findAttribute(attributes[i]).to_string())); | ||||
scriptInterface.CallFunctionVoid(ret, "push", board); | scriptInterface.SetPropertyInt(ret, j++, board); | ||||
} | } | ||||
} | } | ||||
/** | /** | ||||
* Handle requests from the GUI for profile data. | * Handle requests from the GUI for profile data. | ||||
* | * | ||||
* @return A JS array containing the specific user's profile data | * @return A JS array containing the specific user's profile data | ||||
*/ | */ | ||||
void XmppClient::GUIGetProfile(const ScriptInterface& scriptInterface, JS::MutableHandleValue ret) | void XmppClient::GUIGetProfile(const ScriptInterface& scriptInterface, JS::MutableHandleValue ret) | ||||
{ | { | ||||
JSContext* cx = scriptInterface.GetContext(); | JSContext* cx = scriptInterface.GetContext(); | ||||
JSAutoRequest rq(cx); | JSAutoRequest rq(cx); | ||||
scriptInterface.Eval("([])", ret); | scriptInterface.CreateArray(ret); | ||||
int j = 0; | |||||
const char* stats[] = { "player", "rating", "totalGamesPlayed", "highestRating", "wins", "losses", "rank" }; | const char* stats[] = { "player", "rating", "totalGamesPlayed", "highestRating", "wins", "losses", "rank" }; | ||||
for (const glooxwrapper::Tag* const& t : m_Profile) | for (const glooxwrapper::Tag* const& t : m_Profile) | ||||
{ | { | ||||
JS::RootedValue profile(cx); | JS::RootedValue profile(cx); | ||||
scriptInterface.Eval("({})", &profile); | scriptInterface.CreateObject(&profile); | ||||
for (size_t i = 0; i < ARRAY_SIZE(stats); ++i) | 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.SetProperty(profile, stats[i], wstring_from_utf8(t->findAttribute(stats[i]).to_string())); | ||||
scriptInterface.CallFunctionVoid(ret, "push", profile); | scriptInterface.SetPropertyInt(ret, j++, profile); | ||||
} | } | ||||
} | } | ||||
/***************************************************** | /***************************************************** | ||||
* Message interfaces * | * Message interfaces * | ||||
*****************************************************/ | *****************************************************/ | ||||
void XmppClient::CreateGUIMessage( | void XmppClient::CreateGUIMessage( | ||||
Show All 16 Lines | void XmppClient::CreateGUIMessage( | ||||
m_GuiMessageQueue.push_back(std::move(message)); | m_GuiMessageQueue.push_back(std::move(message)); | ||||
} | } | ||||
JS::Value XmppClient::GuiMessageToJSVal(const ScriptInterface& scriptInterface, const GUIMessage& message, const bool historic) | JS::Value XmppClient::GuiMessageToJSVal(const ScriptInterface& scriptInterface, const GUIMessage& message, const bool historic) | ||||
{ | { | ||||
JSContext* cx = scriptInterface.GetContext(); | JSContext* cx = scriptInterface.GetContext(); | ||||
JSAutoRequest rq(cx); | JSAutoRequest rq(cx); | ||||
JS::RootedValue ret(cx); | JS::RootedValue ret(cx); | ||||
scriptInterface.Eval("({})", &ret); | |||||
scriptInterface.SetProperty(ret, "type", wstring_from_utf8(message.type)); | scriptInterface.CreateObject( | ||||
&ret, | |||||
"type", wstring_from_utf8(message.type), | |||||
"time", static_cast<double>(message.time), | |||||
"historic", historic); | |||||
if (!message.level.empty()) | if (!message.level.empty()) | ||||
scriptInterface.SetProperty(ret, "level", wstring_from_utf8(message.level)); | scriptInterface.SetProperty(ret, "level", wstring_from_utf8(message.level)); | ||||
if (!message.property1_name.empty()) | if (!message.property1_name.empty()) | ||||
scriptInterface.SetProperty(ret, message.property1_name.c_str(), wstring_from_utf8(message.property1_value)); | scriptInterface.SetProperty(ret, message.property1_name.c_str(), wstring_from_utf8(message.property1_value)); | ||||
if (!message.property2_name.empty()) | if (!message.property2_name.empty()) | ||||
scriptInterface.SetProperty(ret, message.property2_name.c_str(), wstring_from_utf8(message.property2_value)); | scriptInterface.SetProperty(ret, message.property2_name.c_str(), wstring_from_utf8(message.property2_value)); | ||||
scriptInterface.SetProperty(ret, "time", (double)message.time); | |||||
scriptInterface.SetProperty(ret, "historic", historic); | |||||
return ret; | return ret; | ||||
} | } | ||||
JS::Value XmppClient::GuiPollNewMessage(const ScriptInterface& scriptInterface) | JS::Value XmppClient::GuiPollNewMessage(const ScriptInterface& scriptInterface) | ||||
{ | { | ||||
if (m_GuiMessageQueue.empty()) | if (m_GuiMessageQueue.empty()) | ||||
return JS::UndefinedValue(); | return JS::UndefinedValue(); | ||||
GUIMessage message = m_GuiMessageQueue.front(); | GUIMessage message = m_GuiMessageQueue.front(); | ||||
m_GuiMessageQueue.pop_front(); | m_GuiMessageQueue.pop_front(); | ||||
// Since there can be hundreds of presence changes while playing a game, ignore these for performance | // Since there can be hundreds of presence changes while playing a game, ignore these for performance | ||||
if (message.type == "chat" && message.level != "presence") | if (message.type == "chat" && message.level != "presence") | ||||
m_HistoricGuiMessages.push_back(message); | m_HistoricGuiMessages.push_back(message); | ||||
return GuiMessageToJSVal(scriptInterface, message, false); | return GuiMessageToJSVal(scriptInterface, message, false); | ||||
} | } | ||||
JS::Value XmppClient::GuiPollHistoricMessages(const ScriptInterface& scriptInterface) | JS::Value XmppClient::GuiPollHistoricMessages(const ScriptInterface& scriptInterface) | ||||
{ | { | ||||
JSContext* cx = scriptInterface.GetContext(); | JSContext* cx = scriptInterface.GetContext(); | ||||
JSAutoRequest rq(cx); | JSAutoRequest rq(cx); | ||||
JS::RootedObject ret(cx, JS_NewArrayObject(cx, 0)); | |||||
uint32_t i = 0; | JS::RootedValue ret(cx); | ||||
scriptInterface.CreateArray(&ret); | |||||
int j = 0; | |||||
for (const GUIMessage& message : m_HistoricGuiMessages) | for (const GUIMessage& message : m_HistoricGuiMessages) | ||||
{ | { | ||||
JS::RootedValue msg(cx, GuiMessageToJSVal(scriptInterface, message, true)); | JS::RootedValue msg(cx, GuiMessageToJSVal(scriptInterface, message, true)); | ||||
JS_SetElement(cx, ret, i++, msg); | scriptInterface.SetPropertyInt(ret, j++, msg); | ||||
} | } | ||||
return JS::ObjectValue(*ret); | return ret; | ||||
} | } | ||||
/** | /** | ||||
* Send a standard MUC textual message. | * Send a standard MUC textual message. | ||||
*/ | */ | ||||
void XmppClient::SendMUCMessage(const std::string& message) | void XmppClient::SendMUCMessage(const std::string& message) | ||||
{ | { | ||||
m_mucRoom->send(message); | m_mucRoom->send(message); | ||||
▲ Show 20 Lines • Show All 565 Lines • Show Last 20 Lines |
Wildfire Games · Phabricator