_init=false

players={}


function eventNewPlayer(name)
	if not players[name] then
		players[name] = {
			name=name,
			voted=false
		}
	end
end

function eventLoop(current, remaining)
	if _init then
	if remaining <= 500 then
		if index <= #maps then
			local sum = 0
			local num = #vote
			for i,value in pairs(vote) do
				sum = sum + value
			end
			votes[index] = {sum, num}
			vote = {}
			showVote(false)
			voteVisible = false
			index = index + 1
			if index <= #maps then
				tfm.exec.newGame(maps[index])
			else
				tfm.exec.setGameTime(5,false)
			end
		elseif index==#maps+1 then
			print("終了")
			local str=""
			for key,value in pairs(votes) do
				str=str..tostring(value[1])..","..tostring(value[2])..","
			end
			print("グループ番号: "..group.." 結果: "..str)
			print("上記のマップグループ番号および結果の出力をコピーしてください")
			index = index + 1
		end
	elseif not voteVisible and remaining <= 21000 and remaining > 10000 then
		showVote(true)
		voteVisible = true
	end
	end
end

function eventNewGame()
	tfm.exec.setGameTime(25)
end

function showVote(show, pname)
	if show then
		local text1='このマップの難易度を10段階で表すと? (クリックで選択)  進捗:'..tostring(index)..'/'..tostring(#maps) ..'\n<font size="15"><b><a href="event:1">1</a> <a href="event:2">2</a> <a href="event:3">3</a> <a href="event:4">4</a> <a href="event:5">5</a> <a href="event:6">6</a> <a href="event:7">7</a> <a href="event:8">8</a> <a href="event:9">9</a> <a href="event:10">10</a></b></font>'
		local text2="アンケートに回答するにはデバインモードに設定してください"
		for name, player in pairs(tfm.get.room.playerList) do
			if player.inHardMode==2 then
				ui.addTextArea(1, text1, name, 600,340, 190,50)
				players[name].voted = false
			else
				ui.addTextArea(1, text2, name, 600,340, 190,50)
				players[name].voted = true
			end
		end
	else
		ui.removeTextArea(1, pname)
	end
end

function eventTextAreaCallback(id, name, text)
	if id==2 then
		ui.removeTextArea(2)
		_init = true
		maps={}
		maps[1] = {"@81774","@156461","@176748","@182896","@183895","@198976","@200667","@204475","@235616","@237240","@241331","@243537","@247831","@262174","@274033","@281833","@287193","@293083","@310702","@332117","@332119","@354251","@361032","@366109","@385110","@401476","@417959","@421718","@427016","@427023","@476169","@477415","@477492","@480500","@508150","@527710","@557888","@559622","@572116","@574364","@574800","@578002","@602479","@606674","@610829","@623676","@624973","@630425","@647186","@690330","@696654","@713589","@743083","@748088","@757907","@760201","@763212","@763690","@768523","@776830"}
		maps[2] = {"@778057","@778148","@781146","@812763","@183895","@814436","@868945","@916285","@922115","@926583","@936403","@944019","@948398","@948691","@967780","@978618","@979376","@1004996","@1050116","@1055974","@1073256","@1087982","@1102863","@1105814","@1125907","@1131261","@1135954","@1140334","@1143491","@1149658","@1166486","@1182072","@1182595","@1209559","@1216684","@1219811","@1224938","@1229462","@1269049","@1271929","@1272788","@1280289","@1294406","@1297364","@1299281","@1300215","@1300790","@1301071","@1304826","@1306423","@1307522","@1314906","@1315398","@1319761","@1326880","@1326777","@1327572","@1333807","@1334046","@1348694","@1359541"}
		maps[3] = {"@1359698","@1360084","@1364105","@1371252","@183895","@1371872","@1377348","@1386266","@1388745","@1388956","@1389098","@1389147","@1389410","@1393387","@1396171","@1397316","@1408736","@1409468","@1412529","@1415500","@1416001","@1420083","@1422135","@1422667","@1423534","@1428997","@1430267","@1433182","@1438510","@1439407","@1440257","@1442623","@1443660","@1444186","@1464255","@1467417","@1471211","@1472964","@1473813","@1476823","@1478116","@1481452","@1481868","@1483437","@1488677","@1488740","@1491293","@1492268","@1499645","@1500980","@1502262","@1502781","@1503617","@1515500","@1523085","@1523386","@1524647","@1524689","@1527376","@1529262","@1529282"}
		maps[4] = {"@1530225","@1530317","@1530873","@1534223","@183895","@1539005","@1539331","@1539530","@1543147","@1548014","@1548458","@1554169","@1558031","@1559425","@1560892","@1565418","@1571660","@1574676","@1576764","@1579308","@1581202","@1581846","@1582420","@1584356","@1586272","@1587710","@1589616","@1591637","@1592421","@1593943","@1595454","@1599706","@1600536","@1601257","@1602207","@1602434","@1602747","@1604029","@1609004","@1610945","@1617260","@1611428","@1617816","@1620772","@1621386","@1622434","@1623388","@1624462","@1627350","@1629869","@1630082","@1631115","@1633536","@1635599","@1638600","@1640348","@1641211","@1643185","@1646191","@1647443","@1653663"}
		maps[5] = {"@1653718","@1654965","@1655228","@1659815","@183895","@1660299","@1662903","@1664506","@1665491","@1667676","@1670221","@1670884","@1671013","@1671288","@1674360","@1678312","@1680103","@1680221","@1681512","@1683566","@1688022","@1693341","@1695322","@1696341","@1696499","@1697574","@1697959","@1707569","@1715789","@1725454","@1728253","@1729485","@1739366","@1740280","@1743871","@1745295","@1746907","@1749585","@1750581","@1752120","@1752499","@1753788","@1755835","@1758418","@1762092","@1763970","@1764571","@1764726","@1769843","@1770393","@1770646","@1771344","@1775722","@1776941","@1778724","@1778893","@1778930","@1780763","@1782143","@1785819","@1786839"}
		maps[6] = {"@1786848","@1787105","@1789245","@1792177","@183895","@1797025","@1798640","@1799300","@1799538","@1800727","@1801370","@1806188","@1806202","@1809433","@1814655","@1815762","@1816414","@1821432","@1822538","@1827925","@1831326","@1839857","@1843385","@1844002","@1852823","@1852915","@1859557","@1862054","@1874963","@1889467","@1891565","@1892379","@1893513","@1893864","@1896302","@1897182","@1897961","@1900914","@1906376","@1910562","@1911637","@1912792","@1914997","@1917509","@1918334","@1919537","@1922612","@1922800","@1924117","@1927719","@1928112","@1928599","@1928915","@1929267","@1929601","@1929909","@1930007","@1935805","@1937289","@1950754","@1953670"}
		maps[7] = {"@1957362","@1960774","@1961008","@1963242","@183895","@1964504","@1965433","@1973472","@1975038","@1975278","@1977901","@1981715","@1984121","@1985475","@1986291","@1988097","@1989190","@1991687","@1996067","@1997537","@1998164","@1998444","@1998569","@1999016","@1999534","@2005459","@2005970","@2006524","@2010596","@2013974","@2014113","@2016104","@2018861","@2023070","@2024503","@2024751","@2026827","@2028605","@2030798","@2032876","@2037528","@2039283","@2040226","@2041369","@2042602","@2047122","@2048692","@2048809","@2049965","@2054791","@2058146","@2062724","@2063328","@2064832","@2069312","@2070576","@2071641","@2074500","@2082296","@2082304","@2083506"}
		maps[8] = {"@2084246","@2084868","@2085162","@2086229","@183895","@2086282","@2086745","@2087140","@2087334","@2087436","@2089346","@2089640","@2090203","@2090909","@2090751","@2091254","@2091283","@2091478","@2092138","@2092309","@2092796","@2093022","@2093350","@2094205","@2095300","@2098490","@2100845","@2101168","@2102063","@2103304","@2106937","@2107666","@2108543","@2109497","@2113093","@2114192","@2114986","@2115001","@2118561","@2126788","@2129977","@2130189","@2131479","@2132495","@2133923","@2135210","@2135561","@2135779","@2136058","@2138269","@2138748","@2140319","@2142013","@2144709","@2151708","@2154655","@2161989","@2162919","@2162973","@2163628","@2164853"}
		maps[9] = {"@2166325","@2169324","@2171957","@2172048","@183895","@2173037","@2173855","@2175740","@2177388","@2177875","@2178769","@2180076","@2183351","@2183544","@2184423","@2188235","@2190852","@2194095","@2194909","@2195142","@2197046","@2197740","@2197874","@2199738","@2200350","@2205212","@2207182","@2207438","@2207527","@2207591","@2209792","@2211525","@2215095","@2217000","@2230462","@2245854","@2262259","@2277993","@2258320","@2270700","@2287800","@2320106","@2328523","@2359943","@2363233","@2363815","@2363966","@2364362","@2368474","@2374979","@2378082","@2385317","@2385346","@2391938","@2392394","@2392528","@2409960","@2430506","@2430807","@2433899","@2436432"}
		maps[10] = {"@2439540","@2440028","@2440410","@2441919","@183895","@2444269","@2446144","@2453397","@2458837","@2459597","@2462425","@2465274","@2471745","@2476267","@2477040","@2477416","@2478516","@2481060","@2484122","@2515593","@2515693","@2531772","@2533238","@2551172","@2559227","@2566283","@2566847","@2578895","@2580002","@2581289","@2585592","@2587777","@2588156","@2589119","@2589205","@2590788","@2591587","@2593490","@2593739","@2596544","@2596693","@2597751","@2597879","@2599051","@2599129","@2599160","@2599715","@2601125","@2604336","@2605201","@2610016","@2611641","@2612485","@2612987","@2617940","@2626940","@2628658","@2628865","@2635474","@2639154","@2647688"}
		maps[11] = {"@2647813","@2651322","@2654111","@2654758","@183895","@2655003","@2658749","@2661598","@2668632","@2668703","@2681452","@2685755","@2686029","@2686187","@2691559","@2692131","@2692287","@2694031","@2703245","@2706099","@2707107","@2707801","@2707779","@2709853","@2710443","@2712951","@2715086","@2720765","@2729755","@2738152","@2738221","@2746800","@2748118","@2749692","@2756310","@2757300","@2764951","@2765732","@2766459","@2773530","@2776363","@2777511","@2784031","@2793379","@2794992","@2796324","@2798311","@2798437","@2800239","@2801055","@2802826","@2835846","@2809503","@2815493","@2816408","@2840108","@2844234","@2846873","@2853151","@2858429","@2860148"}
		maps[12] = {"@2860887","@2861423","@2865489","@2872486","@183895","@2877296","@2878646","@2878706","@2880103","@2882407","@2886043","@2890623","@2890815","@2900610","@2900645","@2904938","@2904946","@2908440","@2915857","@2920853","@2921895","@2922065","@2922668","@2923933","@2924910","@2925988","@2926042","@2927946","@2934316","@2939112","@2941333","@2943685","@2947587","@2958092","@2962752","@2963237","@2965012","@2967511","@2973513","@2973874","@2981182","@3009352","@3011863","@3012836","@3015583","@3015873","@3030176","@3033672","@3036695","@3038757","@3040125","@3040421","@3043688","@3051156","@3059311","@3062381","@3064851","@3066197","@3067847","@3081680","@3084843"}
		maps[13] = {"@3088028","@3101635","@3102266","@3105582","@183895","@3120603","@3125267","@3138063","@3139627","@3141806","@3143857","@3148225","@3151306","@3153364","@3154665","@3202756","@3210514","@3225478","@3226890","@3230785","@3233204","@3233351","@3236003","@3238661","@3245656","@3250130","@3256509","@3258909","@3259279","@3260557","@3282022","@3283275","@3287841","@3293045","@3300010","@3307840","@3320970","@3321923","@3330136","@3331521","@3340984","@3351019","@3354168","@3357647","@3358184","@3360376","@3394251","@3395957","@3411248","@3421953","@3428667","@3439659","@3440411","@3443731","@3457077","@3458567","@3459289","@3462066","@3463622","@3477414","@3487387"}
		maps[14] = {"@3506849","@3513925","@3514186","@3515351","@183895","@3520523","@3524657","@3524843","@3526378","@3531801","@3533365","@3535468","@3535441","@3540177","@3576439","@3576562","@3586019","@3588086","@3590143","@3590243","@3590367","@3590468","@3590860","@3599383","@3600555","@3600733","@3607667","@3610599","@3612899","@3614164","@3628801","@3630055","@3633725","@3633987","@3635156","@3636374","@3637352","@3639385","@3640466","@3643600","@3644018","@3648697","@3674679","@3674887","@3675618","@3678010","@3680100","@3681317","@3686429","@3686772","@3687060","@3688472","@3690125","@3693960","@3699061","@3703628","@3710589","@3719719","@3720804","@3721807","@3728061"}
		maps[15] = {"@3728446","@3745193","@3745201","@3747953","@183895","@3751472","@3753791","@3755513","@3760738","@3765532","@3774710","@3775788","@3776347","@3777866","@3784796","@3775607","@3837639","@3858296","@3860765","@3860782","@3862824","@3866466","@3883654","@3894338","@3900973","@3902671","@3906576","@3917927","@3919670","@3938611","@3944076","@3964455","@3996013","@4011152","@4045340","@4050070","@4051197","@4056626","@4079924","@4101389","@4132617","@4137538","@4140146","@4240091","@4268312","@4276657","@4290970","@4292420","@4299790","@4423291","@4434303","@4453811","@4456849","@4457285","@4478188","@4500743","@4535793","@4545060","@4607919","@4638467","@4679741"}
		maps[16] = {"@4696193","@4697945","@4723117","@4727661","@183895","@4789940","@4806524","@4833719","@4867835","@4901446","@4903937","@4946573","@4979418","@5002429","@5030384","@5030648","@5030654","@5032528","@5036104","@5049504","@5051632","@5059021","@5061227","@5088589","@5110346","@5173817","@5179857","@5180404","@5185061","@5186430","@5188280","@5189159","@5189652","@5189707","@5200038","@5202179","@5205058","@5228127","@5228305","@5313515","@5391079","@5591826","@5611481","@5641135","@5716404","@5716423","@5806780","@5873071","@5921940","@5959104","@6088531"}

		group = tonumber(text)
		maps = maps[group]
		votes = {}
		voteVisible = false
		index = 1
		vote = {}
		tfm.exec.disableAutoNewGame(true)
		tfm.exec.disableAutoShaman(true)
		for name, player in pairs(tfm.get.room.playerList) do
			eventNewPlayer(name)
		end
		tfm.exec.newGame(maps[1])
		return
	end

	
	table.insert(vote,tonumber(text)-1)
	showVote(false, name)
	players[name].voted = true
	for name,player in pairs(tfm.get.room.playerList) do
		if not players[name].voted then
			return
		end
	end
	eventLoop(0,0)
end



(function ()
	local str='<font size="30">デバインマップの難易度分けアンケート</font>\n\n・初めに16個のマップグループから適当に一つを選びそのグループのマップについて回答していただきます\n・マップグループはそれぞれ60個程度のマップを含みます。\n・マップ開始後4秒経つと右下に投票ボックスが出るので、それぞれ制限時間25秒以内に回答してください\n・複数人で回答可能です\n・終了後、全員分の結果がLuaコードを投稿した人の#luaタブに表示されるので、グループ番号と結果をコピーしてカフェに投稿してください\n\n以下のグループ番号をクリックすると開始します\n<b><a href="event:1">1</a>  <a href="event:2">2</a>  <a href="event:3">3</a>  <a href="event:4">4</a>  <a href="event:5">5</a>  <a href="event:6">6</a>  <a href="event:7">7</a>  <a href="event:8">8</a>  <a href="event:9">9</a>  <a href="event:10">10</a>  <a href="event:11">11</a>  <a href="event:12">12</a>  <a href="event:13">13</a>  <a href="event:14">14</a>  <a href="event:15">15</a>  <a href="event:16">16</a></b>'
	ui.addTextArea(2,str,nil,110, 50,580,200)

end)()