Exemplo n.º 1
0
 public System.Object ServerCommunication(Version clientversion, ICommunicateable communicator, bool full, System.Object obj)
 {
     try
     {
         Player player = (Player)obj;
         if (clientversion.Major > 0)
         {
             lock (SynchronizeRoot)
             {
                 if (full)
                 {
                     // Receive information about the new player.
                     string company = communicator.ReceiveString();
                     string name    = communicator.ReceiveString();
                     // Create the new player.
                     player         = new Player(Players.NextPlayerId, company, name, Stocks, false, false);
                     player.Capital = ((int)System.Math.Round(Players.AverageValue / 1000, 0)) * 1000;
                     player.ValueHistory.Clear();
                     Players.Add(player);
                 }
                 // Send game informations.
                 StockIndexes.ServerCommunication(clientversion, communicator, full, null);
                 Stocks.ServerCommunication(clientversion, communicator, full, StockIndexes);
                 Players.ServerCommunication(clientversion, communicator, full, player.Id);
                 MarketState.ServerCommunication(clientversion, communicator, full, null);
             }
         }
         return(player);
     }
     catch (System.Exception ex)
     {
         throw ex;
     }
 }
 public void Buy(MarketState marketstate, int stockstobuy)
 {
     try
     {
         if (OnClientBuyStocksEvent != null)
         {
             if (stockstobuy > 0 && stockstobuy <= Stock.Available && Deposit.Player.Capital >= Stock.CalculatePrice(stockstobuy) + Stock.CalculateBrokerage(marketstate, stockstobuy))
             {
                 OnClientBuyStocksEvent(Stock.Id, this, marketstate, stockstobuy);
             }
         }
         else if (stockstobuy > 0 && stockstobuy <= Stock.Available && Deposit.Player.Capital >= Stock.CalculatePrice(stockstobuy) + Stock.CalculateBrokerage(marketstate, stockstobuy))
         {
             Count                  += stockstobuy;
             Stock.Available        -= stockstobuy;
             Stock.OwnedByPlayers   += stockstobuy;
             LastBuyPrice            = Stock.Price;
             Deposit.Player.Capital -= Stock.CalculatePrice(stockstobuy) + Stock.CalculateBrokerage(marketstate, stockstobuy);
         }
     }
     catch (System.Exception ex)
     {
         throw ex;
     }
 }
Exemplo n.º 3
0
 public double CalculateBrokerage(MarketState marketstate, int count)
 {
     try
     {
         double brokerage = (CalculatePrice(count) * (marketstate.Brokerage / 100)) * 100;
         double d = brokerage % 100;
         if (d >= 0 && d < 26)
         {
             brokerage = (brokerage - d + (d >= 13 ? 25 : 0)) / 100;
         }
         else if (d >= 26 && d < 51)
         {
             brokerage = (brokerage - d + (d >= 38 ? 50 : 25)) / 100;
         }
         else if (d >= 51 && d < 75)
         {
             brokerage = (brokerage - d + (d >= 63 ? 75 : 50)) / 100;
         }
         else if (d >= 75 && d < 100)
         {
             brokerage = (brokerage - d + (d >= 88 ? 100 : 75)) / 100;
         }
         return brokerage;
     }
     catch (System.Exception ex)
     {
         throw ex;
     }
 }
 public void Sell(MarketState marketstate, int stockstosell)
 {
     try
     {
         if (OnClientSellStocksEvent != null)
         {
             if (stockstosell > 0 && stockstosell <= Count && Stock.CalculatePrice(stockstosell) >= Stock.CalculateBrokerage(marketstate, stockstosell))
             {
                 OnClientSellStocksEvent(Stock.Id, this, marketstate, stockstosell);
             }
         }
         else if (stockstosell > 0 && stockstosell <= Count && Stock.CalculatePrice(stockstosell) >= Stock.CalculateBrokerage(marketstate, stockstosell))
         {
             Count -= stockstosell;
             Stock.OwnedByPlayers -= stockstosell;
             Stock.Available      += stockstosell;
             if (Count <= 0)
             {
                 LastBuyPrice  = 0;
                 LastSellPrice = 0;
             }
             else
             {
                 LastBuyPrice = Stock.Price;
             }
             Deposit.Player.Capital += Stock.CalculatePrice(stockstosell) - Stock.CalculateBrokerage(marketstate, stockstosell);
         }
     }
     catch (System.Exception ex)
     {
         throw ex;
     }
 }
Exemplo n.º 5
0
 public void Buy(Player player, MarketState marketstate, int stockstobuy)
 {
     try
     {
         player.Buy(this, marketstate, stockstobuy);
     }
     catch (System.Exception ex)
     {
         throw ex;
     }
 }
