Example #1
0
 def __init__(self, name=''):
     self.name = name
     self.game = None
     self.deck = Deck("%s's Deck" % self.name)
     self.board = Board(self)
     self.domainPanel = DomainPanel(self)
     self.domains = self.domainPanel.domains
     self.discardPile = DiscardPile(self)
     self.wonStories = CardHeap()
     self.hand = Hand(self)
Example #2
0
    def InitUI(self):

        pnl = wx.Panel(self, -1)

        sizer = wx.BoxSizer(wx.VERTICAL)

        nestedNotebook = wx.Notebook(pnl, wx.NewId())
        self.domainTab = DomainPanel(nestedNotebook, self)
        self.instanceTab = InstancePanel(nestedNotebook, self)
        nestedNotebook.AddPage(self.domainTab, "Domain")
        nestedNotebook.AddPage(self.instanceTab, "Instance")

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(nestedNotebook, 1, wx.ALL | wx.EXPAND, 5)

        pnl.SetSizer(sizer)

        self.SetTitle('Anonadado - Annotation Tool')
        self.Centre()
        self.Show(True)
        self.Maximize(True)
Example #3
0
class Player:
    def __init__(self, name=''):
        self.name = name
        self.game = None
        self.deck = Deck("%s's Deck" % self.name)
        self.board = Board(self)
        self.domainPanel = DomainPanel(self)
        self.domains = self.domainPanel.domains
        self.discardPile = DiscardPile(self)
        self.wonStories = CardHeap()
        self.hand = Hand(self)

        
    #-- Reports
    def handReport(self):
        header = reportColor("%s's HAND\n" % self.name)
        return header + '\n'.join(map(repr,self.hand))

    def domainReport(self):
        header = reportColor("%s's DOMAINS\n" % self.name)
        return header +\
               '\n'.join(map(lambda d: d.report(), self.domains))

    def boardReport(self):
        header = reportColor("%s's BOARD\n" % self.name)
        return header + '\n'.join(map(repr,self.board.characters)) +\
                        '\n\n' +\
                        '\n'.join(map(repr,self.board.supports))

    def discardReport(self):
        header = reportColor("%s's DISCARD PILE\n" % self.name)
        return header + '\n'.join(map(repr,self.discardPile))

    def report(self):
        return '\n'.join([self.handReport(),
                          self.domainReport(),
                          self.discardReport()])


    #~~ Information
    def nCards(self):
        return len(self.hand)


    def opponent(self):
        players = self.game.players[:]
        players.remove(self)
        return players[0]

    def randHandCard(self):
        return rnd.choice(self.hand)


    

    #~~ Actions
    def attach2Domain(self, card, domain):
        if card not in self.hand:
            raise RuleError("This card is not in your hand")
        elif domain not in self.domains:
            raise RuleError("This is not one of your domains")
        else:
            self.hand.remove(card)
            domain.addResource(card)
        if graphicsOn(self):
            self.hand.redraw()
            card.image.turnLeft()
            domain.redraw()
            #self.screen.update()


    def commit(self, card, story):
        if card not in self.board.characters:
            raise RuleError("This character is not on your board")
        elif story not in self.game.stories:
            raise RuleError("This story is not on the board")
        elif card.isExhausted():
            raise RuleError("This character is exhausted. It cannot commit to a story.")
        else:
            self.board.characters.remove(card)
            card.exhaust(draw=False)
            story.committed[self].append(card)
            card.position = story.committed[self]
            self.board.redraw()
            story.redrawCommitted(self)


    def drawCard(self, n=1):
        for i in range(n):
            card = self.deck.pop()
            if hasattr(self.game, 'screen'):
                card.setScreen(self.game.screen)
            self.hand.add(card)


    def payCost(self, card, domain):
        if domain is None:
            pass # do not drain anything
        elif domain not in self.domains:
            raise RuleError("This domain does not belong to you")
        elif domain.isDrained():
            raise RuleError("This domain has already been drained")
        elif card.cost > domain.totalRes():
            raise RuleError("This domain cannot afford this card")
        else:
            domain.drain()
        
    def play(self, card, target, domain):
        if card not in self.hand:
            raise RuleError("This card is not in your hand")
        elif card.cost == 0 and domain is not None:
            raise RuleError("Cannot drain a domain for 0 cost")
        else:

            # play from hand
            self.payCost(card, domain)
            self.hand.remove(card)

            # Effect of the card, where does it end up?
            # Playing without a target
            if target is None:
                # Characters and Board Supports go to on player's board
                if card.category in ['character', 'support']:
                    self.board.add(card)
                    card.enterGame(self)
                # Events end up in the discard pile
                elif card.category == 'event':
                    self.discardPile.add(card)
                # Anything else is Undefined
                else:
                    raise RuleError("Don't know how to play category %s" % card.category)

            #Playing with a target
            elif target is not None:
                # Attachments are attached to the target
                if card.category == 'support' and "Attachment" in card.subtypes:
                    try:
                        target.attach(card)
                        self.board.redraw()
                        card.enterGame(self)
                    except AttributeError:
                        raise RuleError("You cannot attach to this target")
                # Anything else is undefined
                else:
                    raise RuleError("Don't know how to play this card")

    def useDeck(self, deck):
        self.deck = deck
        for card in deck:
            card.owner = self


    def winStory(self, story):
        self.screen.msgBox("%s wins the story\n%s!" % (self.name, story.name), colorscheme=1)
        self.wonStories.add(story)
        if len(self.wonStories) >= 3:
            # win the game
            self.game.win(self)
        self.game.replace(story)
        # MORE ON WINNING HERE!!!!!!!!!!!!!!!!!!
        


    #-- Graphics
    def drawDomainsOnScreen(self):
        self.domainPanel.draw()
