<commit_before>/// <reference path="./typings/tsd.d.ts" />

import Admiral = require('./Admiral');
import Hull3 = require('./Hull3');
import Settings = require('./Settings');
import fs = require('fs-extra');
import _ = require('lodash');

import {Ast, Lexer, Parser, PrettyPrinter, Mission as CpMission} from 'config-parser';
import {parseFile} from './Common'
import {Side, MissionType, Terrain, Faction, Addons, Mission, Config, GeneratedMission, getSideNames, getMissionTypeNames, stringToMissionType, missionTypeToGameType, missionTypeToMissionNamePrefix} from '../common/Mission';
export {Side, MissionType, Terrain, Faction, Addons, Mission, Config, GeneratedMission, getSideNames, getMissionTypeNames, stringToMissionType, missionTypeToGameType} from '../common/Mission';

var missionIdCounter: number = 0,
    TERRAINS_JSON_PATH = `${Settings.PATH.SERVER_RESOURCES_HOME}/terrains.json`,
    POSITION_X_SHIFT = 350;

var terrains: Terrain[] = [];

function nextMissionId(): number {
    missionIdCounter = missionIdCounter + 1;
    return missionIdCounter;
}

function cleanWorkingDir() {
    fs.emptyDirSync(Settings.PATH.Mission.WORKING_DIR);
}

function getFactionMissionAsts(factions: Hull3.FactionRequest[]): Parser.Node[] {
    return _.map(factions, (f, idx) => {
        var ast = parseFile(`${Settings.PATH.SERVER_RESOURCES_HOME}/mission/${f.sideName}.sqm`);
        Hull3.removeUnselectedItems(ast, f.factionId, Hull3.getFactionRolePrefixById(f.factionId), f.groupTemplateIds, f.vehicleClassnames, idx * POSITION_X_SHIFT);
        return ast;
    });
}

function mergeGroupsAndVehicles(missionAst: Parser.Node, factionAsts: Parser.Node[]) {
    var groupItems = _.foldl(factionAsts, (acc, fa) => acc.concat(Ast.select(fa, 'Mission.Groups.Item*')), <Parser.Node[]>[]),
        vehicleItems = _.foldl(factionAsts, (acc, fa) => acc.concat(Ast.select(fa, 'Mission.Vehicles.Item*')), <Parser.Node[]>[]),
        missionGroups = Ast.select(missionAst, 'Mission.Groups')[0],
        missionVehicles = Ast.select(missionAst, 'Mission.Vehicles')[0];
    missionGroups.fields = [];
    missionVehicles.fields = [];
    CpMission.mergeItems(missionGroups, groupItems);
    CpMission.mergeItems(missionVehicles, vehicleItems);
}

function getPlayableUnitCount(missionAst: Parser.Node): number {
    return _.foldl(Ast.select(missionAst, 'Mission.Groups.Item*'), (acc, g) => acc + Ast.select(g, 'Vehicles.Item*').length, 0);
}

function generateMissionSqm(missionAst: Parser.Node, mission: Mission) {
    Ast.select(missionAst, 'Mission.Intel.overviewText')[0].value = mission.overviewText;
    mergeGroupsAndVehicles(missionAst, getFactionMissionAsts(mission.factions));
}

function generateHull3Header(missionDir: string, mission: Mission) {
    var hull3Ast = parseFile(`${Hull3.getSampleMissionPath()}/hull3/hull3.h`);
    Hull3.addFactionsToHull3Config(hull3Ast, mission.factions);
    fs.writeFileSync(`${missionDir}/hull3/hull3.h`, PrettyPrinter.create('    ').print(hull3Ast), 'UTF-8');
}

