示例#1
0
    def trace(self,message):
        ''' Print debugging messages. '''
        if self.destination_port != self.plot_port_number:
            return

        if not self.plot_sequence_on and not self.plot_rate_on and not self.plot_queue_on and not self.plot_window_on:
            Sim.trace("TCP",message)
示例#2
0
文件: node.py 项目: Steve525/bene
 def forward_packet(self,packet):
     if packet.destination_address not in self.forwarding_table:
         Sim.trace("%d no routing entry for %d" % (self.links[0].address,packet.destination_address))
         return
     link = self.forwarding_table[packet.destination_address]
     Sim.trace("%d forwarding packet to %d" % (link.address,packet.destination_address))
     link.handle_packet(packet)
示例#3
0
 def retransmit(self,event):
     if not self.send_buffer:
         return
     if not self.packet_is_outstanding:
         return
     Sim.trace("%d retransmission timer fired" % (self.source_address))
     packet = self.send_one_packet(self.last_sent)
示例#4
0
文件: main.py 项目: zigapk/racorodja
def plot_range_for_velocity(v_min, v_max, step=500, save_to_file=None):
    vx, vy_min, vy_max = [], [], []
    for v in range(v_min, v_max, step):
        # print v to indicate progress
        print(v)
        # run sim for desired velocity
        sim = Sim(planet_name, capsule_name, 80000)
        sim.set_v0(v)
        rng = get_angle_range(sim, step=0.01)

        if len(rng) > 0:  # makes sure that even if it is impossible to land, the program won't crash after hours
            # store data
            vx.append(v)
            vy_min.append(rng[0])
            vy_max.append(rng[-1])

    # plot
    f, ax = plt.subplots(1)
    ax.plot(vx, vy_min, 'bo')
    ax.plot(vx, vy_max, 'ro')
    plt.xlabel('$ v_0 $ [m/s]')
    plt.ylabel('$ \phi_0 [°]$')
    plt.title(r'Min and max $ \phi_0(v_0) $')

    if save_to_file is not None:
        plt.savefig(save_to_file)

    plt.show()
示例#5
0
    def __init__(self, username=None, password=None, uni='145'):
        self.uni = uni
        self.username = username
        self.password = password
        self.logged_in = False

        self._prepare_logger()
        self._prepare_browser()

        self.MAIN_URL = 'https://s%s-pl.ogame.gameforge.com/game/index.php' % self.uni
        self.PAGES = {
            'main': self.MAIN_URL + '?page=overview',
            'resources': self.MAIN_URL + '?page=resources',
            'station': self.MAIN_URL + '?page=station',
            'research': self.MAIN_URL + '?page=research',
            'shipyard': self.MAIN_URL + '?page=shipyard',
            'defense': self.MAIN_URL + '?page=defense',
            'fleet': self.MAIN_URL + '?page=fleet1',
            'galaxy': self.MAIN_URL + '?page=galaxy',
            'galaxyCnt': self.MAIN_URL + '?page=galaxyContent',
            'events': self.MAIN_URL + '?page=eventList',
        }
        self.planets = []
        self.moons = []
        self.active_attacks = []

        self.fleet_slots = 0
        self.active_fleets = 0
        self.transport_manager = TransportManager()
        self.server_time = self.local_time = datetime.now()
        self.time_diff = 0
        self.sim = Sim()
示例#6
0
 def receive_packet(self, packet):
     # handle ACK
     if self.packet_is_outstanding and packet.ack_number == self.sequence:
         # this acks new data, so advance the send buffer, reset
         # the outstanding flag, cancel the timer, and send if possible
         Sim.trace(
             "%d received StopAndWait ACK from %d for %d"
             % (packet.destination_address, packet.source_address, packet.ack_number)
         )
         self.send_buffer = self.send_buffer[self.sequence - self.last_sent :]
         self.packet_is_outstanding = False
         self.cancel_timer()
         self.send_if_possible()
     # handle data
     if packet.length > 0:
         Sim.trace(
             "%d received StopAndWait segment from %d for %d"
             % (packet.destination_address, packet.source_address, packet.sequence)
         )
         # if the packet is the one we're expecting increment our
         # ack number and add the data to the receive buffer
         if packet.sequence == self.ack:
             self.increment_ack(packet.sequence + packet.length)
             self.receive_buffer += packet.body
             # deliver data that is in order
             self.app.receive_packet(packet)
         # always send an ACK
         self.send_ack()
示例#7
0
 def forward_unicast_packet(self,packet):
     if packet.destination_address not in self.forwarding_table:
         Sim.trace("%s no routing entry for %d" % (self.hostname,packet.destination_address))
         return
     link = self.forwarding_table[packet.destination_address]
     Sim.trace("%s forwarding packet to %d" % (self.hostname,packet.destination_address))
     link.send_packet(packet)
示例#8
0
def part_iii_evaluation(sim_filename):
    print sim_filename
    mdp = MDP("blank_2_actions_81_states_mdp.txt")
    results = []
    # prior: assume each transition seen once
    transition_count = [[[0.1 for _ in range(81)] for _ in range(81)]
                        for _ in range(2)]

    for n in range(10):
        print "Big loop " + str(n)
        results.append([])
        for i in range(100):
            mdp, transition_count = adp_rl(mdp, Sim(MDP(sim_filename)),
                                           transition_count)
        value_fn, policy, iterations = plan(mdp, 0.99, 0.01)
        print "Value: " + str(value_fn)
        print "Policy: " + str(policy)
        #print "Reward: " + str(mdp.rewards)
        #print "Transitions: " + str(mdp.transitions)
        for i in range(100):
            reward = run_policy(Sim(MDP(sim_filename)), policy)
            results[n].append(reward)

        print "Average reward of policy: " + str(average(results[n]))

    for l in results:
        print average(l)
示例#9
0
    def plot_sequence(self, sequence_number, isACK = False, dropped=False):
        if self.destination_port != self.plot_port_number:
            return

        message = "%i %i %d" % (sequence_number, dropped, isACK)

        if self.plot_sequence_on:
            Sim.trace("TCP", message)
示例#10
0
    def plot_window(self, size):
        if self.destination_port != self.plot_port_number:
            return

        message = "%i" % size

        if self.plot_window_on:
            Sim.trace("TCP", message)
示例#11
0
文件: link.py 项目: Steve525/bene
 def next(self,event):
     if len(self.queue) > 0:
         Sim.trace_custom('%d' % (len(self.queue)), 'queue', self.filename)
         packet = self.queue.pop(0)
         #Sim.trace_custom('%d' % (len(self.queue)), 'queue', self.filename)
         self.transmit(packet)
     else:
         self.busy = False
示例#12
0
    def run(self):
        # parameters
        Sim.scheduler.reset()
        Sim.set_debug('AppHandler')
        Sim.set_debug('TCP')

        # setup network
        net = Network('../networks/setup.txt')
        net.loss(self.loss)

        # setup routes
        n1 = net.get_node('n1')
        n2 = net.get_node('n2')
        n1.add_forwarding_entry(address=n2.get_address('n1'), link=n1.links[0])
        n2.add_forwarding_entry(address=n1.get_address('n2'), link=n2.links[0])

        # setup transport
        t1 = Transport(n1)
        t2 = Transport(n2)

        # setup application
        a = AppHandler(self.filename, self.out_directory)

        # setup connection
        c1 = TCP(t1,
                 n1.get_address('n2'),
                 1,
                 n2.get_address('n1'),
                 1,
                 a,
                 window=self.window)
        c2 = TCP(t2,
                 n2.get_address('n1'),
                 1,
                 n1.get_address('n2'),
                 1,
                 a,
                 window=self.window)

        # send a file
        with open(self.in_directory + '/' + self.filename, 'r') as f:
            while True:
                data = f.read(10000)
                if not data:
                    break
                Sim.scheduler.add(delay=0, event=data, handler=c1.send)

        # run the simulation
        Sim.scheduler.run()
        # print str(self.window) + " & " + \
        #         str(Sim.scheduler.current_time()) + " & " + \
        #         str(4116160.0 / float(Sim.scheduler.current_time())) + " & " + \
        #         str(c2.totalQueueingDelay / float(c1.totalPacketsSent)) + " \\\\"

        # print str(self.window) + "," + str(4116160.0 / float(Sim.scheduler.current_time()))

        print str(self.window) + "," + str(
            c2.totalQueueingDelay / float(c1.totalPacketsSent))
示例#13
0
 def send_ack(self):
     packet = TCPPacket(source_address=self.source_address,
                        source_port=self.source_port,
                        destination_address=self.destination_address,
                        destination_port=self.destination_port,
                        sequence=self.sequence,ack_number=self.ack)
     # send the packet
     Sim.trace("%d sending StopAndWait ACK to %d for %d" % (self.source_address,self.destination_address,packet.ack_number))
     self.transport.send_packet(packet)
def gemRun(reps):
    taxMeans = []
    taxSEs = []

    p['verboseDebugging'] = False
    p['singleRunGraphs'] = False
    p['interactiveGraphics'] = False

    dataFile = open('GEMSA data new.txt', 'a')
    meansFile = open('GEMSA means new.txt', 'a')
    outFile = open('GEMSA outputs new.txt', 'a')

    #    agingParentList = [ 0.0, 0.1, 0.2, 0.4 ]
    #    careProbList = [ 0.0004, 0.0008, 0.0012, 0.0016 ]
    #    retiredHoursList = [ 20.0, 30.0, 40.0, 60.0 ]
    #    retiredAgeList = [ 60.0 ]
    #    ageingParentList = [ 0.0, 0.1 ]
    #    careProbList = [ 0.0004 ]
    #    retiredHoursList = [ 20.0 ]
    #    retiredAgeList = [ 60.0 ]

    for variableCare in p['ageingParentList']:
        for variableProb in p['careProbList']:
            for variableRetired in p['retiredHoursList']:
                for variableAge in p['retiredAgeList']:
                    p['agingParentsMoveInWithKids'] = variableCare
                    p['personCareProb'] = variableProb
                    p['retiredHours'] = variableRetired
                    p['ageOfRetirement'] = variableAge
                    print "Trying parents-moving-in probability: ", variableCare
                    print "Trying person care probability: ", variableProb
                    print "Trying retired hours: ", variableRetired
                    print "Trying retirement age: ", variableAge
                    taxList = []
                    taxSum = 0.0
                    meansFile.write(
                        str(variableCare) + "\t" + str(variableProb) + "\t" +
                        str(variableRetired) + "\t" + str(variableAge) + "\n")
                    for i in range(0, reps):
                        print i,
                        s = Sim(p)
                        tax, seed = s.run()
                        taxList.append(tax)
                        taxSum += tax
                        print tax
                        dataFile.write(
                            str(seed) + "\t" + str(variableCare) + "\t" +
                            str(variableProb) + "\t" + str(variableRetired) +
                            "\t" + str(variableAge) + "\t" + str(tax) + "\n")
                    taxMeans.append(pylab.mean(taxList))
                    outFile.write(str(taxSum / reps) + "\n")
                    taxSEs.append(pylab.std(taxList) / math.sqrt(reps))

    dataFile.close()
    meansFile.close()
    outFile.close()
示例#15
0
 def __init__(self, planets=None):
     if planets is None:
         planets = []
     self.planets = planets
     self.sim = Sim()
     self.dest_planet = None
     self.building = None
     self.resources_needed = {'metal': 0, 'crystal': 0, 'deuterium': 0}
     self.resources_sent = self.resources_needed
     self.building_queue = set()
def multiprocessingSim(params):
    # Create Sim instance
    folderRun = params[0]['rootFolder'] + '/Rep_' + str(params[0]['repeatIndex'])
    
    s = Sim(params[0]['scenarioIndex'], params[0], folderRun)
    
    print''
    print params[1]['policyIndex']
    print''
    
    s.run(params[1]['policyIndex'], params[1], params[1]['randomSeed'])
def sensitivityRun(runtype, ageingList, careList, retiredHList, retiredAList,
                   reps):
    taxMeans = []
    taxSEs = []

    p['verboseDebugging'] = False
    p['singleRunGraphs'] = False
    p['interactiveGraphics'] = False

    dataFile = open(runtype + ' GEMSA data.txt', 'a')
    meansFile = open(runtype + ' GEMSA means.txt', 'a')
    outFile = open(runtype + ' GEMSA outputs.txt', 'a')

    #    agingParentList = [ 0.0, 0.1, 0.2, 0.4 ]
    #    careProbList = [ 0.0004, 0.0008, 0.0012, 0.0016 ]
    #    retiredHoursList = [ 20.0, 30.0, 40.0, 60.0 ]
    #    retiredAgeList = [ 60.0 ]
    #    ageingParentList = [ 0.0, 0.1 ]
    #    careProbList = [ 0.0004 ]
    #    retiredHoursList = [ 20.0 ]
    #    retiredAgeList = [ 60.0 ]

    for run in xrange(len(ageingList)):
        p['agingParentsMoveInWithKids'] = ageingList[run]
        p['personCareProb'] = careList[run]
        p['retiredHours'] = retiredHList[run]
        p['ageOfRetirement'] = retiredAList[run]
        print "Trying parents-moving-in probability: ", ageingList[run]
        print "Trying person care probability: ", careList[run]
        print "Trying retired hours: ", retiredHList[run]
        print "Trying retirement age: ", retiredAList[run]
        taxList = []
        taxSum = 0.0
        meansFile.write(
            str(ageingList[run]) + "\t" + str(careList[run]) + "\t" +
            str(retiredHList[run]) + "\t" + str(retiredAList[run]) + "\n")
        for i in range(0, reps):
            print i,
            s = Sim(p)
            tax, seed = s.run()
            taxList.append(tax)
            taxSum += tax
            print tax
            dataFile.write(
                str(seed) + "\t" + str(ageingList[run]) + "\t" +
                str(careList[run]) + "\t" + str(retiredHList[run]) + "\t" +
                str(retiredAList[run]) + "\t" + str(tax) + "\n")
        taxMeans.append(pylab.mean(taxList))
        outFile.write(str(taxSum / reps) + "\n")
        taxSEs.append(pylab.std(taxList) / math.sqrt(reps))

    dataFile.close()
    meansFile.close()
    outFile.close()
def batchRun(num):
    p['interactiveGraphics'] = False
    dataFile = open('batchRunData.txt', 'w')
    for i in range(0, num):
        print "Doing batch run: ", i
        taxList = []
        s = Sim(p)
        tax = s.run()
        taxList.append(tax)
        print "Social care cost per taxpayer: ", tax
        dataFile.write(str(i) + "\t" + str(tax) + "\n")
    dataFile.close()
def sensitivityLarge(runtype, input_list, reps):
    taxMeans = []
    taxSEs = []

    p['verboseDebugging'] = False
    p['singleRunGraphs'] = False
    p['interactiveGraphics'] = False

    outFile = open(runtype + ' GEMSA outputs large.txt', 'a')

    for run in xrange(len(input_list[0])):
        print("Running simulation number {}...".format(run))
        print("Number of reps: {}".format(reps))
        sim_list = np.array(input_list)
        print(sim_list)
        p['agingParentsMoveInWithKids'] = sim_list[0, run]
        print(p['agingParentsMoveInWithKids'])
        p['personCareProb'] = sim_list[1, run]
        p['retiredHours'] = sim_list[2, run]
        p['ageOfRetirement'] = sim_list[3, run]
        p['baseDieProb'] = sim_list[4, run]
        p['babyDieProb'] = sim_list[5, run]
        p['personCareProb'] = sim_list[6, run]
        p['maleAgeCareScaling'] = sim_list[7, run]
        p['femaleAgeCareScaling'] = sim_list[8, run]
        p['childHours'] = sim_list[9, run]
        p['homeAdultHours'] = sim_list[10, run]
        p['workingAdultHours'] = sim_list[11, run]
        p['lowCareHandicap'] = sim_list[12, run]
        p['growingPopBirthProb'] = sim_list[13, run]
        p['basicDivorceRate'] = sim_list[14, run]
        p['variableDivorce'] = sim_list[15, run]
        p['basicMaleMarriageProb'] = sim_list[16, run]
        p['basicFemaleMarriageProb'] = sim_list[17, run]
        p['probApartWillMoveTogether'] = sim_list[18, run]
        p['coupleMovesToExistingHousehold'] = sim_list[19, run]
        p['basicProbAdultMoveOut'] = sim_list[20, run]
        p['variableMoveBack'] = sim_list[21, run]
        taxList = []
        taxSum = 0.0
        for i in range(0, reps):
            print i,
            s = Sim(p)
            tax, seed = s.run()
            taxList.append(tax)
            taxSum += tax
            print tax
        taxMeans.append(pylab.mean(taxList))
        outFile.write(str(taxSum / reps) + "\n" + str(seed) + "\n")
        taxSEs.append(pylab.std(taxList) / math.sqrt(reps))

    outFile.close()
