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

	「任意の艦隊の艦娘名取得」Ver. 1.00

					Produced by sakura

	編成の現在のデータを取得します。

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

/* 設定 */
var conintKantai = 1;
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 SendStype(){
		var fstrJsonText = PostHttpSend(['http\:\/\/', objSetting.Server, '\/kcsapi\/api_get_master\/stype'].join(''), ['api%5Fverno=1&api%5Ftoken=', strToken].join(''));
		objStype = eval(['\(', fstrJsonText, '\)'].join(''));
		return(objStype.api_result == 1);
	}

	function SendShip(){
		var fstrJsonText = PostHttpSend(['http\:\/\/', objSetting.Server, '\/kcsapi\/api_get_member\/ship'].join(''), ['api%5Fverno=1&api%5Ftoken=', strToken].join(''));
		objShip = eval(['\(', fstrJsonText, '\)'].join(''));
		return(objShip.api_result == 1);
	}

	function SendDeck(){
		var fstrJsonText = PostHttpSend(['http\:\/\/', objSetting.Server, '\/kcsapi\/api_get_member\/deck'].join(''), ['api%5Fverno=1&api%5Ftoken=', strToken].join(''));
		objDeck = eval(['\(', fstrJsonText, '\)'].join(''));
		return(objDeck.api_result == 1);
	}

	function GetShipId(fintShoyuId){
		var fintShipLen, fi, fintShipId;
		fintShipLen = objShip.api_data.length;
		fintShipId = -1;
		for (fi=0;fi<fintShipLen;fi++){
			if (objShip.api_data[fi].api_id == fintShoyuId){
				fintShipId = objShip.api_data[fi].api_ship_id;
				break;
			}
		}
		return(fintShipId);
	}

	function GetStypeId(fintShipId){
		var fintShipLen, fi, fintStypeId;
		fintShipLen = objShip.api_data.length;
		fintStypeId = -1;
		for (fi=0;fi<fintShipLen;fi++){
			if (objShip.api_data[fi].api_ship_id == fintShipId){
				fintStypeId = objShip.api_data[fi].api_stype;
				break;
			}
		}
		return(fintStypeId);
	}

	function GetStypeName(fintStypeId){
		var fintStypeLen, fi, fstrStypeText;
		if (fintStypeId > 0){
			fintStypeLen = objStype.api_data.length;
			fstrStypeText = '';
			for (fi=0;fi<fintStypeLen;fi++){
				if (objStype.api_data[fi].api_id == fintStypeId){
					fstrStypeText = objStype.api_data[fi].api_name;
					break;
				}
			}
			return(fstrStypeText.substr(0,4));
		} else {
			return('');
		}
	}

	function GetShipName(fintShipId){
		var fintShipLen, fi, fstrShipName;
	 	fintShipLen = objShip.api_data.length;
	 	fstrShipName = '';
	 	for (fi=0;fi<fintShipLen;fi++){
			if (objShip.api_data[fi].api_ship_id == fintShipId){
				fstrShipName = objShip.api_data[fi].api_name;
				break;
			}
		}
		return(fstrShipName.substr(0,4));
	}

	function GetLevel(fintShoyuId){
		var fintShipLen, fi, fintLevel;
		fintShipLen = objShip.api_data.length;
		fintLevel = -1;
		for (fi=0;fi<fintShipLen;fi++){
			if (objShip.api_data[fi].api_id == fintShoyuId){
				fintLevel = objShip.api_data[fi].api_lv;
				break;
			}
		}
		return(fintLevel);
	}

	function GetData(){
		if(SendStype()){
			if(SendShip()){
				if(SendDeck()){
					return('success');
				} else {
					return('deck');
				}
			} else {
				return('ship');
			}
		} else {
			return('stype');
		}
	}

	var strCurrentDirectory, strSetting, objSetting, strToken, strFlashVersion, strStatus, objStype, objShip, objDeck, arrText, strText, intShipId, strShipName, intLevel;
	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');
	strStatus = GetData();
	if (strStatus == 'success'){
		arrText = new Array();
		strText = '';
		for (i=0; i<6; i++){
			intShoyuId = objDeck.api_data[conintKantai - 1].api_ship[i];
			arrText.push((i + 1).toString());
			arrText.push('. ');
			arrText.push(intShoyuId.toString());
			arrText.push('\t');
			arrText.push(GetStypeName(GetStypeId(GetShipId(intShoyuId))));
			arrText.push('\t');
			strShipName = GetShipName(GetShipId(intShoyuId));
			arrText.push((strShipName != '') ? strShipName : '\(空\)');
			intLevel = GetLevel(intShoyuId);
			if (intLevel > 0){
				arrText.push('\tLv.');
				arrText.push(intLevel.toString());
			}
			arrText.push('\r\n');
		}
		strText = arrText.join('').replace(/\s+$/,'');
		PopupMsg(strText, 0, 64);
	} else {
		PopupMsg('「' + strStatus + '」の受信に失敗しました。', 0, 16);
	}
}

Main();
