//process next MovesQueue item private void step() { if (adjacencyPairQueue.Count > 0) { AdjacencyPair p = adjacencyPairQueue.Dequeue(); p.enqueueRecursively(MovesQueue); } if (MovesQueue.Count > 0) { Type t = typeof(Conversation);//necessary for invoke for some reason MovesQueueItem p = MovesQueue.Dequeue(); //loop makes array of types from the paramaters //(necessary for the getmethod function in order to differentiate between overloaded methods) Type[] paramaterTypes = new Type[p.paramaters.Length]; for (int i = 0; i < p.paramaters.Length; i++) { //if an exception is ever thrown here it might be becuse the name or paramaters of a production rule was wrong paramaterTypes[i] = p.paramaters[i].GetType(); } //search for correct method MethodInfo methodInfo = t.GetMethod(p.methodToCall, paramaterTypes); /*INVOKE THE METHOD*/ try { /*The following if statement determines whether the method produces output or not * methods producing output need to enqueue string return values onto the output buffer * this is determined by the fact that only middlelayer functions take a PairParamaters as paramater */ //this if statement used to be a hack to determine if it was a terminal symbol (which had output) or a rule (which would just be processed) //if (paramaterTypes[0] == typeof(PairParamaters)) // methodInfo.Invoke(this/*utteranceGenerator*/, p.paramaters); //else outputQueue.Enqueue(new Turn(((PairParamaters)p.paramaters[0]).initiatingSpeaker, (string)methodInfo.Invoke(this /*utteranceGenerator*/, p.paramaters))); } catch (Exception e) { outputQueue.Enqueue(new Turn("Exception thrown in reflection invoke of " + p.methodToCall)); if (e.InnerException != null) { string err = e.InnerException.Message; //get the inner exception throw; //rethrow the exception } } } }
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 }