Ticket #3185: serialise_messages.diff

File serialise_messages.diff, 5.4 KB (added by sanderd17, 9 years ago)
  • binaries/data/mods/public/gui/session/messages.js

     
    179179    var playerID = Engine.GetPlayerID();
    180180    for (var n of notifications)
    181181    {
    182         if (!n.players)
    183         {
    184             warn("notification has unknown player list. Text:\n"+n.message);
    185             continue;
    186         }
    187         if (n.players.indexOf(playerID) == -1)
    188             continue;
    189182        var message = n.message;
    190183        if (n.translateMessage)
    191184            message = translate(message);
     
    192185        var parameters = n.parameters || {};
    193186        if (n.translateParameters)
    194187            translateObjectKeys(parameters, n.translateParameters);
    195         parameters.time = timeToString(n.time);
     188        parameters.time = timeToString(n.endTime - g_SimState.timeElapsed);
    196189        notificationText += sprintf(message, parameters) + "\n";
    197190    }
    198191    Engine.GetGUIObjectByName("notificationText").caption = notificationText;
  • binaries/data/mods/public/maps/scripts/WonderVictory.js

     
    3434    messages.otherMessage = cmpGuiInterface.AddTimeNotification({
    3535        "message": markForTranslation("%(player)s will have won in %(time)s"),
    3636        "players": players,
    37         "duration": time,
    3837        "parameters": {"player": cmpPlayer.GetName()},
    3938        "translateMessage": true,
    4039        "translateParameters": [],
    41     });
     40    }, time);
    4241    messages.ownMessage = cmpGuiInterface.AddTimeNotification({
    4342        "message": markForTranslation("You will have won in %(time)s"),
    4443        "players": [data.to],
    45         "duration": time,
    4644        "translateMessage": true,
    47     });
     45    }, time);
    4846    timer = cmpTimer.SetTimeout(SYSTEM_ENTITY, IID_EndGameManager, "MarkPlayerAsWon", time, data.to);
    4947
    5048    this.wonderVictoryTimers[ent] = timer;
  • binaries/data/mods/public/simulation/components/GuiInterface.js

     
    55
    66GuiInterface.prototype.Serialize = function()
    77{
    8     // This component isn't network-synchronised so we mustn't serialise
    9     // its non-deterministic data. Instead just return an empty object.
    10     return {};
     8    // This component isn't network-synchronised for the biggest part
     9    // So most of the attributes shouldn't be serialized
     10    // Return an object with a small selection of deterministic data
     11    return {
     12        "timeNotifications": this.timeNotifications,
     13        "timeNotificationID": this.timeNotificationID
     14    };
    1115};
    1216
    13 GuiInterface.prototype.Deserialize = function(obj)
     17GuiInterface.prototype.Deserialize = function(data)
    1418{
    1519    this.Init();
     20    this.timeNotifications = data.timeNotifications;
     21    this.timeNotificationID = data.timeNotificationID;
    1622};
    1723
    1824GuiInterface.prototype.Init = function()
     
    695701    return cmpPlayer.GetNeededResources(amounts);
    696702};
    697703
    698 GuiInterface.prototype.AddTimeNotification = function(notification)
     704/**
     705 * Add a timed notification.
     706 * Warning: timed notifacations are serialised
     707 * (to also display them on saved games or after a rejoin)
     708 * so they should allways be added and deleted in a deterministic way.
     709 */
     710GuiInterface.prototype.AddTimeNotification = function(notification, duration = 10000)
    699711{
    700     var time = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer).GetTime();
    701     notification.endTime = notification.duration + time;
     712    var cmpTimer = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer);
     713    notification.endTime = duration + cmpTimer.GetTime();
    702714    notification.id = ++this.timeNotificationID;
    703715    this.timeNotifications.push(notification);
    704716    this.timeNotifications.sort(function (n1, n2){return n2.endTime - n1.endTime});
     717
     718    cmpTimer.SetTimeout(this.entity, IID_GuiInterface, "DeleteTimeNotification", duration, this.timeNotificationID);
     719
    705720    return this.timeNotificationID;
    706721};
    707722
    708723GuiInterface.prototype.DeleteTimeNotification = function(notificationID)
    709724{
    710     for (var i in this.timeNotifications)
    711     {
    712         if (this.timeNotifications[i].id == notificationID)
    713         {
    714             this.timeNotifications.splice(i);
    715             return;
    716         }
    717     }
     725    var filter = function(n) {
     726        return n.id != notificationID;
     727    };
     728    this.timeNotifications = this.timeNotifications.filter(filter);
    718729};
    719730
    720731GuiInterface.prototype.GetTimeNotifications = function(playerID)
    721732{
    722733    var time = Engine.QueryInterface(SYSTEM_ENTITY, IID_Timer).GetTime();
    723     var toDelete = [];
    724     for (var n of this.timeNotifications)
    725     {
    726         n.time = n.endTime - time;
    727         if (n.time < 0)
    728             toDelete.push(n.id);
    729     }
    730     for (var id of toDelete)
    731         this.DeleteTimeNotification(id);
    732     return this.timeNotifications;
     734    // filter on players and time, since the delete timer might be executed with a delay
     735    var filter = function(n) {
     736        return n.players.indexOf(playerID) != -1 && n.endTime > time;
     737    };
     738    return this.timeNotifications.filter(filter);
    733739};
    734740
    735741GuiInterface.prototype.PushNotification = function(notification)
    736742{
    737743    if (!notification.type || notification.type == "text")
    738     {
    739         if (!notification.duration)
    740             notification.duration = 10000;
    741744        this.AddTimeNotification(notification);
    742         return;
    743     }
    744     this.notifications.push(notification);
     745    else
     746        this.notifications.push(notification);
    745747};
    746748
    747749GuiInterface.prototype.GetNextNotification = function()