Example #4
0
class Anonadado(wx.Frame):

    def __init__(self, *args, **kw):
        super(Anonadado, self).__init__(*args, **kw)
        self.am = None
        self.InitUI()

    def setAnnotator(self, am):
        self.am = am

    def InitUI(self):

        pnl = wx.Panel(self, -1)

        sizer = wx.BoxSizer(wx.VERTICAL)

        nestedNotebook = wx.Notebook(pnl, wx.NewId())
        self.domainTab = DomainPanel(nestedNotebook, self)
        self.instanceTab = InstancePanel(nestedNotebook, self)
        nestedNotebook.AddPage(self.domainTab, "Domain")
        nestedNotebook.AddPage(self.instanceTab, "Instance")

        sizer = wx.BoxSizer(wx.VERTICAL)
        sizer.Add(nestedNotebook, 1, wx.ALL | wx.EXPAND, 5)

        pnl.SetSizer(sizer)

        self.SetTitle('Anonadado - Annotation Tool')
        self.Centre()
        self.Show(True)
        self.Maximize(True)

    def OnClose(self, e):
        self.Close(True)

    def OnQuit(self, e):
        self.Close()

    def get_file_dialog(self):
        return wx.FileDialog(self, message="Choose a file",
                             defaultDir=os.getcwd(),
                             defaultFile="",
                             wildcard="Json (*.json)|*.json|" \
                                      "All files (*.*)|*.*",
                             style=wx.OPEN
                            )

    def OnLoadDomain(self, e):
        dlg = self.get_file_dialog()
        if dlg.ShowModal() == wx.ID_OK:
            path = dlg.GetPaths()[0]
            self.am.parse_domain(path)
            self.domainTab.load_domain()
            self.instanceTab.load_domain()
        dlg.Destroy()

        if len(self.am.domain.keys()) > 0:
            self.domainTab.select_label(0)

    def OnLoadInstance(self, e):
        dlg = self.get_file_dialog()
        if dlg.ShowModal() == wx.ID_OK:
            path = dlg.GetPaths()[0]
            self.am.parse_instance(path)
            self.instanceTab.load_instance()
            self.instanceTab.load_domain()
        dlg.Destroy()

    def OnSaveDomain(self, e):
        dlg = wx.FileDialog(self, message="Save domain",
                            defaultDir=os.getcwd(),
                            defaultFile="",
                            wildcard="Json (*.json)|*.json|" \
                                     "All files (*.*)|*.*",
                            style=wx.FD_SAVE | wx.FD_OVERWRITE_PROMPT
                           )

        if dlg.ShowModal() == wx.ID_OK:
            path = dlg.GetPaths()[0]

            f = open(path, 'w')
            json.dump(self.am.domain_to_json(), f, indent=4)
            f.close()

        dlg.Destroy()

    def OnSaveInstance(self, e):
        dlg = wx.FileDialog(self, message="Save instance",
                            defaultDir=os.getcwd(),
                            defaultFile="",
                            wildcard="Json (*.json)|*.json|" \
                                     "All files (*.*)|*.*",
                            style=wx.FD_SAVE | wx.FD_OVERWRITE_PROMPT
                           )

        if dlg.ShowModal() == wx.ID_OK:
            path = dlg.GetPaths()[0]

            f = open(path, 'w')
            json.dump(self.am.instance_to_json(), f, indent=4)
            f.close()

        dlg.Destroy()

    def OnNewProject(self, e):
        dial = wx.MessageDialog(None, 'Are you sure to create a new project? '\
                                'You will lose your local changes.',
                                'Question',
                                wx.YES_NO | wx.NO_DEFAULT | wx.ICON_QUESTION)
        r = dial.ShowModal()
        if r == wx.ID_YES:
            self.am = annotation_manager()
            self.domainTab.load_domain()
        elif r == wx.ID_NO:
            pass

    def OnNewInstance(self, e):
        pass

    # TODO: remove occurences of this label in the instance
    def OnRemoveLabel(self, e):
        dial = wx.MessageDialog(None, 'Are you sure to remove this label?',
                                'Question',
                                wx.YES_NO | wx.NO_DEFAULT | wx.ICON_QUESTION)
        r = dial.ShowModal()

        if r == wx.ID_YES:
            sannotation = self.domainTab.domainLabelsList.GetSelection()
            name = self.domainTab.domainLabelsList.GetString(sannotation)
            self.am.domain.pop(name, None)
            self.domainTab.load_domain()
        elif r == wx.ID_NO:
            pass