Ticket #3277: Market_js.patch

File Market_js.patch, 5.7 KB (added by otero, 8 years ago)
  • binaries/data/mods/public/simulation/components/Market.js

     
     1function Market() {}
     2
     3Market.prototype.Schema = "<a:component type='system'/><empty/>";
     4
     5Market.prototype.Init = function()
     6{
     7    this.tradeEntities = []; // List of trade entities from which this market works as a target
     8};
     9
     10//
     11// Method designed to subscribe a new trader to this market.
     12//
     13Market.prototype.SubscribeTrader = function(trader)
     14{
     15    // First check that this trader has not been already registered inside the market.
     16    // If that is the case then do nothing and return true
     17    if (trader in this.tradeEntities)
     18        return true;
     19
     20    // If the entity has not been registered for this market,
     21    // add it to the list of traders
     22    this.tradeEntities.push(trader);
     23
     24    // If everything was correct return true
     25    return true;
     26};
     27
     28//
     29// Method designed for the management of the registered tarders when the market
     30// is destroyed
     31//
     32Market.prototype.OnDestroy = function()
     33{
     34        // Find the entity inside the one of the list used in the market
     35        //
     36        // If the trader is found in the SourceMarket list then
     37        // send him to his target market.
     38        //
     39        // If the trader is found in the TargetMarket list then
     40        // send him to his source market
     41        //
     42        // If the source and target have been destroyed then stop
     43        // trading
     44
     45        let currentMarket = this.entity;
     46        this.tradeEntities.forEach(function(trader)
     47        {
     48            // If there is at least a related market related with this trader
     49            // then send him to it, otherwise stop trading
     50            if (trader && trader.GetFirstMarket() && trader.GetFirstMarket() === currentMarket)
     51            {
     52                if (trader.GetSecondMarket())
     53                {
     54                    trader.InvalidateFirstMarket();
     55                    trader.MoveToSecondMarket();
     56                }
     57                else
     58                    trader.StopTrading();
     59            }
     60            else if(trader && trader.GetSecondMarket() && trader.GetSecondMarket() === currentMarket)
     61            {
     62                if (trader.GetFirstMarket())
     63                {
     64                    trader.InvalidateSecondMarket();
     65                    trader.MoveToFirstMarket();
     66                }
     67                else
     68                    trader.StopTrading();
     69            }
     70        });
     71};
     72
     73//
     74// Method designed for the management of the trade units when the market is captured
     75//
     76Market.prototype.OnOwnershipChanged = function(msg)
     77{
     78    if (msg.to != -1 && msg.from != -1)
     79    {
     80        let cmpPlayerManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_PlayerManager);
     81        let cmpPlayer = Engine.QueryInterface(cmpPlayerManager.GetPlayerByID(msg.to), IID_Player);
     82
     83        // If the new owner is an not an ally the trader should display a behavior
     84        // similar when the market is destroyed.
     85
     86        if(!cmpPlayer.IsAlly(msg.from))
     87        {
     88
     89            // Find the entity inside the one of the list used in the market
     90            //
     91            // If the trader is found in the SourceMarket list then
     92            // send him to his target market.
     93            //
     94            // If the trader is found in the TargetMarket list then
     95            // send him to his source market
     96            //
     97            // If the source and target have been captured then stop
     98            // trading
     99
     100            let currentMarket = this.entity;
     101            this.tradeEntities.forEach(function(trader)
     102            {
     103                // If there is at least a related market related with this trader
     104                // then send him to it, otherwise stop trading
     105                if (trader && trader.GetFirstMarket() && trader.GetFirstMarket() === currentMarket)
     106                {
     107                    if (trader.GetSecondMarket())
     108                    {
     109                        trader.InvalidateFirstMarket();
     110                        trader.MoveToSecondMarket();
     111                    }
     112                    else
     113                        trader.StopTrading();
     114                }
     115                else if(trader && trader.GetSecondMarket() && trader.GetSecondMarket() === currentMarket)
     116                {
     117                    if (trader.GetFirstMarket())
     118                    {
     119                        trader.InvalidateSecondMarket();
     120                        trader.MoveToFirstMarket();
     121                    }
     122                    else
     123                        trader.StopTrading();
     124                }
     125            });
     126        }
     127    }
     128};
     129
     130//
     131// Method designed to handle the ocation when a market changes diplomacy into
     132// an hostil position
     133Market.prototype.OnDiplomacyChanged = function(msg)
     134{
     135    let cmpOwnership = Engine.QueryInterface(this.entity, IID_Ownership);
     136    let cmpPlayerManager = Engine.QueryInterface(SYSTEM_ENTITY, IID_PlayerManager);
     137    let cmpPlayer = Engine.QueryInterface(cmpPlayerManager.GetPlayerByID(msg.player), IID_Player);
     138
     139    // If the new diplomatic position of the player is not an ally return to
     140    // respective ally markets
     141    warn(JSON.stringify(cmpPlayer.IsAlly(cmpOwnership.GetOwner())));
     142    if (cmpPlayer.IsAlly(cmpOwnership.GetOwner()))
     143    {
     144
     145        // Find the entity inside the one of the list used in the market
     146        //
     147        // If the trader is found in the SourceMarket list then
     148        // send him to his target market.
     149        //
     150        // If the trader is found in the TargetMarket list then
     151        // send him to his source market
     152        //
     153        // If the source and target have been captured then stop
     154        // trading
     155
     156        let currentMarket = this.entity;
     157        this.tradeEntities.forEach(function(trader)
     158        {
     159            // If there is at least a related market related with this trader
     160            // then send him to it, otherwise stop trading
     161            if (trader && trader.GetFirstMarket() && trader.GetFirstMarket() === currentMarket)
     162            {
     163                if (trader.GetSecondMarket())
     164                {
     165                    trader.InvalidateFirstMarket();
     166                    trader.MoveToSecondMarket();
     167                }
     168                else
     169                    trader.StopTrading();
     170            }
     171            else if(trader && trader.GetSecondMarket() && trader.GetSecondMarket() === currentMarket)
     172            {
     173                if (trader.GetFirstMarket())
     174                {
     175                    trader.InvalidateSecondMarket();
     176                    trader.MoveToFirstMarket();
     177                }
     178                else
     179                    trader.StopTrading();
     180            }
     181        });
     182    }
     183}
     184
     185Engine.RegisterComponentType(IID_Market, "Market", Market);