from valeurs import *
from scanValeur import *
from timer import *
from achatVente import *
import time
from threading import Thread, RLock

f = open('cac.txt','r')

valeur=[]

for i in f:
	t = i.split(';')
	url = t[1]
	nom = t[0]
	valeur.append(Valeur(nom,url))

f.close()

tempsdepart =time.time()

class AchatVente(Thread):

	
    def __init__(self):
        Thread.__init__(self)
       
        
    def run(self):
		i=0
		gain =0.0
		while 1:
			with verrou:
				if valeur[i].getAAnule() == 1:
					valeur[i].Anulation()
				if valeur[i].getAVendre() == 1:
					gain=gain+valeur[i].Vente()
					print ("Gain Total: "+ str(gain)+ " en " + str(time.time()-tempsdepart) + "s")
				if valeur[i].getMise()==0:
					valeur[i].Achat(250)
			time.sleep(00.001)
			i=i+1
			if i>39:
				i=0


class Scan(Thread):

    def __init__(self):
        Thread.__init__(self)
        
    def run(self):
        i=0
        while 1:
			with verrou:
				tmp=valeur[i].getNewTaux()- valeur[i].getTaux()
				
				if tmp > 0.5:
					valeur[i].MiseEnVente()
			time.sleep(0.4)
			i=i+1
			if i>39:
				i=0
			
class Timer(Thread):

    def __init__(self):
        Thread.__init__(self)
        
    def run(self):
        i=0
        while 1:
			with verrou:
				if valeur[i].tempsRestant() < 30:
					if valeur[i].getNewTaux() > 0:
						valeur[i].MiseEnVente()
					else:
						valeur[i].MiseEnAnnulation()
			time.sleep(0.4)
			i=i+1
			if i>39:
				i=0

verrou = RLock()

threadAchat = AchatVente()
threadTimer = Timer()
threadScan  = Scan() 

threadAchat.start()
threadTimer.start()
threadScan.start()

threadAchat.join()
threadTimer.join()
threadScan.join()

#~ print(str(valeur[1].getTaux()))
#~ print(str(valeur[1].getNewTaux()))
