@ -1,63 +1,17 @@
gLoadVsavHandler = null ;
// --------------------------------------------------------------------
function on _update _vsav ( )
{
// FOR TESTING PORPOISES! We can't control a file upload from Selenium (since
// the browser will use native controls), so we get the data from a <textarea>).
if ( getUrlParam ( "vsav_persistence" ) ) {
var $elem = $ ( "#_vsav-persistence_" ) ;
var vsav _data = $elem . val ( ) ;
$elem . val ( "" ) ; // nb: let the test suite know we've received the data
do _update _vsav ( vsav _data , "test.vsav" ) ;
return ;
}
// if we are running inside the PyQt wrapper, let it handle everything
if ( gWebChannelHandler ) {
gWebChannelHandler . load _vsav ( function ( data ) {
if ( ! data )
return ;
do _update _vsav ( data . data , data . filename ) ;
} ) ;
return ;
}
function on _update _vsav ( ) { _load _and _process _vsav ( _do _update _vsav ) ; }
// ask the user to upload the VSAV file
$ ( "#load-vsav" ) . trigger ( "click" ) ; // nb: this will call on_load_vsav_file_selected() when a file has been selected
}
function on _load _vsav _file _selected ( )
{
// read the selected file
var fileReader = new FileReader ( ) ;
var file = $ ( "#load-vsav" ) . prop ( "files" ) [ 0 ] ;
fileReader . onload = function ( ) {
vsav _data = fileReader . result ;
if ( vsav _data . substring ( 0 , 5 ) === "data:" )
vsav _data = vsav _data . split ( "," ) [ 1 ] ;
do _update _vsav ( vsav _data , file . name ) ;
} ;
fileReader . readAsDataURL ( file ) ;
}
function do _update _vsav ( vsav _data , fname )
function _do _update _vsav ( vsav _data , fname )
{
// show the progress dialog
var $dlg = $ ( "#update-vsav" ) . dialog ( {
dialogClass : "update-vsav" ,
modal : true ,
width : 300 ,
height : 60 ,
resizable : false ,
closeOnEscape : false ,
} ) ;
// generate all the snippets
var $dlg = _show _vassal _shim _progress _dlg ( "Updating your VASL scenario..." ) ;
var snippets = _generate _snippets ( ) ;
// send a request to update the VSAV
var data = { "filename" : fname , vsav _data : vsav _data , snippets : snippets } ;
var data = { filename : fname , vsav _data : vsav _data , snippets : snippets } ;
$ . ajax ( {
url : gUpdateVsavUrl ,
type : "POST" ,
@ -65,40 +19,9 @@ function do_update_vsav( vsav_data, fname )
contentType : "application/json" ,
} ) . done ( function ( data ) {
$dlg . dialog ( "close" ) ;
data = JSON . parse ( data ) ;
// check if there was an error
if ( data . error ) {
if ( getUrlParam ( "vsav_persistence" ) ) {
$ ( "#_vsav-persistence_" ) . val (
"ERROR: " + data . error + "\n\n=== STDOUT ===\n" + data . stdout + "\n=== STDERR ===\n" + data . stderr
) ;
return ;
}
$ ( "#vassal-shim-error" ) . dialog ( {
dialogClass : "vassal-shim-error" ,
title : "Can't update the scenario" ,
modal : true ,
width : 600 , height : "auto" ,
open : function ( ) {
$ ( "#vassal-shim-error .message" ) . html ( data . error ) ;
var log = "" ;
if ( data . stdout && data . stderr )
log = "=== STDOUT ===\n" + data . stdout + "\n=== STDERR ===\n" + data . stderr ;
else if ( data . stdout )
log = data . stdout ;
else if ( data . stderr )
log = data . stderr ;
if ( log )
$ ( "#vassal-shim-error .log" ) . val ( log ) . show ( ) ;
else
$ ( "#vassal-shim-error .log" ) . hide ( ) ;
} ,
buttons : {
Close : function ( ) { $ ( this ) . dialog ( "close" ) ; } ,
} ,
} ) ;
data = _check _vassal _shim _response ( data , "Can't update the VASL scenario." ) ;
if ( ! data )
return ;
}
// check if anything was changed
if ( ! data . report . was _modified ) {
showInfoMsg ( "No changes were made to the VASL scenario." ) ;
@ -348,3 +271,212 @@ function _get_raw_content( snippet_id, $btn, params )
return null ;
}
// --------------------------------------------------------------------
function on _analyze _vsav ( )
{
// check if there are any vehicles/ordnance already defined
var voDefined1 = $ ( "#ob_vehicles-sortable_1 .vo-entry" ) . length > 0 || $ ( "#ob_ordnance-sortable_1 .vo-entry" ) . length > 0 ;
var voDefined2 = $ ( "#ob_vehicles-sortable_2 .vo-entry" ) . length > 0 || $ ( "#ob_ordnance-sortable_2 .vo-entry" ) . length > 0 ;
if ( voDefined1 || voDefined2 ) {
// yup - confirm the operation
ask ( "Analyze VASL scenario" ,
"<p>There are some vehicles/ordnance already configured. <p>They will be <i>replaced</i> with those found in the analyzed VASL scenario." , {
ok : function ( ) { _load _and _process _vsav ( _do _analyze _vsav ) ; } ,
} ) ;
return ;
}
// ask the user to select a VASL scenario, then analyze it
_load _and _process _vsav ( _do _analyze _vsav ) ;
}
function _do _analyze _vsav ( vsav _data , fname )
{
// show the progress dialog
var $dlg = _show _vassal _shim _progress _dlg ( "Analyzing the VASL scenario..." ) ;
// send a request to analyze the VSAV
var data = { filename : fname , vsav _data : vsav _data } ;
$ . ajax ( {
url : gAnalyzeVsavUrl ,
type : "POST" ,
data : JSON . stringify ( data ) ,
contentType : "application/json" ,
} ) . done ( function ( data ) {
$dlg . dialog ( "close" ) ;
data = _check _vassal _shim _response ( data , "Can't analyze the VASL scenario." ) ;
if ( ! data )
return ;
_create _vo _entries _from _analysis ( data ) ;
} ) . fail ( function ( xhr , status , errorMsg ) {
$dlg . dialog ( "close" ) ;
showErrorMsg ( "Can't analyze the VASL scenario:<div class='pre'>" + escapeHTML ( errorMsg ) + "</div>" ) ;
} ) ;
}
function _create _vo _entries _from _analysis ( report )
{
function create _vo _entries ( player _no , vo _type ) {
// clear the existing vehicles/ordnance
$ ( "#ob_" + vo _type + "-sortable_" + player _no ) . sortable2 ( "delete-all" ) ;
// build an index of GPID's that belong to the specified player and V/O type
var entries _index = { } ;
var entries = gVehicleOrdnanceListings [ vo _type ] [ get _player _nat ( player _no ) ] ;
var gpids , i ;
for ( i = 0 ; i < entries . length ; ++ i ) {
gpids = $ . isArray ( entries [ i ] . gpid ) ? entries [ i ] . gpid : [ entries [ i ] . gpid ] ;
for ( var j = 0 ; j < gpids . length ; ++ j )
entries _index [ gpids [ j ] ] = entries [ i ] ;
}
// add a vehicle/ordnance for each relevant GPID
var nCreated = 0 ;
gpids = Object . keys ( report ) ;
for ( i = 0 ; i < gpids . length ; ++ i ) {
var gpid = gpids [ i ] ;
var entry = entries _index [ gpid ] ;
if ( ! entry )
continue ;
var image _id = $ . isArray ( entry . gpid ) ? [ gpid , 0 ] : null ;
do _add _vo ( vo _type , player _no , entry , image _id , false , null , null , null ) ;
++ nCreated ;
}
return nCreated ;
}
// import any vehicles/ordnance found
var imported = [
[ create _vo _entries ( 1 , "vehicles" ) , create _vo _entries ( 1 , "ordnance" ) ] ,
[ create _vo _entries ( 2 , "vehicles" ) , create _vo _entries ( 2 , "ordnance" ) ]
] ;
// report what happened
var report _strings = [ ] ;
function make _report _string ( nat , nVehicles , nOrdnance ) {
var buf = [ ] ;
if ( nVehicles > 0 )
buf . push ( nVehicles + "{{NAT}} " + pluralString ( nVehicles , "vehicle" , "vehicles" ) ) ;
if ( nOrdnance > 0 )
buf . push ( nOrdnance + "{{NAT}} ordnance" ) ;
if ( buf . length == 1 ) {
report _strings . push (
"Imported " + buf [ 0 ] . replace ( "{{NAT}}" , " " + nat ) + "."
) ;
} else if ( buf . length == 2 ) {
report _strings . push (
"Imported " + buf [ 0 ] . replace ( "{{NAT}}" , " " + nat ) + " and " + buf [ 1 ] . replace ( "{{NAT}}" , "" ) + "."
) ;
}
}
make _report _string ( get _nationality _display _name ( get _player _nat ( 1 ) ) , imported [ 0 ] [ 0 ] , imported [ 0 ] [ 1 ] ) ;
make _report _string ( get _nationality _display _name ( get _player _nat ( 2 ) ) , imported [ 1 ] [ 0 ] , imported [ 1 ] [ 1 ] ) ;
if ( report _strings . length === 0 )
showWarningMsg ( "<p>No vehicles/ordnance were imported. <p style='margin-top:0.25em;'>Have you set the player nationalities?" ) ;
else
showInfoMsg ( report _strings . join ( "<p style='margin-top:0.25em;'>" ) ) ;
}
// --------------------------------------------------------------------
function _load _and _process _vsav ( handler )
{
// FOR TESTING PORPOISES! We can't control a file upload from Selenium (since
// the browser will use native controls), so we get the data from a <textarea>).
if ( getUrlParam ( "vsav_persistence" ) ) {
var $elem = $ ( "#_vsav-persistence_" ) ;
var vsav _data = $elem . val ( ) ;
$elem . val ( "" ) ; // nb: let the test suite know we've received the data
handler ( vsav _data , "test.vsav" ) ;
return ;
}
// if we are running inside the PyQt wrapper, let it handle everything
if ( gWebChannelHandler ) {
gWebChannelHandler . load _vsav ( function ( data ) {
if ( ! data )
return ;
handler ( data . data , data . filename ) ;
} ) ;
return ;
}
// ask the user to upload the VSAV file
gLoadVsavHandler = handler ;
$ ( "#load-vsav" ) . trigger ( "click" ) ; // nb: this will call on_load_vsav_file_selected() when a file has been selected
}
function on _load _vsav _file _selected ( )
{
// read the selected file
var fileReader = new FileReader ( ) ;
var file = $ ( "#load-vsav" ) . prop ( "files" ) [ 0 ] ;
fileReader . onload = function ( ) {
vsav _data = fileReader . result ;
if ( vsav _data . substring ( 0 , 5 ) === "data:" )
vsav _data = vsav _data . split ( "," ) [ 1 ] ;
gLoadVsavHandler ( vsav _data , file . name ) ;
gLoadVsavHandler = null ;
} ;
fileReader . readAsDataURL ( file ) ;
}
// --------------------------------------------------------------------
function _check _vassal _shim _response ( data , caption )
{
// check if there was an error
if ( ! data . error )
return data ;
// yup - report the error
if ( getUrlParam ( "vsav_persistence" ) ) {
$ ( "#_vsav-persistence_" ) . val (
"ERROR: " + data . error + "\n\n=== STDOUT ===\n" + data . stdout + "\n=== STDERR ===\n" + data . stderr
) ;
return null ;
}
$ ( "#vassal-shim-error" ) . dialog ( {
dialogClass : "vassal-shim-error" ,
title : caption ,
modal : true ,
width : 600 , height : "auto" ,
open : function ( ) {
$ ( "#vassal-shim-error .message" ) . html ( data . error ) ;
var log = "" ;
if ( data . stdout && data . stderr )
log = "=== STDOUT ===\n" + data . stdout + "\n=== STDERR ===\n" + data . stderr ;
else if ( data . stdout )
log = data . stdout ;
else if ( data . stderr )
log = data . stderr ;
if ( log )
$ ( "#vassal-shim-error .log" ) . val ( log ) . show ( ) ;
else
$ ( "#vassal-shim-error .log" ) . hide ( ) ;
} ,
buttons : {
Close : function ( ) { $ ( this ) . dialog ( "close" ) ; } ,
} ,
} ) ;
return null ;
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
function _show _vassal _shim _progress _dlg ( caption )
{
// show the progress dialog
return $ ( "#vassal-shim-progress" ) . dialog ( {
dialogClass : "vassal-shim-progress" ,
modal : true ,
width : 300 ,
height : 60 ,
resizable : false ,
closeOnEscape : false ,
open : function ( ) {
$ ( this ) . find ( ".message" ) . text ( caption ) ;
} ,
} ) ;
}