private ACommand initProcess(InitCommand command, Battlefield.NetworkStreamAndBattlefield networkStreamAndBattlefield) { Battlefield battlefield = networkStreamAndBattlefield.BATTLEFIELD; IClassEquipment classEquipment = null; // get teamId for this robot if (!battlefield.robotTeamIdByTeamName.TryGetValue(command.TEAM_NAME, out int teamId)) { lock (battlefield.robotsByTeamId) { lock (battlefield.robotTeamIdByTeamName) { if (battlefield.robotTeamIdByTeamName.Count < battlefield.TEAMS) { teamId = battlefield.idForTeam++; battlefield.robotsByTeamId[teamId] = new List <BattlefieldRobot>(); battlefield.robotTeamIdByTeamName.Add(command.TEAM_NAME, teamId); } else { return(new ErrorCommand("Too many teams.")); } } } } else { lock (battlefield.robotsByTeamId) { if (battlefield.robotsByTeamId[teamId].Count >= battlefield.ROBOTS_IN_TEAM) { return(new ErrorCommand("Too many robots in one team.")); } } } BattlefieldRobot robot; // create robot switch (command.ROBOT_TYPE) { case RobotType.MINE_LAYER: robot = new MineLayer(teamId, idForRobot++, networkStreamAndBattlefield.NETWORK_STREAM); initRobot(robot); classEquipment = ((MineLayer)robot).MineGun; break; case RobotType.TANK: robot = new Tank(teamId, idForRobot++, networkStreamAndBattlefield.NETWORK_STREAM); initRobot(robot); classEquipment = ((Tank)robot).Gun; break; case RobotType.REPAIRMAN: robot = new Repairman(teamId, idForRobot++, networkStreamAndBattlefield.NETWORK_STREAM); initRobot(robot); classEquipment = ((Repairman)robot).RepairTool; break; default: return(new ErrorCommand("Unsupported RobotType (" + command.ROBOT_TYPE + ") support only" + RobotType.MINE_LAYER + ", " + RobotType.TANK + ", " + RobotType.REPAIRMAN)); } lock (battlefield.robots) { battlefield.robots.Add(robot); } lock (battlefield.robotsById) { battlefield.robotsById[robot.ID] = robot; } lock (battlefield.robotsByTeamId) { battlefield.robotsByTeamId[teamId].Add(robot); } lock (battlefield.robotsByStream) { battlefield.robotsByStream.Add(networkStreamAndBattlefield.NETWORK_STREAM, robot); } robot.NAME = command.NAME; lock (battlefield.battlefieldTurn) { battlefield.battlefieldTurn.AddRobot(new Robot(robot.ID, robot.TEAM_ID, robot.Score, robot.Gold, robot.HitPoints, robot.X, robot.Y, robot.AngleDrive, robot.NAME)); battlefield.turnDataModel?.Add(battlefieldTurn.ConvertToTurn(), false); battlefield.viewer?.StepNext(); } return(battlefield.addToInitAnswerCommand(new InitAnswerCommand(battlefield.MAX_TURN, battlefield.lap, battlefield.MAX_LAP, robot.ID, robot.TEAM_ID, classEquipment.ID, robot.Armor.ID, robot.Motor.ID))); }