function generateDescriptionExt(missionDir: string, mission: Mission, missionType: MissionType, maxPlayers: number) {
    var descriptionExt = fs.readFileSync(`${missionDir}/description.ext`, 'UTF-8')
        .replace(/onLoadName = "[^"]*";/g, `onLoadName = "${mission.onLoadName}";`)
        .replace(/author = "[^"]*";/g, `author = "${mission.author}";`)
        .replace(/gameType = [^;]*;/g, `gameType = ${missionTypeToGameType(missionType)};`)
        .replace(/maxPlayers = [^;]*;/g, `maxPlayers = ${maxPlayers.toString()};`);
    descriptionExt = tryAddAdmiralInclude(descriptionExt, mission);<commit_msg>Setting first unit to player for 3DEN.<commit_after>/// <reference path="./typings/tsd.d.ts" />

import Admiral = require('./Admiral');
import Hull3 = require('./Hull3');
import Settings = require('./Settings');
import fs = require('fs-extra');
import _ = require('lodash');

import {Ast, Lexer, Parser, PrettyPrinter, Mission as CpMission} from 'config-parser';
import {parseFile} from './Common'
import {Side, MissionType, Terrain, Faction, Addons, Mission, Config, GeneratedMission, getSideNames, getMissionTypeNames, stringToMissionType, missionTypeToGameType, missionTypeToMissionNamePrefix} from '../common/Mission';
export {Side, MissionType, Terrain, Faction, Addons, Mission, Config, GeneratedMission, getSideNames, getMissionTypeNames, stringToMissionType, missionTypeToGameType} from '../common/Mission';

var missionIdCounter: number = 0,
    TERRAINS_JSON_PATH = `${Settings.PATH.SERVER_RESOURCES_HOME}/terrains.json`,
    POSITION_X_SHIFT = 350;

var terrains: Terrain[] = [];

function nextMissionId(): number {
    missionIdCounter = missionIdCounter + 1;
    return missionIdCounter;
}

function cleanWorkingDir() {
    fs.emptyDirSync(Settings.PATH.Mission.WORKING_DIR);
}

function getFactionMissionAsts(factions: Hull3.FactionRequest[]): Parser.Node[] {
    return _.map(factions, (f, idx) => {
        var ast = parseFile(`${Settings.PATH.SERVER_RESOURCES_HOME}/mission/${f.sideName}.sqm`);
        Hull3.removeUnselectedItems(ast, f.factionId, Hull3.getFactionRolePrefixById(f.factionId), f.groupTemplateIds, f.vehicleClassnames, idx * POSITION_X_SHIFT);
        return ast;
    });
}

function mergeGroupsAndVehicles(missionAst: Parser.Node, factionAsts: Parser.Node[]) {
    var groupItems = _.foldl(factionAsts, (acc, fa) => acc.concat(Ast.select(fa, 'Mission.Groups.Item*')), <Parser.Node[]>[]),
        vehicleItems = _.foldl(factionAsts, (acc, fa) => acc.concat(Ast.select(fa, 'Mission.Vehicles.Item*')), <Parser.Node[]>[]),
        missionGroups = Ast.select(missionAst, 'Mission.Groups')[0],
        missionVehicles = Ast.select(missionAst, 'Mission.Vehicles')[0];
    missionGroups.fields = [];
    missionVehicles.fields = [];
    CpMission.mergeItems(missionGroups, groupItems);
    CpMission.mergeItems(missionVehicles, vehicleItems);
    makeFirstUnitPlayerFor3DEN(missionAst);
}

function makeFirstUnitPlayerFor3DEN(missionAst: Parser.Node) {
    var groups = Ast.select(missionAst, 'Mission.Groups.Item*');
    var units = Ast.select(groups[0], 'Vehicles.Item*');
    console.log(units);
    if (units.length > 0) {
        Ast.select(units[0], 'player')[0].value = 'PLAYER COMMANDER';
    }
}

function getPlayableUnitCount(missionAst: Parser.Node): number {
    return _.foldl(Ast.select(missionAst, 'Mission.Groups.Item*'), (acc, g) => acc + Ast.select(g, 'Vehicles.Item*').length, 0);
}

function generateMissionSqm(missionAst: Parser.Node, mission: Mission) {
    Ast.select(missionAst, 'Mission.Intel.overviewText')[0].value = mission.overviewText;
    mergeGroupsAndVehicles(missionAst, getFactionMissionAsts(mission.factions));
}

function generateHull3Header(missionDir: string, mission: Mission) {
    var hull3Ast = parseFile(`${Hull3.getSampleMissionPath()}/hull3/hull3.h`);
    Hull3.addFactionsToHull3Config(hull3Ast, mission.factions);
    fs.writeFileSync(`${missionDir}/hull3/hull3.h`, PrettyPrinter.create('    ').print(hull3Ast), 'UTF-8');
}

function generateDescriptionExt(missionDir: string, mission: Mission, missionType: MissionType, maxPlayers: number) {
    var descriptionExt = fs.readFileSync(`${missionDir}/description.ext`, 'UTF-8')
        .replace(/onLoadName = "[^"]*";/g, `onLoadName = "${mission.onLoadName}";`)
        .replace(/author = "[^"]*";/g, `author = "${mission.author}";`)
        .replace(/gameType = [^;]*;/g, `gameType = ${missionTypeToGameType(missionType)};`)
        .replace(/maxPlayers = [^;]*;/g, `maxPlayers = ${maxPlayers.toString()};`);
    descriptionExt = tryAddAdmiralInclude(descriptionExt, mission);
{'fraction_non_alphanumeric': 0.15841324050965455, 'fraction_numerical': 0.008012610009194798, 'mean_word_length': 5.392947103274559, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3916611', 'n_tokens_mistral': 2581, 'n_tokens_neox': 2472, 'n_words': 435}