using System.Collections;
using UnityEngine;
namespace GrassCutter
{
public partial class DemoBot
{
///
/// -demoyt: a long YouTube video on the phone for well over the couple of minutes it used to stop at, what the page
/// thinks it runs in, the video full screen with the phone on its side, and the tool belt out of the way of the phone.
///
IEnumerator TubeDemo()
{
TeamState.DeleteSave();
yield return new WaitForSeconds(2f);
NetSession.Instance.HostLocal();
while (!Me || !Ts) yield return null;
yield return new WaitForSeconds(2f);
var at = new Vector3(-16f, Soil + 0.1f, 14f);
Me.Teleport(at);
Me.LookAt(at + new Vector3(-6f, 1.4f, 9f));
yield return new WaitForSeconds(0.8f);
var phone = Phone.I;
phone.Show(PhoneMode.Side);
yield return new WaitForSeconds(0.6f);
phone.Show(PhoneMode.Up);
yield return new WaitForSeconds(0.6f);
var scr = phone.Screen;
var web = scr.Web;
scr.Open("https://m.youtube.com/watch?v=aqz-KE-bpKQ");
yield return Until(() => web.Url != null && web.Url.Contains("watch"), 30f);
yield return new WaitForSeconds(4f);
yield return Ask(web, "String(navigator.webdriver)+' '+(navigator.userAgentData?navigator.userAgentData.brands.map(b=>b.brand).join('/')+' '+navigator.userAgentData.platform+' mobile '+navigator.userAgentData.mobile:'no hints')");
Log($"the page sees: webdriver {_said}");
yield return Ask(web, "(()=>{const v=document.querySelector('video');if(!v)return 'no video';v.play().catch(()=>{});return 'playing'})()");
BotCapture.From = null;
yield return Shot("yt_1_up");
const string state = "(()=>{const v=document.querySelector('video');const e=document.querySelector('.ytp-error,ytm-player-error-message-renderer,.player-error,.ytp-error-content');" +
"return (v?Math.round(v.currentTime)+' s, paused '+v.paused:'no video')+(e&&e.offsetParent?' ERROR '+e.innerText.slice(0,90).replace(/\\s+/g,' '):'')})()";
for (int i = 1; i <= (CommandLine.Has("-demoytlong") ? 10 : 1); i++)
{
yield return new WaitForSecondsRealtime(20f);
yield return Ask(web, state);
Log($"after {i * 20} s: {_said}");
if (i == 5) yield return Shot("yt_2_100s");
}
yield return Shot("yt_3_200s");
// on its side and full screen: the video must fill the page, square on
scr.Turn();
yield return new WaitForSeconds(1.5f);
yield return Ask(web, "(()=>{const p=document.querySelector('.html5-video-player')||document.querySelector('video');if(!p)return Promise.resolve('nothing to fill');" +
"return p.requestFullscreen().then(()=>new Promise(r=>setTimeout(r,800))).then(()=>{const b=document.fullscreenElement.getBoundingClientRect();" +
"return 'full screen at '+Math.round(b.x)+','+Math.round(b.y)+' size '+Math.round(b.width)+'x'+Math.round(b.height)+', page '+innerWidth+'x'+innerHeight+', scrolled '+scrollY}).catch(e=>'refused: '+e.message)})()");
Log($"wide, {_said} (phone css {web.CssW}x{web.CssH})");
yield return Shot("yt_4_full");
yield return Ask(web, state);
Log($"full screen: {_said}");
// a page of our own that goes full screen on a real tap, the way a site's own button does
scr.Open("data:text/html,
" +
"go full
" +
"");
yield return Until(() => web.Url != null && web.Url.StartsWith("data:"), 15f);
yield return new WaitForSeconds(1.5f);
var tap = scr.PageAt(0.5f, 0.5f);
scr.Pointer(tap, true, true, false);
yield return null;
scr.Pointer(tap, false, false, true);
yield return new WaitForSeconds(1.5f);
yield return Ask(web, "(()=>{const f=document.fullscreenElement;if(!f)return 'not full screen '+(b.dataset.e||'');const r=f.getBoundingClientRect();return 'at '+Math.round(r.x)+','+Math.round(r.y)+' size '+Math.round(r.width)+'x'+Math.round(r.height)+', page '+innerWidth+'x'+innerHeight})()");
Log($"full screen by a tap: {_said} (phone css {web.CssW}x{web.CssH})");
yield return Shot("yt_5_tapfull");
Log("yt done");
Application.Quit();
}
}
}