Exemplo n.º 6
0
 public void Buy(Stock stock, MarketState marketstate, int stockstobuy)
 {
     try
     {
         Deposit.Buy(stock, marketstate, stockstobuy);
     }
     catch (System.Exception ex)
     {
         throw ex;
     }
 }
Exemplo n.º 7
0
 public void Buy(DepositContent content, MarketState marketstate, int stockstobuy)
 {
     try
     {
         Deposit.Buy(content, marketstate, stockstobuy);
     }
     catch (System.Exception ex)
     {
         throw ex;
     }
 }
Exemplo n.º 8
0
 public void Sell(Stock stock, MarketState marketstate, int stockstosell)
 {
     try
     {
         Deposit.Sell(stock, marketstate, stockstosell);
     }
     catch (System.Exception ex)
     {
         throw ex;
     }
 }
Exemplo n.º 9
0
 public void Sell(DepositContent content, MarketState marketstate, int stockstosell)
 {
     try
     {
         Deposit.Sell(content, marketstate, stockstosell);
     }
     catch (System.Exception ex)
     {
         throw ex;
     }
 }
Exemplo n.º 10
0
 public void Sell(Player player, MarketState marketstate, int stockstosell)
 {
     try
     {
         player.Sell(this, marketstate, stockstosell);
     }
     catch (System.Exception ex)
     {
         throw ex;
     }
 }
Exemplo n.º 11
0
 public void Play(MarketState marketstate, System.Random random)
 {
     try
     {
         double d_up = 0D, d_down = 0D;
         int i_up_down = 0, i_min_adjust = 0, i_max_adjust = 0;
         switch (marketstate.State)
         {
             case MarketStateType.Normal:
                 d_up = (Price / 100) * 7;
                 d_down = (Price / 100) * 7;
                 i_up_down = (Available / 100) * 10;
                 i_min_adjust = 1000;
                 i_max_adjust = 1000;
                 if (d_up <= MinPrice)
                     d_up = MinPrice;
                 if (d_down <= MinPrice)
                     d_down = MinPrice;
                 break;
             case MarketStateType.Depression:
                 d_up = (Price / 100) * 3.5;
                 d_down = (Price / 100) * 7;
                 i_up_down = (Available / 100) * 5;
                 i_min_adjust = 1500;
                 i_max_adjust = 500;
                 if (d_up <= MinPrice)
                     d_up = MinPrice;
                 if (d_down <= MinPrice * 2)
                     d_down = MinPrice * 2;
                 break;
             case MarketStateType.Boom:
                 d_up = (Price / 100) * 7;
                 d_down = (Price / 100) * 3.5;
                 i_up_down = (Available / 100) * 15;
                 i_min_adjust = 500;
                 i_max_adjust = 1500;
                 if (d_up <= MinPrice * 2)
                     d_up = MinPrice * 2;
                 if (d_down <= MinPrice)
                     d_down = MinPrice;
                 break;
         }
         if (i_up_down <= 0)
             i_up_down = MAX_AVAILABLE / 1000;
         MinPrice += (random.Next(i_max_adjust) / 100) - (random.Next(i_min_adjust) / 100);
         MaxPrice += (random.Next(i_max_adjust) / 100) - (random.Next(i_min_adjust) / 100);
         Price += random.Next(d_up > int.MaxValue ? int.MaxValue : (int) d_up) - random.Next(d_down > int.MaxValue ? int.MaxValue : (int) d_down);
         Available += random.Next(i_up_down) - random.Next(i_up_down);
     }
     catch (System.Exception ex)
     {
         throw ex;
     }
 }
Exemplo n.º 12
0
 public void Buy(DepositContent content, MarketState marketstate, int stockstobuy)
 {
     try
     {
         if (this.ContainsValue(content))
         {
             content.Buy(marketstate, stockstobuy);
         }
     }
     catch (System.Exception ex)
     {
         throw ex;
     }
 }
Exemplo n.º 13
0
 public void Save(Version fv, WsgFileStream fs)
 {
     try
     {
         if (BeforeSaveEvent != null)
         {
             BeforeSaveEvent();
         }
         if (fv.Major > 0)
         {
             while (PlayTimer.Enabled)
             {
                 PlayTimer.Stop();
             }
             fs.Seek(0, System.IO.SeekOrigin.Begin);
             FileVersion.Save(fv, fs);
             StockIndexes.Save(fv, fs);
             Stocks.Save(fv, fs);
             Players.Save(fv, fs);
             MarketState.Save(fv, fs);
             fs.Flush();
             FileName = fs.Name;
             while (!PlayTimer.Enabled)
             {
                 PlayTimer.Start();
             }
         }
         if (AfterSaveEvent != null)
         {
             AfterSaveEvent();
         }
         if (UpdateStockInformationsEvent != null)
         {
             UpdateStockInformationsEvent();
         }
         if (UpdatePlayerInformationsEvent != null)
         {
             UpdatePlayerInformationsEvent();
         }
     }
     catch (System.Exception ex)
     {
         while (!PlayTimer.Enabled)
         {
             PlayTimer.Start();
         }
         throw ex;
     }
 }