示例#20
0
文件: node.py 项目: Steve525/bene
    def handle_packet(self,packet):
        # if this is the first time we have seen this packet, set its
        # creation timestamp
        if packet.created == None:
            packet.created = Sim.scheduler.current_time()
        # check if the packet is for me
        for link in self.links:
            if link.address == packet.destination_address:
                Sim.trace("%d received packet" % (packet.destination_address))
                self.receive_packet(packet)
                return

        # forward the packet
        self.forward_packet(packet)
示例#21
0
    def __init__(self):
        self.sim = Sim()

        pygame.init()
        pygame.font.init()
        self.screen = pygame.display.set_mode((G.screenWidth, G.screenHeight))
        pygame.display.set_caption("AntBridgeSim")
        self.clock = pygame.time.Clock()
        self.setupButtons()
        self.drawGrid()
        for button in self.buttons:
            button.draw(self.screen)

        oldpos = self.sim.ant.pos
        oldId = self.sim.antId

        dead = None
        while G.running:
            time.sleep(G.sleep)
            self.eventHandler()
            self.drawBlock(self.sim.ant.pos)
            self.drawJoint(self.sim.ant.pos)
            self.drawBlock(oldpos)
            self.drawJoint(oldpos)
            oldpos = self.sim.ant.pos
            if self.sim.antId != oldId:
                oldId = self.sim.antId
                self.drawGrid()
            self.drawJoints()  #TODO incrementally draw
            pygame.display.flip()

            if not dead:
                try:
                    if not self.sim.step():
                        break
                except Error as e:
                    print e
                    dead = True
                except Success as e:
                    print e
                    dead = True
            else:
                paused = True
                while paused:
                    for event in pygame.event.get():
                        if event.type == pygame.QUIT:
                            paused = False
                            G.running = False
示例#22
0
    def send_one_packet(self,sequence):
        # get one packet worth of data
        body = self.send_buffer[0:self.mss]
        packet = TCPPacket(source_address=self.source_address,
                           source_port=self.source_port,
                           destination_address=self.destination_address,
                           destination_port=self.destination_port,
                           body=body,
                           sequence=sequence,ack_number=self.ack)

        # send the packet
        Sim.trace("%d sending StopAndWait segment to %d for %d" % (self.source_address,self.destination_address,packet.sequence))
        self.transport.send_packet(packet)

        # set a timer
        self.timer = Sim.scheduler.add(delay=self.timeout, event='retransmit', handler=self.retransmit)
        return packet
示例#23
0
 def send_packet(self,packet):
     # check if link is running
     if not self.running:
         return
     # drop packet due to queue overflow
     if self.queue_size and len(self.queue) == self.queue_size:
         Sim.trace("%d dropped packet due to queue overflow" % (self.address))
         return
     # drop packet due to random loss
     if self.loss > 0 and random.random() < self.loss:
         Sim.trace("%d dropped packet due to random loss" % (self.address))
         return
     packet.enter_queue = Sim.scheduler.current_time()
     if len(self.queue) == 0 and not self.busy:
         # packet can be sent immediately
         self.busy = True
         self.transmit(packet)
     else:
         # add packet to queue
         self.queue.append(packet)
示例#24
0
    def run(self):
        # parameters
        Sim.scheduler.reset()

        if hasattr(self, 'debug') and "a" in self.debug:
            Sim.set_debug('AppHandler')
        if hasattr(self, 'debug') and "t" in self.debug:
            Sim.set_debug('TCP')

        # setup network
        net = Network('networks/one-hop.txt')
        net.loss(self.loss)

        # setup routes
        n1 = net.get_node('n1')
        n2 = net.get_node('n2')
        n1.add_forwarding_entry(address=n2.get_address('n1'),link=n1.links[0])
        n2.add_forwarding_entry(address=n1.get_address('n2'),link=n2.links[0])

        # setup transport
        t1 = Transport(n1)
        t2 = Transport(n2)

        # setup application
        a = AppHandler(self.filename)

        # setup connection
        c1 = TCP(t1,n1.get_address('n2'),1,n2.get_address('n1'),1,a,dynamic_rto=self.dynamic_rto)
        c2 = TCP(t2,n2.get_address('n1'),1,n1.get_address('n2'),1,a,dynamic_rto=self.dynamic_rto)

        f = open(self.filename, "rb")
        try:
            data = f.read(1000)
            while data != "":
                Sim.scheduler.add(delay=0, event=data, handler=c1.send)
                data = f.read(1000)
        finally:
            f.close()

        # run the simulation
        Sim.scheduler.run()
def retireRun(reps):
    taxMeans = []
    taxSEs = []
    p['verboseDebugging'] = False
    p['singleRunGraphs'] = False
    p['interactiveGraphics'] = False
    dataFile = open('retirementAgeData2.txt', 'w')
    #p['ageingParentList'] = [50, 55, 65, 70, 75, 80]
    for variableCare in p['ageingParentList']:
        p['ageOfRetirement'] = variableCare
        print "Trying retirement age: ", variableCare
        taxList = []
        for i in range(0, reps):
            print i,
            s = Sim(p)
            tax = s.run()
            taxList.append(tax)
            print tax
            dataFile.write(
                str(variableCare) + "\t" + str(i) + "\t" + str(tax) + "\n")
        taxMeans.append(pylab.mean(taxList))
        taxSEs.append(pylab.std(taxList) / math.sqrt(reps))

    dataFile.close()

    indices1 = pylab.arange(len(p['ageingParentList']))

    taxFig = pylab.figure()
    taxBar = taxFig.add_subplot(1, 1, 1)
    taxBar.bar(indices1,
               taxMeans,
               facecolor='red',
               align='center',
               yerr=taxSEs,
               ecolor='black')
    taxBar.set_ylabel('Mean social care cost per taxpayer')
    taxBar.set_xlabel('Age of retirement')
    taxBar.set_xticks(indices1)
    taxBar.set_xticklabels(p['ageingParentList'])
    pylab.savefig('retirementAgeRunSet1.pdf')
    pylab.show()
示例#26
0
	def __init__(self, planets=[]):
		self.planets = planets
		self.sim = Sim()
		self.dest_planet = None
		self.building = None
		self.resources_needed = {
			'metal': 0,
			'crystal': 0,
			'deuterium': 0
		}
		self.resources_sent = self.resources_needed
		self.building_queue = set()
示例#27
0
def part_ii_evaluation():
    random_results_1 = []
    safe_results_1 = []
    range_results_1 = []
    random_results_2 = []
    safe_results_2 = []
    range_results_2 = []

    for i in range(1000):
        print i
        random_results_1.append(
            random_policy(Sim(MDP("parking_mdp_linear_rewards_n_10.txt"))))
        random_results_2.append(
            random_policy(Sim(MDP("parking_mdp_quad_rewards_n_10.txt"))))

        safe_results_1.append(
            safe_policy(Sim(MDP("parking_mdp_linear_rewards_n_10.txt")), 0.5))
        safe_results_2.append(
            safe_policy(Sim(MDP("parking_mdp_quad_rewards_n_10.txt")), 0.5))

        range_results_1.append(
            range_policy(Sim(MDP("parking_mdp_linear_rewards_n_10.txt")), 2,
                         8))
        range_results_2.append(
            range_policy(Sim(MDP("parking_mdp_quad_rewards_n_10.txt")), 2, 6))

    print average(random_results_1)
    print average(safe_results_1)
    print average(range_results_1)
    print average(random_results_2)
    print average(safe_results_2)
    print average(range_results_2)
def sensitivityTenParams(runtype, input_list, reps):
    taxMeans = []
    taxSEs = []

    p['verboseDebugging'] = False
    p['singleRunGraphs'] = False
    p['interactiveGraphics'] = False

    outFile = open(runtype + ' GEMSA outputs.txt', 'a')

    for run in xrange(len(input_list[0])):
        print("Running simulation number {}...".format(run))
        print("Number of reps: {}".format(reps))
        sim_list = np.array(input_list)
        print(sim_list)
        p['agingParentsMoveInWithKids'] = sim_list[0, run]
        p['baseCareProb'] = sim_list[1, run]
        p['retiredHours'] = sim_list[2, run]
        p['ageOfRetirement'] = sim_list[3, run]
        p['personCareProb'] = sim_list[4, run]
        p['maleAgeCareScaling'] = sim_list[5, run]
        p['femaleAgeCareScaling'] = sim_list[6, run]
        p['childHours'] = sim_list[7, run]
        p['homeAdultHours'] = sim_list[8, run]
        p['workingAdultHours'] = sim_list[9, run]

        taxList = []
        taxSum = 0.0
        for i in range(0, reps):
            print i,
            s = Sim(p)
            tax, seed = s.run()
            taxList.append(tax)
            taxSum += tax
            print tax
        taxMeans.append(pylab.mean(taxList))
        outFile.write(str(taxSum / reps) + "\t" + str(seed) + "\n")
        taxSEs.append(pylab.std(taxList) / math.sqrt(reps))

    outFile.close()
示例#29
0
def main():
    conf.init_cnf()

    order = Order()
    conn = MySQLdb.connect(host=mysql_cnf['host'],
                           port=int(mysql_cnf['port']),
                           user=mysql_cnf['user'],
                           passwd=mysql_cnf['passwd'],
                           db=mysql_cnf['db'],
                           connect_timeout=3,
                           local_infile=1)

    mysql_read_order(order, conn)


    sim = Sim(order)
    logger.info('Calculate user similarity ...')
    sim.user_based()
    logger.info('Calculate item similarity ...')
    sim.item_based()

    mysql_write_sim(sim, conn)
示例#30
0
文件: bot.py 项目: HePax/ogame-bot
    def __init__(self, username, password, server):
        self.username = username
        self.password = password
        self.logged_in = False

        self._prepare_logger()
        self._prepare_browser()
        farms = options['farming']['farms']
        self.farm_no = randint(0, len(farms) - 1) if farms else 0

        self.MAIN_URL = 'http://labdcc.fceia.unr.edu.ar/~jgalat/game.php'
        server = server.replace('http://', '')
        if server[-1] == '/':
            server = server[:-1]
        self.MAIN_URL = 'http://' + server + '/game.php'
        self.PAGES = {
            'main': self.MAIN_URL + '?page=overview',
            'buildings': self.MAIN_URL + '?page=buildings',
            'station': self.MAIN_URL + '?page=station',
            'research': self.MAIN_URL + '?page=buildings&mode=research',
            'shipyard': self.MAIN_URL + '?page=buildings&mode=fleet',
            'defense': self.MAIN_URL + '?page=defense',
            'fleet': self.MAIN_URL + '?page=fleet',
            'galaxy': self.MAIN_URL + '?page=galaxy',
            'galaxyCnt': self.MAIN_URL + '?page=galaxyContent',
            'events': self.MAIN_URL + '?page=eventList',
        }
        self.planets = []
        self.moons = []
        self.active_attacks = []

        self.fleet_slots = 0
        self.active_fleets = 0

        self.server_time = self.local_time = datetime.now()
        self.time_diff = 0
        self.emergency_sms_sent = False
        self.transport_manager = TransportManager()
        self.sim = Sim()
示例#31
0
    def __init__(self, username=None, password=None, uni='69'):
        self.uni = uni
        self.username = username
        self.password = password
        self.logged_in = False

        self._prepare_logger()
        self._prepare_browser()
        farms = options['farming']['farms']
        self.farm_no = randint(0, len(farms) - 1) if farms else 0

        self.MAIN_URL = 'https://s%s-%s.ogame.gameforge.com/game/index.php' % (
            self.uni, options['credentials']['server'])
        self.PAGES = {
            'main': self.MAIN_URL + '?page=overview',
            'resources': self.MAIN_URL + '?page=resources',
            'station': self.MAIN_URL + '?page=station',
            'research': self.MAIN_URL + '?page=research',
            'shipyard': self.MAIN_URL + '?page=shipyard',
            'defense': self.MAIN_URL + '?page=defense',
            'fleet': self.MAIN_URL + '?page=fleet1',
            'galaxy': self.MAIN_URL + '?page=galaxy',
            'galaxyCnt': self.MAIN_URL + '?page=galaxyContent',
            'events': self.MAIN_URL + '?page=eventList',
            'resSettings': self.MAIN_URL + '?page=resourceSettings',
        }
        self.planets = []
        self.moons = []
        self.active_attacks = []

        self.fleet_slots = 0
        self.active_fleets = 0

        self.server_time = self.local_time = datetime.now()
        self.time_diff = 0
        self.emergency_sms_sent = False
        self.transport_manager = TransportManager()
        self.sim = Sim()
示例#32
0
def run_a_sim(run_id):
    logging.debug('**** Starting run {}'.format(run_id))
    sim = Sim(TRIBUTE_COUNT)
    while not sim.game_over():
        sim.epoch()
    logging.debug('**** Ending run {}'.format(run_id))
    output = {
        'run_id': run_id,
        'tributes': sim.compressed_result(),
        'ticks': sim.ticks
    }
    return output
示例#33
0
文件: link.py 项目: Steve525/bene
 def handle_packet(self,packet):
     # drop packet due to queue overflow
     if self.queue_size and len(self.queue) == self.queue_size:
         Sim.trace("! %d dropped packet due to queue overflow" % (self.address))
         Sim.trace_custom('x', 'queue', self.filename)
         return
     # drop packet due to random loss
     if self.loss > 0 and random.random() < self.loss:
         Sim.trace("! %d dropped packet due to random loss" % (self.address))
         return
     packet.enter_queue = Sim.scheduler.current_time()
     #Sim.trace("packet %d entered queue" % packet.sequence)
     if len(self.queue) == 0 and not self.busy:
         # packet can be sent immediately
         self.busy = True
         self.transmit(packet)
     else:
         # add packet to queue
         Sim.trace_custom('%d' % (len(self.queue)), 'queue', self.filename)
         self.queue.append(packet)
示例#34
0
def main():

    # Create first generation
    pool = Pop(size_gen)

    # Step through generations
    for _ in range(num_gen):

        # initialize data histogram for sim visualization
        hist = Data()
        terrain = Terrain(400, 4)

        # create pybox2d dynamic bodies based on individual's gene
        gen = [Biped(pool.population[i].name, pool.population[i].gene)
               for i in range(size_gen)]

        for k, biped in enumerate(gen):

            # specify terrain and biped to race
            race = Sim(terrain, biped)

            if not hist.terrain:
                hist.set_terrain(terrain)

            # run simulation without visualization
            score, bb, time = race.run(1e4)

            # store sim data and fitness evaluation
            pool.population[k].fitness = score
            hist.timelines[biped.name] = race.history.timelines['timeline']

        # resort gene pool
        pool.population = list(
            sorted(pool.population, key=lambda x: x.fitness))

        # visualize top bipeds' simulations
        shown = pool.population[-num_shown:]
        timelines = [s.name for s in shown]
        view.start()
        view.run(hist, timelines, speed=3)

        # evolve gene pool
        pool.evolve()
示例#35
0
def parse(path):
    with open(path, 'r') as f:
        replay = json.load(f)

    size = replay['mapHeight'] * replay['mapWidth']

    tiles = [const.EMPTY] * size
    for x in replay['mountains']:
        tiles[x] = const.MOUNTAIN

    armies = [0] * size
    for x, n in zip(replay['cities'], replay['cityArmies']):
        armies[x] = n

    for i, x in enumerate(replay['generals']):
        armies[x] = 1
        tiles[x] = i

    sim = Sim(replay['mapWidth'],
              replay['mapHeight'],
              replay['generals'],
              replay['cities'],
              tiles,
              armies)
    num_players = len(replay['generals'])

    turns = max(move['turn'] for move in replay['moves']) + 1 if replay['moves'] else 0
    moves = [{i: (-1, -1, False, False)
             for i in range(num_players)}
             for _ in range(turns)]

    for move in replay['moves']:
        turn = move['turn']
        player = move['index']
        moves[turn][player] = move['start'], move['end'], move['is50'], False

    for afk in replay['afks']:
        player = afk['index']
        turn = afk['turn'] + 50
        if turn < len(moves):
            moves[turn][player][-1] = True

    return sim, moves
def solve_w_SharedArray(model, sol_init, Nsteps, dt, Nspm, processes=None):
    try:
        shm_y = SharedArray.create("shm://y", [sol_init.y.shape[0], Nspm],
                                   dtype=float)
        shm_t = SharedArray.create("shm://t", [Nspm], dtype=float)
        shm_i_app = SharedArray.create("shm://i_app", [Nspm], dtype=float)
    except:
        SharedArray.delete("shm://y")
        SharedArray.delete("shm://t")
        SharedArray.delete("shm://i_app")
        shm_y = SharedArray.create("shm://y", [sol_init.y.shape[0], Nspm],
                                   dtype=float)
        shm_t = SharedArray.create("shm://t", [Nspm], dtype=float)
        shm_i_app = SharedArray.create("shm://i_app", [Nspm], dtype=float)

    i_app = 1.0
    for i in range(Nspm):
        shm_y[:, i] = sol_init.y[:, -1]
        shm_t[i] = 0.0
        shm_i_app[i] = i_app * (1 + (i + 1) / Nspm)

    time = 0
    tstep = 0
    end_time = Nsteps * dt
    while time < end_time:
        work = [Sim(model, sol_init, dt, ind, tstep) for ind in range(Nspm)]
        with ProcessPoolExecutor() as ex:
            ex.map(shm_step, work)
            ex.shutdown(wait=True)
        time += dt
        tstep += 1

    y = copy_array(shm_y)
    t = copy_array(shm_t)
    SharedArray.delete("shm://y")
    SharedArray.delete("shm://t")
    SharedArray.delete("shm://i_app")

    return y, t
