Ticket #1839: tribute-notifications.patch

File tribute-notifications.patch, 2.5 KB (added by alpha123, 11 years ago)
  • simulation/components/Player.js

     
    547547        var cmpTheirStatisticsTracker = QueryPlayerIDInterface(player, IID_StatisticsTracker);
    548548        if (cmpTheirStatisticsTracker)
    549549            cmpTheirStatisticsTracker.IncreaseTributesReceivedCounter(total);
    550         // TODO: notify the receiver
     550       
     551        var notification = {"type": "tribute", "player": player, "player1": this.playerID, "amounts": amounts};
     552        var cmpGUIInterface = Engine.QueryInterface(SYSTEM_ENTITY, IID_GuiInterface);
     553        cmpGUIInterface.PushNotification(notification);
    551554    }
    552555    // else not enough resources... TODO: send gui notification
    553556};
  • gui/session/messages.js

     
    5353        if (isDiplomacyOpen)
    5454            openDiplomacy();
    5555    }
     56    else if (notification.type == "tribute")
     57    {
     58        addChatMessage({
     59            "type": "tribute",
     60            "player": notification.player,
     61            "player1": notification.player1,
     62            "amounts": notification.amounts
     63        })
     64    }
    5665    else if (notification.type == "quit")
    5766    {
    5867        // Used for AI testing
     
    306315        else // No need for other players to know of this.
    307316            return;
    308317        break;
     318    case "tribute":
     319        if (msg.player != Engine.GetPlayerID())
     320            return;
     321       
     322        username = escapeText(g_Players[msg.player].name);
     323        playerColor = g_Players[msg.player].color.r + " " + g_Players[msg.player].color.g + " " + g_Players[msg.player].color.b;
     324       
     325        // Format the amounts to proper English: 200 food, 100 wood, and 300 metal; 100 food; 400 wood and 200 stone
     326        var amounts = Object.keys(msg.amounts)
     327                 .filter(function (type) msg.amounts[type] > 0)
     328                 .map(function (type) msg.amounts[type] + " " + type);
     329        // This is one of the times I actually miss Common Lisp's incredibly cryptic but enormously useful format function.
     330        // With that this would be a cute one-liner. I've racked my brain a bit and can't think of anything as elegant in JS.
     331        // Now before I end up writing an essay in the comments, the code:
     332        if (amounts.length > 1)
     333        {
     334            var lastAmount = amounts.pop();
     335            amounts = amounts.join(", ") + " and " + lastAmount;
     336        }
     337       
     338        formatted = "[color=\"" + playerColor + "\"]" + username + "[/color] has sent you " + amounts + ".";
     339        break;
    309340    case "message":
    310341        // May have been hidden by the 'team' command.
    311342        if (msg.hide)