from asyncore import dispatcher from asynchat import async_chat import socket, asyncore, time, sys from cmdhandler import * chandler = Handler() class PlayerSession(async_chat): """ This class represents a player's sesssion. From here we branch down into other various classes, please try to keep this one tidy! """ def __init__(self, server, sock, addr): async_chat.__init__(self, sock) self.server = server self.address = addr self.set_terminator("\n") self.name = None self.data = [] self.sock = sock self.logged_in = False self.user = None # The time the user last issued a command. self.cmd_last = time.time() # Total number of commands issued. self.cmd_total = 0 # The time when the user connected. self.conn_time = time.time() # Player's room location. Move this to a player sub-class. self.player_loc = 4 def collect_incoming_data(self, data): """ Stuff any incoming data into our buffer, self.data """ self.data.append(data) def found_terminator(self): """ Any line return indicates a command for the purpose of a MUD. So we take the user input, split it up by spaces into a list, and pass it to our command handler. """ line = (''.join(self.data)) line = line.strip('\r') uinput = line.split(' ') self.data = [] # Increment our user's command counter. self.cmd_total += 1 # Store the timestamp of the user's last command. self.cmd_last = time.time() # Stuff anything we need to pass in this dictionary. cdat = {"server": self.server, "uinput": uinput, "session": self} chandler.handle(cdat) def handle_close(self): """ Break the connection and do some accounting. """ async_chat.handle_close(self) self.logged_in = False self.server.session_list.remove(self) print 'Sessions active:', len(self.server.session_list) def game_connect_screen(self, session): """ Show our banner screen. """ buffer = '-'*50 buffer += ' \n\rWelcome to Evennia!\n\r' buffer += '-'*50 + '\n\r' buffer += """Please type one of the following to begin:\n\r connect \n\r create \n\r""" buffer += '-'*50 + '\n\r' session.push(buffer) def login(self, user): """ After the user has authenticated, handle logging him in. """ self.user = user self.name = user.username self.logged_in = True self.conn_time = time.time() self.push("Logging in as %s.\n\r" % (self.name,)) print "Login: %s" % (self,) def create_user(self, uname, email, password): """ Handles the creation of new users. """ # print uname, email, password user = User.objects.create_user(uname, email, password) self.login(user) print 'Registration: %s' % (self,) self.push("Welcome to the game, %s.\n\r" % (self.name,)) def nextfree_objnum(self): """ Returns the next free object number. """ def __str__(self): """ String representation of the user session class. We use this a lot in the server logs and stuff. """ if self.logged_in: symbol = '#' else: symbol = '?' return "<%s> %s@%s" % (symbol, self.name, self.address,) # def handle_error(self): # self.handle_close()