示例#37
0
文件: bot.py 项目: mvpossum/ogame-bot
    def __init__(self, username, password, server):
        self.username = username
        self.password = password
        self.logged_in = False

        self._prepare_logger()
        self._prepare_browser()
        farms = options['farming']['farms']
        self.farm_no = randint(0, len(farms)-1) if farms else 0

        self.MAIN_URL = 'http://labdcc.fceia.unr.edu.ar/~jgalat/game.php'
        server=server.replace('http://','')
        if server[-1]=='/':
            server=server[:-1]
        self.MAIN_URL = 'http://'+server+'/game.php'
        self.PAGES = {
            'main':        self.MAIN_URL + '?page=overview',
            'buildings':   self.MAIN_URL + '?page=buildings',
            'station':     self.MAIN_URL + '?page=station',
            'research':    self.MAIN_URL + '?page=buildings&mode=research',
            'shipyard':    self.MAIN_URL + '?page=buildings&mode=fleet',
            'defense':     self.MAIN_URL + '?page=defense',
            'fleet':       self.MAIN_URL + '?page=fleet',
            'galaxy':      self.MAIN_URL + '?page=galaxy',
            'galaxyCnt':   self.MAIN_URL + '?page=galaxyContent',
            'events':      self.MAIN_URL + '?page=eventList',
        }
        self.planets = []
        self.moons = []
        self.active_attacks = []

        self.fleet_slots = 0
        self.active_fleets = 0

        self.server_time = self.local_time = datetime.now()
        self.time_diff = 0
        self.emergency_sms_sent = False
        self.transport_manager = TransportManager()
        self.sim = Sim()
示例#38
0
    def __init__(self, username=None, password=None, uni='69'):
        self.uni = uni
        self.username = username
        self.password = password
        self.logged_in = False
        
        self._prepare_logger()
        self._prepare_browser()
        farms = options['farming']['farms']
        self.farm_no = randint(0, len(farms)-1) if farms else 0
        
        self.MAIN_URL = 'http://s%s-pl.ogame.gameforge.com/game/index.php' % self.uni
        self.PAGES = {
            'main':        self.MAIN_URL + '?page=overview',
            'resources':   self.MAIN_URL + '?page=resources',
            'station':     self.MAIN_URL + '?page=station',
            'research':    self.MAIN_URL + '?page=research',
            'shipyard':    self.MAIN_URL + '?page=shipyard',
            'defense':     self.MAIN_URL + '?page=defense',
            'fleet':       self.MAIN_URL + '?page=fleet1',
            'galaxy':      self.MAIN_URL + '?page=galaxy',
            'galaxyCnt':   self.MAIN_URL + '?page=galaxyContent',
            'events':      self.MAIN_URL + '?page=eventList',
        }
        self.planets = []
        self.moons = []
        self.active_attacks = []

        self.fleet_slots = 0
        self.active_fleets = 0

        self.server_time = self.local_time = datetime.now()
        self.time_diff = 0
        self.emergency_sms_sent = False
        self.transport_manager = TransportManager()
        self.sim = Sim()
示例#39
0
    def receive_packet(self,packet):
        # handle broadcast packets
        if packet.destination_address == 0:
            Sim.trace("%s received packet" % (self.hostname))
            self.deliver_packet(packet)
        else:
            # check if unicast packet is for me
            for link in self.links:
                if link.address == packet.destination_address:
                    Sim.trace("%s received packet" % (self.hostname))
                    self.deliver_packet(packet)
                    return

        # decrement the TTL and drop if it has reached the last hop
        packet.ttl = packet.ttl - 1
        if packet.ttl <= 0:
            Sim.trace("%s dropping packet due to TTL expired" % (self.hostname))
            return

        # forward the packet
        self.forward_packet(packet)
示例#40
0
文件: link.py 项目: mkeagar/cs460
 def handle_packet(self,packet):
     # drop packet due to queue overflow
     if self.queue_size and len(self.queue) == self.queue_size:
         Sim.trace("%d dropped packet due to queue overflow" % (self.address))
         if self.printOut:
             self.myfile.write(str(Sim.scheduler.current_time()) + " x\n")
             Sim.print_packet_loss(packet)
         return
     # drop packet due to random loss
     if self.loss > 0 and random.random() < self.loss:
         Sim.trace("%d dropped packet due to random loss" % (self.address))
         if self.printOut:
             self.myfile.write(str(Sim.scheduler.current_time()) + " x\n")
         return
     packet.enter_queue = Sim.scheduler.current_time()
     if len(self.queue) == 0 and not self.busy:
         # packet can be sent immediately
         self.busy = True
         self.transmit(packet)
     else:
         # add packet to queue
         self.queue.append(packet)
示例#41
0
文件: link.py 项目: nathand8/bene
 def trace(self,message):
     Sim.trace("Link",message)
