Ticket #3185: serialise_messages.diff
File serialise_messages.diff, 5.4 KB (added by , 9 years ago) |
---|
-
binaries/data/mods/public/gui/session/messages.js
179 179 var playerID = Engine.GetPlayerID(); 180 180 for (var n of notifications) 181 181 { 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;189 182 var message = n.message; 190 183 if (n.translateMessage) 191 184 message = translate(message); … … 192 185 var parameters = n.parameters || {}; 193 186 if (n.translateParameters) 194 187 translateObjectKeys(parameters, n.translateParameters); 195 parameters.time = timeToString(n. time);188 parameters.time = timeToString(n.endTime - g_SimState.timeElapsed); 196 189 notificationText += sprintf(message, parameters) + "\n"; 197 190 } 198 191 Engine.GetGUIObjectByName("notificationText").caption = notificationText; -
binaries/data/mods/public/maps/scripts/WonderVictory.js
34 34 messages.otherMessage = cmpGuiInterface.AddTimeNotification({ 35 35 "message": markForTranslation("%(player)s will have won in %(time)s"), 36 36 "players": players, 37 "duration": time,38 37 "parameters": {"player": cmpPlayer.GetName()}, 39 38 "translateMessage": true, 40 39 "translateParameters": [], 41 } );40 }, time); 42 41 messages.ownMessage = cmpGuiInterface.AddTimeNotification({ 43 42 "message": markForTranslation("You will have won in %(time)s"), 44 43 "players": [data.to], 45 "duration": time,46 44 "translateMessage": true, 47 } );45 }, time); 48 46 timer = cmpTimer.SetTimeout(SYSTEM_ENTITY, IID_EndGameManager, "MarkPlayerAsWon", time, data.to); 49 47 50 48 this.wonderVictoryTimers[ent] = timer; -
binaries/data/mods/public/simulation/components/GuiInterface.js
5 5 6 6 GuiInterface.prototype.Serialize = function() 7 7 { 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 }; 11 15 }; 12 16 13 GuiInterface.prototype.Deserialize = function( obj)17 GuiInterface.prototype.Deserialize = function(data) 14 18 { 15 19 this.Init(); 20 this.timeNotifications = data.timeNotifications; 21 this.timeNotificationID = data.timeNotificationID; 16 22 }; 17 23 18 24 GuiInterface.prototype.Init = function() … … 695 701 return cmpPlayer.GetNeededResources(amounts); 696 702 }; 697 703 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 */ 710 GuiInterface.prototype.AddTimeNotification = function(notification, duration = 10000) 699 711 { 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(); 702 714 notification.id = ++this.timeNotificationID; 703 715 this.timeNotifications.push(notification); 704 716 this.timeNotifications.sort(function (n1, n2){return n2.endTime - n1.endTime}); 717 718 cmpTimer.SetTimeout(this.entity, IID_GuiInterface, "DeleteTimeNotification", duration, this.timeNotificationID); 719 705 720 return this.timeNotificationID; 706 721 }; 707 722 708 723 GuiInterface.prototype.DeleteTimeNotification = function(notificationID) 709 724 { 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); 718 729 }; 719 730 720 731 GuiInterface.prototype.GetTimeNotifications = function(playerID) 721 732 { 722 733 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); 733 739 }; 734 740 735 741 GuiInterface.prototype.PushNotification = function(notification) 736 742 { 737 743 if (!notification.type || notification.type == "text") 738 {739 if (!notification.duration)740 notification.duration = 10000;741 744 this.AddTimeNotification(notification); 742 return; 743 } 744 this.notifications.push(notification); 745 else 746 this.notifications.push(notification); 745 747 }; 746 748 747 749 GuiInterface.prototype.GetNextNotification = function()