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

	「編成変更」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.toString());
		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 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 SendChange(fintKantaiNo, fintKanNo, fintShoyuId){
		var fstrShoyuId, fstrJsonText;
		fstrShoyuId = (fintShoyuId == -1) ? '%2D1' : fintShoyuId.toString();
		fstrJsonText = PostHttpSend(['http\:\/\/', objSetting.Server, '\/kcsapi\/api_req_hensei\/change'].join(''), ['api%5Fship%5Fid=', fstrShoyuId, '&api%5Fship%5Fidx=', fintKanNo.toString(), '&api%5Fid=', (fintKantaiNo + 1).toString(), '&api%5Fverno=1&api%5Ftoken=', strToken].join(''));
		objChange = eval(['\(', fstrJsonText, '\)'].join(''));
		return(objChange.api_result == 1);
	}

	/* 任意の艦隊の編成を変更する */
	function HenseiChange(fintKantaiNo, fintKanNo, fintShoyuId){
		if (objDeck.api_data[fintKantaiNo].api_ship[fintKanNo] == fintShoyuId){
			return('success');
		} else {
			if (SendChange(fintKantaiNo, fintKanNo, fintShoyuId)){
				if (SendDeck()){
					return('success');
				} else {
					return('deck');
				}
			} else {
				return('change');
			}
		}
	}

	var i, strCurrentDirectory, strSetting, objSetting, strToken, objUriJSON, strTemp, strFlashVersion, strSelfFileName, objHensei, intArrLen, intKantaiNo, intKanNo, intShoyuId, strStasus;
	var objDeck, objChange;
	strCurrentDirectory = GetCurrentDirectory();
	strSetting = adoLoadText(strCurrentDirectory + '\\' + constrSettingFileName, 'Shift_JIS');
	objSetting = eval('\(' + strSetting + '\)');
	strToken = adoLoadText(strCurrentDirectory + '\\' + objSetting.TokenFileName, 'Shift_JIS').replace(/\s/g,'');
	//Flash のバージョン取得
	strFlashVersion = RegistryRead('HKEY_LOCAL_MACHINE\\SOFTWARE\\Macromedia\\FlashPlayer\\CurrentVersion');
	//自ファイルの名前を取得
	strSelfFileName = WScript.ScriptName.replace(/.js$/i,'');
	//設定ファイルの編成データ読み込み
	objHensei = eval('\(' + adoLoadText(strCurrentDirectory + '\\' + objSetting.HenseiFileName, 'Shift_JIS') + '\)');
	intArrLen = objHensei[strSelfFileName].length;
	//deckの取得
	if (SendDeck()){
		for (i=0;i<intArrLen;i++){
			intKantaiNo = objHensei[strSelfFileName][i][0] - 1;
			intKanNo = objHensei[strSelfFileName][i][1] - 1;
			intShoyuId = objHensei[strSelfFileName][i][2];
			//秘書艦を外そうとしていないか判定
			if ((intKantaiNo == 0 && intKanNo == 0 && intShoyuId == -1) || (objDeck.api_data[intKantaiNo].api_ship[intKanNo] == -1 && objDeck.api_data[0].api_ship[0] == intShoyuId)){
				PopupMsg('秘書艦を外そうとしましたので緊急終了いたします。', 0, 16);
				return(false);
			} else {
				strStasus = HenseiChange(intKantaiNo, intKanNo, intShoyuId);
				if (strStasus != 'success'){
					PopupMsg(i + ' 番目\r\n\r\n' + strStasus + 'の取得に失敗しました。', 0, 16);
					return(false);
				}
			}
		}
		PopupMsg('成功！', 1, 64);
	} else {
		PopupMsg('deck の取得に失敗しました。', 1, 64);
		return(false);
	}
	return(true);
}

Main();
