示例#1
0
    void MatchOffers(ResourceMarket market)
    {
        // REMOVE ALL OFFERS WITH 0 amount
        CleanOffers(market);
        // STOP IF THERE IS EITHER NO SUPPLY OR DEMAND TO MATCH
        if (market.Demand.Count == 0 || market.Supply.Count == 0)
        {
            market.AllMatched = true;
            return;
        }
        // GET THE MOST EXPENSIVE DEMAND
        market.Demand.Sort((a, b) => (b.Price.CompareTo(a.Price)));
        Offer offerDemand = market.Demand[0];

        // GET THE CHEAPEST SUPPLY
        market.Supply.Sort((a, b) => (b.Price.CompareTo(a.Price)));
        int   l           = market.Supply.Count;
        Offer offerSupply = market.Supply[l - 1];

        // IF S.price > D.price END
        if (offerSupply.Price > offerDemand.Price)
        {
            market.AllMatched = true;
            return;
        }
        // HOW MUCH CAN WE FULFILL?
        int amountToFulfill = Mathf.Min(offerSupply.Amount, offerDemand.Amount);
        // HOW MUCH CAN THE BUYER AFFORD?
        int amountThatCanBeTraded = Mathf.FloorToInt(offerDemand.Trader.Cash / offerSupply.Price);
        // HOW MUCH CAN WE TRADE?
        int amountToTrade = Mathf.Min(amountToFulfill, amountThatCanBeTraded);

        // STOP IF THAT IS ZERO
        if (amountToTrade == 0)
        {
            return;
        }
        // FOR HOW MUCH CASH?
        float totalPrice = amountToTrade * offerSupply.Price;

        // REMOVE AMOUNT FROM SUPPLY
        offerSupply.Trader.Amount -= amountToTrade;
        offerSupply.Amount        -= amountToTrade;
        // ADD AMOUNT TO BUYER
        offerDemand.Trader.Amount += amountToTrade;
        offerDemand.Amount        -= amountToTrade;
        // REMOVE CASH from BUYER
        offerDemand.Trader.Cash -= totalPrice;
        // ADD CASH TO SELLER
        offerSupply.Trader.Cash += totalPrice;
        // DO IT AGAIN
        print(string.Concat("Trader ", offerSupply.Trader.Name, " sold ", amountToTrade, " ", offerDemand.Commodity, " for ", offerSupply.Price, " each to trader ", offerDemand.Trader.Name, "."));
        MatchOffers(market);
    }
示例#2
0
    private void SetupMarkets()
    {
        // DETERMINE HOW MANY MARKETS WE NEED
        int count = System.Enum.GetValues(typeof(Commodity)).Length;

        for (int i = 0; i <= count - 1; i++)
        {
            // SPLIT INTO SEPERATE MARKETS
            ResourceMarket m = new ResourceMarket((Commodity)i);
            ResourceMarkets.Add(m);
        }
        UIGlue.CreateTraders();
    }
示例#3
0
        public GameObject(int numberOfPlayers, Location location)
        {
            Round   = 2;
            Players = new List <Player>(numberOfPlayers);
            for (int i = 0; i < numberOfPlayers; i++)
            {
                var player = new Player($"Player {i}", 3);
                player.CanBuy = true;
                Players.Add(player);
            }

            AuctionHouse   = new AuctionHouse(location, numberOfPlayers);
            ResourceMarket = new ResourceMarket(location, numberOfPlayers);
            CurrentStep    = Steps.Step1;
            CurrentPhase   = Phase.AuctionPowerPlants;
        }
示例#4
0
    void DisplayMarketStatus(ResourceMarket market)
    {
        print("SUPPLY");
        market.Supply.Sort((a, b) => (b.Price.CompareTo(a.Price)));
        foreach (Offer o in market.Supply)
        {
            print(o.ToString());
        }

        print("DEMAND");
        market.Demand.Sort((a, b) => (b.Price.CompareTo(a.Price)));
        foreach (Offer o in market.Demand)
        {
            print(o.ToString());
        }
    }
示例#5
0
    void CleanOffers(ResourceMarket market)
    {
        for (int i = 0; i <= market.Supply.Count - 1; i++)
        {
            if (market.Supply[i].Amount <= 0)
            {
                market.Supply.RemoveAt(i);
            }
        }

        for (int i = 0; i <= market.Demand.Count - 1; i++)
        {
            if (market.Demand[i].Amount <= 0)
            {
                market.Demand.RemoveAt(i);
            }
        }
    }
示例#6
0
 private void ResupplyResourceMarket()
 {
     ResourceMarket.Refill(CurrentStep);
 }