/**
 * ハイスコアのコモンデータ作成
 */
function CreateHiScoreDataArea(){
	let players = [ "Sunny", "Luna", "Star" ];
	let difficults = [ "Easy", "Normal", "Panic" ];
	let ranks = [ "1", "2", "3", "4", "5" ];
	let idxs = [ "1", "2", "3" ];
	let score = 0;
	
	//データ作成
	ascent( i in 0..length( players ) ){
		ascent( j in 0..length( difficults ) ){
			ascent( k in 0..length( ranks ) ){
				ascent( l in 0..length( idxs ) ){
					alternative( l )
						case( 0, 2 ){
							score = 0;
						}
						case( 1 ){
							score = 100000 - k * 10000;
						}
					
					let ns = players[ i ] ~ "HiScore" ~ difficults[ j ] ~ ranks[ k ] ~ "_" ~ idxs[ l ];
					SetCommonDataEx( "OFT_Save", ns, score );
				}
			}
		}
	}
}

/**
 * ハイスコアを更新する
 * 
 * @param {String}   player    プレイヤー
 * @param {String}   difficult 難易度
 * @param {Integer}  score_1   登録するスコア 桁1
 * @param {Integer}  score_2   登録するスコア 桁2
 * @param {Integer}  score_3   登録するスコア 桁3
 * @return {Integer} 何位に入ったか(入らなかった場合は6)
 */
function SetHiScore( player, difficult, score_1, score_2, score_3 ){
	let ns = player ~ "HiScore" ~ difficult;
	let hiscore1_1 = GetCommonDataDefaultEx( "OFT_Save", ns ~ "1_1", 0 );
	let hiscore2_1 = GetCommonDataDefaultEx( "OFT_Save", ns ~ "2_1", 0 );
	let hiscore3_1 = GetCommonDataDefaultEx( "OFT_Save", ns ~ "3_1", 0 );
	let hiscore4_1 = GetCommonDataDefaultEx( "OFT_Save", ns ~ "4_1", 0 );
	let hiscore5_1 = GetCommonDataDefaultEx( "OFT_Save", ns ~ "5_1", 0 );
	let hiscore1_2 = GetCommonDataDefaultEx( "OFT_Save", ns ~ "1_2", 0 );
	let hiscore2_2 = GetCommonDataDefaultEx( "OFT_Save", ns ~ "2_2", 0 );
	let hiscore3_2 = GetCommonDataDefaultEx( "OFT_Save", ns ~ "3_2", 0 );
	let hiscore4_2 = GetCommonDataDefaultEx( "OFT_Save", ns ~ "4_2", 0 );
	let hiscore5_2 = GetCommonDataDefaultEx( "OFT_Save", ns ~ "5_2", 0 );
	let hiscore1_3 = GetCommonDataDefaultEx( "OFT_Save", ns ~ "1_3", 0 );
	let hiscore2_3 = GetCommonDataDefaultEx( "OFT_Save", ns ~ "2_3", 0 );
	let hiscore3_3 = GetCommonDataDefaultEx( "OFT_Save", ns ~ "3_3", 0 );
	let hiscore4_3 = GetCommonDataDefaultEx( "OFT_Save", ns ~ "4_3", 0 );
	let hiscore5_3 = GetCommonDataDefaultEx( "OFT_Save", ns ~ "5_3", 0 );
	
	let hiscore_1 = [ hiscore1_1, hiscore2_1, hiscore3_1, hiscore4_1, hiscore5_1, score_1 ];
	let hiscore_2 = [ hiscore1_2, hiscore2_2, hiscore3_2, hiscore4_2, hiscore5_2, score_2 ];
	let hiscore_3 = [ hiscore1_3, hiscore2_3, hiscore3_3, hiscore4_3, hiscore5_3, score_3 ];
	let idx = 5;
	let idx1 = 0;
	let idx2 = 1;
	let idx3 = 2;
	let idx4 = 3;
	let idx5 = 4;
	let idx6 = 5;
	let idx_tmp = 0;
	
	//ソート
	loop( 5 ){
		let compscore_1 = hiscore_1[ idx - 1 ];
		let compscore_2 = hiscore_2[ idx - 1 ];
		let compscore_3 = hiscore_3[ idx - 1 ];
		
		let comp = CompareScore( score_1, score_2, score_3, compscore_1, compscore_2, compscore_3 );
		
		if( comp == 1 ){
			alternative( idx )
				case( 1 ){
					idx_tmp = idx1;
					idx1 = idx2;
					idx2 = idx_tmp;
				}
				case( 2 ){
					idx_tmp = idx2;
					idx2 = idx3;
					idx3 = idx_tmp;
				}
				case( 3 ){
					idx_tmp = idx3;
					idx3 = idx4;
					idx4 = idx_tmp;
				}
				case( 4 ){
					idx_tmp = idx4;
					idx4 = idx5;
					idx5 = idx_tmp;
				}
				case( 5 ){
					idx_tmp = idx5;
					idx5 = idx6;
					idx6 = idx_tmp;
				}
			
			idx -= 1;
		}else{
			break;
		}
	}
	
	//再設定
	SetCommonDataEx( "OFT_Save", ns ~ "1_1", hiscore_1[ idx1 ] );
	SetCommonDataEx( "OFT_Save", ns ~ "2_1", hiscore_1[ idx2 ] );
	SetCommonDataEx( "OFT_Save", ns ~ "3_1", hiscore_1[ idx3 ] );
	SetCommonDataEx( "OFT_Save", ns ~ "4_1", hiscore_1[ idx4 ] );
	SetCommonDataEx( "OFT_Save", ns ~ "5_1", hiscore_1[ idx5 ] );
	SetCommonDataEx( "OFT_Save", ns ~ "1_2", hiscore_2[ idx1 ] );
	SetCommonDataEx( "OFT_Save", ns ~ "2_2", hiscore_2[ idx2 ] );
	SetCommonDataEx( "OFT_Save", ns ~ "3_2", hiscore_2[ idx3 ] );
	SetCommonDataEx( "OFT_Save", ns ~ "4_2", hiscore_2[ idx4 ] );
	SetCommonDataEx( "OFT_Save", ns ~ "5_2", hiscore_2[ idx5 ] );
	SetCommonDataEx( "OFT_Save", ns ~ "1_3", hiscore_3[ idx1 ] );
	SetCommonDataEx( "OFT_Save", ns ~ "2_3", hiscore_3[ idx2 ] );
	SetCommonDataEx( "OFT_Save", ns ~ "3_3", hiscore_3[ idx3 ] );
	SetCommonDataEx( "OFT_Save", ns ~ "4_3", hiscore_3[ idx4 ] );
	SetCommonDataEx( "OFT_Save", ns ~ "5_3", hiscore_3[ idx5 ] );
	
	//順位を返す
	return idx + 1;
}

