#! /bin/python

import socket
import time
import thread
import sys
import random
try:
	import readline
except:
	pass #readline not available

angle = 0
velocity = 15
clientRandom = False

def connect(HOST = 'localhost', PORT = 3490):

	s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
	#bind the socket to a public host,
	# and a well-known port
	s.connect((HOST, PORT))
	return s

def name():
	s.sendall('n memo  \n')
	send()
	time.sleep( 1 )
	s.sendall('n  memo \n')
	send()
	time.sleep( 1 )
	s.sendall('n   memo\n')
	send()
	time.sleep( 1 )
	s.sendall('n    mem\n')
	send()
	time.sleep( 1 )
	s.sendall('n     me\n')
	send()
	time.sleep( 1 )
	s.sendall('n      m\n')
	send()
	time.sleep( 1 )
	s.sendall('n       \n')
	send()
	time.sleep( 1 )
	s.sendall('n o     \n')
	send()
	time.sleep( 1 )
	s.sendall('n mo    \n')
	send()
	time.sleep( 1 )
	s.sendall('n emo   \n')
	send()
	time.sleep( 1 )

def send():
	s.sendall('v ' + str(velocity)+'\n')
	s.sendall(str(angle) + '\n')

def ninput(s):
	while True:
		ninput = raw_input("> ")
		if('v' in ninput):
			global velocity
			velocity = ninput.split().pop()
		elif('c' in ninput):
			s.sendall('c\n')
		elif('r' in ninput):
			global clientRandom
			clientRandom = True
			print "Random Mode - randomly shooting +-10 around the set angle"
		elif('q' in ninput):
			global clientRandom
			clientRandom = False
			print "Random Mode disabled"
		elif ninput:
			try:
				global angle
				angle = float(ninput)
			except ValueError:
				print "not a valid angle"
			print angle

def plusOneBot(s, startangle):
	s.sendall('n memoBot\n')
	print "sending..."
	if startangle:
		angle = float(startangle)
	while True:
		angle = angle + 1
		s.sendall('v ' + str(velocity) + '\n')
		s.sendall(str(angle) + '\n')
		time.sleep(5)

def randomBot(s):
	s.sendall('n memoBot\n')
	print "sending..."
	random.seed()
	while True:
		angle = random.random() * 360
		s.sendall('v ' + str(velocity) + '\n')
		s.sendall(str(angle) + '\n')
		time.sleep(5)

def client(s):
	s.sendall('n memo\n')
	thread.start_new_thread(ninput,(s,))
	while True:
		global clientRandom
		global angle
		if clientRandom == True:
			angle = random.random() * 20 - 10 + angle
			print "Angle: " + str(angle) + " - Random mode - press q to exit"
		send()
		name()

if len(sys.argv) == 5:
	s = connect(sys.argv[1], int(sys.argv[2]))
	if('b' in sys.argv[3]):
		plusOneBot(s, sys.argv[4])
elif len(sys.argv) == 4:
	if 'b' in sys.argv[2]:
		s = connect(sys.argv[1])
		plusOneBot(s, sys.argv[3])
	elif 'r' in sys.argv[3]:
		s = connect(sys.argv[1], int(sys.argv[2]))
		randomBot(s)
elif len(sys.argv) == 3:
	if 'r' in sys.argv[2]:
		s = connect(sys.argv[1])
		randomBot(s)
	elif 'b' in sys.argv[1]:
		s = connect()
		plusOneBot(s, sys.argv[2])
	else:
		s = connect(sys.argv[1], int(sys.argv[2]))
		client(s)
elif len(sys.argv) == 2:
	if 'r' in sys.argv[1]:
		s = connect()
		randomBot(s)
	else:
		s = connect(sys.argv[1])
		client(s)
elif len(sys.argv) == 1:
	s = connect()
	client(s)
else:
	print "wrong number of arguments"
