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

	「任務(クエスト)状況取得」Ver. 1.0.0

					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 SendRecord(){
		var fstrJsonText = PostHttpSend(['http\:\/\/', objSetting.Server, '\/kcsapi\/api_get_member\/record'].join(''), ['api%5Fverno=1&api%5Ftoken=', strToken].join(''));
		if (fstrJsonText != ''){
			objRecord = eval(['\(', fstrJsonText, '\)'].join(''));
			return(objRecord.api_result == 1);
		} else {
			return(false);
		}
	}

	function SendQuestlist(fintPageNo){
		var fstrJsonText = PostHttpSend(['http\:\/\/', objSetting.Server, '\/kcsapi\/api_get_member\/questlist'].join(''), ['api%5Fpage%5Fno=', fintPageNo.toString(), '&api%5Fverno=1&api%5Ftoken=', strToken].join(''));
		if (fstrJsonText != ''){
			objQuestlist = eval(['\(', fstrJsonText, '\)'].join(''));
			return(objQuestlist.api_result == 1);
		} else {
			return(false);
		}
	}

	/* 任務（クエスト）画面を表示する */
	function DutyWindow(){
		if (SendRecord()){
			if (SendQuestlist(1)){
				return('success');
			} else {
				return('questlist');
			}
		} else {
			return('record');
		}
	}

	/* 任務（クエスト）処理 */
	function QuestOperate(){
		var fstrStasus, fintExecListLen, farrQuestList, fintListLen, fintArrQuestListLen, farrText;
		fstrStasus = DutyWindow();
		if (fstrStasus == 'success'){
			if (objQuestlist.api_data.api_count > 0){
				//達成済み任務全取得
				fintExecListLen = 5;
				farrQuestList = new Array();
				for (fi=1; fi<=objQuestlist.api_data.api_page_count; fi++){
					if (objQuestlist.api_data.api_disp_page != fi && !SendQuestlist(fi)){
						PopupMsg('任務画面の「questlist」の受信に失敗しました。', 0, 16);
						return(false);
					}
					fintListLen = objQuestlist.api_data.api_list.length;
					for (fj=0; fj<fintListLen; fj++){
						if (objQuestlist.api_data.api_list[fj] != -1 && objQuestlist.api_data.api_list[fj].api_state > 1) farrQuestList.push([objQuestlist.api_data.api_list[fj].api_no, objQuestlist.api_data.api_list[fj].api_title, objQuestlist.api_data.api_list[fj].api_state, objQuestlist.api_data.api_list[fj].api_progress_flag]);
					}
				}
				//
				//farrQuestList
				fintArrQuestListLen = farrQuestList.length;
				farrText = new Array();
				for (fi=0; fi<fintArrQuestListLen; fi++){
					farrText.push(farrQuestList[fi][1]);
					farrText.push('\r\n　　→ ');
					farrText.push((farrQuestList[fi][2] > 2) ? '達成' : '遂行中');
					farrText.push('\t');
					farrText.push((farrQuestList[fi][3] > 1) ? '80%以上達成' : ((farrQuestList[fi][3] == 1) ? '50%以上達成' : ''));
					if (fi < fintArrQuestListLen - 1) farrText.push('\r\n');
				}
				PopupMsg(farrText.join(''), 0, 64);
			} else {
				PopupMsg('発生中の任務はありません', 0, 64);
			}
		} else {
			PopupMsg(['エラー：\r\n任務画面の「', fstrStasus, '」の受信に失敗しました。'].join(''), 0, 16);
			return(false);
		}
	}
	
	var strCurrentDirectory, strSetting, objSetting, strToken, strFlashVersion;
	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');
	QuestOperate();
}

Main();