示例#42
0
文件: bot.py 项目: tivisse/yogame
class Bot(object):

    BASE_URL = 'https://en.ogame.gameforge.com/'
    LOGIN_URL = 'https://en.ogame.gameforge.com/main/login'
    HEADERS = [('User-agent', 'Mozilla/5.0 (Windows NT 6.2; WOW64)\
     AppleWebKit/537.15 (KHTML, like Gecko) Chrome/24.0.1295.0 Safari/537.15')]
    RE_BUILD_REQUEST = re.compile(r"sendBuildRequest\(\'(.*)\', null, 1\)")
    RE_RESEARCH_REQUEST = re.compile(r"sendBuildRequest\(\'(.*)\'")
    RE_SERVER_TIME = re.compile(
        r"var serverTime=new Date\((.*)\);var localTime")

    #ship -> ship id on the page
    SHIPS = {
        'lm': '204',
        'hm': '205',
        'cr': '206',
        'ow': '207',
        'pn': '215',
        'bb': '211',
        'ns': '213',
        'gs': '214',
        'lt': '202',
        'dt': '203',
        'cs': '208',
        'rc': '209',
        'ss': '210'
    }

    # mission ids
    MISSIONS = {
        'attack': '1',
        'transport': '3',
        'station': '4',
        'expedition': '15',
        'collect': '8'
    }

    TARGETS = {'planet': '1', 'moon': '3', 'debris': '2'}

    SPEEDS = {
        100: '10',
        90: '9',
        80: '8',
        70: '7',
        60: '6',
        50: '5',
        40: '4',
        30: '3',
        20: '2',
        10: '1'
    }

    def __init__(self, username=None, password=None, uni='69'):
        self.uni = uni
        self.username = username
        self.password = password
        self.logged_in = False

        self._prepare_logger()
        self._prepare_browser()
        farms = options['farming']['farms']
        self.farm_no = randint(0, len(farms) - 1) if farms else 0

        self.MAIN_URL = 'https://s%s-en.ogame.gameforge.com/game/index.php' % self.uni
        self.PAGES = {
            'main': self.MAIN_URL + '?page=overview',
            'resources': self.MAIN_URL + '?page=resources',
            'station': self.MAIN_URL + '?page=station',
            'research': self.MAIN_URL + '?page=research',
            'shipyard': self.MAIN_URL + '?page=shipyard',
            'defense': self.MAIN_URL + '?page=defense',
            'fleet': self.MAIN_URL + '?page=fleet1',
            'galaxy': self.MAIN_URL + '?page=galaxy',
            'galaxyCnt': self.MAIN_URL + '?page=galaxyContent',
            'events': self.MAIN_URL + '?page=eventList',
        }
        self.planets = []
        self.moons = []
        self.active_attacks = []

        self.fleet_slots = 0
        self.active_fleets = 0

        self.server_time = self.local_time = datetime.now()
        self.time_diff = 0
        self.emergency_sms_sent = False
        self.transport_manager = TransportManager()
        self.sim = Sim()

    def _get_url(self, page, planet=None):
        url = self.PAGES[page]
        if planet is not None:
            url += '&cp=%s' % planet.id
        return url

    def _prepare_logger(self):
        self.logger = logging.getLogger("mechanize")
        fh = RotatingFileHandler('bot.log', maxBytes=100000, backupCount=5)
        sh = logging.StreamHandler()
        fmt = logging.Formatter(fmt='%(asctime)s %(levelname)s %(message)s',
                                datefmt='%m-%d, %H:%M:%S')
        fh.setFormatter(fmt)
        sh.setFormatter(fmt)
        self.logger.setLevel(logging.INFO)
        self.logger.addHandler(fh)
        self.logger.addHandler(sh)

    def _prepare_browser(self):
        self.br = mechanize.Browser()
        self.br.set_handle_equiv(True)
        self.br.set_handle_redirect(True)
        self.br.set_handle_referer(True)
        self.br.set_handle_robots(False)
        self.br.addheaders = self.HEADERS

    def _parse_build_url(self, js):
        """
        convert: `sendBuildRequest('url', null, 1)`; into: `url`
        """
        return self.RE_BUILD_REQUEST.findall(js)[0]

    def _parse_research_url(self, js):
        """
        convert: `sendBuildRequest('url', null, false)`; into: `url`
        """
        return self.RE_RESEARCH_REQUEST.findall(js)[0]

    def _parse_server_time(self, content):
        return self.RE_SERVER_TIME.findall(content)[0]

    def get_mother(self):
        for p in self.planets:
            if p.mother:
                return p
        return p[0] if self.planets else None

    def get_closest_planet(self, p):
        def min_dist(p, d):
            return d

        _, d, _ = p.split(":")
        return sorted([(planet, planet.get_distance(p))
                       for planet in self.planets],
                      key=lambda x: x[1])[0][0]

    def find_planet(self, name=None, coords=None, id=None, is_moon=None):
        if is_moon:
            planets = self.moons
        else:
            planets = self.planets
        for p in planets:
            if name == p.name or coords == p.coords or id == p.id:
                return p

    def get_safe_planet(self, planet):
        '''
        Get first planet which is not under attack and isn't `planet`
        '''
        unsafe_planets = [a.planet for a in self.active_attacks]
        for p in self.planets:
            if not p in unsafe_planets and p != planet:
                return p
        # no safe planets! go to mother
        return self.planets[0]

    def login(self, username=None, password=None):
        username = username or self.username
        password = password or self.password

        try:
            resp = self.br.open(self.MAIN_URL, timeout=10)
            soup = BeautifulSoup(resp)
        except:
            return False

        alert = soup.find(id='attack_alert')

        # no redirect on main page == user logged in
        if resp.geturl() != self.BASE_URL and alert:
            self.logged_in = True
            self.logger.info('Logged as: %s' % username)
            return True

        self.logger.info('Logging in..')
        self.br.select_form(name='loginForm')
        # self.br.form['uni'] = ['s%s-en.ogame.gameforge.com' % self.uni]
        # self.br.form['login'] = username
        # self.br.form['pass'] = password
        self.br.form.set_value_by_label(['Hyperion'], 'uni')
        self.br.form.set_value(username, 'login')
        self.br.form.set_value(password, 'pass')
        self.br.submit()

        if self.br.geturl().startswith(self.MAIN_URL):
            self.logged_in = True
            self.logger.info('Logged as: %s' % username)
            return True
        else:
            self.logged_in = False
            self.logger.error('Login failed!')
            return False

    def calc_time(self, resp):
        try:
            y, mo, d, h, mi, sec = map(
                int,
                self._parse_server_time(resp).split(','))
        except:
            self.logger.error('Exception while calculating time')
        else:
            self.local_time = n = datetime.now()
            self.server_time = datetime(n.year, n.month, n.day, h, mi, sec)
            self.time_diff = self.server_time - self.local_time

            self.logger.info('Server time: %s, local time: %s' % \
                (self.server_time, self.local_time))

    def fetch_planets(self):
        self.logger.info('Fetching planets..')
        resp = self.br.open(self.PAGES['main']).read()

        self.calc_time(resp)

        soup = BeautifulSoup(resp)
        self.planets = []
        self.moons = []

        try:
            for i, c in enumerate(soup.findAll('a', 'planetlink')):
                name = c.find('span', 'planet-name').text
                coords = c.find('span', 'planet-koords').text[1:-1]
                url = c.get('href')
                p_id = int(c.parent.get('id').split('-')[1])
                construct_mode = len(c.parent.findAll('a',
                                                      'constructionIcon')) != 0
                p = Planet(p_id, name, coords, url, construct_mode)
                if i == 0:
                    p.mother = True
                self.planets.append(p)

                #check if planet has moon
                moon = c.parent.find('a', 'moonlink')
                if moon and 'moonlink' in moon['class']:
                    url = moon.get('href')
                    m_id = url.split('cp=')[1]
                    m = Moon(m_id, coords, url)
                    self.moons.append(m)
        except:
            self.logger.exception('Exception while fetching planets')
        else:
            self.check_attacks(soup)

    def handle_planets(self):
        self.fetch_planets()

        for p in iter(self.planets):
            self.update_planet_info(p)
            self.update_planet_research(p)
            self.update_planet_facilities(p)
            self.update_planet_fleet(p)
        for m in iter(self.moons):
            self.update_planet_info(m)
            self.update_planet_fleet(m)

    def update_planet_fleet(self, planet):
        resp = self.br.open(self._get_url('fleet', planet))
        soup = BeautifulSoup(resp)
        ships = {}
        for k, v in self.SHIPS.iteritems():
            available = 0
            try:
                s = soup.find(id='button' + v)
                available = int(
                    s.find('span', 'textlabel').nextSibling.replace('.', ''))
            except:
                available = 0
            ships[k] = available

        #self.logger.info('Updating %s fleet' % planet)
        #self.logger.info('%s' % fleet)
        planet.ships = ships

    def update_planet_info(self, planet):
        in_construction_mode = False
        resp = self.br.open(self._get_url('resources', planet))
        soup = BeautifulSoup(resp)

        try:
            metal = int(soup.find(id='resources_metal').text.replace('.', ''))
            planet.resources['metal'] = metal
            crystal = int(
                soup.find(id='resources_crystal').text.replace('.', ''))
            planet.resources['crystal'] = crystal
            deuterium = int(
                soup.find(id='resources_deuterium').text.replace('.', ''))
            planet.resources['deuterium'] = deuterium
            energy = int(
                soup.find(id='resources_energy').text.replace('.', ''))
            planet.resources['energy'] = energy
        except:
            self.logger.exception('Exception while updating resources info')
        else:
            self.logger.info('Updating resources info for %s:' % planet)
            s = 'metal - %(metal)s, crystal - %(crystal)s, deuterium - %(deuterium)s'
            self.logger.info(s % planet.resources)
        if planet.is_moon():
            return
        try:
            buildingList = soup.find(id='building')
            storageList = soup.find(id='storage')
            buildings = ('metalMine', 'crystalMine', 'deuteriumMine',
                         'solarPlant', 'fusionPlant', 'solarSatellite',
                         'metalStorage', 'crystalStorage', 'deuteriumTank')
            storages = ('metalStorage', 'crystalStorage', 'deuteriumTank')
            allBuildingList = zip(buildings, buildingList.findAll('li')) + zip(
                storages, storageList.findAll('li'))
            for building, b in allBuildingList:
                can_build = 'on' in b.get('class')
                fb = b.find('a', 'fastBuild')
                build_url = fb.get('onclick') if fb else ''
                if build_url:
                    build_url = self._parse_build_url(build_url)
                try:
                    level = int(b.find('span', 'textlabel').nextSibling)
                except AttributeError:
                    try:
                        level = int(b.find('span', 'level').text)
                    except:
                        pass
                suff_energy = planet.resources[
                    'energy'] - self.sim.upgrade_energy_cost(
                        building, level + 1) > 0
                res = dict(level=level,
                           can_build=can_build,
                           build_url=build_url,
                           sufficient_energy=suff_energy)

                planet.buildings[building] = res

            if buildingList.find('div', 'construction'):
                in_construction_mode = True
        except:
            self.logger.exception('Exception while updating buildings info')
            return False
        else:
            self.logger.info('%s buildings were updated' % planet)
        if not in_construction_mode:
            text, url = planet.get_mine_to_upgrade()
            if url:
                self.logger.info('Building upgrade on %s: %s' % (planet, text))
                self.br.open(url)
                planet.in_construction_mode = True
                #let now transport manager to clear building queue
                self.transport_manager.update_building(planet)
        else:
            self.logger.info('Building queue is not empty')
        return True

    def update_planet_research(self, planet):
        resp = self.br.open(self._get_url('research', planet))
        soup = BeautifulSoup(resp)
        try:
            ButtonList = soup.find(id='buttonz')
            AllResearchList = ButtonList.findAll('li')
            for research in AllResearchList:
                if research.get('class') == 'on':
                    fb = research.find('a', 'fastBuild')
                    if fb:
                        build_url = fb.get('onclick') if fb else ''
                        build_url = self._parse_research_url(build_url)
                        self.logger.info('Research launched on %s:%s' %
                                         (planet, fb.get('title')))
                        self.br.open(build_url)
                        break
        except:
            self.logger.exception('Exception while retrieving researches')

    def update_planet_facilities(self, planet):
        resp = self.br.open(self._get_url('station', planet))
        soup = BeautifulSoup(resp)
        try:
            ButtonList = soup.find(id='stationbuilding')
            AllResearchList = ButtonList.findAll('li')
            for research in AllResearchList:
                if research.get('class') == 'on':
                    fb = research.find('a', 'fastBuild')
                    if fb:
                        build_url = fb.get('onclick') if fb else ''
                        build_url = self._parse_research_url(build_url)
                        self.logger.info('Facility upgraded on %s:%s' %
                                         (planet, fb.get('title')))
                        self.br.open(build_url)
                        break
        except:
            self.logger.exception(
                'Exception while retrieving facilities statuses')

        return True

    def transport_resources(self):
        tasks = self.transport_manager.find_dest_planet(self.planets)
        if tasks is None:
            return False
        self.logger.info(self.transport_manager.get_summary())
        for task in iter(tasks):
            self.logger.info('Transport attempt from: %s, to: %s with resources %s' \
                % (task['from'], task['where'], task['resources']))
            result = self.send_fleet(
                task['from'],
                task['where'].coords,
                fleet=task['from'].get_fleet_for_resources(task['resources']),
                resources=task['resources'],
                mission='transport')
            if result:
                self.transport_manager.update_sent_resources(task['resources'])
                self.logger.info('Resources sent: %s, resources needed: %s' \
                    % (task['resources'], self.transport_manager.get_resources_needed()))

        return True

    def build_defense(self, planet):
        """
        Build defense for all resources on the planet
        1. plasma
        2. gauss
        3. heavy cannon
        4. light cannon
        5. rocket launcher
        """
        url = self._get_url('defense', planet)
        resp = self.br.open(url)
        for t in ('406', '404', '403', '402', '401'):
            self.br.select_form(name='form')
            self.br.form.new_control('text', 'menge', {'value': '100'})
            self.br.form.fixup()
            self.br['menge'] = '100'

            self.br.form.new_control('text', 'type', {'value': t})
            self.br.form.fixup()
            self.br['type'] = t

            self.br.form.new_control('text', 'modus', {'value': '1'})
            self.br.form.fixup()
            self.br['modus'] = '1'

            self.br.submit()

    def get_player_status(self, destination, origin_planet=None):
        if not destination:
            return

        status = {}
        origin_planet = origin_planet or self.get_closest_planet(destination)
        galaxy, system, position = destination.split(':')

        url = self._get_url('galaxyCnt', origin_planet)
        data = urlencode({'galaxy': galaxy, 'system': system})
        resp = self.br.open(url, data=data)
        soup = BeautifulSoup(resp)

        soup.find(id='galaxytable')
        planets = soup.findAll('tr', {'class': 'row'})
        target_planet = planets[int(position) - 1]
        name_el = target_planet.find('td', 'playername')
        status['name'] = name_el.find('span').text

        status['inactive'] = 'inactive' in name_el.get('class', '')
        return status

    def find_inactive_nearby(self, planet, radius=15):

        self.logger.info("Searching idlers near %s in radius %s" %
                         (planet, radius))

        nearby_systems = planet.get_nearby_systems(radius)
        idlers = []

        for system in nearby_systems:
            galaxy, system = system.split(":")
            url = self._get_url('galaxyCnt', planet)
            data = urlencode({'galaxy': galaxy, 'system': system})
            resp = self.br.open(url, data=data)
            soup = BeautifulSoup(resp)

            galaxy_el = soup.find(id='galaxytable')
            planets = galaxy_el.findAll('tr', {'class': 'row'})
            for pl in planets:
                name_el = pl.find('td', 'playername')
                debris_el = pl.find('td', 'debris')
                inactive = 'inactive' in name_el.get('class', '')
                debris_not_found = 'js_no_action' in debris_el.get('class', '')
                if not inactive or not debris_not_found:
                    continue
                position = pl.find('td', 'position').text
                coords = "%s:%s:%s" % (galaxy, system, position)
                player_id = name_el.find('a').get('rel')

                player_info = soup.find(id=player_id)
                rank_el = player_info.find('li', 'rank')

                if not rank_el:
                    continue

                rank = int(rank_el.find('a').text)
                if rank > 4000 or rank < 900:
                    continue

                idlers.append(coords)
                time.sleep(2)

        return idlers

    def find_inactives(self):

        inactives = []
        for p in self.planets:
            try:
                idlers = self.find_inactive_nearby(p)
                self.logger.info(" ".join(idlers))
                inactives.extend(idlers)
            except Exception as e:
                self.logger.exception(e)
                continue
            time.sleep(5)

        self.logger.info(" ".join(inactives))
        self.inactives = list(set(inactives))
        self.logger.info(inactives)

    def send_fleet(self,
                   origin_planet,
                   destination,
                   fleet={},
                   resources={},
                   mission='attack',
                   target='planet',
                   speed=None):
        if origin_planet.coords == destination:
            self.logger.error('Cannot send fleet to the same planet')
            return False
        self.logger.info('Sending fleet from %s to %s (%s)' \
            % (origin_planet, destination, mission))
        resp = self.br.open(self._get_url('fleet', origin_planet))
        try:
            try:
                self.br.select_form(name='shipsChosen')
            except mechanize.FormNotFoundError:
                self.logger.info('No available ships on the planet')
                return False

            soup = BeautifulSoup(resp)
            for ship, num in fleet.iteritems():
                s = soup.find(id='button' + self.SHIPS[ship])
                num = int(num)
                try:
                    available = int(
                        s.find('span',
                               'textlabel').nextSibling.replace('.', ''))
                except:
                    available = 0
                if available < num and mission in ('attack', 'expedition'):
                    self.logger.info('No available ships to send')
                    return False
                if num > 0:
                    self.br.form['am' + self.SHIPS[ship]] = str(num)

            self.br.submit()

            try:
                self.br.select_form(name='details')
            except mechanize.FormNotFoundError:
                self.logger.info('No available ships on the planet')
                return False

            galaxy, system, position = destination.split(':')
            self.br['galaxy'] = galaxy
            self.br['system'] = system
            self.br['position'] = position
            self.br.form.find_control("type").readonly = False
            self.br['type'] = self.TARGETS[target]
            self.br.form.find_control("speed").readonly = False
            if speed:
                self.br['speed'] = self.SPEEDS[speed]
            self.br.submit()

            self.br.select_form(name='sendForm')
            self.br.form.find_control("mission").readonly = False
            self.br.form['mission'] = self.MISSIONS[mission]
            if 'metal' in resources:
                self.br.form['metal'] = str(resources['metal'])
            if 'crystal' in resources:
                self.br.form['crystal'] = str(resources['crystal'])
            if 'deuterium' in resources:
                self.br.form['deuterium'] = str(resources['deuterium'])
            self.br.submit()
        except Exception as e:
            self.logger.exception(e)
            return False
        else:
            if mission == 'attack':
                self.farm_no += 1
        return True

    def send_message(self, url, player, subject, message):
        self.logger.info('Sending message to %s: %s' % (player, message))
        self.br.open(url)
        self.br.select_form(nr=0)
        self.br.form['betreff'] = subject
        self.br.form['text'] = message
        self.br.submit()

    def send_sms(self, msg):
        from smsapigateway import SMSAPIGateway
        try:
            SMSAPIGateway().send(msg)
        except Exception as e:
            self.logger.exception(str(e))

    def handle_attacks(self):
        attack_opts = options['attack']
        send_sms = bool(options['sms']['send_sms'])

        for a in self.active_attacks:
            if a.is_dangerous():
                self.logger.info('Handling attack: %s' % a)
                if not a.planet.is_moon():
                    self.build_defense(a.planet)
                if send_sms and not a.sms_sent:
                    self.send_sms(a.get_sms_text())
                    a.sms_sent = True
                if send_sms and not a.message_sent:
                    self.send_message(a.message_url, a.player,
                                      attack_opts['message_topic'],
                                      a.get_random_message())
                    a.message_sent = True
                self.fleet_save(a.planet)

    def check_attacks(self, soup):
        alert = soup.find(id='attack_alert')
        if not alert:
            self.logger.exception('Check attack failed')
            return
        if 'noAttack' in alert.get('class', ''):
            self.logger.info('No attacks')
            self.active_attacks = []
        else:
            self.logger.info('ATTACK!')
            resp = self.br.open(self.PAGES['events'])
            soup = BeautifulSoup(resp)
            hostile = False
            try:
                for tr in soup.findAll('tr'):
                    countDown = tr.find('td', 'countDown')
                    if countDown and 'hostile' in countDown.get('class', ''):
                        hostile = True
                        # First: check if attack was noticed
                        if tr.get('id'):
                            attack_id = tr.get('id').split('-')[1]
                        elif countDown.get('id'):
                            attack_id = countDown.get('id').split('-')[2]
                        if not attack_id or attack_id in [
                                a.id for a in self.active_attacks
                        ]:
                            continue
                        try:
                            # Attack first discovered: save attack info
                            arrivalTime = tr.find(
                                'td', 'arrivalTime').text.split(' ')[0]
                            coordsOrigin = tr.find('td', 'coordsOrigin')
                            if coordsOrigin:
                                if coordsOrigin.find('a'):
                                    coordsOrigin = coordsOrigin.find(
                                        'a').text.strip()[1:-1]
                            destCoords = tr.find('td', 'destCoords')
                            if destCoords:
                                destCoords = destCoords.find(
                                    'a').text.strip()[1:-1]
                            originFleet = tr.find('td', 'originFleet')
                            detailsFleet = int(
                                tr.find('td',
                                        'detailsFleet').span.text.replace(
                                            '.', ''))
                            player_info = originFleet.find('a')
                            message_url = player_info.get('href')
                            player = player_info.get('data-player-name')
                            is_moon = False  # TODO!
                            planet = self.find_planet(coords=destCoords,
                                                      is_moon=is_moon)
                            a = Attack(planet, attack_id, arrivalTime,
                                       coordsOrigin, destCoords, detailsFleet,
                                       player, message_url)

                            self.active_attacks.append(a)
                        except Exception as e:
                            self.logger.exception(e)
                            self.send_sms('ATTACKEROR')
                if not hostile:
                    self.active_attacks = []
            except Exception as e:
                self.logger.exception(e)

    def fleet_save(self, p):
        if not p.has_ships():
            return
        fleet = p.ships
        #recyclers are staying!
        #fleet['rc'] = 0
        self.logger.info('Making fleet save from %s' % p)
        self.send_fleet(p,
                        self.get_safe_planet(p).coords,
                        fleet=fleet,
                        mission='station',
                        speed=10,
                        resources={
                            'metal': p.resources['metal'] + 500,
                            'crystal': p.resources['crystal'] + 500,
                            'deuterium': p.resources['deuterium'] + 500
                        })

    def collect_debris(self, p):
        if not p.has_ships():
            return
        self.logger.info('Collecting debris from %s using %s recyclers' %
                         (p, p.ships['rc']))
        self.send_fleet(p,
                        p.coords,
                        fleet={'rc': p.ships['rc']},
                        mission='collect',
                        target='debris')

    def send_expedition(self):
        expedition = options['expedition']
        planets = expedition['planets'].split(' ')
        random.shuffle(planets)
        for coords in planets[:3]:
            planet = self.find_planet(coords=coords)
            if planet:
                galaxy, system, position = planet.coords.split(':')
                expedition_coords = '%s:%s:16' % (galaxy, system)
                self.send_fleet(planet,
                                expedition_coords,
                                fleet={
                                    expedition['ships_kind']:
                                    expedition['ships_number']
                                },
                                mission='expedition')

    def farm(self):
        farms = options['farming']['farms'].split(' ')
        ships_kind = options['farming']['ships_kind']
        ships_number = options['farming']['ships_number']

        l = len(farms)
        if l == 0 or not farms[0]:
            return
        farm = farms[self.farm_no % l]
        if not self.get_player_status(farm)['inactive']:
            self.farm_no += 1
            self.logger.error('farm %s seems not to be inactive!', farm)
            return
        self.send_fleet(self.get_closest_planet(farm),
                        farm,
                        fleet={ships_kind: ships_number})

    def sleep(self):
        sleep_options = options['general']
        sleep_time = randint(0, int(sleep_options['seed'])) + int(
            sleep_options['check_interval'])
        self.logger.info('Sleeping for %s secs' % sleep_time)
        if self.active_attacks:
            sleep_time = 60
        time.sleep(sleep_time)

    def stop(self):
        self.logger.info('Stopping bot')
        os.unlink(self.pidfile)

    def start(self):
        self.logger.info('Starting bot')
        self.pid = str(os.getpid())
        self.pidfile = 'bot.pid'
        file(self.pidfile, 'w').write(self.pid)

        #main loop
        while True:
            if self.login():
                try:
                    self.handle_planets()
                    #self.find_inactives()
                    if not self.active_attacks:
                        if True or not self.transport_resources():
                            self.send_expedition()
                            self.farm()
                            self.farm()
                    else:
                        self.handle_attacks()

                except Exception as e:
                    self.logger.exception(e)
                    #self.stop()
                    #return
            else:
                self.logger.error('Login failed!')
                #self.stop()
                #return
            self.sleep()
示例#43
0
import sys
from sim import Sim
from robot import BipedRobot, QuadrupedRobot
from controller import *

if len(sys.argv) != 2:
    print 'Usage:', sys.argv[0],'server_port'
    sys.exit(0)

# Create a simulation
s = Sim()

# Create a robot above the ground
# obs.: changing last argument to False
#       allows it to fall
r = BipedRobot(s, (0.0, 0.0, 0.0), False)

# Create a controller
c = UDPCtrlServer(s, r, sys.argv[1])

# Start the simulation
s.start()

示例#44
0
            # toggle variables
            self._holding = False
            self._last_buy_price = None
            self._last_buy_time = None

    def pnl(self):
        return sum([(x.sell_price - x.buy_price) * x.amount
                    for x in self._txs])

    def transactions(self):
        return self._txs


if __name__ == '__main__':
    fname = './data/eth_usdt_med.csv'
    sim = Sim(fname)
    sim_iters = 50000
    buy_len = 3
    sell_len = 2
    limit = 0.01  # amount to buy
    trader = SimpleTrader(buy_len, sell_len, limit)
    done = False

    for i in range(sim_iters):
        exchange_data, done = sim.step()

        if done:
            break

        trader.act(exchange_data)
示例#45
0
    fSeed = int(metaParams['favouriteSeed'])

    if metaParams['multiprocessing'] == False or parametersFromFiles == False:

        for r in range(numRepeats):
            # Create Run folders
            folderRun = folder + '/Rep_' + str(r)
            if not os.path.exists(folderRun):
                os.makedirs(folderRun)
            # Set seed
            seed = fSeed
            if r > 0:
                seed = int(time.time() / (r + 1))
            for i in range(len(scenariosParams)):
                n = OrderedDict(scenariosParams[i])
                s = Sim(i, n, folderRun)
                for j in range(len(policiesParams[i])):
                    p = OrderedDict(policiesParams[i][j])
                    s.run(j, p, seed)  # Add policy paramters later

    else:
        processors = int(metaParams['numberProcessors'])
        if processors > multiprocessing.cpu_count():
            processors = multiprocessing.cpu_count()

        pool = multiprocessing.Pool(processors)
        # Create a list of dictionaries (number repetitions times number of scenarios), adding repeat index for folders' creation
        params = multiprocessParams(scenariosParams, policiesParams,
                                    metaParams['numRepeats'], fSeed, folder, 0)
        pool.map(multiprocessingSim, params)
        pool.close()
示例#46
0
 def receive_data(self,data):
     Sim.trace('AppHandler',"application got %d bytes" % (len(data)))
     self.f.write(data)
     self.f.flush()
示例#47
0
    p['popY'] = 50
    p['pixelsInPopPyramid'] = 2000
    p['careLevelColour'] = ['blue', 'green', 'yellow', 'orange', 'red']
    p['houseSizeColour'] = ['brown', 'purple', 'yellow']
    p['pixelsPerTown'] = 56
    p['maxTextUpdateList'] = 22

    return p


p = init_params()

#######################################################
## A basic single run

s = Sim(p)
tax = s.run()

