FreeOrion

Forums for the FreeOrion project
It is currently Fri May 24, 2013 2:45 pm

All times are UTC




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: Introducing Myself by lrh9
PostPosted: Mon Jul 02, 2012 4:47 pm 
Offline
Space Krill

Joined: Mon Jul 02, 2012 2:40 pm
Posts: 12
Hello. I enjoy space 4x games and I can program in Python. I am thinking about trying to contribute to FreeOrion's Python artificial intelligence, but I wanted to introduce myself first.

If I am welcomed here, where is the most appropriate place for me to post about the Python artificial intelligence? I see a Programming topic, but the description advises that the topic is primarily for developers. Does that include the Python artificial intelligence developers?

My primary experience is with writing standalone scripts. I don't know the best way to use third party modules and packages with the Python artificial intelligence. Should I try to manipulate the bundled Python, or should I add a module or package directory to sys.path? What would be the best place for that directory?

I can code in Python 3 and Python 2. Here is an example of my work. It is a simple Python 3 IRC bot framework with asynchronous I/O and an event-oriented architecture.

Code:
import asynchat
import asyncore
import collections
import socket

#hack substitute for regular expressions or even a parser
def parse(message):
    data = message.split(' ')
    prefix = None
    if data[0].startswith(':'): #prefix present
        prefix = data[0][1:]
        data = data[1:]
    command = data[0]
    data = ' '.join(data[1:])
    if data.startswith(':'): #only trailing parameter
        parameters = [data[1:]]
    else:
        data = data.split(' :')
        trailing = None
        if len(data) > 1: #trailing parameter present
            trailing = ' :'.join(data[1:])
        parameters = data[0].split(' ') #regular parameters
        if trailing is not None:
            parameters.append(trailing) #add trailing parameter to regular parameters
    return prefix, command, parameters

class Connection(asynchat.async_chat):

    def __init__(self, nick, user, realname, host, port=6667):
        self.nick = nick
        self.user = user
        self.realname = realname
        self.address = (host, port)
        self.received = list()
        asynchat.async_chat.__init__(self)
        self.set_terminator(b'\n')
        self.handlers = collections.defaultdict(set)

    def collect_incoming_data(self, data):
        self.received.append(data)

    def found_terminator(self):
        data = b''.join(self.received).rstrip(b'\r')
        del self.received[:]
        prefix, command, parameters = parse(data.decode())
        #comment out next line
        #print(prefix, command, parameters)
        for handler in self.handlers[command]:
            try:
                handler(self, prefix, parameters)
            except Exception as exception:
                for handler in self.handlers[exception]:
                    handler(self, exception)

    def message(self, string):
        string = ''.join((string, '\r\n'))
        self.push(string.encode())

    def establish(self):
        self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
        self.connect(self.address)

    def subscribe(self, topic, handler):
        self.handlers[topic].add(handler)

    def unsubscribe(self, topic, handler):
        self.handlers[topic].discard(handler)

    def handle_connect(self):
        for handler in self.handlers['connect']:
            try:
                handler(self)
            except Exception as exception:
                for handler in self.handlers[exception]:
                    handler(self, exception)

if __name__ == '__main__':

    #define event handlers here
    def on_keyerror(connection, exception):
        print(connection, exception)
   
    def pong(connection, prefix, parameters):
        connection.message('PONG {0}'.format(parameters[0]))

    def on_connect(connection):
        connection.message('NICK {0}'.format(connection.nick))
        connection.message('USER {0} {1} bla :{2}'.format(connection.user, connection.address[0], connection.realname))

    #additional code
    class Channel:

        def __init__(self, channel):
            self.channel = channel

        def __call__(self, connection, prefix, parameters):
            connection.message('JOIN {0}'.format(self.channel))


    #set up connections here
    c = Connection('lrh9bot', 'lrh9bot', 'lrh9bot', 'irc.dal.net')
    #registering handlers to events
    c.subscribe(KeyError, on_keyerror)
    c.subscribe('PING', pong)
    c.subscribe('connect', on_connect)
    c.subscribe('372', Channel('#ai'))
    c.establish()

    #start asyncore
    asyncore.loop()


Top
 Profile  
 
 Post subject: Re: Introducing Myself by lrh9
PostPosted: Mon Jul 02, 2012 5:08 pm 
Offline
Programming, Design, and De Facto Lead
User avatar

Joined: Wed Oct 08, 2003 1:33 am
Posts: 7898
Location: Vancouver, BC
lrh9 wrote:
...where is the most appropriate place for me to post about the Python artificial intelligence? I see a Programming topic, but the description advises that the topic is primarily for developers. Does that include the Python artificial intelligence developers?
Yes, AI scripting falls under programming. Post any questions or ideas you want to discuss there.


Top
 Profile  
 
 Post subject: Re: Introducing Myself by lrh9
PostPosted: Mon Jul 02, 2012 5:09 pm 
Offline
Space Krill

Joined: Mon Jul 02, 2012 2:40 pm
Posts: 12
Geoff the Medio wrote:
lrh9 wrote:
...where is the most appropriate place for me to post about the Python artificial intelligence? I see a Programming topic, but the description advises that the topic is primarily for developers. Does that include the Python artificial intelligence developers?
Yes, AI scripting falls under programming. Post any questions or ideas you want to discuss there.

Thank you.


Top
 Profile  
 
 Post subject: Re: Introducing Myself by lrh9
PostPosted: Mon Jul 02, 2012 5:22 pm 
Offline
Design & Graphics Lead
User avatar

Joined: Sat Sep 23, 2006 7:09 pm
Posts: 3693
Location: USA — midwest
Welcome!
The AI currently is pretty sad, we could use some more hands on board.

_________________
—• Read this First before posting Game Design Ideas!
—• Design Philosophy

—•— My Ideas, Organized —•— Get an Avatar —•— Acronyms —•—


Top
 Profile  
 
 Post subject: Re: Introducing Myself by lrh9
PostPosted: Mon Jul 02, 2012 9:29 pm 
Offline
Space Krill

Joined: Mon Jul 02, 2012 2:40 pm
Posts: 12
eleazar wrote:
Welcome!
The AI currently is pretty sad, we could use some more hands on board.

Thanks!

I know how to use IRC and I am on the FreeOrion channel if anyone ever wants to chat.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 5 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group