public Configuration(string setupfilename, System.Random random, StockIndexes stockindexes, Stocks stocks, Players players) : base()
 {
     try
     {
         SetupFileName = setupfilename;
         StockIndexes  = stockindexes;
         if (StockIndexes == null)
         {
             StockIndexes = new StockIndexes();
         }
         Stocks = stocks;
         if (Stocks == null)
         {
             Stocks = new Stocks();
         }
         Players = players;
         if (Players == null)
         {
             Players = new Players();
         }
         Load(random);
     }
     catch (System.Exception ex)
     {
         throw ex;
     }
 }
Beispiel #2
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 StockIndex(Version serverversion, ICommunicateable communicator, bool full, System.Object obj) : base()
 {
     try
     {
         Stocks = new Stocks();
         ClientCommunication(serverversion, communicator, full, obj);
     }
     catch (System.Exception ex)
     {
         throw ex;
     }
 }
 public StockIndex(Version fv, WsgFileStream fs, System.Object obj) : base()
 {
     try
     {
         Stocks = new Stocks();
         Load(fv, fs, obj);
     }
     catch (System.Exception ex)
     {
         throw ex;
     }
 }
 public StockIndex(string id, string name) : base()
 {
     try
     {
         Id     = id;
         Name   = name;
         Stocks = new Stocks();
     }
     catch (System.Exception ex)
     {
         throw ex;
     }
 }
Beispiel #6
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;
     }
 }
Beispiel #7
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;
     }
 }
Beispiel #8
0
 public Player(int id, string company, string name, Stocks stocks, bool iscomputer, bool isyou) : base()
 {
     try
     {
         Id           = id;
         Company      = company;
         Name         = name;
         IsComputer   = iscomputer;
         IsYou        = isyou && !IsComputer;
         Deposit      = new Deposit(this, stocks);
         ValueHistory = new DoubleHistory();
         Reset(null);
     }
     catch (System.Exception ex)
     {
         throw ex;
     }
 }
 public Deposit(Player player, Stocks stocks) : base(stocks.Count)
 {
     try
     {
         Player = player;
         if (stocks.Count > 0)
         {
             foreach (Stock stock in stocks.Values)
             {
                 this.Add(stock.Id, new DepositContent(this, stock));
             }
         }
     }
     catch (System.Exception ex)
     {
         throw ex;
     }
 }
Beispiel #10
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;
     }
 }
Beispiel #11
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;
     }
 }