##runs for sensitivity analysis using GEM-SA
##taxMeans = []
##taxSEs = []
##
##p['verboseDebugging'] = False
##p['singleRunGraphs'] = False
##p['interactiveGraphics'] = False
##
##dataFile = open('GEMSA data new.txt','a')
##meansFile = open('GEMSA means new.txt', 'a')
##
##agingParentList = [ 0.0, 0.1, 0.2, 0.4 ]
##careProbList = [ 0.0004, 0.0008, 0.0012, 0.0016 ]
示例#48
0
    def __init__(self, numRuns, output):
        # desired statistics
        antsPerRun = 0
        successfulRuns = 0
        failedRuns = 0
        maxHeightAchieved = 0
        heightPerRun = 0

        success = True

        if output is not None:
            outfile = open(output, "w")
            G.outfile = outfile

        for i in range(numRuns):
            if G.verbose:
                print >> G.outfile, "RUNNING BATCH " + str(i + 1)
            print "RUNNING BATCH " + str(i + 1)
            try:
                self.sim = Sim()
                while G.running:
                    if not self.sim.step():
                        break
                G.running = True

            except Error as e:
                if G.verbose:
                    print e
                success = None
            except Success as s:
                if G.verbose:
                    print s

            # accumulate statistics
            heightPerRun += self.sim.maxHeight + 1
            if self.sim.maxHeight > maxHeightAchieved:
                maxHeightAchieved = self.sim.maxHeight
            if success:
                antsPerRun += self.sim.numAnts
                successfulRuns += 1
            else:
                failedRuns += 1
                success = True

        #sanity check
        if numRuns != successfulRuns + failedRuns:
            raise WeirdError("Runs weren't counted right... weird!")

        # summarize statistics
        statsString = "Ran a batch of " + str(numRuns) \
         + " simulations. "
        if successfulRuns != 0:
            statsString += "\n Average Ants To Complete a Bridge: " \
            + str(float(antsPerRun) / float(successfulRuns))
        statsString += "\n Percentage of Successful Runs: " \
         + str(float(successfulRuns) * 100.0 / float(numRuns)) \
         + "%" \
         + "\n Average Height of Bridges Built: " \
         + str(float(heightPerRun) / float(numRuns)) \
         + "\n Maximum Height of Bridges Built: " \
         + str(maxHeightAchieved + 1)
        print >> G.outfile, statsString
示例#49
0
class FrontEnd(object):
    def __init__(self):
        self.sim = Sim()

        pygame.init()
        pygame.font.init()
        self.screen = pygame.display.set_mode((G.screenWidth, G.screenHeight))
        pygame.display.set_caption("AntBridgeSim")
        self.clock = pygame.time.Clock()
        self.setupButtons()
        self.drawGrid()
        for button in self.buttons:
            button.draw(self.screen)

        oldpos = self.sim.ant.pos
        oldId = self.sim.antId

        dead = None
        while G.running:
            time.sleep(G.sleep)
            self.eventHandler()
            self.drawBlock(self.sim.ant.pos)
            self.drawJoint(self.sim.ant.pos)
            self.drawBlock(oldpos)
            self.drawJoint(oldpos)
            oldpos = self.sim.ant.pos
            if self.sim.antId != oldId:
                oldId = self.sim.antId
                self.drawGrid()
            self.drawJoints()  #TODO incrementally draw
            pygame.display.flip()

            if not dead:
                try:
                    if not self.sim.step():
                        break
                except Error as e:
                    print e
                    dead = True
                except Success as e:
                    print e
                    dead = True
            else:
                paused = True
                while paused:
                    for event in pygame.event.get():
                        if event.type == pygame.QUIT:
                            paused = False
                            G.running = False

    def eventHandler(self):
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                G.running = False
            for button in self.buttons:
                button.checkPressed(event)

    def drawGrid(self):
        for x in range(G.numBlocksX):
            for y in range(G.numBlocksY):
                self.drawBlock((x, y))

        for x in range(G.numBlocksX + 1):
            pygame.draw.line(
                self.screen, G.lineColor, (x * G.blockWidth, 0),
                (x * G.blockWidth, G.screenHeight - G.buttonPanelHeight),
                G.lineWidth)

        for y in range(G.numBlocksY + 1):
            pygame.draw.line(self.screen, G.lineColor, (0, y * G.blockHeight),
                             (G.screenWidth, y * G.blockHeight), G.lineWidth)

    def setupButtons(self):
        self.buttons = []

        buttonTexts = []
        actions = []

        # Button for speeding up animation
        buttonTexts.append("Speed up!")

        def action(button):
            G.sleep = G.sleep / 2.0
            print "Speed up! New speed %f" % (G.sleep)

        actions.append(action)

        # Button for slowing down animation
        buttonTexts.append("Slow down.")

        def action(button):
            G.sleep = G.sleep * 2.0
            print "Slow down. New speed %f" % (G.sleep)

        actions.append(action)

        # Button for pausing animation
        buttonTexts.append("Pause")

        def action(button):
            button.text = "Resume"
            button.draw(self.screen)
            pygame.display.flip()
            paused = True
            while paused:
                for event in pygame.event.get():
                    if event.type == MOUSEBUTTONDOWN:
                        paused = False
                    elif event.type == pygame.QUIT:
                        paused = False
                        G.running = False
            button.text = "Pause"
            button.draw(self.screen)
            pygame.display.flip()

        actions.append(action)

        # This sets up all the buttons based on the above definitions
        # To add a new button, just append the appropriate text and action above.
        # When adding buttons, you don't need to change the code below.
        numButtons = float(len(actions))
        for i, (text, action) in enumerate(zip(buttonTexts, actions)):
            button = Button(text, i * G.screenWidth / numButtons,
                            G.screenHeight - G.buttonPanelHeight,
                            G.screenWidth / numButtons, G.buttonPanelHeight,
                            action)
            self.buttons.append(button)

    def drawBlock(self, (x, y)):
        if self.sim.ant.pos == (x, y):
            color = G.searchColor
        elif G.state[(x, y)] == G.SHAKING:
            color = G.shakeColor
        elif G.state[(x, y)] == G.NORMAL:
            color = G.fillColor
        elif G.state[(x, y)] == G.DEAD:
            color = G.deadColor
        else:
            color = G.emptyColor
        rect = pygame.draw.rect(
            self.screen, color,
            (x * G.blockWidth + G.lineWidth, y * G.blockHeight + G.lineWidth,
             G.blockWidth - G.lineWidth, G.blockHeight - G.lineWidth), 0)
示例#50
0
    def __init__(self,transport,source_address,source_port,destination_address,destination_port,app=None):
        Connection.__init__(self,transport,source_address,source_port, destination_address,destination_port,app)

        ### RTO Timer Properties
        self.rtt_initialized = False
        self.rto = None
        self.srtt = None
        self.rttvar = None
        self.K = 4
        self.initialize_timer()

        # RTT Cap Seconds
        self.max_rtt = 60
        self.min_rtt = 1

        self.alpha = 0.125
        self.beta = 0.25

        ### Sender functionality

        self.transmission_finished = False

        # send buffer
        self.send_buffer = SendBuffer()
        # maximum segment size, in bytes
        self.mss = 1000
        # send window; represents the total number of bytes that may
        # be outstanding at one time
        self.window = self.mss
        # largest sequence number that has been ACKed so far; represents
        # the next sequence number the client expects to receive
        self.sequence = 0
        # retransmission timer
        self.timer = None
        # timeout duration in seconds
        self.timeout = 1
        # is_retransmitting prevents more duplicate ACKs from triggering another send.
        self.is_retransmitting = False

        self.force_drop = True

        ### Congestion Control

        self.restarting_slow_start = False
        self.threshold = 16000
        self.additive_increase_total = 0

        # Fast Retransmit ACKs
        self.retransmit_acks = [-1] * 3

        ### Receiver functionality

        # receive buffer
        self.receive_buffer = ReceiveBuffer()
        # ack number to send; represents the largest in-order sequence
        # number not yet received
        self.ack = 0

        ### Testing
        self.is_aiad = False

        ### FILE WRITING
        self.write_to_disk = True
        self.plot_port_number = 2

        self.plot_sequence_on = False
        self.plot_rate_on = True
        self.plot_queue_on = False
        self.plot_window_on = False

        file_name = "output.txt"
        header_message = "## header message ##"

        if self.plot_sequence_on:
            file_name = "sequence_plot.txt"
            header_message = "# Time (seconds) Sequence (number) Dropped (0 or 1) ACK (0 or 1)"
            Sim.set_debug("Link")
        elif self.plot_rate_on:
            file_name = "rate_plot.txt"
            header_message = "# Time (seconds) Size (number)"
        elif self.plot_queue_on:
            file_name = "queue_plot.txt"
            header_message = "# Time (seconds) Queue Size (bytes)"
            Sim.set_debug("Queue")
        elif self.plot_window_on:
            file_name = "window_plot.txt"
            header_message = "# Time (seconds) Congestion Window Size (bytes)"

        if self.write_to_disk:
            file_title,file_extension = file_name.split('.')
            new_file_name = file_title + str(self.plot_port_number) + '.' + file_extension
            self.trace("PRINTING TO: %s" % new_file_name)
            sys.stdout = open(new_file_name, 'w')
            print header_message
示例#51
0
文件: node.py 项目: T3Fei/bene
 def trace(self,message):
     Sim.trace("Node",message)
