import numpy as np
import matplotlib.pyplot as plt
from pwn import *
from scipy.spatial.transform import Rotation as R
import time

q_att_ts = []
badnesses = []

LOCAL = False

if LOCAL:
    sep = ' '
    r = process('octave challenge.m', shell=True)
else:
    sep = ','
    r = remote('filter.satellitesabove.me', 5014)
    r.clean()
    r.send('THE_TICKET')
    time.sleep(0.1)

def adversary(true_pose):
    lie = 0.25

    euler = true_pose.as_euler('xyz')
    euler[1] = lie

    return R.from_euler('xyz',euler)


for i in range(10000):
    if LOCAL:
        badness = float(r.readline().decode().strip())
        log.info(f'[!] badness: {badness}')
        badnesses.append(badness)

    rl = r.readline(timeout=3)
    if rl.startswith(b'Uh oh,'):
        r.interactive()
    log.info(f'<== [{i}] {rl}')
    
    [w,x,y,z] = [float(x) for x in rl.decode().strip().split()]
    true_pose = R.from_quat([x,y,z,w])

    new_pose = adversary(true_pose)

    [x,y,z,w] = new_pose.as_quat()
    msg = sep.join(map(str,[w,x,y,z]))
    log.info(f'==> {msg}')
    r.send(msg+'\n')