Exemplo n.º 14
0
 public void Sell(Stock stock, MarketState marketstate, int stockstosell)
 {
     try
     {
         DepositContent content = null;
         if (this.TryGetValue(stock.Id, out content))
         {
             this.Sell(content, marketstate, stockstosell);
         }
     }
     catch (System.Exception ex)
     {
         throw ex;
     }
 }
Exemplo n.º 15
0
 public System.Object ClientCommunication(Version serverversion, ICommunicateable communicator, bool full, System.Object obj)
 {
     try
     {
         if (serverversion.Major > 0)
         {
             lock (SynchronizeRoot)
             {
                 if (full)
                 {
                     // Send information about the client.
                     communicator.SendString(CurrentPlayer.Company);
                     communicator.SendString(CurrentPlayer.Name);
                     // Clear and reset game informations.
                     while (PlayTimer.Enabled)
                     {
                         PlayTimer.Stop();
                     }
                     while (StockIndexes.Count > 0)
                     {
                         StockIndexes.Clear();
                     }
                     while (Stocks.Count > 0)
                     {
                         Stocks.Clear();
                     }
                     while (Players.Count > 0)
                     {
                         Players.Clear();
                     }
                     MarketState.Reset(Random);
                     CurrentPlayer = null;
                 }
                 // Receive new game informations.
                 StockIndexes.ClientCommunication(serverversion, communicator, full, null);
                 Stocks.ClientCommunication(serverversion, communicator, full, StockIndexes);
                 CurrentPlayer = (Player)Players.ClientCommunication(serverversion, communicator, full, Stocks);
                 MarketState.ClientCommunication(serverversion, communicator, full, null);
             }
         }
         return(this);
     }
     catch (System.Exception ex)
     {
         throw ex;
     }
 }
Exemplo n.º 16
0
 public void Play(MarketState marketstate, System.Random random)
 {
     try
     {
         if (this.Count > 0)
         {
             foreach (IPlayable p in this.Values)
             {
                 p.Play(marketstate, random);
             }
         }
     }
     catch (System.Exception ex)
     {
         throw ex;
     }
 }
