// NOTE: These fields aren't mandatory in the sense that snippet generation will fail // if they're not set, but they're really, really, really expected to be there. var _MANDATORY_PARAMS = { scenario: { "SCENARIO_NAME": "scenario name", "SCENARIO_DATE": "scenario date" }, } ; var _MONTH_NAMES = [ // nb: we assume English :-/ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December" ] ; var _DAY_OF_MONTH_POSTFIXES = { // nb: we assume English :-/ 0: "th", 1: "st", 2: "nd", 3: "rd", 4: "th", 5: "th", 6: "th", 7: "th", 8: "th", 9: "th", 10: "th", 11: "th", 12: "th", 13: "th" } ; // -------------------------------------------------------------------- function generate_snippet( $btn, extra_params ) { // initialize storeMsgForTestSuite( "_last-info_", "" ) ; storeMsgForTestSuite( "_last-warning_", "" ) ; storeMsgForTestSuite( "_last-error_", "" ) ; // extract the scenario date components var params = {} ; var scenario_date = $("input[name='SCENARIO_DATE']").datepicker( "getDate" ) ; if ( scenario_date ) { params.SCENARIO_DAY_OF_MONTH = scenario_date.getDate() ; var postfix ; if ( params.SCENARIO_DAY_OF_MONTH in _DAY_OF_MONTH_POSTFIXES ) postfix = _DAY_OF_MONTH_POSTFIXES[ params.SCENARIO_DAY_OF_MONTH ] ; else postfix = _DAY_OF_MONTH_POSTFIXES[ params.SCENARIO_DAY_OF_MONTH % 10 ] ; params.SCENARIO_DAY_OF_MONTH_POSTFIX = params.SCENARIO_DAY_OF_MONTH + postfix ; params.SCENARIO_MONTH = 1 + scenario_date.getMonth() ; params.SCENARIO_MONTH_NAME = _MONTH_NAMES[scenario_date.getMonth()] ; params.SCENARIO_YEAR = scenario_date.getFullYear() ; } // unload the template parameters var template_id = $btn.data( "id" ) ; unload_params( params, true ) ; // set player-specific parameters var nationalities = gTemplatePack.nationalities ; var curr_tab = $("#tabs .ui-tabs-active a").attr( "href" ) ; if ( curr_tab === "#tabs-ob1" ) { params.PLAYER_NAME = nationalities[params.PLAYER_1].display_name ; params.OB_COLOR = nationalities[params.PLAYER_1].ob_colors[0] ; params.OB_COLOR_2 = nationalities[params.PLAYER_1].ob_colors[1] ; } else if ( curr_tab === "#tabs-ob2" ) { params.PLAYER_NAME = nationalities[params.PLAYER_2].display_name ; params.OB_COLOR = nationalities[params.PLAYER_2].ob_colors[0] ; params.OB_COLOR_2 = nationalities[params.PLAYER_2].ob_colors[1] ; } // set player-specific parameters if ( template_id == "vehicles_1" ) { template_id = "vehicles" ; params.VEHICLES = params.VEHICLES_1 ; params.VEHICLES_WIDTH = params.VEHICLES_WIDTH_1 ; } else if ( template_id == "vehicles_2" ) { template_id = "vehicles" ; params.VEHICLES = params.VEHICLES_2 ; params.VEHICLES_WIDTH = params.VEHICLES_WIDTH_2 ; } if ( template_id == "ordnance_1" ) { template_id = "ordnance" ; params.ORDNANCE = params.ORDNANCE_1 ; params.ORDNANCE_WIDTH = params.ORDNANCE_WIDTH_1 ; } else if ( template_id == "ordnance_2" ) { template_id = "ordnance" ; params.ORDNANCE = params.ORDNANCE_2 ; params.ORDNANCE_WIDTH = params.ORDNANCE_WIDTH_2 ; } // include the player display names params.PLAYER_1_NAME = nationalities[params.PLAYER_1].display_name ; params.PLAYER_2_NAME = nationalities[params.PLAYER_2].display_name ; // generate PF parameters if ( params.SCENARIO_YEAR < 1944 || (params.SCENARIO_YEAR == 1944 && params.SCENARIO_MONTH < 6) ) params.PF_RANGE = 1 ; else if ( params.SCENARIO_YEAR == 1944 ) params.PF_RANGE = 2 ; else params.PF_RANGE = 3 ; if ( params.SCENARIO_YEAR < 1943 || (params.SCENARIO_YEAR == 1943 && params.SCENARIO_MONTH <= 9) ) { params.PF_CHECK_DRM = "+1" ; params.PF_CHECK_DR = 2 ; } else if ( params.SCENARIO_YEAR >= 1945 ) { params.PF_CHECK_DRM = "-1" ; params.PF_CHECK_DR = 4 ; } else { params.PF_CHECK_DRM = "" ; params.PF_CHECK_DR = 3 ; } // generate BAZ parameters if ( params.SCENARIO_YEAR >= 1945 ) { params.BAZ_TYPE = 45 ; params.BAZ_BREAKDOWN = 11 ; params.BAZ_TOKILL = 16 ; params.BAZ_WP = 6 ; params.BAZ_RANGE = 5 ; } else if ( params.SCENARIO_YEAR >= 1944 ) { params.BAZ_TYPE = 44 ; params.BAZ_BREAKDOWN = 11 ; params.BAZ_TOKILL = 16 ; params.BAZ_RANGE = 4 ; } else if ( params.SCENARIO_YEAR == 1943 || (params.SCENARIO_YEAR == 1942 && params.SCENARIO_MONTH >= 11) ) { params.BAZ_TYPE = 43 ; params.BAZ_BREAKDOWN = 10 ; params.BAZ_TOKILL = 13 ; params.BAZ_RANGE = 4 ; } // check for mandatory parameters if ( template_id in _MANDATORY_PARAMS ) { var missing_params = [] ; for ( var param_id in _MANDATORY_PARAMS[template_id] ) { if ( ! (param_id in params && params[param_id].length > 0) ) missing_params.push( _MANDATORY_PARAMS[template_id][param_id] ) ; } if ( missing_params.length > 0 ) showWarningMsg( makeBulletListMsg( "Missing parameters:", missing_params, li_class="pre" ) ) ; } // check for date-specific parameters if ( template_id === "pf" ) { if ( params.SCENARIO_DATE === "" || params.SCENARIO_YEAR <= 1942 || (params.SCENARIO_YEAR == 1943 && params.SCENARIO_MONTH <= 9) ) showWarningMsg( "PF are only available after September 1943." ) ; } if ( template_id === "baz" ) { if ( params.SCENARIO_DATE === "" || params.SCENARIO_YEAR <= 1941 || (params.SCENARIO_YEAR == 1942 && params.SCENARIO_MONTH < 11) ) showWarningMsg( "BAZ are only available from November 1942." ) ; } if ( template_id === "atmm" ) { if ( params.SCENARIO_DATE === "" || params.SCENARIO_YEAR < 1944 ) showWarningMsg( "ATMM are only available from 1944." ) ; } // add in any extra parameters if ( extra_params ) $.extend( true, params, extra_params ) ; // check that the players have different nationalities if ( params.PLAYER_1 === params.PLAYER_2 ) showWarningMsg( "Both players have the same nationality!" ) ; // get the template to generate the snippet from var templ = get_template( template_id ) ; if ( templ === null ) return ; var func ; try { func = jinja.compile( templ ).render ; } catch( ex ) { showErrorMsg( "Can't compile template: