public void SendLifePackUpdates() { Boolean showLifePack = true; foreach (Contestant c in playerList) { if (c.CurrentP.Equals(GameEngine.lifePackList[GameEngine.lifePacksToDisclose - 1].Position)) { showLifePack = false; break; } } if (showLifePack) { int x = GameEngine.lifePackList[GameEngine.lifePacksToDisclose - 1].Position.X; int y = GameEngine.lifePackList[GameEngine.lifePacksToDisclose - 1].Position.Y; int lifeTime = GameEngine.lifePackList[GameEngine.lifePacksToDisclose - 1].LifeTime; string tStatus = "L:" + x + "," + y + ":" + lifeTime + Constant.S2C_DEL; Monitor.Enter(GameEngine.availableLifePackList); GameEngine.availableLifePackList.Add(GameEngine.lifePackList[GameEngine.lifePacksToDisclose - 1]); GameEngine.availableLifePackList.Sort(this.CompareMapItemsByDisappearTime); Monitor.Exit(GameEngine.availableLifePackList); GameEngine.lifePacksToDisclose--; DataObject dO = new DataObject(tStatus, GameEngine.playerIPList, GameEngine.playerPort); ThreadPool.QueueUserWorkItem(new WaitCallback(com.BroadCast), (object)dO); Console.WriteLine("............................................................................"); Console.WriteLine("NEXT Life Packet IN :" + GameEngine.nextLifePackSend + " :: T-To-Disclose :" + GameEngine.lifePacksToDisclose); Console.WriteLine(tStatus); gui.AddLifePack(x, y, lifeTime); } else { GameEngine.lifePackList[GameEngine.lifePacksToDisclose - 1].AppearTime += 2000; GameEngine.lifePackList[GameEngine.lifePacksToDisclose - 1].DisappearTime += 2000; GameEngine.lifePackList.Sort(CompareMapItemsByTime); } }
public void SendPlayerUpdates() { if (!GameEngine.gameFinished) { Monitor.Enter(GameEngine.playerList); string msg = "G:"; foreach (Contestant p in GameEngine.playerList) { int shotNum = 0; if (p.Shot) { shotNum = 1; p.Shot = false; //reset the shoot variable. } msg += p.Name + ";" + p.CurrentP.X + "," + p.CurrentP.Y + ";" + p.Direction + ";" + shotNum + ";" + p.Health + ";" + p.Coins + ";" + p.PointsEarned + ":"; } foreach (BrickWall b in GameEngine.brickWalls) { msg += b.Pos.X + "," + b.Pos.Y + "," + b.DamageLevel + ";"; } msg = msg.Substring(0, msg.Length - 1); gui.UpdateMap(msg); msg += Constant.S2C_DEL; Console.WriteLine(msg); DataObject dO = new DataObject(msg, GameEngine.playerIPList, GameEngine.playerPort); ThreadPool.QueueUserWorkItem(new WaitCallback(com.BroadCast), (object)dO); List<CoinPile> currentCoinPiles = new List<CoinPile>(); currentCoinPiles.AddRange(availableCoinPileList); currentCoinPiles.AddRange(plunderCoinPileList); AIMain.GetInstance().AvailableCoinPileList = currentCoinPiles; AIMain.GetInstance().moveShips(); List<AIMessage> nextAIupdates = AIMain.GetInstance().NextAIupdates; for (int i = 0; i < nextAIupdates.Count; i++) { updateContestant(nextAIupdates[i].Contestant, nextAIupdates[i].Command); } } }
public void SendCoinPileUpdates() { // System.Windows.Forms.MessageBox.Show("Test"); Boolean showTress = true; foreach (Contestant c in playerList) { if (c.CurrentP.Equals(GameEngine.coinPileList[GameEngine.coinPilesToDisclose - 1].Position)) { showTress = false; break; } } if (showTress) { int x = GameEngine.coinPileList[GameEngine.coinPilesToDisclose - 1].Position.X; int y = GameEngine.coinPileList[GameEngine.coinPilesToDisclose - 1].Position.Y; int lifeT = GameEngine.coinPileList[GameEngine.coinPilesToDisclose - 1].LifeTime; int price = GameEngine.coinPileList[GameEngine.coinPilesToDisclose - 1].Price; string tStatus = "C:" + x + "," + y + ":" + lifeT + ":" + price + Constant.S2C_DEL; Monitor.Enter(GameEngine.availableCoinPileList); GameEngine.availableCoinPileList.Add(GameEngine.coinPileList[GameEngine.coinPilesToDisclose - 1]); GameEngine.availableCoinPileList.Sort(this.CompareMapItemsByDisappearTime); Monitor.Exit(GameEngine.availableCoinPileList); GameEngine.coinPilesToDisclose--; DataObject dO = new DataObject(tStatus, GameEngine.playerIPList, GameEngine.playerPort); ThreadPool.QueueUserWorkItem(new WaitCallback(com.BroadCast), (object)dO); Console.WriteLine("............................................................................"); Console.WriteLine("NEXT CoinPile IN :" + GameEngine.nextCoinPileSend + " :: T-To-Disclose :" + GameEngine.coinPilesToDisclose); Console.WriteLine(tStatus); gui.AddCoin(x, y, price, lifeT); } else { GameEngine.coinPileList[GameEngine.coinPilesToDisclose - 1].AppearTime += 2000; GameEngine.coinPileList[GameEngine.coinPilesToDisclose - 1].DisappearTime += 2000; GameEngine.coinPileList.Sort(CompareMapItemsByTime); } }
public void PrepareStartingGame() { Thread.Sleep(int.Parse(ConfigurationManager.AppSettings.Get("StartDelay"))); while (playerList.Count < minPlayerNumber) { Thread.Sleep(100);//Looping delay. To reduce the burden on the processor } Thread.Sleep(1000);//Real player delay. Let the message go through the network Monitor.Enter(GameEngine.playerList); Monitor.Enter(GameEngine.playerPort); Monitor.Enter(GameEngine.playerIPList); string msg = "S:"; foreach (Contestant p in GameEngine.playerList) { msg += p.Name + ";" + p.StartP.X + "," + p.StartP.Y + ";" + p.Direction + ":"; p.UpdatedTime = DateTime.Now; } msg = msg.Substring(0, msg.Length - 1); msg += Constant.S2C_DEL; Console.WriteLine(msg); DataObject dO = new DataObject(msg, GameEngine.playerIPList, GameEngine.playerPort); ThreadPool.QueueUserWorkItem(new WaitCallback(com.BroadCast), (object)dO); if (!GameEngine.gameStarted) { gameStarted = true; gameFinished = false; this.RaiseGameJustStartedEvent(); } Monitor.Exit(GameEngine.playerIPList); Monitor.Exit(GameEngine.playerPort); Monitor.Exit(GameEngine.playerList); }
public void FinishGame() { GameEngine.gameStarted = false; GameEngine.gameFinished = true; GameEngine.coinPilesToDisclose = 0; DataObject dO = new DataObject(Constant.S2C_GAMEJUSTFINISHED + Constant.S2C_DEL, GameEngine.playerIPList, GameEngine.playerPort); ThreadPool.QueueUserWorkItem(new WaitCallback(com.BroadCast), (object)dO); }
private void IssuePlunderCoinPile(Point pos, int value) { string tStatus = "C:" + pos.X + "," + pos.Y + ":" + Constant.PLUNDER_TREASUR_LIFE + ":" + value + Constant.S2C_DEL; DataObject dO = new DataObject(tStatus, GameEngine.playerIPList, GameEngine.playerPort); ThreadPool.QueueUserWorkItem(new WaitCallback(com.BroadCast), (object)dO); Console.WriteLine("NEXT CoinPile IN :" + GameEngine.nextCoinPileSend + " :: T-To-Disclose :" + GameEngine.coinPilesToDisclose); Console.WriteLine(tStatus); CoinPile c = new CoinPile(pos.X, pos.Y); c.Price = value; int waitAmout = 0; for (int i = 0; i < plunderCoinPileList.Count; i++) { waitAmout += plunderCoinPileList[i].DisappearBalance; } c.DisappearBalance = Math.Max(0, (Constant.PLUNDER_TREASUR_LIFE - waitAmout)); plunderCoinPileList.Add(c); gui.AddCoin(pos.X, pos.Y, value, Constant.PLUNDER_TREASUR_LIFE); }
public void ReceiveData() { bool errorOcurred = false; Socket connection = null; //The socket that is listened to try { //Creating listening Socket this.listener = new TcpListener(IPAddress.Parse(Constant.SERVER_IP), Constant.SERVER_PORT); //Starts listening this.listener.Start(); //Establish connection upon client request DataObject dataObj; while (true) { //connection is connected socket connection = listener.AcceptSocket(); if (connection.Connected) { //To read from socket create NetworkStream object associated with socket this.serverStream = new NetworkStream(connection); SocketAddress sockAdd = connection.RemoteEndPoint.Serialize(); string s = connection.RemoteEndPoint.ToString(); List<Byte> inputStr = new List<byte>(); int asw = 0; while (asw != -1) { asw = this.serverStream.ReadByte(); inputStr.Add((Byte)asw); } reply = Encoding.UTF8.GetString(inputStr.ToArray()); this.serverStream.Close(); string ip = s.Substring(0, s.IndexOf(":")); int port = Constant.CLIENT_PORT; try { string ss = reply.Substring(0, reply.IndexOf(";")); port = Convert.ToInt32(ss); } catch (Exception) { port= Constant.CLIENT_PORT; } Console.WriteLine(ip + ": " + reply.Substring(0, reply.Length - 1)); dataObj = new DataObject(reply.Substring(0, reply.Length - 1), ip, port); ThreadPool.QueueUserWorkItem(new WaitCallback(GameEngine.Resolve), (object)dataObj); } } } catch (Exception e) { Console.WriteLine("Communication (RECEIVING) Failed! \n " + e.Message); errorOcurred = true; } finally { if(connection != null) if(connection.Connected) connection.Close(); if (errorOcurred) this.ReceiveData(); } }