from multiprocessing import Process, Queue, Event
from math import pi

from ..communication import grsim
from time import time


class Commander(Process):
#class Commander(object):

    def __init__(self):
        #Process.__init__(self)
        self.queue = Queue()
        self._exit = Event()

    def run(self):
        while self._exit.is_set():
            try:
                self.send(self.queue.get())
            except KeyboardInterrupt:
                break

    def stop(self):
        self._exit.set()

    def sendall(self, actions):
        while not actions.empty():
            self.send(actions.get())

    def send(self, actions):
        pass


class SimCommander(Commander):

    def __init__(self, team):
        Commander.__init__(self)
        self.team = team
        self.sender = grsim.grSimSender()

    def send(self, actions):
        packet = self.sender.new_packet()

        if len(actions) > 0:
            packet.commands.isteamyellow = self.team.is_yellow
            packet.commands.timestamp = time()
            for a in actions:
                if not a:
                    continue
                vx, vy, va = a.speeds
                c = packet.commands.robot_commands.add()
                c.id = a.uid
                c.kickspeedx = (a.kick or 0.0) * 5
                c.kickspeedz = a.chipkick or 0.0
                c.veltangent = vx
                c.velnormal = vy
                c.velangular = va * pi / 180
                c.spinner = (a.dribble or 0.0) > 0
                c.wheelsspeed = False

                # reset action values
                a.reset()

        #print packet
        self.sender.send_packet(packet)

@@ -781,16 +781,84 @@
  = team%0A
+        #self.sender = grsim.grSimSender(('200.20.120.133', 20011))%0A
         
@@ -1343,15 +1343,19 @@
 peed
-x
+z
  = (a.
+chip
 kick
@@ -1369,9 +1369,9 @@
 ) * 
-5
+3
 %0A   
@@ -1375,32 +1375,35 @@
                 
+if 
 c.kickspeedz = a
@@ -1403,16 +1403,282 @@
 edz 
-= a.chip
+%3E 0:%0A                    # XXX FIXME this should be tested,%0A                    # we don't know at what angle we%0A                    # will be able to chipkick%0A                    c.kickspeedx = 1.5 * c.kickspeedz%0A                else:%0A                    c.kickspeedx = (a.
 kick
@@ -1684,16 +1684,21 @@
 k or 0.0
+) * 5
 %0A       

Add some unit fixing.