/**
 * スコアの大小を比較する(A1,A2,A3,B1,B2,B3)
 * 
 * @param {Integer} a1 比較するスコアA1
 * @param {Integer} a2 比較するスコアA2
 * @param {Integer} a3 比較するスコアA3
 * @param {Integer} b1 比較するスコアB1
 * @param {Integer} b2 比較するスコアB2
 * @param {Integer} b3 比較するスコアB3
 * @return {Integer}   比較結果
 *                        1:A > B
 *                        0:A = B
 *                       -1:A < B
 */
function CompareScore( a1, a2, a3, b1, b2, b3 ){
	let comp = 0;
	
	if( a1 > b1 ){
		comp = 1;
	}else if( a1 < b1 ){
		comp = -1;
	}else{
		if( a2 > b2 ){
			comp = 1;
		}else if( a2 < b2 ){
			comp = -1;
		}else{
			if( a3 > b3 ){
				comp = 1;
			}else if( a3 < b3 ){
				comp = -1;
			}
		}
	}
	
	return comp;
}

/**
 * スコアを文字列に変換する
 * 
 * @param {Integer} s1 スコア1
 * @param {Integer} s2 スコア2
 * @param {Integer} s3 スコア3
 * @return {String}    変換した文字列
 */
function ScoreToString( s1, s2, s3 ){
	let retstr = "";
	
	let isTopS1 = true;
	let isTopS2 = ( s1 == 0 );
	let isTopS3 = ( s1 == 0 && s2 == 0 );
	let strs1 = "";
	let strs2 = "";
	let strs3 = "";
	
	if( isTopS1 ){
		if( s1 != 0 ){
			strs1 = IntToString( s1 );
		}
	}else{
		strs1 = LPad( IntToString( strs1 ), 7, "0" );
	}
	if( isTopS2 ){
		if( s2 != 0 ){
			strs2 = IntToString( s2 );
		}
	}else{
		strs2 = LPad( IntToString( s2 ), 7, "0" );
	}
	if( isTopS3 ){
		//if( s3 != 0 ){
			strs3 = IntToString( s3 );
		//}
	}else{
		strs3 = LPad( IntToString( s3 ), 7, "0" );
	}
	
	retstr = strs1 ~ strs2 ~ strs3;
	
	return retstr;
}
