<commit_before>from milecastles import AnonymousContainer, Box, Card
from engines import Engine

class ConsoleSiteEmulator(AnonymousContainer):
    required = AnonymousContainer.required + ["story"]

    def __init__(self, *a, **k):
        super().__init__(*a, **k)
        
        # get boxes from story registry, create+store an engine for each 
        #import pdb; pdb.set_trace()
        boxTable = self.story._get_table(Box)
        self.engines = dict()        
        for boxIdString,box in boxTable.items():
            engine = ConsoleEngine(box=box)
            engine.registerStory(self.story)
            self.engines[boxIdString] = engine
            
        # for each card, register it with the emulator
        cardIds = ["a","b","c","d"]
        for cardId in cardIds:
            card = story.createBlankCard(cardId)
            self.registerCard(card)
            
        self.currentCard = None
    
    def registerCard(self, card):
        self._register(Card, card)
        
    def lookupCard(self, cardUid):
        self._lookup(Card, cardUid)
        
    def run(self):
        while True:
            command = input()
            
            # handle commands indentifying a card in the emulator
            cardTable = self._get_table(Card) # use box lookup from story
            if command in cardTable: 
                self.currentCard = cardTable[command]
                print("Current card set to '" + command + "'")

            else:
                # handle commands identifying a box in the story
                boxes = list(self.story._get_table(Box).values())
                boxCommands = [str(pos + 1) for pos in range(len(boxes))]
                try:
                    boxPos = boxCommands.index(command)
                    if self.currentCard != None:
                        print("Tapping box '" + command + "' with card '" + self.currentCard.uid.idString + "'")
                        engine = self.engines[boxes[boxPos].uid.idString]
                        engine.handleCard(self.currentCard)
                    else:
                        print("Cannot tap box. No card selected. Choose one of " + str(list(cardTable.keys())))            
                except ValueError:
                    print("Command not a card id or a box position. Can't understand command")
    
class ConsoleEngine(Engine):
    
    def renderText(self, text):
        print(text)
        
if __name__ == "__main__":
    from milecastles import loadStory
    story = loadStory("exampleShort")
    emulator = ConsoleSiteEmulator(story=story)
    emulator.run()<commit_msg>Updated for removal of Uid class and non-sequential box uids. Standardised to new Engine API<commit_after>from milecastles import AnonymousContainer, Box, Card
from engines import Engine

class ConsoleSiteEmulator(AnonymousContainer):
    required = AnonymousContainer.required + ["story"]

    def __init__(self, *a, **k):
        super().__init__(*a, **k)
        
        # get boxes from story registry, create+store an engine for each 
        #import pdb; pdb.set_trace()
        boxTable = self.story._get_table(Box)
        self.engines = dict()        
        for boxUid,box in boxTable.items():
            engine = ConsoleEngine(box=box)
            engine.registerStory(self.story)
            self.engines[boxUid] = engine
            
        # for each card, register it with the emulator
        cardIds = ["a","b","c","d"]
        for cardId in cardIds:
            card = self.story.createBlankCard(cardId)
            self.registerCard(card)
            
        self.currentCard = None
    
    def registerCard(self, card):
        self._register(Card, card)
        
    def lookupCard(self, cardUid):
        self._lookup(Card, cardUid)
        
    def run(self):
        while True:
            command = input()
            
            # handle commands indentifying a card in the emulator
            cardTable = self._get_table(Card) # use box lookup from story
            if command in cardTable: 
                self.currentCard = cardTable[command]
                print("Current card set to '" + command + "'")

            else:
                # handle commands identifying a box in the story
                boxUids = list(self.story._get_table(Box).keys())
                matchingBoxUids = [boxUid for boxUid in boxUids if command in boxUid]
                numMatching = len(matchingBoxUids)
                if numMatching == 0:
                    print("No box uids match '" + command + "'")
                elif numMatching >= 2:
                    print("Too many box uids match '" + command + "' : " + str(matchingBoxUids) )
                elif numMatching == 1: # a single box got matched
                    boxUid = matchingBoxUids[0]
                    if self.currentCard != None:
                        print("Tapping box '" + command + "' with card '" + self.currentCard.uid + "'")
                        self.engines[boxUid].handleCard(self.currentCard)
                    else:
                        print("Cannot tap box. No card selected. Choose one of " + str(list(cardTable.keys())))            
    
class ConsoleEngine(Engine):
    
    def displayText(self, text):
        print(text)
        
if __name__ == "__main__":
    from milecastles import loadStory
    story = loadStory("exampleShort")
    emulator = ConsoleSiteEmulator(story=story)
    emulator.run()
{'fraction_non_alphanumeric': 0.09352649917476619, 'fraction_numerical': 0.0009169264624977076, 'mean_word_length': 1.8171487603305785, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2068341', 'n_tokens_mistral': 1458, 'n_tokens_neox': 1403, 'n_words': 395}