// kicks a trader from the market public static void KillTrader(Trader trader) { if (trader == null) { return; } trader.KillTrader(); Console.WriteLine(Message.traderLeftUI(trader.ID)); BroadCastMessage(Message.traderLeftBroadCast(trader.ID)); lock (traderListLock) { if (_traderList.Remove(trader)) { AddMarketMessage(Message.traderLeftUI(trader.ID)); } } // resets stock holder if trader leaving is stock holder lock (stockHolderLock) { if (Equals(_stockHolder, trader)) { if (_traderList.Count == 0) { _stockHolder = null; } else { // gives stock to random trader Random random = new Random(); int randomTraderIndex = (int)(random.NextDouble() * _traderList.Count); Trader randomTrader = _traderList[randomTraderIndex]; _stockHolder = randomTrader; BroadCastMessage(Message.traderAcqBroadCast(randomTrader.ID)); Console.WriteLine(Message.traderAcqUI(randomTrader.ID)); AddMarketMessage(Message.traderAcqUI(randomTrader.ID)); } } } }
// instantiates and returns a new trader, assigning as stockholder if no stock holder current private static Trader CreateNewTrader() { Trader newTrader = new Trader(GetNewTraderID()); Market.BroadCastMessage(Message.traderJoincedBroadCast(newTrader.ID)); Console.WriteLine(Message.traderJoinedUI(newTrader.ID)); AddMarketMessage(Message.traderJoinedUI(newTrader.ID)); lock (traderListLock) { _traderList.Add(newTrader); } lock (stockHolderLock) { if (_stockHolder == null) { //TODO: add server message for automatically giving stock away Console.WriteLine(Message.traderAcqUI(newTrader.ID)); AddMarketMessage(Message.traderAcqUI(newTrader.ID)); _stockHolder = newTrader; } } return(newTrader); }
public ClientHandler() { _trader = null; }