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

	「全艦まとめて補給」Ver. 1.0.1

					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 SendShip(){
		var fstrJsonText = PostHttpSend(['http\:\/\/', objSetting.Server, '\/kcsapi\/api_get_member\/ship'].join(''), ['api%5Fverno=1&api%5Ftoken=', strToken].join(''));
		if (fstrJsonText != ''){
			objShip = eval(['\(', fstrJsonText, '\)'].join(''));
			return(objShip.api_result == 1);
		} else {
			return(false);
		}
	}
	
	function SendDeck(){
		var fstrJsonText = PostHttpSend(['http\:\/\/', objSetting.Server, '\/kcsapi\/api_get_member\/deck'].join(''), ['api%5Fverno=1&api%5Ftoken=', strToken].join(''));
		if (fstrJsonText != ''){
			objDeck = eval(['\(', fstrJsonText, '\)'].join(''));
			return(objDeck.api_result == 1);
		} else {
			return(false);
		}
	}
	
	function SendShip2(){
		var fstrJsonText = PostHttpSend(['http\:\/\/', objSetting.Server, '\/kcsapi\/api_get_member\/ship2'].join(''), ['api%5Fsort%5Forder=2&api%5Fsort%5Fkey=1&api%5Fverno=1&api%5Ftoken=', strToken].join(''));
		if (fstrJsonText != ''){
			objShip2 = eval(['\(', fstrJsonText, '\)'].join(''));
			return(objShip2.api_result == 1);
		} else {
			return(false);
		}
	}
	
	function SendCharge(fstr_id_items){
		var fstrJsonText = PostHttpSend(['http\:\/\/', objSetting.Server, '\/kcsapi\/api_req_hokyu\/charge'].join(''), ['api%5Fkind=3&api%5Fid%5Fitems=', fstr_id_items.replace(/,/g, '%2C'), '&api%5Fverno=1&api%5Ftoken=', strToken].join(''));
		if (fstrJsonText != ''){
			objCharge = eval(['\(', fstrJsonText, '\)'].join(''));
			return(objCharge.api_result == 1);
		} else {
			return(false);
		}
	}

	/* 所有IDからshipの配列の番号を返す */
	function GetShipNo(fintShoyuId){
		var fintShipLen, fi, fintShipNo;
		fintShipLen = objShip.api_data.length;
		fintShipNo = -1;
		for (fi=0;fi<fintShipLen;fi++){
			if (objShip.api_data[fi].api_id == fintShoyuId){
				fintShipNo = fi;
				break;
			}
		}
		return(fintShipNo);
	}

	/* 艦IDから最大燃料を返す */
	function GetFuelMax(fintShipId){
		var fintShipLen, fi, fintFuelMax;
		fintShipLen = objShip.api_data.length;
		fintFuelMax = 0;
		for (fi=0;fi<fintShipLen;fi++){
			if (objShip.api_data[fi].api_ship_id == fintShipId){
				fintFuelMax = objShip.api_data[fi].api_fuel_max;
				break;
			}
		}
		return(fintFuelMax);
	}

	/* 艦IDから最大弾薬を返す */
	function GetBullMax(fintShipId){
		var fintShipLen, fi, fintBullMax;
		fintShipLen = objShip.api_data.length;
		fintBullMax = 0;
		for (fi=0;fi<fintShipLen;fi++){
			if (objShip.api_data[fi].api_ship_id == fintShipId){
				fintBullMax = objShip.api_data[fi].api_bull_max;
				break;
			}
		}
		return(fintBullMax);
	}

	/* 任意の艦隊を全艦補給する */
	function KantaiCharge(fintKantaiNo){
		var farr_id_items, fi, fintShoyuId, fstrStatus, fintShipNo;
		farr_id_items = new Array();
		for (fi=0; fi<6; fi++){
			fintShoyuId = objDeck.api_data[fintKantaiNo].api_ship[fi];
			if (fintShoyuId < 1) break;
			fintShipNo = GetShipNo(fintShoyuId);
			if (fintShipNo > -1 && (objShip.api_data[fintShipNo].api_fuel < objShip.api_data[fintShipNo].api_fuel_max || objShip.api_data[fintShipNo].api_bull < objShip.api_data[fintShipNo].api_bull_max)){
				objShip.api_data[fintShipNo].api_fuel = objShip.api_data[fintShipNo].api_fuel_max;
				objShip.api_data[fintShipNo].api_bull = objShip.api_data[fintShipNo].api_bull_max;
				farr_id_items.push(fintShoyuId.toString());
			}
		}
		if (farr_id_items.length > 0){
			fstrStatus = ChargeFromId(farr_id_items.join());
			if (fstrStatus == 'success'){
				return('success');
			} else {
				return(fstrStatus);
			}
		} else {
			return('success');
		}
	}

	/* 任意の艦を所有IDを指定して補給する */
	function ChargeFromId(fstrId){
		var fstrTemp;
		fstrTemp = fstrId.toString();
		if (typeof fstrTemp == 'string' && fstrTemp != ''){
			if (SendCharge(fstrTemp)){
				if (SendShip2()){
					return('success');
				} else {
					return('ship2');
				}
			} else {
				return('charge');
			}
		} else {
			return('success');
		}
	}

	/* [一斉補給] ボタンを押した時の処理 */
	function HokyuProcess(){
		var fintDeckLen, fstrStatus, fintShipLen, farr_id_items, fi;
		if (SendDeck()){
			fintDeckLen = objDeck.api_data.length;
			for (fi=0; fi<fintDeckLen; fi++){
				fstrStatus = KantaiCharge(fi);
				if (fstrStatus != 'success') return(fstrStatus);
			}
			fintShipLen = objShip.api_data.length;
			farr_id_items = new Array();
			for (fi=0; fi<fintShipLen; fi++){
				if (objShip.api_data[fi].api_fuel < objShip.api_data[fi].api_fuel_max || objShip.api_data[fi].api_bull < objShip.api_data[fi].api_bull_max){
					farr_id_items.push(objShip.api_data[fi].api_id.toString());
				}
			}
			if (farr_id_items.length > 0){
				fstrStatus = ChargeFromId(farr_id_items.join());
				if (fstrStatus != 'success') return(fstrStatus);
			}
		} else {
			return('deck');
		}
		return('success');
	}

	var strCurrentDirectory, strSetting, objSetting, strToken, strFlashVersion, objShip, objDeck, objShip2, objCharge, blnHokyu, strStatus;
	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');
	if (SendShip()){
		blnHokyu = false;
		intShipLen = objShip.api_data.length;
		for (i=0; i<intShipLen; i++){
			if (objShip.api_data[i].api_fuel < objShip.api_data[i].api_fuel_max || objShip.api_data[i].api_bull < objShip.api_data[i].api_bull_max){
				blnHokyu = true;
				break;
			}
		}
		if (blnHokyu){
			strStatus = HokyuProcess();
			if (strStatus == 'success'){
				PopupMsg('成功', 1, 64);
			} else {
				PopupMsg(['エラー：\r\n「', strStatus, '」の受信に失敗しました。'].join(''), 0, 16);
			}
		} else {
			PopupMsg('既に全艦満タンですよ！', 1, 64);
		}
	} else {
		PopupMsg('エラー：\r\n「menber/ship」の受信に失敗しました。', 0, 16);
	}
}

Main();