Beispiel #12
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;
     }
 }
 public void Load(System.Random random)
 {
     try
     {
         while (StockIndexes.Count > 0)
         {
             StockIndexes.Clear();
         }
         while (Stocks.Count > 0)
         {
             Stocks.Clear();
         }
         if (XmlDocument == null)
         {
             XmlDocument = new System.Xml.XmlDocument();
         }
         XmlDocument.Load(SetupFileName);
         AllStocksXmlNode = XmlDocument.DocumentElement.SelectSingleNode("AllStocks");
         if (AllStocksXmlNode == null)
         {
             throw new System.Exception("No node named 'AllStocks' in the file named '" + SetupFileName + "'.");
         }
         else if (AllStocksXmlNode.Attributes["indexid"] == null)
         {
             throw new System.Exception("No attribute named 'indexid' on a node named '" + AllStocksXmlNode.LocalName + "' in the file named '" + SetupFileName + "'.");
         }
         System.Xml.XmlNodeList xmlnodes = XmlDocument.DocumentElement.SelectNodes("StockIndex");
         if (xmlnodes == null)
         {
             throw new System.Exception("No nodes named 'StockIndex' in the file named '" + SetupFileName + "'.");
         }
         else if (xmlnodes.Count == 0)
         {
             throw new System.Exception("No nodes named 'StockIndex' in the file named '" + SetupFileName + "'.");
         }
         foreach (System.Xml.XmlNode xmlnode in xmlnodes)
         {
             if (xmlnode.Attributes["indexid"] == null)
             {
                 throw new System.Exception("No attribute named 'indexid' on a node named '" + xmlnode.LocalName + "' in the file named '" + SetupFileName + "'.");
             }
             if (xmlnode.Attributes["name"] == null)
             {
                 throw new System.Exception("No attribute named 'named' on a node named '" + xmlnode.LocalName + "' in the file named '" + SetupFileName + "'.");
             }
             StockIndex stockindex = null;
             if (!StockIndexes.TryGetValue(xmlnode.Attributes["indexid"].Value, out stockindex))
             {
                 stockindex = new StockIndex(xmlnode.Attributes["indexid"].Value, xmlnode.Attributes["name"].Value);
                 StockIndexes.Add(stockindex.Id, stockindex);
             }
             System.Xml.XmlNodeList xmlchildnodes = xmlnode.SelectNodes("Stock");
             if (xmlchildnodes == null)
             {
                 throw new System.Exception("No nodes named 'Stock' under the node named '" + xmlnode.LocalName + "' where indexid is '" + xmlnode.Attributes["indexid"].Value + "' in the file named '" + SetupFileName + "'.");
             }
             if (xmlchildnodes.Count == 0)
             {
                 throw new System.Exception("No nodes named 'Stock' under the node named '" + xmlnode.LocalName + "' where indexid is '" + xmlnode.Attributes["indexid"].Value + "' in the file named '" + SetupFileName + "'.");
             }
             foreach (System.Xml.XmlNode xmlchildnode in xmlchildnodes)
             {
                 if (xmlchildnode.Attributes["stockid"] == null)
                 {
                     throw new System.Exception("No attribute named 'stockid' on a node named '" + xmlchildnode.LocalName + "' under the node named '" + xmlnode.LocalName + "' where indexid is '" + xmlnode.Attributes["indexid"].Value + "' in the file named '" + SetupFileName + "'.");
                 }
                 Stock stock = null;
                 if (!Stocks.TryGetValue(xmlchildnode.Attributes["stockid"].Value, out stock))
                 {
                     if (xmlchildnode.HasChildNodes)
                     {
                         if (xmlchildnode.HasChildNodes)
                         {
                             stock = new Stock(xmlchildnode.Attributes["stockid"].Value, xmlchildnode.FirstChild.Value, stockindex, random);
                             Stocks.Add(stock.Id, stock);
                         }
                     }
                 }
                 else if (xmlchildnode.HasChildNodes)
                 {
                     stock.AddStockIndex(stockindex);
                 }
             }
         }
         if (AllStocksIndexId.Length > 0 && AllStocksIndexName.Length > 0)
         {
             if (!StockIndexes.ContainsKey(AllStocksIndexId))
             {
                 StockIndex stockindex = new StockIndex(AllStocksIndexId, AllStocksIndexName);
                 foreach (Stock stock in Stocks.Values)
                 {
                     stock.StockIndexes.Add(stockindex.Id, stockindex);
                     stockindex.Stocks.Add(stock.Id, stock);
                 }
                 StockIndexes.Add(stockindex.Id, stockindex);
             }
         }
         xmlnodes = XmlDocument.DocumentElement.SelectNodes("Player");
         if (xmlnodes == null)
         {
             throw new System.Exception("No nodes named 'Player' in the file named '" + SetupFileName + "'.");
         }
         else if (xmlnodes.Count == 0)
         {
             throw new System.Exception("No nodes named 'Player' in the file named '" + SetupFileName + "'.");
         }
         foreach (System.Xml.XmlNode xmlnode in xmlnodes)
         {
             if (xmlnode.Attributes["company"] == null)
             {
                 throw new System.Exception("No attribute named 'company' on a node named '" + xmlnode.LocalName + "' in the file named '" + SetupFileName + "'.");
             }
             if (xmlnode.HasChildNodes)
             {
                 Players.Add(new Player(Players.NextPlayerId, xmlnode.Attributes["company"].Value, xmlnode.FirstChild.Value, Stocks));
             }
         }
     }
     catch (System.Exception ex)
     {
         throw ex;
     }
 }
Beispiel #14
0
 public Player(int id, string company, string name, Stocks stocks) : this(id, company, name, stocks, true, false)
 {
 }
Beispiel #15
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;
     }
 }
Beispiel #16
0
 public void Dispose(bool disposing)
 {
     try
     {
         if (!Disposed)
         {
             if (PlayTimer != null)
             {
                 while (PlayTimer.Enabled)
                 {
                     PlayTimer.Stop();
                 }
             }
             if (disposing)
             {
                 if (Random != null)
                 {
                     Random = null;
                 }
                 if (StockIndexes != null)
                 {
                     while (StockIndexes.Count > 0)
                     {
                         StockIndexes.Clear();
                     }
                     StockIndexes = null;
                 }
                 if (Stocks != null)
                 {
                     while (Stocks.Count > 0)
                     {
                         Stocks.Clear();
                     }
                     Stocks = null;
                 }
                 if (Players != null)
                 {
                     while (Players.Count > 0)
                     {
                         Players.Clear();
                     }
                     Players = null;
                 }
                 if (MarketState != null)
                 {
                     MarketState = null;
                 }
                 if (CurrentPlayer != null)
                 {
                     CurrentPlayer = null;
                 }
                 if (SynchronizeRoot != null)
                 {
                     SynchronizeRoot = null;
                 }
                 if (PlayTimer != null)
                 {
                     PlayTimer.Dispose();
                 }
                 if (BeforeResetEvent != null)
                 {
                     BeforeResetEvent = null;
                 }
                 if (AfterResetEvent != null)
                 {
                     AfterResetEvent = null;
                 }
                 if (BeforeLoadEvent != null)
                 {
                     BeforeLoadEvent = null;
                 }
                 if (AfterLoadEvent != null)
                 {
                     AfterLoadEvent = null;
                 }
                 if (BeforeSaveEvent != null)
                 {
                     BeforeSaveEvent = null;
                 }
                 if (AfterSaveEvent != null)
                 {
                     AfterSaveEvent = null;
                 }
                 if (UpdateStockInformationsEvent != null)
                 {
                     UpdateStockInformationsEvent = null;
                 }
                 if (UpdatePlayerInformationsEvent != null)
                 {
                     UpdatePlayerInformationsEvent = null;
                 }
             }
             Disposed = true;
         }
     }
     catch (System.Exception ex)
     {
         throw ex;
     }
 }