示例#52
0
class Bot(object):

    BASE_URL = 'http://pl.ogame.gameforge.com/'
    LOGIN_URL = 'http://pl.ogame.gameforge.com/main/login'
    HEADERS = [('User-agent', 'Mozilla/5.0 (Windows NT 6.2; WOW64)\
     AppleWebKit/537.15 (KHTML, like Gecko) Chrome/24.0.1295.0 Safari/537.15')]
    RE_BUILD_REQUEST = re.compile(r"sendBuildRequest\(\'(.*)\', null, 1\)")
    RE_SERVER_TIME = re.compile(r"var serverTime=new Date\((.*)\);var localTime")

    #ship -> ship id on the page
    SHIPS = {
        'lm': '204',
        'hm': '205',
        'cr': '206',
        'ow': '207',
        'pn': '215',
        'bb': '211',
        'ns': '213',
        'gs': '214',
        'lt': '202',
        'dt': '203',
        'cs': '208',
        'rc': '209',
        'ss': '210'
    }

    # mission ids
    MISSIONS = {
        'attack': '1',
        'transport': '3',
        'station': '4',
        'expedition': '15',
        'collect' : '8'
    }

    TARGETS = {
        'planet' : '1',
        'moon' : '3',
        'debris' : '2'
    }

    SPEEDS = {
        100: '10',
        90: '9',
        80: '8',
        70: '7',
        60: '6',
        50: '5',
        40: '4',
        30: '3',
        20: '2',
        10: '1'
    }
    
    def __init__(self, username=None, password=None, uni='69'):
        self.uni = uni
        self.username = username
        self.password = password
        self.logged_in = False
        
        self._prepare_logger()
        self._prepare_browser()
        farms = options['farming']['farms']
        self.farm_no = randint(0, len(farms)-1) if farms else 0
        
        self.MAIN_URL = 'http://s%s-pl.ogame.gameforge.com/game/index.php' % self.uni
        self.PAGES = {
            'main':        self.MAIN_URL + '?page=overview',
            'resources':   self.MAIN_URL + '?page=resources',
            'station':     self.MAIN_URL + '?page=station',
            'research':    self.MAIN_URL + '?page=research',
            'shipyard':    self.MAIN_URL + '?page=shipyard',
            'defense':     self.MAIN_URL + '?page=defense',
            'fleet':       self.MAIN_URL + '?page=fleet1',
            'galaxy':      self.MAIN_URL + '?page=galaxy',
            'galaxyCnt':   self.MAIN_URL + '?page=galaxyContent',
            'events':      self.MAIN_URL + '?page=eventList',
        }
        self.planets = []
        self.moons = []
        self.active_attacks = []

        self.fleet_slots = 0
        self.active_fleets = 0

        self.server_time = self.local_time = datetime.now()
        self.time_diff = 0
        self.emergency_sms_sent = False
        self.transport_manager = TransportManager()
        self.sim = Sim()

    def _get_url(self, page, planet=None):
        url = self.PAGES[page]
        if planet is not None:
            url += '&cp=%s' % planet.id
        return url
        
    def _prepare_logger(self):
        self.logger = logging.getLogger("mechanize")
        fh = RotatingFileHandler('bot.log', maxBytes=100000, backupCount=5)
        sh = logging.StreamHandler()
        fmt = logging.Formatter(fmt='%(asctime)s %(levelname)s %(message)s',
                                datefmt='%m-%d, %H:%M:%S')
        fh.setFormatter(fmt)
        sh.setFormatter(fmt)
        self.logger.setLevel(logging.INFO)
        self.logger.addHandler(fh)
        self.logger.addHandler(sh)
        
    def _prepare_browser(self):
        self.br = mechanize.Browser()
        self.br.set_handle_equiv(True)
        self.br.set_handle_redirect(True)
        self.br.set_handle_referer(True)
        self.br.set_handle_robots(False)
        self.br.addheaders = self.HEADERS
        
    def _parse_build_url(self, js):
        """
        convert: `sendBuildRequest('url', null, 1)`; into: `url`
        """
        return self.RE_BUILD_REQUEST.findall(js)[0]

    def _parse_server_time(self, content):
        return self.RE_SERVER_TIME.findall(content)[0]
    
    def get_mother(self):
        for p in self.planets:
            if p.mother:
                return p
        return p[0] if self.planets else None

    def get_closest_planet(self, p):
        def min_dist(p, d):
            return d
        _, d, _ = p.split(":")
        return sorted([(planet, planet.get_distance(p)) for planet in self.planets], 
            key=lambda x: x[1])[0][0]


    def find_planet(self, name=None, coords=None, id=None, is_moon=None):
        if is_moon:
            planets = self.moons
        else:
            planets = self.planets
        for p in planets:
            if name == p.name or coords == p.coords or id == p.id:
                return p

    def get_safe_planet(self, planet):
        '''
        Get first planet which is not under attack and isn't `planet`
        '''
        unsafe_planets = [a.planet for a in self.active_attacks]
        for p in self.planets:
            if not p in unsafe_planets and p != planet:
                return p
        # no safe planets! go to mother
        return self.planets[0]

    def login(self, username=None, password=None):
        username = username or self.username
        password = password or self.password
        
        try:
            resp = self.br.open(self.MAIN_URL, timeout=10)
            soup = BeautifulSoup(resp)
        except:
            return False
        
        alert = soup.find(id='attack_alert')

        # no redirect on main page == user logged in
        if resp.geturl() != self.BASE_URL and alert:
            self.logged_in = True
            self.logger.info('Logged as: %s' % username)
            return True
        
        self.logger.info('Logging in..')
        self.br.select_form(name='loginForm')
        self.br.form['uni'] = ['s%s-pl.ogame.gameforge.com' % self.uni]
        self.br.form['login'] = username
        self.br.form['pass'] = password
        self.br.submit()

        if self.br.geturl().startswith(self.MAIN_URL):
            self.logged_in = True
            self.logger.info('Logged as: %s' % username)
            return True
        else:
            self.logged_in = False
            self.logger.error('Login failed!')
            return False

    def calc_time(self, resp):
        try:
            y, mo, d, h, mi, sec = map(int, self._parse_server_time(resp).split(','))
        except:
            self.logger.error('Exception while calculating time')
        else:
            self.local_time = n = datetime.now()
            self.server_time = datetime(n.year, n.month, n.day, h, mi, sec)
            self.time_diff = self.server_time - self.local_time

            self.logger.info('Server time: %s, local time: %s' % \
                (self.server_time, self.local_time))

    def fetch_planets(self):
        self.logger.info('Fetching planets..')
        resp = self.br.open(self.PAGES['main']).read()

        self.calc_time(resp)

        soup = BeautifulSoup(resp)
        self.planets = []
        self.moons = []

        try:
            for i, c in enumerate(soup.findAll('a', 'planetlink')):
                name = c.find('span', 'planet-name').text
                coords = c.find('span', 'planet-koords').text[1:-1]
                url = c.get('href')
                p_id = int(c.parent.get('id').split('-')[1])
                construct_mode = len(c.parent.findAll('a', 'constructionIcon')) != 0
                p = Planet(p_id, name, coords, url, construct_mode)
                if i == 0:
                    p.mother = True
                self.planets.append(p)

                #check if planet has moon
                moon = c.parent.find('a', 'moonlink')
                if moon and 'moonlink' in moon['class']:
                    url = moon.get('href')
                    m_id = url.split('cp=')[1]
                    m = Moon(m_id, coords, url)
                    self.moons.append(m)
        except:
            self.logger.exception('Exception while fetching planets')
        else:
            self.check_attacks(soup)

    def handle_planets(self):
        self.fetch_planets()

        for p in iter(self.planets):
            self.update_planet_info(p)
            self.update_planet_fleet(p)
        for m in iter(self.moons):
            self.update_planet_info(m)
            self.update_planet_fleet(m)

    def update_planet_fleet(self, planet):
        resp = self.br.open(self._get_url('fleet', planet))
        soup = BeautifulSoup(resp)
        ships = {}
        for k, v in self.SHIPS.iteritems():
            available = 0
            try:
                s = soup.find(id='button' + v)
                available = int(s.find('span', 'textlabel').nextSibling.replace('.', ''))
            except:
                available = 0
            ships[k] = available

        #self.logger.info('Updating %s fleet' % planet)
        #self.logger.info('%s' % fleet)
        planet.ships = ships

    def update_planet_info(self, planet):
        in_construction_mode = False
        resp = self.br.open(self._get_url('resources', planet))
        soup = BeautifulSoup(resp)

        try:
            metal = int(soup.find(id='resources_metal').text.replace('.',''))
            planet.resources['metal'] = metal
            crystal = int(soup.find(id='resources_crystal').text.replace('.',''))
            planet.resources['crystal'] = crystal
            deuterium = int(soup.find(id='resources_deuterium').text.replace('.',''))
            planet.resources['deuterium'] = deuterium
            energy = int(soup.find(id='resources_energy').text.replace('.',''))
            planet.resources['energy'] = energy
        except:
            self.logger.exception('Exception while updating resources info')
        else:
            self.logger.info('Updating resources info for %s:' % planet)
            s = 'metal - %(metal)s, crystal - %(crystal)s, deuterium - %(deuterium)s'
            self.logger.info(s % planet.resources)
        if planet.is_moon():
            return
        try:
            buildingList = soup.find(id='building')
            buildings = ('metalMine', 'crystalMine', 'deuteriumMine', 'solarPlant',
                'fusionPlant', 'solarSatellite'
            )
            for building, b in zip(buildings, buildingList.findAll('li')):
                can_build = 'on' in b.get('class')
                fb = b.find('a', 'fastBuild')
                build_url = fb.get('onclick') if fb else ''
                if build_url:
                    build_url = self._parse_build_url(build_url)
                try:
                    level = int(b.find('span', 'textlabel').nextSibling)
                except AttributeError:
                    try:
                        level = int(b.find('span', 'level').text)
                    except:
                        pass
                suff_energy = planet.resources['energy'] - self.sim.upgrade_energy_cost(building, level+1) > 0
                res = dict(
                    level=level, 
                    can_build=can_build,
                    build_url=build_url,
                    sufficient_energy=suff_energy
                )

                planet.buildings[building] = res

            if buildingList.find('div', 'construction'):
                in_construction_mode = True
        except:
            self.logger.exception('Exception while updating buildings info')
            return False
        else:
            self.logger.info('%s buildings were updated' % planet)
        if not in_construction_mode:
            text, url = planet.get_mine_to_upgrade()
            if url:
                self.logger.info('Building upgrade on %s: %s'% (planet, text))
                self.br.open(url)
                planet.in_construction_mode = True
                #let now transport manager to clear building queue
                self.transport_manager.update_building(planet)
        else:
            self.logger.info('Building queue is not empty')
        return True

    def transport_resources(self):
        tasks = self.transport_manager.find_dest_planet(self.planets)
        if tasks is None:
            return False
        self.logger.info(self.transport_manager.get_summary())
        for task in iter(tasks):
            self.logger.info('Transport attempt from: %s, to: %s with resources %s' \
                % (task['from'], task['where'], task['resources']))
            result = self.send_fleet(
                task['from'], 
                task['where'].coords, 
                fleet=task['from'].get_fleet_for_resources(task['resources']),
                resources=task['resources'],
                mission='transport'
            )
            if result:
                self.transport_manager.update_sent_resources(task['resources'])
                self.logger.info('Resources sent: %s, resources needed: %s' \
                    % (task['resources'], self.transport_manager.get_resources_needed()))

        return True

    def build_defense(self, planet):
        """
        Build defense for all resources on the planet
        1. plasma
        2. gauss
        3. heavy cannon
        4. light cannon
        5. rocket launcher
        """
        url = self._get_url('defense', planet)
        resp = self.br.open(url)
        for t in ('406', '404', '403', '402', '401'):
            self.br.select_form(name='form')
            self.br.form.new_control('text','menge',{'value':'100'})
            self.br.form.fixup()
            self.br['menge'] = '100'

            self.br.form.new_control('text','type',{'value':t})
            self.br.form.fixup()
            self.br['type'] = t

            self.br.form.new_control('text','modus',{'value':'1'})
            self.br.form.fixup()
            self.br['modus'] = '1'

            self.br.submit()

    def get_player_status(self, destination, origin_planet=None):
        if not destination:
            return
            
        status = {}
        origin_planet = origin_planet or self.get_closest_planet(destination)
        galaxy, system, position = destination.split(':')

        url = self._get_url('galaxyCnt', origin_planet)
        data = urlencode({'galaxy': galaxy, 'system': system})
        resp = self.br.open(url, data=data)
        soup = BeautifulSoup(resp)

        soup.find(id='galaxytable')
        planets = soup.findAll('tr', {'class': 'row'})
        target_planet = planets[int(position)-1]
        name_el = target_planet.find('td', 'playername')
        status['name'] = name_el.find('span').text

        status['inactive'] = 'inactive' in name_el.get('class', '')
        return status

    def find_inactive_nearby(self, planet, radius=15):

        self.logger.info("Searching idlers near %s in radius %s" 
            % (planet, radius))

        nearby_systems = planet.get_nearby_systems(radius)
        idlers = []

        for system in nearby_systems:
            galaxy, system = system.split(":")
            url = self._get_url('galaxyCnt', planet)
            data = urlencode({'galaxy': galaxy, 'system': system})
            resp = self.br.open(url, data=data)
            soup = BeautifulSoup(resp)

            galaxy_el = soup.find(id='galaxytable')
            planets = galaxy_el.findAll('tr', {'class': 'row'})
            for pl in planets:
                name_el = pl.find('td', 'playername')
                debris_el = pl.find('td', 'debris')
                inactive = 'inactive' in name_el.get('class', '')
                debris_not_found = 'js_no_action' in debris_el.get('class', '')
                if not inactive or not debris_not_found:
                    continue
                position = pl.find('td', 'position').text
                coords = "%s:%s:%s" % (galaxy, system, position)
                player_id = name_el.find('a').get('rel')

                player_info = soup.find(id=player_id)
                rank_el = player_info.find('li', 'rank')

                if not rank_el:
                    continue

                rank = int(rank_el.find('a').text)
                if rank > 4000 or rank < 900:
                    continue

                idlers.append(coords)
                time.sleep(2)

        return idlers

    def find_inactives(self):

        inactives = []
        for p in self.planets:
            try:
                idlers = self.find_inactive_nearby(p)
                self.logger.info(" ".join(idlers))
                inactives.extend(idlers)
            except Exception as e:
                self.logger.exception(e)
                continue
            time.sleep(5)

        self.logger.info(" ".join(inactives))
        self.inactives = list(set(inactives))
        self.logger.info(inactives)

    def send_fleet(self, origin_planet, destination, fleet={}, resources={},
            mission='attack', target='planet', speed=None):
        if origin_planet.coords == destination:
            self.logger.error('Cannot send fleet to the same planet')
            return False
        self.logger.info('Sending fleet from %s to %s (%s)' \
            % (origin_planet, destination, mission))
        resp = self.br.open(self._get_url('fleet', origin_planet))
        try:
            try:
                self.br.select_form(name='shipsChosen')
            except mechanize.FormNotFoundError:
                self.logger.info('No available ships on the planet')
                return False

            soup = BeautifulSoup(resp)
            for ship, num in fleet.iteritems():
                s = soup.find(id='button' + self.SHIPS[ship])
                num = int(num)
                try:
                    available = int(s.find('span', 'textlabel').nextSibling.replace('.', ''))
                except:
                    available = 0
                if available < num and mission in ('attack', 'expedition'):
                    self.logger.info('No available ships to send')
                    return False
                if num > 0:
                    self.br.form['am' + self.SHIPS[ship]] = str(num)

            self.br.submit()

            try:
                self.br.select_form(name='details')
            except mechanize.FormNotFoundError:
                self.logger.info('No available ships on the planet')
                return False

            galaxy, system, position = destination.split(':')
            self.br['galaxy'] = galaxy
            self.br['system'] = system
            self.br['position'] = position
            self.br.form.find_control("type").readonly = False
            self.br['type'] = self.TARGETS[target]
            self.br.form.find_control("speed").readonly = False
            if speed:
                self.br['speed'] = self.SPEEDS[speed]
            self.br.submit()

            self.br.select_form(name='sendForm')
            self.br.form.find_control("mission").readonly = False
            self.br.form['mission'] = self.MISSIONS[mission]
            if 'metal' in resources:
                self.br.form['metal'] = str(resources['metal'])
            if 'crystal' in resources:
                self.br.form['crystal'] = str(resources['crystal'])
            if 'deuterium' in resources:
                self.br.form['deuterium'] = str(resources['deuterium'])
            self.br.submit()
        except Exception as e:
            self.logger.exception(e)
            return False
        else:
            if mission == 'attack':
                self.farm_no += 1
        return True

    def send_message(self, url, player, subject, message):
        self.logger.info('Sending message to %s: %s' % (player, message))
        self.br.open(url)
        self.br.select_form(nr=0)
        self.br.form['betreff'] = subject
        self.br.form['text'] = message
        self.br.submit()

    def send_sms(self, msg):
        from smsapigateway import SMSAPIGateway
        try:
            SMSAPIGateway().send(msg)
        except Exception as e:
            self.logger.exception(str(e))

    def handle_attacks(self):
        attack_opts = options['attack']
        send_sms = bool(options['sms']['send_sms'])

        for a in self.active_attacks:
            if a.is_dangerous():
                self.logger.info('Handling attack: %s' % a)
                if not a.planet.is_moon():
                    self.build_defense(a.planet)
                if send_sms and not a.sms_sent:
                    self.send_sms(a.get_sms_text())
                    a.sms_sent = True
                if send_sms and not a.message_sent:
                    self.send_message(a.message_url, a.player, attack_opts['message_topic'], 
                        a.get_random_message())
                    a.message_sent = True
                self.fleet_save(a.planet)

    def check_attacks(self, soup):
        alert = soup.find(id='attack_alert')
        if not alert:
            self.logger.exception('Check attack failed')
            return
        if 'noAttack' in alert.get('class', ''):
            self.logger.info('No attacks')
            self.active_attacks = []
        else:
            self.logger.info('ATTACK!')
            resp = self.br.open(self.PAGES['events'])
            soup = BeautifulSoup(resp)
            hostile = False
            try:
                for tr in soup.findAll('tr'):
                    countDown = tr.find('td', 'countDown')
                    if countDown and 'hostile' in countDown.get('class', ''):
                        hostile = True
                        # First: check if attack was noticed
                        if tr.get('id'):
                            attack_id = tr.get('id').split('-')[1]
                        elif countDown.get('id'):
                            attack_id = countDown.get('id').split('-')[2]
                        if not attack_id or attack_id in [a.id for a in self.active_attacks]:
                            continue
                        try:
                            # Attack first discovered: save attack info
                            arrivalTime = tr.find('td', 'arrivalTime').text.split(' ')[0]
                            coordsOrigin = tr.find('td', 'coordsOrigin')
                            if coordsOrigin:
                                if coordsOrigin.find('a'):
                                    coordsOrigin = coordsOrigin.find('a').text.strip()[1:-1]
                            destCoords = tr.find('td', 'destCoords')
                            if destCoords:
                                destCoords = destCoords.find('a').text.strip()[1:-1]
                            originFleet = tr.find('td', 'originFleet')
                            detailsFleet = int(tr.find('td', 'detailsFleet').span.text.replace('.', ''))
                            player_info = originFleet.find('a')
                            message_url = player_info.get('href')
                            player = player_info.get('data-player-name')
                            is_moon = False # TODO!
                            planet = self.find_planet(coords=destCoords, is_moon=is_moon)
                            a = Attack(planet, attack_id, arrivalTime, coordsOrigin, 
                                destCoords, detailsFleet, player, message_url)

                            self.active_attacks.append(a)
                        except Exception as e:
                            self.logger.exception(e)
                            self.send_sms('ATTACKEROR')
                if not hostile:
                    self.active_attacks = []
            except Exception as e:
                self.logger.exception(e)
                
    def fleet_save(self, p):
        if not p.has_ships():
            return
        fleet = p.ships
        #recyclers are staying!
        #fleet['rc'] = 0
        self.logger.info('Making fleet save from %s' % p)
        self.send_fleet(p, 
            self.get_safe_planet(p).coords, 
            fleet=fleet, 
            mission='station',
            speed=10,
            resources={'metal': p.resources['metal']+500,
                       'crystal': p.resources['crystal']+500,
                       'deuterium': p.resources['deuterium']+500})

    def collect_debris(self, p):
        if not p.has_ships():
            return
        self.logger.info('Collecting debris from %s using %s recyclers' % (p, p.ships['rc']))
        self.send_fleet(p, 
            p.coords, 
            fleet={'rc':p.ships['rc']},
            mission='collect',
            target='debris')

    def send_expedition(self):
        expedition = options['expedition']
        planets = expedition['planets'].split(' ')
        random.shuffle(planets)
        for coords in planets[:3]:
            planet = self.find_planet(coords=coords)
            if planet:
                galaxy, system, position = planet.coords.split(':')
                expedition_coords = '%s:%s:16' % (galaxy, system)
                self.send_fleet(planet, expedition_coords,
                    fleet={expedition['ships_kind']:expedition['ships_number']}, 
                    mission='expedition')

    def farm(self):
        farms = options['farming']['farms'].split(' ')
        ships_kind = options['farming']['ships_kind']
        ships_number = options['farming']['ships_number']

        l = len(farms)
        if l == 0 or not farms[0]:
            return
        farm = farms[self.farm_no%l]
        if not self.get_player_status(farm)['inactive']:
            self.farm_no += 1
            self.logger.error('farm %s seems not to be inactive!', farm)
            return
        self.send_fleet(
            self.get_closest_planet(farm),
            farm,
            fleet={ships_kind:ships_number}
        )

    def sleep(self):
        sleep_options = options['general']
        sleep_time = randint(0, int(sleep_options['seed']))+int(sleep_options['check_interval'])
        self.logger.info('Sleeping for %s secs' % sleep_time)
        if self.active_attacks:
            sleep_time = 60
        time.sleep(sleep_time)

    def stop(self):
        self.logger.info('Stopping bot')
        os.unlink(self.pidfile)
        
    def start(self):
        self.logger.info('Starting bot')
        self.pid = str(os.getpid())
        self.pidfile = 'bot.pid'
        file(self.pidfile, 'w').write(self.pid)
        
        #main loop
        while True:
            if self.login():
                try:
                    self.handle_planets()
                    #self.find_inactives()
                    if not self.active_attacks:
                        if True or not self.transport_resources():
                            self.send_expedition()
                            self.farm()
                            self.farm()
                    else:
                        self.handle_attacks()

                except Exception as e:
                    self.logger.exception(e)
                    #self.stop()
                    #return
            else:
                self.logger.error('Login failed!')
                #self.stop()
                #return
            self.sleep()
示例#53
0
        hash_cols = ('FT_hash','FC_hash','MT_hash','MC_hash')
        all_cols = (max_cols,quad_cols,hash_cols)
        names = ('max','quad','hash')
        for cols,name in zip(all_cols,names):
            R_min_data = find_R_mins(self.R_data,cols)
            the_min_idx = R_min_data['Lowest R'].idxmin()
            minimum_R[name] = tuple(
                R_min_data[['Lowest R','Mode']].iloc[the_min_idx])

        return minimum_R

if __name__ == '__main__':
    import numpy

    #Create a Simulation object composed of a laminate.
    sim = Sim(laminate = Laminate('0_2/p25/0_2s',
                               materialID = 5, #5
                               core_thick = 0.01))
    #Define and apply load
    P = Q_(1000,'N'); b = Q_(0.11,'m'); L = Q_(0.51,'m')
    M1 = -P*L/(4*b); N1 = 0.5*P/b;
    M = Q_([M1.magnitude,0,0],M1.units)
    N = Q_([N1.magnitude,0,0],N1.units)
    #Apply load
    sim.apply_M(M)
    sim.apply_N(N)
    sim.solve()
    fail = FailureAnalysis(sim)
    R_data = fail.make_table()
# print M
# print N
示例#54
0
        names = ('max', 'quad', 'hash')
        for cols, name in zip(all_cols, names):
            R_min_data = find_R_mins(self.R_data, cols)
            the_min_idx = R_min_data['Lowest R'].idxmin()
            minimum_R[name] = tuple(R_min_data[['Lowest R',
                                                'Mode']].iloc[the_min_idx])

        return minimum_R


