• Facebook
  • Twitter
  • Reddit
  • StumbleUpon
  • Digg
  • email

import sys
import traceback
import socket
 
from blueplanet.creatures.ItGeneticClient import ItGeneticClient, PORT
 
def ask_threads(pop):
    tc= raw_input('Set threads(%i) to:\t' % len(pop.thread_id))
    if tc:
        try:
            c= int(tc)
        except ValueError, e:
            print e
        else:
            for i in range(c - len(pop.thread_id)):
                pop.start_exec_all_thread()
            for i in range(len(pop.thread_id) - c):
                pop.stop_exec_all_thread()
 
def ask_join(pop):
    gr= raw_input('Join group:\t')
    while gr:
        p= raw_input('\tPassword:\t')
        print '\t', pop.group_join(gr, p)
        gr= raw_input('Join group:\t')
 
def ask_group(pop):
    if not raw_input('Group information?\t'):
        return
    l= pop.group_mine()
    if type(l) is not list:
        print l
        l= []
    l2= pop.group_all()
    l2.sort()
    if type(l2) != list:
        print l2
    else:
        if l2:
            print '\tgroups:'
            for gr in l2:
                print '\t', gr, '\t(joined)' * (gr in l)
        else:
            print '\tno groups'
    if l:
        gr= raw_input('Info about Group:\t')
        while gr:
            print '\tmembers:'
            m= pop.group_members(gr)
            if type(m) is list:
                for m in m:
                    s= hasattr(pop, 'trusting') and pop.trusting(m) and \
                       '(trusting)' or '(not trusted)'
                    print '\t', m, s
            else:
                print '\t', m
            gr= raw_input('Info about Group:\t')
 
def ask_quit(pop):
    gr= raw_input('Quit group:\t')
    while gr:
        print pop.group_quit(gr)
        gr= raw_input('Quit group:\t')
 
def ask_statistics(pop):
    if raw_input('Show statistics?\t'):
        x= True
        while x:
            print '\tcreatures:\t\t', pop.creature_count
            print '\t\tcreated:\t', len([True for c in pop.creature_list if c.is_created()])
            print '\ttime to exec all:\t', pop.exec_all_time
            print '\ttime to exec one:\t', pop.exec_one_time
            print '\tlast exec time for all:\t', pop.exec_all_time_last
            print '\tbytes sent:\t', pop.bytes_sent
            x= not raw_input('break?')
 
def change_property(pop, name, convert, help= ''):
    print '\tchange', name, '=', getattr(pop, name, 'unbekannt')
    if help:
        print help
    r= raw_input('\tvalue:')
    if r:
        r= convert(r)
        setattr(pop, name, r)
        print name, '=', r
 
 
def ask_property(pop):
    c= raw_input('Change Property?\t')
    if c:
        change_property(pop, 'maxdeadcount', int, '\tint')
        change_property(pop, 'com_react_timeout', int, '\tint')
        change_property(pop, 'min_creature_count', int, '\tint')
        change_property(pop, 'max_creature_count', int, '\tint')
        change_property(pop, 'take_creatures_send_wish_timeout', float, '\tfloat\n\t\
timeout of wish to send creatures')
        change_property(pop, 'recv_creature_max_count_mul', float, '\tfloat')
        change_property(pop, 'send_creatures', int, '\tint')
        # change own selection
        c= raw_input('change attr manually (advanced) (ENTER=no)?')
        if c:
            a= raw_input('attr:')
            if a in dir(pop):
                setattr(pop, a, input('value:'))
            else:
                print dir(pop)
            while a:
                a= raw_input('attr:')
                if a in dir(pop):
                    try:
                        v= input('value(%s):' % str(getattr(pop, a, '-')))
                    except:
                        pass
                    else:
                        setattr(pop, a, v)
                    print a, '=', getattr(pop, a)
                elif a:
                    print dir(pop)
                    print a, 'unknown. Choose another attribute from above.'
 
def main(pop= None, own_stuff= lambda pop: None):
    if pop is None:
        pop= ItGeneticClient()
    x= True
    while x:
        try:
            print pop.connect((raw_input('IP(localhost): ') or 'localhost', int(raw_input('Port('+str(PORT)+'): ') or str(PORT))))
            x= False
        except socket.error, err:
            print 'not able to connect, reason:', ': '.join([str(a) for a in err.args])
            x= raw_input('retry?(NO=n)') != 'n'
            if not x:
                return None
    pop.center= pop.get_sunpos()
    print 'colony:', pop.set_colonyname('Helper')
    print 'ENTER = Ignore question'
    while 1:
        try:
            ask_threads(pop)
            ask_join(pop)
            ask_quit(pop)
            ask_group(pop)
            ask_statistics(pop)
            ask_property(pop)
            own_stuff(pop)
        except:
            traceback.print_exception(*sys.exc_info())
        if raw_input('Close?(j/n)\t') == 'j' and \
           raw_input('Really close?(j/n)\t') == 'j':
            pop.give_away_creatures_all()
            break
    return pop
 
 
if __name__ == '__main__':
    pop= main()