public void StartGamePlay() { #region Send Teams foreach (var client in avatarDictionary) { // GamePlay is starting - get ready !!! string team = null; int teamCounter = avatarDictionary.Values.ToList().FindIndex(c => c.Equals(client.Value)) + 1; if (_numberOfTeams > 0) { if (_numberOfTeams == 2) { if (teamCounter % 2 == 0) { team = teamList[1]; } else { team = teamList[0]; } } else if (_numberOfTeams == 4) { if (teamCounter % 4 == 0) { team = teamList[3]; } else if (teamCounter % 4 == 3) { team = teamList[2]; } else if (teamCounter % 4 == 2) { team = teamList[1]; } else { team = teamList[0]; } } client.Value.Team = team; } client.Key.gamePlayStarted(client.Value.CurrentMapForRobot.SerializeMap(), _roundNumber, team); // send team with initialMap EventLog.WriteMessageToLog(strLogPath, "Client: " + client.Value.Login + " is in TEAM: " + team); } #endregion // #region Send visible map //foreach (var client in avatarDictionary) // client.Key.reciveInitialData(InitialServerResponse.PlayerRegistered(avatarDictionary[client.Key].Color, (int)_roundTime, client.Value.RobotPosition,new MapSize(_currentMap.MapWidth, _currentMap.MapHeight))); // #endregion _isGameStarted = true; _globalHistory = new Dictionary <int, List <ActionHistory> >(); EventLog.WriteMessageToLog(strLogPath, "GAME IS STARTING!"); while (_currentRound != _roundNumber) { EventLog.WriteMessageToLog(strLogPath, "ROUND nr: " + _currentRound + " IS STARTING!"); if (avatarDictionary.Values.ToList().Exists(rob => rob.ErrorNumber >= 10)) { Disconnect(avatarDictionary.FirstOrDefault(cl => cl.Value == avatarDictionary.Values.ToList().Find(rob => rob.ErrorNumber >= 10)).Key); } _globalHistory.Add(_currentRound, new List <ActionHistory>()); // add history card for this round try { foreach (IArenaCallback client in avatarDictionary.Keys) {// Round is starting - get ready !!! var possibleMovesSet = Move.GetPossibleActions(avatarDictionary[client], _currentMap, avatarDictionary.Values.ToList()); _globalHistory[_currentRound].Add(new ActionHistory(avatarDictionary[client].Login, _currentRound, possibleMovesSet)); client.gameRoundStart(_currentRound, possibleMovesSet); } } catch (Exception e) { EventLog.WriteErrorToLog(strErrorLogPath, e); } EventLog.WriteMessageToLog(strLogPath, "ROUND nr: " + _currentRound + " SEND gameRoundStart message"); try { Thread.Sleep((int)_roundTime); } catch (ThreadInterruptedException e) // wake up! { } foreach (var history in _globalHistory[_currentRound]) { if (_currentMovesQueue.Exists(m => m.Robot.Login.Equals(history.RobotLogin))) { history.MadeMove = _currentMovesQueue.Find(m => m.Robot.Login.Equals(history.RobotLogin)).MadeMove; } } var TimeoutPlayers = avatarDictionary.Keys.ToList <IArenaCallback>().FindAll(client => !_currentMovesQueue.Exists(m => m.Client.Equals(client))); foreach (var client in TimeoutPlayers) { // send Timout to those who do not send move _globalHistory[_currentRound].Find(history => history.RobotLogin.Equals(avatarDictionary[client].Login)).MadeMove = MoveType.Timeout; // set madeMove = Timeout for those who make timeout EventLog.WriteMessageToLog(strLogPath, "TIMEOUT: Client: " + avatarDictionary[client].Login); avatarDictionary[client].ErrorNumber++; client.reciveGamePlayData(_currentMap.getSmallerPartForRobot(avatarDictionary[client].RobotPosition).SerializeMap(), new GamePlayServerResponse(_currentRound, avatarDictionary[client].RobotPosition, 0, 0, avatarDictionary[client].HasBigItem, avatarDictionary[client].SmallItem, MoveConsequence.TimeOut, GamePlayServerResponse.MoveTimout("Time out. Be quick!"))); _currentMovesQueue.Enqueue(new Move(MoveType.WrongAction, _currentMap, client, avatarDictionary, avatarDictionary[client], Directions.Down, _currentRound)); } if (_disconnectedRobotAvatarList.Count > 0) { foreach (var robot in _disconnectedRobotAvatarList) { _currentMovesQueue.Add(new Move(MoveType.DisconnectedPlayer, robot)); } } var roundScore = GamePlay.PlayTurn(_currentMovesQueue, _globalHistory); _scoreCounter.AddScore(_currentRound, roundScore); try { foreach (IArenaCallback client in avatarDictionary.Keys) // Round is starting - get ready !!! { try { client.gameRoundEnd(_currentRound); EventLog.WriteMessageToLog(strLogPath, "SEND: gameRoundEnd"); } catch (Exception e) { EventLog.WriteErrorToLog(strErrorLogPath, e); } } } catch (Exception e) { EventLog.WriteErrorToLog(strErrorLogPath, e); } _currentRound++; if (_pauseGame) { try { Thread.Sleep(Timeout.Infinite); } catch (ThreadInterruptedException e) // wake up! { } } } if (!_endGameInformationSend) { _endGameInformationSend = true; if (_scoreModule != null) { foreach (IArenaCallback client in avatarDictionary.Keys) {// GamePlay is ending; EventLog.WriteMessageToLog(strLogPath, "SEND: gamePlayEnd"); client.gamePlayEnd(_scoreCounter.GetTotalScoreForRobot(avatarDictionary[client])); } } else { foreach (IArenaCallback client in avatarDictionary.Keys) {// GamePlay is ending; client.gamePlayEnd(0); EventLog.WriteMessageToLog(strLogPath, "SEND: gamePlayEnd"); } } } }
public void Rest(int round) { Directions direction = new Directions(); direction = Directions.NULL; var client = OperationContext.Current.GetCallbackChannel <IArenaCallback>(); var robot = avatarDictionary[client]; if (round == _currentRound) { if (!_currentMovesQueue.Enqueue(new Move(MoveType.Rest, _currentMap, client, avatarDictionary, robot, direction, _currentRound))) { if (robot.ErrorNumber++ > 10) { Disconnect(client); } //DISCONECT client.reciveGamePlayData(_currentMap.getSmallerPartForRobot(robot.RobotPosition).SerializeMap(), new GamePlayServerResponse(_currentRound, avatarDictionary[client].RobotPosition, 0, 0, avatarDictionary[client].HasBigItem, avatarDictionary[client].SmallItem, MoveConsequence.InvalidMove, GamePlayServerResponse.InvalidMoveMessage("You cannot move two times in one round"))); EventLog.WriteMessageToLog(strLogPath, "ERROR: Client: " + robot.Login + " moved two times in one round"); } else { robot.ErrorNumber = 0; EventLog.WriteMessageToLog(strLogPath, "Client: " + robot.Login + " Rest " + "in round " + round.ToString()); } } }
public Dictionary <RobotAvatar, double> PlayTurn(MovesQueue actionQueue, Dictionary <int, List <ActionHistory> > globalHistory) { Dictionary <RobotAvatar, double> ScoreDictionary = new Dictionary <RobotAvatar, double>(); _movesToScore = new List <Move>(); List <Move> MoveListToVisualization = new List <Move>(); #region INVOKE MOVES while (actionQueue.Count != 0) { var currentMove = actionQueue.Dequeue(); Move move; if (currentMove.MadeMove != MoveType.DisconnectedPlayer) { MethodInfo method = currentMove.GetType().GetMethod(currentMove.MadeMove.ToString() + "Action"); // INVOKE ALL ACTIONS IN QUEUE move = (Move)method.Invoke(currentMove, null); if (move.MadeMove == MoveType.Burn) { globalHistory[move.CurrentRound].Find(hist => hist.RobotLogin.Equals(move.Robot.Login)).MadeMove = MoveType.Burn; EventLog.WriteMessageToLog(strLogPath, "Client: " + move.Robot.Login + " is burning"); } if (move.MadeMove == MoveType.WrongAction) { globalHistory[move.CurrentRound].Find(hist => hist.RobotLogin.Equals(move.Robot.Login)).MadeMove = MoveType.WrongAction; EventLog.WriteMessageToLog(strLogPath, "Client: " + move.Robot.Login + " made wrong action"); } if (globalHistory[move.CurrentRound].Exists(hist => hist.MadeMove.Equals(move.MadeMove))) { globalHistory[move.CurrentRound].Find(hist => hist.MadeMove.Equals(move.MadeMove)).Consequence = move.Consequence; // add move conseqence to global history for each move } EventLog.WriteMessageToLog(strLogPath, "Client: " + move.Robot.Login + " " + move.MadeMove.ToString() + " with consequence " + move.Consequence.ToString()); MoveListToVisualization.Add(move); _movesToScore.Add(move); } else if (!_movesToScore.Exists(m => m.Robot.Equals(currentMove.Robot))) { ScoreDictionary.Add(currentMove.Robot, -10.0); //_movesToScore.Add(currentMove); currentMove.MyPay = -10.0; MoveListToVisualization.Add(currentMove); } //if (PlayerActionEvent != null) //PlayerActionEvent(move,null); } #endregion #region SCORE MODULE if (_scoreModule != null) { Dictionary <RobotAvatar, double> ScoreDictionaryScoreModule = new Dictionary <RobotAvatar, double>(); try { ScoreDictionaryScoreModule = _scoreModule.GetScore(_currentMap, _movesToScore, globalHistory); // score moves } catch (Exception e) { } foreach (var score in ScoreDictionaryScoreModule) { ScoreDictionary.Add(score.Key, score.Value); } foreach (var move in _movesToScore.FindAll(m => m.MadeMove == MoveType.DisconnectedPlayer)) { move.MyPay = ScoreDictionary[move.Robot]; } foreach (var score in ScoreDictionary) { var robot = globalHistory.Values.ToList().Last().Find(a => a.RobotLogin.Equals(score.Key.Login)); if (robot != null) { robot.MyCurrentPay = score.Value; robot.MyTotalPay += score.Value; } } double totalPay = 0; // count total score foreach (var points in ScoreDictionary.Values) { totalPay += points; } foreach (var history in globalHistory.Values.ToList().Last()) { history.TotalPay = totalPay; } try { foreach (var client in _avatarDictionary) // send Message to client { var move = _movesToScore.Find(m => m.Robot.Equals(client.Value)); if (move.MadeMove != MoveType.Disconnect) { move.MyPay = ScoreDictionary[move.Robot]; var mapForRobot = _currentMap.getSmallerPartForRobot(move.Robot.RobotPosition).SerializeMap(); var roundNumber = move.CurrentRound; var response = new GamePlayServerResponse(roundNumber, move.Robot.RobotPosition, move.MyPay, totalPay, move.Robot.HasBigItem, move.Robot.SmallItem, move.Consequence, move.Response); try { client.Key.reciveGamePlayData(mapForRobot, response); } catch (Exception e) { EventLog.WriteErrorToLog(strLogPath, e); } EventLog.WriteMessageToLog(strLogPath, "Client: " + client.Value.Login + " receive " + move.MyPay.ToString() + " points for move " + move.MadeMove.ToString()); } } } catch (Exception e) { EventLog.WriteErrorToLog(strLogPath, e); } } #endregion else { #region WITHOUT SCORE MODULE foreach (var client in _avatarDictionary) // send Message to client { var move = _movesToScore.Find(m => m.Robot.Equals(client.Value)); if (move.MadeMove != MoveType.Disconnect) { move.MyPay = 0; var mapForRobot = _currentMap.getSmallerPartForRobot(client.Value.RobotPosition).SerializeMap(); var roundNumber = _movesToScore.Find(m => m.Robot.Equals(client.Value)).CurrentRound; var response = new GamePlayServerResponse(roundNumber, client.Value.RobotPosition, 0, 0, client.Value.HasBigItem, client.Value.SmallItem, _movesToScore.Find(m => m.Client.Equals(client.Key)).Consequence, _movesToScore.Find(m => m.Client.Equals(client.Key)).Response); client.Key.reciveGamePlayData(mapForRobot, response); EventLog.WriteMessageToLog(strLogPath, "Client: " + client.Value.Login + " - sending gameplay data"); } } } if (PlayerActionEvent != null) { PlayerActionEvent(MoveListToVisualization, null); } return(ScoreDictionary); #endregion }
//public void Punch(Directions direction, int round) //{ // var client = OperationContext.Current.GetCallbackChannel<IArenaCallback>(); // var robot = avatarDictionary[client]; // if (round == _currentRound) // if (!_currentMovesQueue.Enqueue(new Move(MoveType.Punch, _currentMap, client, avatarDictionary, robot, direction, _currentRound, _hostileMode))) // { // if (robot.ErrorNumber++ > 10) // Disconnect(client); // client.reciveGamePlayData(_currentMap.getSmallerPartForRobot(robot.RobotPosition).SerializeMap(), new GamePlayServerResponse(_currentRound, avatarDictionary[client].RobotPosition, 0, 0, avatarDictionary[client].HasBigItem, avatarDictionary[client].SmallItem, MoveConsequence.TimeOut, GamePlayServerResponse.InvalidMoveMessage("You cannot move two times in one round"))); // EventLog.WriteMessageToLog(strLogPath, "ERROR: Client: " + robot.Login + " moved two times in one round"); // } // EventLog.WriteMessageToLog(strLogPath, "Client: " + robot.Login + " Punched in direction " + direction.ToString() + " in round " + round.ToString()); //} public void Shoot(Directions direction, int round) { var client = OperationContext.Current.GetCallbackChannel <IArenaCallback>(); var robot = avatarDictionary[client]; if (direction == null || direction == Directions.NULL) { if (robot.ErrorNumber++ > 10) { Disconnect(client); } client.reciveGamePlayData(_currentMap.getSmallerPartForRobot(robot.RobotPosition).SerializeMap(), new GamePlayServerResponse(_currentRound, avatarDictionary[client].RobotPosition, 0, 0, avatarDictionary[client].HasBigItem, avatarDictionary[client].SmallItem, MoveConsequence.InvalidMove, GamePlayServerResponse.InvalidMoveMessage("Shoot Action cannot have null direction"))); EventLog.WriteMessageToLog(strLogPath, "ERROR: Client: " + robot.Login + " Direction of shoot cannot be null"); _currentMovesQueue.Enqueue(new Move(MoveType.WrongAction, _currentMap, client, avatarDictionary, robot, Directions.NULL, _currentRound, _hostileMode)); return; } else { if (round == _currentRound) { if (!_currentMovesQueue.Enqueue(new Move(MoveType.Shoot, _currentMap, client, avatarDictionary, robot, direction, _currentRound, _hostileMode))) { if (robot.ErrorNumber++ > 10) { Disconnect(client); } client.reciveGamePlayData(_currentMap.getSmallerPartForRobot(robot.RobotPosition).SerializeMap(), new GamePlayServerResponse(_currentRound, avatarDictionary[client].RobotPosition, 0, 0, avatarDictionary[client].HasBigItem, avatarDictionary[client].SmallItem, MoveConsequence.InvalidMove, GamePlayServerResponse.InvalidMoveMessage("You cannot move two times in one round"))); EventLog.WriteMessageToLog(strLogPath, "ERROR: Client: " + robot.Login + " moved two times in one round"); } else { robot.ErrorNumber = 0; EventLog.WriteMessageToLog(strLogPath, "Client: " + robot.Login + " Shot in direction " + direction.ToString() + " in round " + round.ToString()); } } } }
public MessageRecivedArgs(Map map, GamePlayServerResponse response) { Map = map; this.response = response; }