if __name__ == '__main__':
    import numpy

    #Create a Simulation object composed of a laminate.
    sim = Sim(laminate=Laminate(
        '0_2/p25/0_2s',
        materialID=5,  #5
        core_thick=0.01))
    #Define and apply load
    P = Q_(1000, 'N')
    b = Q_(0.11, 'm')
    L = Q_(0.51, 'm')
    M1 = -P * L / (4 * b)
    N1 = 0.5 * P / b
    M = Q_([M1.magnitude, 0, 0], M1.units)
    N = Q_([N1.magnitude, 0, 0], N1.units)
    #Apply load
    sim.apply_M(M)
    sim.apply_N(N)
    sim.solve()
    fail = FailureAnalysis(sim)
    R_data = fail.make_table()
示例#55
0
 def forward_broadcast_packet(self,packet):
     for link in self.links:
         Sim.trace("%s forwarding broadcast packet to %s" % (self.hostname,link.endpoint.hostname))
         packet_copy = copy.deepcopy(packet)
         link.send_packet(packet_copy)
示例#56
0
    def __init__(self,transport,source_address,source_port,
                 destination_address,destination_port,app=None,window=1000):
        Connection.__init__(self,transport,source_address,source_port,
                            destination_address,destination_port,app)

        ### Sender functionality
        self.totalQueueingDelay = 0.0
        self.totalPacketsSent = 0
        self.port = source_port
        self.dynamic = True
        self.output = False
        self.proveTimer = False
        self.proveCong = False
        self.stand_trace = False
        self.seq_plot = False
        self.graph1 = True
        self.graph2 = False   # == This is controlled in the transfer file by Sim.set_debug('Link')
        self.graph3 = False
        self.graph4 = False

        if self.graph2:
            Sim.set_debug('Link')
        # send window; represents the total number of bytes that may
        # be outstanding at one time
        # maximum segment size, in bytes
        self.mss = 1000
        # Step 2
        self.window = self.mss  ######################### This one gets the mess adjusted out of it
        # threshold for slow start
        self.thresh = 100000
        self.inc_sum = 0
        # for fast retransmit
        self.last_ack = 0
        self.dup_counter = 0
        self.drop_next = False
        self.has_dropped = False
        self.dropped_count = 0
        # send buffer
        self.send_buffer = SendBuffer()
        ############################################# (this never gets adjusted)
        #self.mss = min(1000, window) ################ TODO: handle the case where window is bigger than mss
        # largest sequence number that has been ACKed so far; represents
        # the next sequence number the client expects to receive
        self.sequence = 0
        # retransmission timer
        self.timer = None
        # timeout duration in seconds
        self.timeout = 1.0
        if self.dynamic:
            self.timeout = 3.0
        # estimated rtt
        self.est_rtt = None
        # alpha
        self.alpha = 0.125
        # variation rtt
        self.var_rtt = None  ## TODO: revisit this later
        # beta
        self.beta = 0.25
        ### Receiver functionality

        # receive buffer
        self.receive_buffer = ReceiveBuffer()
        # ack number to send; represents the largest in-order sequence
        # number not yet received
        self.ack = 0
示例#57
0
 def trace(self,message):
     ''' Print debugging messages. '''
     Sim.trace("TCP",message)
示例#58
0
文件: bot.py 项目: mvpossum/ogame-bot
class Bot(object):

    BASE_URL = 'http://labdcc.fceia.unr.edu.ar/~jgalat/'
    LOGIN_URL = 'http://labdcc.fceia.unr.edu.ar/~jgalat/'
    HEADERS = [('User-agent', 'Mozilla/5.0 (Windows NT 6.2; WOW64)\
     AppleWebKit/537.15 (KHTML, like Gecko) Chrome/24.0.1295.0 Safari/537.15')]
    RE_BUILD_REQUEST = re.compile(r"sendBuildRequest\(\'(.*)\', null, 1\)")
    RE_SERVER_TIME = re.compile(r"<th>Server time </th>\s*<th.*>(.*)</th>")
    RE_RESOURCE = re.compile(r"(?:<font >)?(\d+)(?:/\d*)?(?:</font>)?")
    RE_BUILD_LVL = re.compile(r"(?: \(level (\d*)\))|<br />")
    RE_IN_CONSTRUCTION = re.compile(r"\d+.: (.*) (\d+)")
    RE_SHIP_COST = re.compile(
        r"(\w*): <b style=\"color:\w*;\">(?: <t title=\"-(?:\d|\.)+\"><span class=\"noresources\">)?((?:\d|\.)+)")

    # ship -> ship id on the page
    SHIPS = {
        'lm': '204',
        'hm': '205',
        'cr': '206',
        'ow': '207',
        'pn': '215',
        'bb': '211',
        'ns': '213',
        'gs': '214',
        'lt': '202',
        'dt': '203',
        'cs': '208',
        'rc': '209',
        'ss': '210'
    }

    # mission ids
    MISSIONS = {
        'Attack': '1',
        'Transport': '3',
        'Hold Position': '5',
        'Expedition': '15',
        'Collect': '8'
    }

    TARGETS = {
        'Planet': '1',
        'Moon': '3',
        'Debris': '2'
    }

    def __init__(self, username, password, server):
        self.username = username
        self.password = password
        self.logged_in = False

        self._prepare_logger()
        self._prepare_browser()
        farms = options['farming']['farms']
        self.farm_no = randint(0, len(farms)-1) if farms else 0

        self.MAIN_URL = 'http://labdcc.fceia.unr.edu.ar/~jgalat/game.php'
        server=server.replace('http://','')
        if server[-1]=='/':
            server=server[:-1]
        self.MAIN_URL = 'http://'+server+'/game.php'
        self.PAGES = {
            'main':        self.MAIN_URL + '?page=overview',
            'buildings':   self.MAIN_URL + '?page=buildings',
            'station':     self.MAIN_URL + '?page=station',
            'research':    self.MAIN_URL + '?page=buildings&mode=research',
            'shipyard':    self.MAIN_URL + '?page=buildings&mode=fleet',
            'defense':     self.MAIN_URL + '?page=defense',
            'fleet':       self.MAIN_URL + '?page=fleet',
            'galaxy':      self.MAIN_URL + '?page=galaxy',
            'galaxyCnt':   self.MAIN_URL + '?page=galaxyContent',
            'events':      self.MAIN_URL + '?page=eventList',
        }
        self.planets = []
        self.moons = []
        self.active_attacks = []

        self.fleet_slots = 0
        self.active_fleets = 0

        self.server_time = self.local_time = datetime.now()
        self.time_diff = 0
        self.emergency_sms_sent = False
        self.transport_manager = TransportManager()
        self.sim = Sim()

    def _get_url(self, page, planet=None):
        url = self.PAGES[page]
        if planet is not None:
            url += '&cp=%s' % planet.id
        return url

    def _prepare_logger(self):
        self.logger = logging.getLogger("mechanize")
        fh = RotatingFileHandler('bot.log', maxBytes=100000, backupCount=5)
        sh = logging.StreamHandler()
        fmt = logging.Formatter(fmt='%(asctime)s %(levelname)s %(message)s',
                                datefmt='%m-%d, %H:%M:%S')
        fh.setFormatter(fmt)
        sh.setFormatter(fmt)
        self.logger.setLevel(logging.INFO)
        self.logger.addHandler(fh)
        self.logger.addHandler(sh)
        self.logger.propagate = False

    def _prepare_browser(self):
        self.br = mechanize.Browser()
        self.br.set_handle_equiv(True)
        self.br.set_handle_redirect(True)
        self.br.set_handle_referer(True)
        self.br.set_handle_robots(False)
        self.br.addheaders = self.HEADERS

    def _parse_build_url(self, js):
        """
        convert: `sendBuildRequest('url', null, 1)`; into: `url`
        """
        return self.RE_BUILD_REQUEST.findall(js)[0]

    def _parse_server_time(self, content):
        return self.RE_SERVER_TIME.findall(content)[0]

    def get_mother(self):
        for p in self.planets:
            if p.mother:
                return p
        return p[0] if self.planets else None

    def get_closest_planet(self, p):
        def min_dist(p, d):
            return d
        _, d, _ = p.split(":")
        return sorted([(planet, planet.get_distance(p))
                      for planet in self.planets], key=lambda x: x[1])[0][0]

    def find_planet(self, name=None, coords=None, id=None, is_moon=None):
        if is_moon:
            planets = self.moons
        else:
            planets = self.planets
        for p in planets:
            if name == p.name or coords == p.coords or id == p.id:
                return p

    def get_safe_planet(self, planet):
        '''
        Get first planet which is not under attack and isn't `planet`
        '''
        unsafe_planets = [a.planet for a in self.active_attacks]
        for p in self.planets:
            if not p in unsafe_planets and p != planet:
                return p
        # no safe planets! go to mother
        return self.planets[0]

    def login(self, username=None, password=None):
        username = username or self.username
        password = password or self.password

        try:
            resp = self.br.open(self.MAIN_URL, timeout=10)
            soup = BeautifulSoup(resp)
        except:
            return False

        # no redirect on main page == user logged in
        if resp.geturl() == self.MAIN_URL:
            self.logged_in = True
            self.logger.info('Logged as: %s' % username)
            return True

        self.logger.info('Logging in..')
        self.br.select_form(nr=0)
        self.br.form['username'] = username
        self.br.form['password'] = password
        self.br.submit()
        if self.br.geturl().startswith(self.MAIN_URL):
            self.logged_in = True
            self.logger.info('Logged as: %s' % username)
            return True
        else:
            self.logged_in = False
            self.logger.error('Login failed!')
            return False

    def calc_time(self, resp):

        try:
            self.server_time = datetime.strptime(
                str(date.today().year) + ' ' + self._parse_server_time(resp),
                "%Y %a %b %d %H:%M:%S")
        except:
            self.logger.error('Exception while calculating time')
        else:
            self.local_time = n = datetime.now()
            self.time_diff = self.server_time - self.local_time

            self.logger.info('Server time: %s, local time: %s' %
                             (self.server_time, self.local_time))

    def fetch_planets(self):
        self.logger.info('Fetching planets..')
        resp = self.br.open(self.PAGES['main']).read()
        self.calc_time(resp)
        soup = BeautifulSoup(resp)
        self.planets = []
        self.moons = []
        try:
            for i, c in enumerate(soup.find('select').findAll('option')):
                url = c['value']
                name, coords = c.contents[0].split('&nbsp;')[0:2]
                p = Planet('1', name, coords[1:-1], url, False)
                if i == 0:
                    p.mother = True
                self.planets.append(p)
                     #p_id = int(c.parent.get('id').split('-')[1])
                     # construct_mode = len(
                     #    c.parent.findAll(
                     #        'a',
                     #        'constructionIcon')) != 0

                # check if planet has moon
               # moon = c.parent.find('a', 'moonlink')
               # if moon and 'moonlink' in moon['class']:
               #     url = moon.get('href')
               #     m_id = url.split('cp=')[1]
               #     m = Moon(m_id, coords, url)
               #     self.moons.append(m)
        except:
            self.logger.exception('Exception while fetching planets')
        # else:
        #    self.check_attacks(soup)

    def handle_planets(self):
        for p in iter(self.planets):
            self.upgrade_planet(p)
        self.farm()

    def load_planets(self):
        self.fetch_planets()
        for p in iter(self.planets):
            self.update_planet_shipyard(p)
            self.update_planet_info(p)
            self.update_planet_research(p)
            self.update_planet_fleet(p)
        for m in iter(self.moons):
            self.update_planet_info(m)
            self.update_planet_fleet(m)

    def upgrade_planet(self, planet):
        upds = planet.get_upgrades()
        if 'buildings' in upds:
            for name in upds['buildings']:
                self.logger.info('Building upgrade on %s: %s' % (planet, name))
                self.br.open(planet.buildings[name]['link'])
                # let now transport manager to clear building queue
                # self.transport_manager.update_building(planet)
        if 'researches' in upds:
            for name in upds['researches']:
                self.logger.info('Building research on %s: %s' % (planet, name))
                self.br.open(planet.researches[name]['link'])

        if 'fleets' in upds:
            resp = self.br.open(self._get_url('shipyard', planet))
            soup = BeautifulSoup(resp)
            planet.ships = {}
            formdata = {}
            for c in soup.findAll('td', {'class': 'l'}):
                name = c.find('a').contents[0]
                intxt = c.findNext('th')
                if not intxt:
                    continue
                intxt = intxt.find('input')
                if not intxt:
                    continue
                if name in upds['fleets']:
                    formdata[intxt['name']] = str(upds['fleets'][name])
                    self.logger.info(
                        'Building %d %s on %s' %
                        (upds['fleets'][name], name, planet))
                else:
                    formdata[intxt['name']] = "0"
            self.br.select_form(nr=0)
            for name, value in formdata.iteritems():
                self.br.form[name] = value
            self.br.submit()

        if not upds:
            self.logger.info('Nothing to upgrade or build on %s' % planet)
        return True

    def update_planet_fleet(self, planet):
        planet.ships = {}
        try:
            resp = self.br.open(self._get_url('fleet', planet))
            soup = BeautifulSoup(resp)
            for c in soup.find('form', {'action': 'game.php?page=fleet1'}).findAll('tr')[2:-2]:
                name = c.find('a').contents[0].strip()
                cant = int(c.find('th').findNext('th').contents[0].strip())
                planet.ships[name] = cant
        except:
            self.logger.exception('Exception while updating fleets info')
        s = ', '.join(["%s: %d" % (n, c) for n, c in planet.ships.iteritems()])
        self.logger.info('Ships on %s-> %s' % (planet, s))
        return True

    def update_planet_shipyard(self, planet):
        resp = self.br.open(self._get_url('shipyard', planet))
        soup = BeautifulSoup(resp)
        planet.buyable_fleets = {}
        for c in soup.findAll('td', {'class': 'l'}):
            name = c.find('a').contents[0]
            intxt = c.findNext('th')
            if not intxt:
                continue
            res = self.RE_SHIP_COST.findall("".join(map(str, c.contents)))
            res = {x: int(y.replace('.', '')) for x, y in res}
            planet.buyable_fleets[name] = res
        return True

    def update_planet_info(self, planet):
        resp = self.br.open(self._get_url('buildings', planet))
        soup = BeautifulSoup(resp)
        names = ['Metal', 'Crystal', 'Deuterium', 'Energy']
        try:
            for name, c in zip(names, soup.find(id='resources').findAll(width='90')):
                matched = self.RE_RESOURCE.findall(
                    str(c.contents[0]).replace('.', ''))[0]
                planet.resources[name] = int(matched)
        except:
            self.logger.exception('Exception while updating resources info')
        else:
            self.logger.info('Resources in %s:' % planet)
            s = 'Metal - %(Metal)s, Crystal - %(Crystal)s, Deuterium - %(Deuterium)s'
            self.logger.info(s % planet.resources)

        if planet.is_moon():
            return
        try:
            planet.buildings = {}
            for c in soup.find('table', {'width': '530'}).findAll('td', {'class': 'l'}):
                if len(c.attrs) == 1:
                    childs = c.findChildren()
                    if len(childs) > 2:
                        name = c.find('a').contents[0]
                        exp = self.RE_BUILD_LVL.findall(str(c.contents[2]))
                        if not exp or not exp[0]:
                            lvl = 0
                        else:
                            lvl = int(exp[0])
                        suff_energy = planet.resources[
                            'energy'] - self.sim.upgrade_energy_cost(name, lvl+1) > 0
                        canbuild = c.find('b', {'style': "color:red;"}) is None
                        c2 = c.findNext('td').find('a')
                        if c2 and canbuild:
                            link = c2['href']
                        else:
                            link = None
                        planet.buildings[name] = {
                            'link': link,
                            'level': lvl,
                            'sufficient_energy': suff_energy,
                            'in_construction': False}
            for c in soup.find('table', {'width': '530'}).findAll('td', {'class': 'l'}):
                if len(c.attrs) > 1:
                    name, lvl = self.RE_IN_CONSTRUCTION.findall(
                        str(c.contents[0]))[0]
                    planet.buildings[name]['link']=None
                    planet.buildings[name]['level'] = max(
                        planet.buildings[name]['level'],
                        int(lvl))
                    planet.buildings[name]['in_construction'] = True
        except:
            self.logger.exception('Exception while reloading buildings info')
            return False
        else:
            self.logger.info('%s buildings were reloaded' % planet)
        return True

    def update_planet_research(self, planet):
        resp = self.br.open(self._get_url('research', planet))
        soup = BeautifulSoup(resp)
        if planet.is_moon():
            return
        try:
            planet.researches = {}
            tabla=soup.find('table', {'width': '530'})
            if tabla is None:
                return True
            for c in tabla.findAll('td', {'class': 'l'}):
                if len(c.attrs) == 1:
                    childs = c.findChildren()
                    if len(childs) > 2:
                        name = c.find('a').contents[0]
                        exp = self.RE_BUILD_LVL.findall(str(c.contents[2]))
                        if not exp or not exp[0]:
                            lvl = 0
                        else:
                            lvl = int(exp[0])
                        canbuild = c.find('b', {'style': "color:red;"}) is None
                        c2 = c.findNext('th', {'class': 'l'}).find('a')
                        if c2 and canbuild:
                            link = c2['href']
                        else:
                            link = None
                        planet.researches[name] = {
                            'link': link,
                            'level': lvl,
                            'in_construction': False}
        except:
            self.logger.exception('Exception while reloading researches info')
            return False
        else:
            self.logger.info('%s researches were reloaded' % planet)
        return True

    def transport_resources(self):
        tasks = self.transport_manager.find_dest_planet(self.planets)
        if tasks is None:
            return False
        self.logger.info(self.transport_manager.get_summary())
        for task in iter(tasks):
            self.logger.info(
                'Transport attempt from: %s, to: %s with resources %s' %
                (task['from'], task['where'], task['resources']))
            result = self.send_fleet(
                task['from'],
                task['where'].coords,
                fleet=task['from'].get_fleet_for_resources(task['resources']),
                resources=task['resources'],
                mission='transport'
            )
            if result:
                self.transport_manager.update_sent_resources(task['resources'])
                self.logger.info(
                    'Resources sent: %s, resources needed: %s' %
                    (task['resources'], self.transport_manager.get_resources_needed()))

        return True

    def build_defense(self, planet):
        """
        Build defense for all resources on the planet
        1. plasma
        2. gauss
        3. heavy cannon
        4. light cannon
        5. rocket launcher
        """
        url = self._get_url('defense', planet)
        resp = self.br.open(url)
        for t in ('406', '404', '403', '402', '401'):
            self.br.select_form(name='form')
            self.br.form.new_control('text', 'menge', {'value': '100'})
            self.br.form.fixup()
            self.br['menge'] = '100'

            self.br.form.new_control('text', 'type', {'value': t})
            self.br.form.fixup()
            self.br['type'] = t

            self.br.form.new_control('text', 'modus', {'value': '1'})
            self.br.form.fixup()
            self.br['modus'] = '1'

            self.br.submit()

    def get_player_status(self, destination, origin_planet=None):
        if not destination:
            return

        status = {}
        origin_planet = origin_planet or self.get_closest_planet(destination)
        galaxy, system, position = destination.split(':')

        url = self._get_url('galaxyCnt', origin_planet)
        data = urlencode({'galaxy': galaxy, 'system': system})
        resp = self.br.open(url, data=data)
        soup = BeautifulSoup(resp)

        soup.find(id='galaxytable')
        planets = soup.findAll('tr', {'class': 'row'})
        target_planet = planets[int(position)-1]
        name_el = target_planet.find('td', 'playername')
        status['name'] = name_el.find('span').text

        status['inactive'] = 'inactive' in name_el.get('class', '')
        return status

    def find_inactive_nearby(self, planet, radius=15):

        self.logger.info("Searching idlers near %s in radius %s"
                         % (planet, radius))

        nearby_systems = planet.get_nearby_systems(radius)
        idlers = []

        for system in nearby_systems:
            galaxy, system = system.split(":")
            url = self._get_url('galaxyCnt', planet)
            data = urlencode({'galaxy': galaxy, 'system': system})
            resp = self.br.open(url, data=data)
            soup = BeautifulSoup(resp)

            galaxy_el = soup.find(id='galaxytable')
            planets = galaxy_el.findAll('tr', {'class': 'row'})
            for pl in planets:
                name_el = pl.find('td', 'playername')
                debris_el = pl.find('td', 'debris')
                inactive = 'inactive' in name_el.get('class', '')
                debris_not_found = 'js_no_action' in debris_el.get('class', '')
                if not inactive or not debris_not_found:
                    continue
                position = pl.find('td', 'position').text
                coords = "%s:%s:%s" % (galaxy, system, position)
                player_id = name_el.find('a').get('rel')

                player_info = soup.find(id=player_id)
                rank_el = player_info.find('li', 'rank')

                if not rank_el:
                    continue

                rank = int(rank_el.find('a').text)
                if rank > 4000 or rank < 900:
                    continue

                idlers.append(coords)
                time.sleep(2)

        return idlers

    def find_inactives(self):

        inactives = []
        for p in self.planets:
            try:
                idlers = self.find_inactive_nearby(p)
                self.logger.info(" ".join(idlers))
                inactives.extend(idlers)
            except Exception as e:
                self.logger.exception(e)
                continue
            time.sleep(5)

        self.logger.info(" ".join(inactives))
        self.inactives = list(set(inactives))
        self.logger.info(inactives)

    def send_fleet(self, origin_planet, destination, fleet={}, resources={},
                   mission='Attack', target='Planet', speed=None, holdingtime=None):
        if origin_planet.coords == destination:
            self.logger.error('Cannot send fleet to the same planet')
            return False
        self.logger.info('Sending fleet from %s to %s (%s)'
                         % (origin_planet, destination, mission))
        try:
            resp = self.br.open(self._get_url('fleet', origin_planet))
            try:
                self.br.select_form(
                    predicate=lambda f: 'action' in f.attrs and f.attrs
                    ['action'] == 'game.php?page=fleet1')
            except mechanize.FormNotFoundError:
                self.logger.info('No available ships on the planet')
                return False
            #resp=open("samples/fleet.html", "r").read()
            soup = BeautifulSoup(resp)
            sended = set()
            for c in soup.find('form', {'action': 'game.php?page=fleet1'}).findAll('tr')[2:-2]:
                name = c.find('a').contents[0].strip()
                if name in fleet:
                    inp = c.find('input')['name'].strip()
                    sended.add(name)
                    self.br.form[inp] = str(fleet[name])

            for name in fleet.iterkeys():
                if name not in sended:
                    self.logger.info("Couldn't send all ships to mission")
                    return False
            self.br.submit()
            try:
                self.br.select_form(
                    predicate=lambda f: 'action' in f.attrs and f.attrs
                    ['action'] == 'game.php?page=fleet2')
            except mechanize.FormNotFoundError:
                self.logger.info('Error while sending ships, fleet2')
                return False

            galaxy, system, position = destination.split(':')
            self.br.form['galaxy'] = galaxy
            self.br.form['system'] = system
            self.br.form['planet'] = position
            self.br.form['planettype'] = [self.TARGETS[target]]
            if speed:
                self.br.form['speed'][0] = str(floor(int(speed)/10))
            self.br.submit()

            try:
                self.br.select_form(
                    predicate=lambda f: 'action' in f.attrs and f.attrs
                    ['action'] == 'game.php?page=fleet3')
            except mechanize.FormNotFoundError:
                self.logger.info('Error while sendind ships, fleet 3')
                return False
            self.br.form['mission'] = [self.MISSIONS[mission]]
            if holdingtime:
                self.br.form['holdingtime'] = [str(holdingtime)]
            if 'Metal' in resources:
                self.br.form['resource1'] = str(resources['Metal'])
            if 'Crystal' in resources:
                self.br.form['resource2'] = str(resources['Crystal'])
            if 'Deuterium' in resources:
                self.br.form['resource3'] = str(resources['Deuterium'])
            self.br.submit()
            last_response = self.br.response() # This is returned by br.open(...) too
            print last_response.geturl()
            print last_response.info()
        except Exception as e:
            self.logger.exception(e)
            return False
        return True

    def send_message(self, url, player, subject, message):
        self.logger.info('Sending message to %s: %s' % (player, message))
        self.br.open(url)
        self.br.select_form(nr=0)
        self.br.form['betreff'] = subject
        self.br.form['text'] = message
        self.br.submit()

    def send_sms(self, msg):
        from smsapigateway import SMSAPIGateway
        try:
            SMSAPIGateway().send(msg)
        except Exception as e:
            self.logger.exception(str(e))

    def handle_attacks(self):
        attack_opts = options['attack']
        send_sms = bool(options['sms']['send_sms'])

        for a in self.active_attacks:
            if a.is_dangerous():
                self.logger.info('Handling attack: %s' % a)
                if not a.planet.is_moon():
                    self.build_defense(a.planet)
                if send_sms and not a.sms_sent:
                    self.send_sms(a.get_sms_text())
                    a.sms_sent = True
                if send_sms and not a.message_sent:
                    self.send_message(
                        a.message_url,
                        a.player,
                        attack_opts['message_topic'],
                        a.get_random_message())
                    a.message_sent = True
                self.fleet_save(a.planet)

    def check_attacks(self, soup):
        alert = soup.find(id='attack_alert')
        if not alert:
            self.logger.exception('Check attack failed')
            return
        if 'noAttack' in alert.get('class', ''):
            self.logger.info('No attacks')
            self.active_attacks = []
        else:
            self.logger.info('ATTACK!')
            resp = self.br.open(self.PAGES['events'])
            soup = BeautifulSoup(resp)
            hostile = False
            try:
                for tr in soup.findAll('tr'):
                    countDown = tr.find('td', 'countDown')
                    if countDown and 'hostile' in countDown.get('class', ''):
                        hostile = True
                        # First: check if attack was noticed
                        if tr.get('id'):
                            attack_id = tr.get('id').split('-')[1]
                        elif countDown.get('id'):
                            attack_id = countDown.get('id').split('-')[2]
                        if not attack_id or attack_id in [a.id for a in self.active_attacks]:
                            continue
                        try:
                            # Attack first discovered: save attack info
                            arrivalTime = tr.find(
                                'td',
                                'arrivalTime').text.split(' ')[0]
                            coordsOrigin = tr.find('td', 'coordsOrigin')
                            if coordsOrigin:
                                if coordsOrigin.find('a'):
                                    coordsOrigin = coordsOrigin.find(
                                        'a').text.strip()[1:-1]
                            destCoords = tr.find('td', 'destCoords')
                            if destCoords:
                                destCoords = destCoords.find(
                                    'a').text.strip()[1:-1]
                            originFleet = tr.find('td', 'originFleet')
                            detailsFleet = int(
                                tr.find(
                                    'td',
                                    'detailsFleet').span.text.replace(
                                    '.',
                                    ''))
                            player_info = originFleet.find('a')
                            message_url = player_info.get('href')
                            player = player_info.get('data-player-name')
                            is_moon = False  # TODO!
                            planet = self.find_planet(
                                coords=destCoords,
                                is_moon=is_moon)
                            a = Attack(
                                planet,
                                attack_id,
                                arrivalTime,
                                coordsOrigin,
                                destCoords,
                                detailsFleet,
                                player,
                                message_url)

                            self.active_attacks.append(a)
                        except Exception as e:
                            self.logger.exception(e)
                            self.send_sms('ATTACKEROR')
                if not hostile:
                    self.active_attacks = []
            except Exception as e:
                self.logger.exception(e)

    def fleet_save(self, p):
        if not p.has_ships():
            return
        fleet = p.ships
        # recyclers are staying!
        #fleet['rc'] = 0
        self.logger.info('Making fleet save from %s' % p)
        self.send_fleet(p,
                        self.get_safe_planet(p).coords,
                        fleet=fleet,
                        mission='Hold Position',
                        speed=10,
                        resources={'metal': p.resources['metal']+500,
                                   'crystal': p.resources['crystal']+500,
                                   'deuterium': p.resources['deuterium']+500})

    def collect_debris(self, p):
        if not p.has_ships():
            return
        self.logger.info(
            'Collecting debris from %s using %s recyclers' %
            (p, p.ships['rc']))
        self.send_fleet(p,
                        p.coords,
                        fleet={'rc': p.ships['rc']},
                        mission='collect',
                        target='debris')

    def send_expedition(self):
        expedition = options['expedition']
        planets = expedition['planets'].split(' ')
        random.shuffle(planets)
        for coords in planets[:3]:
            planet = self.find_planet(coords=coords)
            if planet:
                galaxy, system, position = planet.coords.split(':')
                expedition_coords = '%s:%s:16' % (galaxy, system)
                self.send_fleet(
                    planet,
                    expedition_coords,
                    fleet={
                        expedition['ships_kind']: expedition['ships_number']},
                    mission='expedition')

    
    def farm(self):
        if options['farming'].has_key('enabled') and not strtobool(options['farming']['enabled']):
            return
        farms =[s.strip().split(' ')[0] for s in  options['farming']['farms'].split(',')]
        if not farms or not farms[0]:
            return
        ships_kind = options['farming']['ships_kind']
        ships_number = options['farming']['ships_number']
        next_farm = int(options['farming']['next_farm'])  % len(farms)

        farm = farms[next_farm]
        #if not self.get_player_status(farm)['inactive']:
        #    self.logger.error('farm %s seems not to be inactive!', farm)
        #    return
        if self.send_fleet(
            self.get_closest_planet(farm),
            farm,
            fleet={ships_kind: ships_number}
        ):
            next_farm = (next_farm+1) % len(farms)
            options.change_item('farming', 'next_farm', str(next_farm))


    def sleep(self):
        sleep_options = options['general']
        sleep_time = randint(
            0, int(sleep_options['seed']))+int(sleep_options['check_interval'])
        self.logger.info('Sleeping for %s secs' % sleep_time)
        if self.active_attacks:
            sleep_time = 60
        time.sleep(sleep_time)

    def stop(self):
        self.logger.info('Stopping bot')
        os.unlink(self.pidfile)

    def interactive(self):
        self.pid = str(os.getpid())
        self.pidfile = 'bot.pid'
        file(self.pidfile, 'w').write(self.pid)
        if not self.login():
            self.logger.error('Login failed!')
        self.load_planets()

    def start(self):
        self.logger.info('Starting bot')
        self.pid = str(os.getpid())
        self.pidfile = 'bot.pid'
        file(self.pidfile, 'w').write(self.pid)

        # main loop
        while True:
            if self.login():
                try:
                    self.load_planets()
                    self.handle_planets()
                    # self.find_inactives()
                    # if not self.active_attacks:
                    #    if True or not self.transport_resources():
                    #        self.send_expedition()
                    #        self.farm()
                    #        self.farm()
                    # else:
                    #    self.handle_attacks()

                except Exception as e:
                    self.logger.exception(e)
                    # self.stop()
                    # return
            else:
                self.logger.error('Login failed!')
                # self.stop()
                # return
            self.sleep()