Exemplo n.º 17
0
        private void SellStocks(string stockid, INetworkTradeable tradeable, MarketState marketstate, int stockstosell)
        {
            try
            {
                if (SelectedServer.Version.Major > 0)
                {
                    lock (SynchronizeRoot)
                    {
                        SendCommand(Commands.SellStocks);
                        SendString(stockid);
                        Game.ClientSellingStocks(SelectedServer.Version, this, tradeable, marketstate, stockstosell);
                    }
                    if (Game.UpdateStockInformationsEvent != null)
                    {
                        Synchronize.Invoke(Game.UpdateStockInformationsEvent, null);
                    }
                    if (Game.UpdatePlayerInformationsEvent != null)
                    {
                        Synchronize.Invoke(Game.UpdatePlayerInformationsEvent, null);
                    }
                }
            }
            catch (System.ObjectDisposedException)
            {
                // The socket has been closed.
            }
            catch (System.Net.Sockets.SocketException ex)
            {
                switch (ex.SocketErrorCode)
                {
                case System.Net.Sockets.SocketError.ConnectionAborted:
                    // Don't throw an excetion, the connection has just aborted.
                    break;

                default:
                    throw ex;
                }
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
        }
Exemplo n.º 18
0
 public void Reset(System.Random random, bool startingserver)
 {
     try
     {
         bool b = true;
         if (BeforeResetEvent != null && !startingserver)
         {
             b = BeforeResetEvent();
         }
         if (b)
         {
             while (PlayTimer.Enabled)
             {
                 PlayTimer.Stop();
             }
             FileName = null;
             Stocks.Reset(random);
             Players.Reset(random);
             MarketState.Reset(random);
             while (!PlayTimer.Enabled)
             {
                 PlayTimer.Start();
             }
             if (AfterResetEvent != null && !startingserver)
             {
                 AfterResetEvent();
             }
             if (UpdateStockInformationsEvent != null)
             {
                 UpdateStockInformationsEvent();
             }
             if (UpdatePlayerInformationsEvent != null)
             {
                 UpdatePlayerInformationsEvent();
             }
         }
     }
     catch (System.Exception ex)
     {
         throw ex;
     }
 }
Exemplo n.º 19
0
 public Game(System.ComponentModel.ISynchronizeInvoke si, string setupfilename) : base()
 {
     try
     {
         Random        = new System.Random();
         FileVersion   = new Version(FILEVERSION_MAJOR, FILEVERSION_MINOR);
         StockIndexes  = new StockIndexes();
         Stocks        = new Stocks();
         Players       = new Players();
         Configuration = new Configuration(setupfilename, Random, StockIndexes, Stocks, Players);
         if (StockIndexes == null)
         {
             StockIndexes = Configuration.StockIndexes;
         }
         if (Stocks == null)
         {
             Stocks = Configuration.Stocks;
         }
         if (Players == null)
         {
             Players = Configuration.Players;
         }
         MarketState   = new MarketState();
         CurrentPlayer = new Player(Players.NextPlayerId, String.Empty, String.Empty, Stocks, false, true);
         Players.Add(CurrentPlayer);
         SynchronizeRoot               = Singleton.Instance;
         PlayTimer                     = new System.Timers.Timer(PLAY_TIMER_INTERVAL);
         PlayTimer.AutoReset           = true;
         PlayTimer.Elapsed            += new System.Timers.ElapsedEventHandler(PlayTimerElapsed);
         PlayTimer.SynchronizingObject = si;
         while (!PlayTimer.Enabled)
         {
             PlayTimer.Start();
         }
         Disposed = false;
     }
     catch (System.Exception ex)
     {
         throw ex;
     }
 }
Exemplo n.º 20
0
 public void Play(MarketState marketstate, System.Random random)
 {
     try
     {
         lock (SynchronizeRoot)
         {
             Players.Play(marketstate, random);
             Stocks.Play(marketstate, random);
             MarketState.Play(marketstate, random);
         }
         if (UpdateStockInformationsEvent != null)
         {
             UpdateStockInformationsEvent();
         }
         if (UpdatePlayerInformationsEvent != null)
         {
             UpdatePlayerInformationsEvent();
         }
     }
     catch (System.Exception ex)
     {
         throw ex;
     }
 }
Exemplo n.º 21
0
 public StockForm(MainForm mainform, Stock stock, Player player, MarketState marketstate) : base()
 {
     InitializeComponent();
     try
     {
         MainForm                = mainform;
         Stock                   = stock;
         Player                  = player;
         MarketState             = marketstate;
         LineGraph               = new LineGraph(this.labelStockName.Location.X, this.labelStockName.Location.Y, this.panelStockGraph.Width - (this.labelStockName.Location.X * 2), this.panelStockGraph.Height - (this.labelStockName.Location.Y * 2), this.panelStockGraph.BackColor);
         LineGraph.IsCurrency    = true;
         LineGraph.XMin          = 0;
         LineGraph.XMax          = Stock.PriceHistory.Capacity;
         LineGraph.GridLineStepX = System.Math.Floor((LineGraph.XMax - LineGraph.XMin) / 5);
         System.Globalization.NumberFormatInfo nfi = System.Globalization.NumberFormatInfo.CurrentInfo;
         this.Text = this.Text + ": " + Stock.Name;
         this.textBoxStockName.ReadOnly          = true;
         this.textBoxStockName.TabStop           = false;
         this.textBoxStockName.Text              = stock.Name;
         this.comboBoxStockIndexes.DisplayMember = "Name";
         this.comboBoxStockIndexes.TabStop       = false;
         if (Stock.StockIndexes.Count > 0)
         {
             foreach (StockIndex stockindex in Stock.StockIndexes.Values)
             {
                 this.comboBoxStockIndexes.Items.Add(stockindex);
             }
         }
         if (this.comboBoxStockIndexes.Items.Count > 0)
         {
             this.comboBoxStockIndexes.SelectedIndex = 0;
         }
         this.textBoxStockPrice.ReadOnly                   = true;
         this.textBoxStockPrice.TabStop                    = false;
         this.textBoxStockPrice.TextAlign                  = System.Windows.Forms.HorizontalAlignment.Right;
         this.labelStockPriceDifference.Text               = nfi.PositiveSign + "/" + nfi.NegativeSign;
         this.textBoxStockPriceDifference.ReadOnly         = true;
         this.textBoxStockPriceDifference.TabStop          = false;
         this.textBoxStockPriceDifference.TextAlign        = System.Windows.Forms.HorizontalAlignment.Right;
         this.textBoxStockPriceDifferenceProcent.ReadOnly  = true;
         this.textBoxStockPriceDifferenceProcent.TabStop   = false;
         this.textBoxStockPriceDifferenceProcent.TextAlign = System.Windows.Forms.HorizontalAlignment.Right;
         this.textBoxStockAvailable.ReadOnly               = true;
         this.textBoxStockAvailable.TabStop                = false;
         this.textBoxStockAvailable.TextAlign              = System.Windows.Forms.HorizontalAlignment.Right;
         this.textBoxPlayerCapital.ReadOnly                = true;
         this.textBoxPlayerCapital.TabStop                 = false;
         this.textBoxPlayerCapital.TextAlign               = System.Windows.Forms.HorizontalAlignment.Right;
         this.textBoxPlayerDepositContent.ReadOnly         = true;
         this.textBoxPlayerDepositContent.TabStop          = false;
         this.textBoxPlayerDepositContent.TextAlign        = System.Windows.Forms.HorizontalAlignment.Right;
         this.radioButtonTradeBuy.Checked                  = true;
         this.textBoxTradeCountValue.ReadOnly              = true;
         this.textBoxTradeCountValue.TabStop               = false;
         this.textBoxTradeCountValue.TextAlign             = System.Windows.Forms.HorizontalAlignment.Right;
         this.textBoxTradeBrokerage.ReadOnly               = true;
         this.textBoxTradeBrokerage.TabStop                = false;
         this.textBoxTradeBrokerage.TextAlign              = System.Windows.Forms.HorizontalAlignment.Right;
         this.textBoxTradeBrokeragePrice.ReadOnly          = true;
         this.textBoxTradeBrokeragePrice.TabStop           = false;
         this.textBoxTradeBrokeragePrice.TextAlign         = System.Windows.Forms.HorizontalAlignment.Right;
         this.textBoxTradeTotal.ReadOnly                   = true;
         this.textBoxTradeTotal.TabStop                    = false;
         this.textBoxTradeTotal.TextAlign                  = System.Windows.Forms.HorizontalAlignment.Right;
         this.numericUpDownTradeCount.Select();
         UpdateStockInformations();
         UpdatePlayerInformations();
         UpdateTradeInformations();
     }
     catch (System.Exception ex)
     {
         throw ex;
     }
 }
Exemplo n.º 22
0
        public void Play(MarketState marketstate, System.Random random)
        {
            try
            {
                if (IsComputer)
                {
                    if (Deposit.Count > 0)
                    {
                        StockIndex lastindex        = null;
                        double     lastpriceaverage = 0D;
                        double     savecapital      = 0D;
                        foreach (DepositContent content in Deposit.Values)
                        {
                            if (content.Stock.StockIndexes.Count > 0)
                            {
                                System.Collections.Generic.Dictionary <string, StockIndex> .Enumerator e = content.Stock.StockIndexes.GetEnumerator();
                                if (e.MoveNext())
                                {
                                    if (lastindex == null)
                                    {
                                        lastindex        = e.Current.Value;
                                        lastpriceaverage = lastindex.PriceAverage;
                                        savecapital      = (lastindex.PriceAverage * 15);
                                    }
                                    else if (e.Current.Value.Id != lastindex.Id)
                                    {
                                        lastindex        = e.Current.Value;
                                        lastpriceaverage = lastindex.PriceAverage;
                                        savecapital      = (lastindex.PriceAverage * 15);
                                    }
                                    // Handle computer players deposit.
                                    int buyprocentlow = 0, buyprocentmiddle = 0, buyprocenthigh = 0, maxbuyprepoch = MAX_BUY_PR_EPOCH, stockstobuy = 0;
                                    int sellprocentlow = 0, sellprocentmiddle = 0, sellprocenthigh = 0, maxsellprepoch = MAX_SELL_PR_EPOCH, stockstosell = 0;
                                    switch (marketstate.State)
                                    {
                                    case MarketStateType.Normal:
                                        buyprocentlow     = 75;
                                        buyprocentmiddle  = 85;
                                        buyprocenthigh    = 95;
                                        sellprocentlow    = 50;
                                        sellprocentmiddle = 70;
                                        sellprocenthigh   = 90;
                                        break;

                                    case MarketStateType.Depression:
                                        buyprocentlow     = 60;
                                        buyprocentmiddle  = 70;
                                        buyprocenthigh    = 80;
                                        sellprocentlow    = 30;
                                        sellprocentmiddle = 50;
                                        sellprocenthigh   = 70;
                                        break;

                                    case MarketStateType.Boom:
                                        buyprocentlow     = 70;
                                        buyprocentmiddle  = 80;
                                        buyprocenthigh    = 90;
                                        sellprocentlow    = 70;
                                        sellprocentmiddle = 80;
                                        sellprocenthigh   = 90;
                                        break;

                                    default:
                                        buyprocentlow     = 100;
                                        buyprocentmiddle  = 100;
                                        buyprocenthigh    = 100;
                                        sellprocentlow    = 100;
                                        sellprocentmiddle = 100;
                                        sellprocenthigh   = 100;
                                        break;
                                    }
                                    // Try to buy stocks.
                                    if (content.Stock.Price < content.Stock.MinPrice * 4 && content.Stock.Available > 0)
                                    {
                                        if (random.Next(100) > buyprocentlow / 1.75 && maxbuyprepoch > 0)
                                        {
                                            stockstobuy = MAX_STOCKS_TO_BUY;
                                            if (stockstobuy > content.Stock.Available)
                                            {
                                                stockstobuy = content.Stock.Available;
                                            }
                                            while (stockstobuy > 0 && content.Stock.CalculatePrice(stockstobuy) + content.Stock.CalculateBrokerage(marketstate, stockstobuy) > Capital - savecapital)
                                            {
                                                stockstobuy = (int)System.Math.Floor((double)(stockstobuy / 10));
                                            }
                                            if (stockstobuy > 5)
                                            {
                                                content.Buy(marketstate, random.Next(stockstobuy));
                                                maxbuyprepoch -= 1;
                                            }
                                            else if (stockstobuy > 0)
                                            {
                                                content.Buy(marketstate, stockstobuy);
                                                maxbuyprepoch -= 1;
                                            }
                                        }
                                    }
                                    else if (content.Stock.Price < lastpriceaverage / 8 && (content.LastSellPrice == 0 || content.Stock.Price < content.LastSellPrice || content.Stock.Price > content.LastSellPrice * 1.5) && content.Stock.Available > 0)
                                    {
                                        // Try to buy stocks.
                                        if (random.Next(100) > buyprocentlow && maxbuyprepoch > 0)
                                        {
                                            stockstobuy = MAX_STOCKS_TO_BUY;
                                            if (stockstobuy > content.Stock.Available)
                                            {
                                                stockstobuy = content.Stock.Available;
                                            }
                                            while (stockstobuy > 0 && content.Stock.CalculatePrice(stockstobuy) + content.Stock.CalculateBrokerage(marketstate, stockstobuy) > Capital - savecapital)
                                            {
                                                stockstobuy = (int)System.Math.Floor((double)(stockstobuy / 10));
                                            }
                                            if (stockstobuy > 5)
                                            {
                                                content.Buy(marketstate, random.Next(stockstobuy));
                                                maxbuyprepoch -= 1;
                                            }
                                            else if (stockstobuy > 0)
                                            {
                                                content.Buy(marketstate, stockstobuy);
                                                maxbuyprepoch -= 1;
                                            }
                                        }
                                    }
                                    else if (content.Stock.Price < lastpriceaverage / 4 && (content.LastSellPrice == 0 || content.Stock.Price < content.LastSellPrice || content.Stock.Price > content.LastSellPrice * 1.5) && content.Stock.Available > 0)
                                    {
                                        // Try to buy stocks.
                                        if (random.Next(100) > buyprocentmiddle && maxbuyprepoch > 0)
                                        {
                                            stockstobuy = MAX_STOCKS_TO_BUY;
                                            if (stockstobuy > content.Stock.Available)
                                            {
                                                stockstobuy = content.Stock.Available;
                                            }
                                            while (stockstobuy > 0 && content.Stock.CalculatePrice(stockstobuy) + content.Stock.CalculateBrokerage(marketstate, stockstobuy) > Capital - savecapital)
                                            {
                                                stockstobuy = (int)System.Math.Floor((double)(stockstobuy / 10));
                                            }
                                            if (stockstobuy >= 10)
                                            {
                                                stockstobuy = stockstobuy / 2;
                                                if (stockstobuy > 5)
                                                {
                                                    content.Buy(marketstate, random.Next(stockstobuy));
                                                    maxbuyprepoch -= 1;
                                                }
                                                else if (stockstobuy > 0)
                                                {
                                                    content.Buy(marketstate, stockstobuy);
                                                    maxbuyprepoch -= 1;
                                                }
                                            }
                                        }
                                    }
                                    else if (content.Stock.Price < lastpriceaverage && (content.LastSellPrice == 0 || content.Stock.Price < content.LastSellPrice || content.Stock.Price > content.LastSellPrice * 1.5) && content.Stock.Available > 0)
                                    {
                                        // Try to buy stocks.
                                        if (random.Next(100) > buyprocenthigh && maxbuyprepoch > 0)
                                        {
                                            stockstobuy = MAX_STOCKS_TO_BUY;
                                            if (stockstobuy > content.Stock.Available)
                                            {
                                                stockstobuy = content.Stock.Available;
                                            }
                                            while (stockstobuy > 0 && content.Stock.CalculatePrice(stockstobuy) + content.Stock.CalculateBrokerage(marketstate, stockstobuy) > Capital - savecapital)
                                            {
                                                stockstobuy = (int)System.Math.Floor((double)(stockstobuy / 10));
                                            }
                                            if (stockstobuy >= 20)
                                            {
                                                stockstobuy = stockstobuy / 4;
                                                if (stockstobuy > 5)
                                                {
                                                    content.Buy(marketstate, random.Next(stockstobuy));
                                                    maxbuyprepoch -= 1;
                                                }
                                                else if (stockstobuy > 0)
                                                {
                                                    content.Buy(marketstate, stockstobuy);
                                                    maxbuyprepoch -= 1;
                                                }
                                            }
                                        }
                                    }
                                    // Try to sell stocks.
                                    if (marketstate.State == MarketStateType.Depression && content.Stock.Price < content.LastBuyPrice / 4 && content.LastBuyPrice > content.Stock.MinPrice * 50)
                                    {
                                        // No profit expected.
                                        stockstosell = content.Count;
                                        if (stockstosell > 5)
                                        {
                                            content.Sell(marketstate, random.Next(stockstosell));
                                            maxsellprepoch -= 1;
                                        }
                                        else
                                        {
                                            content.Sell(marketstate, stockstosell);
                                            maxsellprepoch -= 1;
                                        }
                                    }
                                    else if (content.Count > 0 && content.Stock.Price > content.LastBuyPrice * 2 && content.Stock.Price > content.Stock.MinPrice * 5)
                                    {
                                        // Try to sell stocks.
                                        if (random.Next(100) > sellprocentlow && maxsellprepoch > 0)
                                        {
                                            stockstosell = content.Count;
                                            if (stockstosell > 5)
                                            {
                                                content.Sell(marketstate, random.Next(stockstosell));
                                                maxsellprepoch -= 1;
                                            }
                                            else
                                            {
                                                content.Sell(marketstate, stockstosell);
                                                maxsellprepoch -= 1;
                                            }
                                        }
                                    }
                                    else if (content.Count > 0 && content.Stock.Price > content.LastBuyPrice * 1.5 && content.Stock.Price > content.Stock.MinPrice * 5)
                                    {
                                        // Try to sell stocks.
                                        if (random.Next(100) > sellprocentmiddle && maxsellprepoch > 0)
                                        {
                                            stockstosell = content.Count;
                                            if (stockstosell >= 10)
                                            {
                                                stockstosell = stockstosell / 2;
                                                if (stockstosell > 5)
                                                {
                                                    content.Sell(marketstate, random.Next(stockstosell));
                                                    maxsellprepoch -= 1;
                                                }
                                                else
                                                {
                                                    content.Sell(marketstate, stockstosell);
                                                    maxsellprepoch -= 1;
                                                }
                                            }
                                            else
                                            {
                                                content.Sell(marketstate, stockstosell);
                                                maxsellprepoch -= 1;
                                            }
                                        }
                                    }
                                    else if (content.Count > 0 && content.Stock.Price > content.LastBuyPrice * 1.25 && content.Stock.Price > content.Stock.MinPrice * 5)
                                    {
                                        // Try to sell stocks.
                                        if (random.Next(100) > sellprocenthigh && maxsellprepoch > 0)
                                        {
                                            stockstosell = content.Count;
                                            if (stockstosell >= 20)
                                            {
                                                stockstosell = stockstosell / 4;
                                                if (stockstosell > 5)
                                                {
                                                    content.Sell(marketstate, random.Next(stockstosell));
                                                    maxsellprepoch -= 1;
                                                }
                                                else
                                                {
                                                    content.Sell(marketstate, stockstosell);
                                                    maxsellprepoch -= 1;
                                                }
                                            }
                                            else
                                            {
                                                content.Sell(marketstate, stockstosell);
                                                maxsellprepoch -= 1;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                ValueHistory.AddHistory(Value);
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
        }
Exemplo n.º 23
0
        public void Play(MarketState marketstate, System.Random random)
        {
            try
            {
                int r = 0;
                switch (State)
                {
                case MarketStateType.Normal:
                    r = random.Next(100);
                    if (r < 10)
                    {
                        State = MarketStateType.Depression;
                        while (Epochs == 0)
                        {
                            Epochs = random.Next(20);
                        }
                    }
                    else if (r > 85)
                    {
                        State = MarketStateType.Boom;
                        while (Epochs == 0)
                        {
                            Epochs = random.Next(20);
                        }
                    }
                    r = random.Next(100);
                    if (r < 10 && Brokerage > MIN_BROKERAGE)
                    {
                        Brokerage -= 0.25;
                    }
                    else if (r > 90 && Brokerage < MAX_BROKERAGE)
                    {
                        Brokerage += 0.25;
                    }
                    break;

                case MarketStateType.Depression:
                    if (Epochs > 0)
                    {
                        Epochs -= 1;
                    }
                    else
                    {
                        State  = MarketStateType.Normal;
                        Epochs = 0;
                    }
                    r = random.Next(100);
                    if (r < 10 && Brokerage > MIN_BROKERAGE)
                    {
                        Brokerage -= 0.25;
                    }
                    else if (r > 85 && Brokerage < MAX_BROKERAGE)
                    {
                        Brokerage += 0.25;
                    }
                    break;

                case MarketStateType.Boom:
                    if (Epochs > 0)
                    {
                        Epochs -= 1;
                    }
                    else
                    {
                        State  = MarketStateType.Normal;
                        Epochs = 0;
                    }
                    r = random.Next(100);
                    if (r < 15 && Brokerage > MIN_BROKERAGE)
                    {
                        Brokerage -= 0.25;
                    }
                    else if (r > 90 && Brokerage < MAX_BROKERAGE)
                    {
                        Brokerage += 0.25;
                    }
                    break;
                }
            }
            catch (System.Exception ex)
            {
                throw ex;
            }
        }
Exemplo n.º 24
0
 public System.Object Load(Version fv, WsgFileStream fs, System.Object obj)
 {
     try
     {
         if (BeforeLoadEvent != null)
         {
             BeforeLoadEvent();
         }
         if (fv.Major > 0)
         {
             while (PlayTimer.Enabled)
             {
                 PlayTimer.Stop();
             }
             fs.Seek(0, System.IO.SeekOrigin.Begin);
             Version loadedfv = (Version)FileVersion.Load(fv, fs, null);
             while (StockIndexes.Count > 0)
             {
                 StockIndexes.Clear();
             }
             while (Stocks.Count > 0)
             {
                 Stocks.Clear();
             }
             while (Players.Count > 0)
             {
                 Players.Clear();
             }
             MarketState.Reset(Random);
             CurrentPlayer = null;
             StockIndexes.Load(loadedfv, fs, null);
             Stocks.Load(loadedfv, fs, StockIndexes);
             CurrentPlayer = (Player)Players.Load(loadedfv, fs, Stocks);
             MarketState.Load(loadedfv, fs, null);
             FileName = fs.Name;
             while (!PlayTimer.Enabled)
             {
                 PlayTimer.Start();
             }
         }
         if (AfterLoadEvent != null)
         {
             AfterLoadEvent();
         }
         if (UpdateStockInformationsEvent != null)
         {
             UpdateStockInformationsEvent();
         }
         if (UpdatePlayerInformationsEvent != null)
         {
             UpdatePlayerInformationsEvent();
         }
         return(this);
     }
     catch (VersionNotSupportedException ex)
     {
         while (!PlayTimer.Enabled)
         {
             PlayTimer.Start();
         }
         throw ex;
     }
     catch (System.Exception ex)
     {
         while (!PlayTimer.Enabled)
         {
             PlayTimer.Start();
         }
         throw ex;
     }
 }
Exemplo n.º 25
0
 public void ClientBuyingStocks(Version serverversion, ICommunicateable communicator, INetworkTradeable tradeable, MarketState marketstate, int stockstobuy)
 {
     try
     {
         if (serverversion.Major > 0)
         {
             lock (SynchronizeRoot)
             {
                 tradeable.ClientBuyStocks(serverversion, communicator, marketstate, stockstobuy);
             }
         }
     }
     catch (System.Exception ex)
     {
         throw ex;
     }
 }
 public void ClientSellStocks(Version serverversion, ICommunicateable communicator, MarketState marketstate, int stockstosell)
 {
     try
     {
         if (serverversion.Major > 0)
         {
             communicator.SendInt(stockstosell);
             communicator.SendDouble(Stock.Price);
             communicator.SendDouble(Stock.CalculatePrice(stockstosell));
             communicator.SendDouble(Stock.CalculateBrokerage(marketstate, stockstosell));
             Count = communicator.ReceiveInt();
             Stock.OwnedByPlayers   = communicator.ReceiveInt();
             Stock.Available        = communicator.ReceiveInt();
             LastBuyPrice           = communicator.ReceiveDouble();
             LastSellPrice          = communicator.ReceiveDouble();
             Deposit.Player.Capital = communicator.ReceiveDouble();
         }
     }
     catch (System.Exception ex)
     {
         throw ex;
     }
 }