Ticket #3386: t3386_clear_muc_messages_v2.patch

File t3386_clear_muc_messages_v2.patch, 12.4 KB (added by elexis, 9 years ago)

Adds error messages for invalid updates. As mentioned in the comment above, I'll take a look at simplifying the relevant code.

  • binaries/data/mods/public/gui/lobby/lobby.js

     
    174174    var playersBox = Engine.GetGUIObjectByName("playersBox");
    175175    var playerList = [];
    176176    var presenceList = [];
    177177    var nickList = [];
    178178    var ratingList = [];
    179     var cleanPlayerList = Engine.GetPlayerList();
     179    var cleanPlayerList = Engine.GetPlayerList(true);
    180180    // Sort the player list, ignoring case.
    181181    cleanPlayerList.sort(function(a,b)
    182182    {
    183183        switch (g_PlayerListSortBy)
    184184        {
     
    697697            var ratingList = playersBox.list_rating;
    698698            var nickIndex = nickList.indexOf(nick);
    699699            switch(message.level)
    700700            {
    701701            case "join":
     702                if (nickIndex != -1)
     703                {
     704                    error("User " + nick + " joined, but is present already!");
     705                    break;
     706                }
    702707                var [name, status, rating] = formatPlayerListEntry(nick, presence, "-");
    703708                playerList.push(name);
    704709                presenceList.push(status);
    705710                nickList.push(nick);
    706711                ratingList.push(String(rating));
    707712                addChatMessage({ "text": "/special " + sprintf(translate("%(nick)s has joined."), { nick: nick }), "key": g_specialKey });
    708713                break;
    709714            case "leave":
    710                 if (nickIndex == -1) // Left, but not present (TODO: warn about this?)
     715                if (nickIndex == -1)
     716                {
     717                    error("User " + nick + " left, but has not been online!");
    711718                    break;
     719                }
    712720                playerList.splice(nickIndex, 1);
    713721                presenceList.splice(nickIndex, 1);
    714722                nickList.splice(nickIndex, 1);
    715723                ratingList.splice(nickIndex, 1);
    716724                addChatMessage({ "text": "/special " + sprintf(translate("%(nick)s has left."), { nick: nick }), "key": g_specialKey });
    717725                break;
    718726            case "nick":
    719                 if (nickIndex == -1) // Changed nick, but not present (shouldn't ever happen)
     727                if (nickIndex == -1)
     728                {
     729                    error("User " + nick + " changed nickname, but is not online!");
    720730                    break;
     731                }
    721732                if (!isValidNick(message.data))
    722733                {
    723734                    addChatMessage({ "from": "system", "text": sprintf(translate("Invalid nickname: %(nick)s"), { nick: message.data })});
    724735                    break;
    725736                }
     
    728739                // presence stays the same
    729740                nickList[nickIndex] = message.data;
    730741                addChatMessage({ "text": "/special " + sprintf(translate("%(oldnick)s is now known as %(newnick)s."), { oldnick: nick, newnick: message.data }), "key": g_specialKey });
    731742                break;
    732743            case "presence":
    733                 if (nickIndex == -1) // Changed presence, but not online (shouldn't ever happen)
     744                if (nickIndex == -1)
     745                {
     746                    if (nick != g_Name) // we change presence on init(), before downloading the playerlist
     747                        error("User " + nick + " changed presence, but is not online!");
    734748                    break;
     749                }
    735750                var [name, status, rating] = formatPlayerListEntry(nick, presence, stripColorCodes(ratingList[nickIndex]));
    736751                presenceList[nickIndex] = status;
    737752                playerList[nickIndex] = name;
    738753                ratingList[nickIndex] = rating;
    739754                break;
  • binaries/data/mods/public/gui/lobby/lobby.xml

     
    234234
    235235            <object name="chatPanel" size="0 49% 100% 100%" type="image" sprite="ModernDarkBoxGold">
    236236                <object name="chatText" size="0 0 100% 94%" type="text" style="ChatPanel" font="sans-13"/>
    237237                <object name="chatInput" size="0 94% 100% 100%" type="input" style="ModernInput" font="sans-13">
    238238                    <action on="Press">submitChatInput();</action>
    239                     <action on="Tab">autoCompleteNick("chatInput", Engine.GetPlayerList());</action>
     239                    <action on="Tab">autoCompleteNick("chatInput", Engine.GetPlayerList(false));</action>
    240240                </object>
    241241            </object>
    242242        </object>
    243243
    244244        <!-- START Window for leaderboard stats -->
  • source/lobby/IXmppClient.h

     
    1 /* Copyright (C) 2014 Wildfire Games.
     1/* Copyright (C) 2015 Wildfire Games.
    22 * This file is part of 0 A.D.
    33 *
    44 * 0 A.D. is free software: you can redistribute it and/or modify
    55 * it under the terms of the GNU General Public License as published by
    66 * the Free Software Foundation, either version 2 of the License, or
     
    4646    virtual void SetPresence(const std::string& presence) = 0;
    4747    virtual void GetPresence(const std::string& nickname, std::string& presence) = 0;
    4848    virtual void GetRole(const std::string& nickname, std::string& role) = 0;
    4949    virtual void GetSubject(std::string& subject) = 0;
    5050
    51     virtual void GUIGetPlayerList(ScriptInterface& scriptInterface, JS::MutableHandleValue ret) = 0;
     51    virtual void GUIGetPlayerList(ScriptInterface& scriptInterface, JS::MutableHandleValue ret, bool clearPresenceUpdates) = 0;
    5252    virtual void GUIGetGameList(ScriptInterface& scriptInterface, JS::MutableHandleValue ret) = 0;
    5353    virtual void GUIGetBoardList(ScriptInterface& scriptInterface, JS::MutableHandleValue ret) = 0;
    5454    virtual void GUIGetProfile(ScriptInterface& scriptInterface, JS::MutableHandleValue ret) = 0;
    5555
    5656    virtual void GuiPollMessage(ScriptInterface& scriptInterface, JS::MutableHandleValue ret) = 0;
  • source/lobby/XmppClient.cpp

     
    492492/**
    493493 * Handle requests from the GUI for the list of players.
    494494 *
    495495 * @return A JS array containing all known players and their presences
    496496 */
    497 void XmppClient::GUIGetPlayerList(ScriptInterface& scriptInterface, JS::MutableHandleValue ret)
     497void XmppClient::GUIGetPlayerList(ScriptInterface& scriptInterface, JS::MutableHandleValue ret, bool clearPresenceUpdates)
    498498{
     499    if (clearPresenceUpdates)
     500    {
     501        m_GuiMessageQueue.erase(
     502            std::remove_if(m_GuiMessageQueue.begin(), m_GuiMessageQueue.end(),
     503                [](XmppClient::GUIMessage& message)
     504                {
     505                    return message.type == L"muc" && message.level != L"subject";
     506                }
     507        ), m_GuiMessageQueue.end());
     508    }
     509
    499510    JSContext* cx = scriptInterface.GetContext();
    500511    JSAutoRequest rq(cx);
    501512
    502513    scriptInterface.Eval("([])", ret);
    503514
  • source/lobby/XmppClient.h

     
    1 /* Copyright (C) 2014 Wildfire Games.
     1/* Copyright (C) 2015 Wildfire Games.
    22 * This file is part of 0 A.D.
    33 *
    44 * 0 A.D. is free software: you can redistribute it and/or modify
    55 * it under the terms of the GNU General Public License as published by
    66 * the Free Software Foundation, either version 2 of the License, or
     
    1818#ifndef XXXMPPCLIENT_H
    1919#define XXXMPPCLIENT_H
    2020
    2121#include "IXmppClient.h"
    2222
    23 #include "glooxwrapper/glooxwrapper.h"
    24 
    25 //game - script
    2623#include <deque>
     24
     25#include "glooxwrapper/glooxwrapper.h"
    2726#include "scriptinterface/ScriptVal.h"
    2827
    29 //Game - script
    3028class ScriptInterface;
    3129
    3230namespace glooxwrapper
    3331{
    3432    class Client;
     
    3634}
    3735
    3836class XmppClient : public IXmppClient, public glooxwrapper::ConnectionListener, public glooxwrapper::MUCRoomHandler, public glooxwrapper::IqHandler, public glooxwrapper::RegistrationHandler, public glooxwrapper::MessageHandler
    3937{
    4038    NONCOPYABLE(XmppClient);
     39
    4140private:
    4241    //Components
    4342    glooxwrapper::Client* m_client;
    4443    glooxwrapper::MUCRoom* m_mucRoom;
    4544    glooxwrapper::Registration* m_registration;
     45
    4646    //Account infos
    4747    std::string m_username;
    4848    std::string m_password;
    4949    std::string m_nick;
    5050    std::string m_xpartamuppId;
     51
    5152    // State
    5253    bool m_initialLoadComplete = false;
     54
    5355public:
    5456    //Basic
    5557    XmppClient(const std::string& sUsername, const std::string& sPassword, const std::string& sRoom, const std::string& sNick, const int historyRequestSize = 0, const bool regOpt = false);
    5658    virtual ~XmppClient();
    5759
     
    7476    void SetPresence(const std::string& presence);
    7577    void GetPresence(const std::string& nickname, std::string& presence);
    7678    void GetRole(const std::string& nickname, std::string& role);
    7779    void GetSubject(std::string& subject);
    7880
    79     void GUIGetPlayerList(ScriptInterface& scriptInterface, JS::MutableHandleValue ret);
     81    void GUIGetPlayerList(ScriptInterface& scriptInterface, JS::MutableHandleValue ret, bool clearPresenceUpdates);
    8082    void GUIGetGameList(ScriptInterface& scriptInterface, JS::MutableHandleValue ret);
    8183    void GUIGetBoardList(ScriptInterface& scriptInterface, JS::MutableHandleValue ret);
    8284    void GUIGetProfile(ScriptInterface& scriptInterface, JS::MutableHandleValue ret);
     85
    8386    //Script
    8487    ScriptInterface& GetScriptInterface();
    8588
    8689protected:
    8790    /* Xmpp handlers */
     
    120123
    121124    // Helpers
    122125    void GetPresenceString(const gloox::Presence::PresenceType p, std::string& presence) const;
    123126    void GetRoleString(const gloox::MUCRoomRole r, std::string& role) const;
    124127    std::string StanzaErrorToString(gloox::StanzaError err);
     128
    125129public:
    126130    /* Messages */
    127131    struct GUIMessage
    128132    {
    129133        std::wstring type;
  • source/lobby/scripting/JSInterface_Lobby.cpp

     
    4444    scriptInterface.RegisterFunction<void, std::wstring, &JSI_Lobby::SendGetProfile>("SendGetProfile");
    4545    scriptInterface.RegisterFunction<void, JS::HandleValue, &JSI_Lobby::SendRegisterGame>("SendRegisterGame");
    4646    scriptInterface.RegisterFunction<void, JS::HandleValue, &JSI_Lobby::SendGameReport>("SendGameReport");
    4747    scriptInterface.RegisterFunction<void, &JSI_Lobby::SendUnregisterGame>("SendUnregisterGame");
    4848    scriptInterface.RegisterFunction<void, std::wstring, std::wstring, &JSI_Lobby::SendChangeStateGame>("SendChangeStateGame");
    49     scriptInterface.RegisterFunction<JS::Value, &JSI_Lobby::GetPlayerList>("GetPlayerList");
     49    scriptInterface.RegisterFunction<JS::Value, bool, &JSI_Lobby::GetPlayerList>("GetPlayerList");
    5050    scriptInterface.RegisterFunction<JS::Value, &JSI_Lobby::GetGameList>("GetGameList");
    5151    scriptInterface.RegisterFunction<JS::Value, &JSI_Lobby::GetBoardList>("GetBoardList");
    5252    scriptInterface.RegisterFunction<JS::Value, &JSI_Lobby::GetProfile>("GetProfile");
    5353    scriptInterface.RegisterFunction<JS::Value, &JSI_Lobby::LobbyGuiPollMessage>("LobbyGuiPollMessage");
    5454    scriptInterface.RegisterFunction<void, std::wstring, &JSI_Lobby::LobbySendMessage>("LobbySendMessage");
     
    173173    if (!g_XmppClient)
    174174        return;
    175175    g_XmppClient->SendIqChangeStateGame(utf8_from_wstring(nbp), utf8_from_wstring(players));
    176176}
    177177
    178 JS::Value JSI_Lobby::GetPlayerList(ScriptInterface::CxPrivate* pCxPrivate)
     178JS::Value JSI_Lobby::GetPlayerList(ScriptInterface::CxPrivate* pCxPrivate, bool clearPresenceUpdates)
    179179{
    180180    if (!g_XmppClient)
    181181        return JS::UndefinedValue();
    182182
    183183    JSContext* cx = pCxPrivate->pScriptInterface->GetContext();
    184184    JSAutoRequest rq(cx);
    185185       
    186186    JS::RootedValue playerList(cx);
    187     g_XmppClient->GUIGetPlayerList(*(pCxPrivate->pScriptInterface), &playerList);
     187    g_XmppClient->GUIGetPlayerList(*(pCxPrivate->pScriptInterface), &playerList, clearPresenceUpdates);
    188188
    189189    return playerList;
    190190}
    191191
    192192JS::Value JSI_Lobby::GetGameList(ScriptInterface::CxPrivate* pCxPrivate)
  • source/lobby/scripting/JSInterface_Lobby.h

     
    4242    void SendGetProfile(ScriptInterface::CxPrivate* pCxPrivate, std::wstring player);
    4343    void SendGameReport(ScriptInterface::CxPrivate* pCxPrivate, JS::HandleValue data);
    4444    void SendRegisterGame(ScriptInterface::CxPrivate* pCxPrivate, JS::HandleValue data);
    4545    void SendUnregisterGame(ScriptInterface::CxPrivate* pCxPrivate);
    4646    void SendChangeStateGame(ScriptInterface::CxPrivate* pCxPrivate, std::wstring nbp, std::wstring players);
    47     JS::Value GetPlayerList(ScriptInterface::CxPrivate* pCxPrivate);
     47    JS::Value GetPlayerList(ScriptInterface::CxPrivate* pCxPrivate, bool clearPresenceUpdates);
    4848    JS::Value GetGameList(ScriptInterface::CxPrivate* pCxPrivate);
    4949    JS::Value GetBoardList(ScriptInterface::CxPrivate* pCxPrivate);
    5050    JS::Value GetProfile(ScriptInterface::CxPrivate* pCxPrivate);
    5151    JS::Value LobbyGuiPollMessage(ScriptInterface::CxPrivate* pCxPrivate);
    5252    void LobbySendMessage(ScriptInterface::CxPrivate* pCxPrivate, std::wstring message);