示例#59
0
def main():
    parser = argparse.ArgumentParser(description='Placement')
    parser.add_argument("-c",
                        "--config",
                        help="path to config file",
                        default="")
    parser.add_argument("-i", "--input", help="path to data file", default="")
    parser.add_argument("-d",
                        "--debug",
                        help="debug mode",
                        action="store_const",
                        dest="loglevel",
                        const=logging.DEBUG,
                        default=logging.WARNING)
    parser.add_argument("-p",
                        "--pickle",
                        help="create pickle files",
                        action="store_true",
                        dest="create_pickle")
    parser.add_argument("-v",
                        "--verbose",
                        help="verbose mode",
                        action="store_const",
                        dest="loglevel",
                        const=logging.INFO)

    args = parser.parse_args(sys.argv[1:])
    logging.basicConfig(format='%(asctime)s main: %(message)s',
                        level=args.loglevel)

    config = pandas.read_csv(args.config, sep=',')
    X = pandas.read_csv(args.input, sep=',',
                        header=None).values.astype(np.float64)
    """
    discretize the space into cells
    """
    geo_utils = helpers.GeoUtilities(config['lat_min'].iloc[0],
                                     config['lat_max'].iloc[0],
                                     config['lng_min'].iloc[0],
                                     config['lng_max'].iloc[0],
                                     cell_length_meters)
    geo_utils.set_grids()
    """
    segregate data based on time bins
    """
    train_time_utils = helpers.TimeUtilities(time_bin_width_secs)
    train_time_utils.set_bounds(X)
    logging.info("Loaded %d data points", len(X))

    sim = Sim(X, len(geo_utils.lng_grids), train_time_utils, geo_utils,
              action_dim, time_bins_per_hour)
    """
    starting time-step for training and testing time bins (tb)
    """
    test_tb_starts = [time_bins_per_hour]
    train_tb_starts = [
        time_bins_per_day * 4 + time_bins_per_hour,
        time_bins_per_day * 5 + time_bins_per_hour,
        time_bins_per_day * 6 + time_bins_per_hour
    ]

    train_bins = range(time_bins_per_day * 4 + time_bins_per_hour,
                       time_bins_per_day * 7, time_bins_per_day)

    if args.create_pickle:
        helpers.load_create_pickle(sim, train_time_utils, geo_utils, X,
                                   train_tb_starts, test_tb_starts)

    with open(r"rrs.pickle", "rb") as input_file:
        sim.rrs = cPickle.load(input_file)
        sim.n_reqs = cPickle.load(input_file)
        sim.pre_sim_pickups = cPickle.load(input_file)

    with tf.Session() as sess:
        worker = Worker('worker', sim, 10, train_bins, test_tb_starts,
                        sim.num_cells, sim.n_actions)

        sess.run(tf.global_variables_initializer())
        worker.train(sess)
    """
    model = A2C(sim, 10, 
                train_windows, test_window,
                sim.num_cells,
                sim.n_actions, hidden_units)
    model.train()
    """
    '''
示例#60
0
 def trace_queue(self, message):
     Sim.trace("Queue", message)