/**********************************************************

	「建造中艦娘名取得」Ver. 1.00

					Produced by sakura

	建造中の艦娘名を取得します。

**********************************************************/

/* 設定 */
var constrSettingFileName = 'kcsSettingJSON.txt';

function Main(){
	/* ダイアログ表示 */
	function PopupMsg(d_text, d_time, d_type){
		var objWSHShell, n;
		objWSHShell = new ActiveXObject('WScript.Shell');
		n = objWSHShell.Popup(d_text, d_time, WScript.ScriptName.replace(/.js$/i,''), d_type);
		objWSHShell = null;
	}

	/* カレントディレクトリを取得 */
	function GetCurrentDirectory(){
		var objWSHShell;
		objWSHShell = new ActiveXObject('WScript.Shell');
		return(objWSHShell.CurrentDirectory);
	}

	/* 任意のレジストリの値を取得 */
	function RegistryRead(fstrname){
		var WshShell, fstrTemp, e;
		WshShell = WScript.CreateObject("WScript.Shell");
		try {
			fstrTemp = WshShell.RegRead(fstrname);
		} catch(e){
			fstrTemp = null;
		}
		WshShell = null;
		return(fstrTemp);
	}

	/* 任意の文字コードでテキストファイルの全文字列を取得 */
	function adoLoadText(filename, charset) {
		var stream, text, adTypeText, adReadAll;
		adTypeText = 2;
		adReadAll = -1;
		stream = new ActiveXObject("ADODB.Stream");
		stream.type = adTypeText;
		stream.charset = charset;
		stream.open();
		stream.loadFromFile(filename);
		text = stream.readText(adReadAll);
		stream.close();
		stream = null;
		return(text);
	}

	/* バイナリを文字列に変換 */
	function GetStringFromBinary(fbinary, fstrEncode){
		var stream, adTypeBinary, adTypeText, fstrtemp;
		adTypeBinary = 1;
		adTypeText = 2;
		stream = new ActiveXObject('ADODB.stream');
		stream.Type = adTypeBinary;
		stream.Open();
		stream.Write(fbinary);
		stream.Position = 0;
		stream.Type = adTypeText;
		stream.Charset = fstrEncode;
		fstrtemp = stream.ReadText();
		stream.Close();
		stream = null;
		return(fstrtemp);
	}
	
	/* POST データを送信して文字列を取得 */
	function PostHttpSend(fstrURI, fstrPostData){
		var fobjHttp, fstrtemp, fbinary;
		fobjHttp = new ActiveXObject('WinHttp.WinHttpRequest.5.1');
		if (objSetting.EnableProxy){
			fobjHttp.SetProxy(2, objSetting.ProxyAddress);
		}
		fobjHttp.Open('POST', fstrURI, false);
		fobjHttp.SetRequestHeader('Accept', '\*\/\*');
		fobjHttp.SetRequestHeader('Accept-Language', 'ja-JP');
		fobjHttp.SetRequestHeader('Referer', 'http\:\/\/' + objSetting.Server + '\/kcs\/port.swf\?version=' + objSetting.Version);
		fobjHttp.SetRequestHeader('x-flash-version', strFlashVersion);
		fobjHttp.SetRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		fobjHttp.SetRequestHeader('Content-Length', fstrPostData.Length);
		fobjHttp.SetRequestHeader('Accept-Encoding', 'gzip, deflate');
		fobjHttp.SetRequestHeader('User-Agent', 'Mozilla\/5.0 \(compatible\; MSIE 9.0\; Windows NT 6.1\; WOW64\; Trident\/6.0\)');
		fobjHttp.SetRequestHeader('DNT', '1');
		//fobjHttp.SetRequestHeader('Connection', 'Keep-Alive');
		fobjHttp.SetRequestHeader('Pragma', 'no-cache');
		fobjHttp.Send(fstrPostData);
		fstrtemp = (fobjHttp.Status == 200) ? GetStringFromBinary(fobjHttp.responseBody, 'Shift_JIS') : '';
		return(fstrtemp);
	}
	
	function GetStypeName(fintStypeId){
		var fi, fintArrLen;
		fintArrLen = objStypeJSON.api_data.length;
		for (fi=0;fi<fintArrLen;fi++){
			if (objStypeJSON.api_data[fi].api_id == fintStypeId){
				return(objStypeJSON.api_data[fi].api_name);
			}
		}
		return('?');
	}
	
	function GetShipName(fintShipId){
		var fi, fintArrLen;
		fintArrLen = objShipJSON.api_data.length;
		for (fi=0;fi<fintArrLen;fi++){
			if (objShipJSON.api_data[fi].api_id == fintShipId){
				return(GetStypeName(objShipJSON.api_data[fi].api_stype) + ' ' + objShipJSON.api_data[fi].api_name);
			}
		}
		return('?');
	}
	
	var strCurrentDirectory, strSetting, objSetting, strToken, strFlashVersion, strJsonText, objShipJSON, objStypeJSON, objKdockJSON, intArrLen, arrResult, i, j;
	strCurrentDirectory = GetCurrentDirectory();
	strSetting = adoLoadText(strCurrentDirectory + '\\' + constrSettingFileName, 'Shift_JIS');
	objSetting = eval('\(' + strSetting + '\)');
	strToken = adoLoadText(strCurrentDirectory + '\\' + objSetting.TokenFileName, 'Shift_JIS').replace(/\s/g,'');
	strFlashVersion = RegistryRead('HKEY_LOCAL_MACHINE\\SOFTWARE\\Macromedia\\FlashPlayer\\CurrentVersion');
	strJsonText = PostHttpSend('http\:\/\/' + objSetting.Server + '\/kcsapi\/api_get_master\/ship', 'api%5Fverno=1&api%5Ftoken=' + strToken);
	objShipJSON = eval('\(' + strJsonText + '\)');
	strJsonText = PostHttpSend('http\:\/\/' + objSetting.Server + '\/kcsapi\/api_get_master\/stype', 'api%5Fverno=1&api%5Ftoken=' + strToken);
	objStypeJSON = eval('\(' + strJsonText + '\)');
	strJsonText = PostHttpSend('http\:\/\/' + objSetting.Server + '\/kcsapi\/api_get_member\/kdock', 'api%5Fverno=1&api%5Ftoken=' + strToken);
	objKdockJSON = eval('\(' + strJsonText + '\)');
	intArrLen = objShipJSON.api_data.length;
	arrResult = new Array();
	for (i=0;i<4;i++){
		arrResult.push((i + 1).toString());
		arrResult.push('.  ');
		if (objKdockJSON.api_data[i].api_state == -1){
			arrResult.push('\(未解放\)');
		} else if (objKdockJSON.api_data[i].api_state == 0){
			arrResult.push('\(空\)');
		} else if (objKdockJSON.api_data[i].api_created_ship_id < 1){
			arrResult.push('\(不明\)');
		} else {
			arrResult.push(GetShipName(objKdockJSON.api_data[i].api_created_ship_id));
		}
		if (i < 3) arrResult.push('\r\n');
	}
	PopupMsg(arrResult.join(''), 0, 64);
}

Main();
