joshwingreene
5/22/2015 - 5:14 PM

Inf 124 - Proj 2 - model.py

Inf 124 - Proj 2 - model.py

# model.py - State and logic

import view

from datetime import datetime

# data structures
GENERAL_OPTIONS = ['Feedback', 'Question', 'Other']

QUESTION_OPTIONS = ['How do I place an order?','How do I use the Wishlist?','How do I cancel/edit an order?']

# Client Classes
class Client:
    def __init__(self, host, port):
        self.host = host
        self.port = port
        #self.communication_handler = CommHandler(self.host, self.port) # connects to the server (like what happens with Client(Handler) in client.py)
            # I'm thinking that the handler shouldn't be what Agent and Visitor should inherit from
            # We could make use of the handler as a data member of each client, which is why it is assigned to self.communication_handler.
        self.client_type = None
        self.client_name = None
        self.active_timestamp = None

    def set_active_timestamp (self, session_timestamp):
        self.active_timestamp = session_timestamp

    def reset_timestamp(self):
        ''' Should be called when the client's chat session is terminated '''
        self.active_timestamp = None

    def set_up_client(self): # Overloaded in visitor and agent
        pass

class Visitor(Client):

    def set_up_client(self):
        self.client_type = 'visitor'
        self.client_name = view.get_client_name()

class Agent(Client):

    def set_up_client(self):
        self.client_type = 'agent'
        self.client_name = 'Agent ' + view.get_client_name()

# Chat Session and State Classes
class ChatSession:
    def __init__ (self, client, agent):
        self.isActive = True
        self.client = client
        self.agent = agent
        self.messageLog = []

class State:
    def __init__ (self):
        self._numConnectedClients = 0
        self.chatSessions = {}

    def increment_clients(self):
        ''' Called by the controller when it notices that a client has been connected to the server '''
        self._numConnectedClients += 1

    def add_new_chat_session (self, client, agent): # Input: Client, Agent; Return: datetime
        ''' Creates a new chat session between the given client and agent; Returns the unique starting timestamp for that session
            This function doesn't handle the communication for the chat session. The controller should do this.'''
        timestamp = datetime.today()
        new_chat_session = ChatSession(client, agent)

        self.chatSessions[timestamp] = new_chat_session

        return timestamp

    def log_message (self, session_timestamp, sendor_name, message): # Input: datetime, str, str; Return: None
        ''' Records the message to the chat session with the associated timestamp '''
        self.chatSessions[session_timestamp].messageLog.append(sendor_name + ': ' + message)

    def print_session_transcript (self, session_timestamp):
        ''' Prints the message transcript of the associated chat session '''
        for message in self.chatSessions[session_timestamp].messageLog:
            print(message)

    def end_chat_session (self, session_timestamp): # Input: datetime; Return: None
        ''' Ends the chat session with the associated timestamp.
            This function just marks the session as being inactive, which could be used by the controller to determine which session's communication should cease '''
        self.chatSessions[session_timestamp].isActive = False