[59] | 1 | import json
|
---|
| 2 | import logging
|
---|
| 3 | import sys
|
---|
| 4 | from typing import Dict, Any
|
---|
| 5 | from typing import cast
|
---|
| 6 |
|
---|
| 7 | from geniusweb.actions.Accept import Accept
|
---|
| 8 | from geniusweb.actions.Action import Action
|
---|
| 9 | from geniusweb.actions.Offer import Offer
|
---|
| 10 | from geniusweb.actions.PartyId import PartyId
|
---|
| 11 | from geniusweb.inform.ActionDone import ActionDone
|
---|
| 12 | from geniusweb.inform.Finished import Finished
|
---|
| 13 | from geniusweb.inform.Inform import Inform
|
---|
| 14 | from geniusweb.inform.Settings import Settings
|
---|
| 15 | from geniusweb.inform.YourTurn import YourTurn
|
---|
| 16 | from geniusweb.issuevalue.Bid import Bid
|
---|
| 17 | from geniusweb.party.Capabilities import Capabilities
|
---|
| 18 | from geniusweb.party.DefaultParty import DefaultParty
|
---|
| 19 |
|
---|
| 20 |
|
---|
| 21 | class StupidParty (DefaultParty):
|
---|
| 22 | """
|
---|
| 23 | A Stupid party that places empty bids because it can't download the profile,
|
---|
| 24 | and accepts the first incoming offer.
|
---|
| 25 | """
|
---|
| 26 | def __init__(self):
|
---|
| 27 | super().__init__()
|
---|
| 28 | self.getReporter().log(logging.INFO,"party is initialized")
|
---|
| 29 | self._profile = None
|
---|
| 30 | self._lastReceivedBid:Bid = None
|
---|
| 31 |
|
---|
| 32 | # Override
|
---|
| 33 | def notifyChange(self, info: Inform):
|
---|
| 34 | #self.getReporter().log(logging.INFO,"received info:"+str(info))
|
---|
| 35 | if isinstance(info,Settings) :
|
---|
| 36 | settings:Settings=cast(Settings,info)
|
---|
| 37 | self._me = settings.getID()
|
---|
| 38 | self._protocol = settings.getProtocol()
|
---|
| 39 | elif isinstance(info, ActionDone):
|
---|
| 40 | action:Action=cast( ActionDone,info).getAction()
|
---|
| 41 | if isinstance(action, Offer):
|
---|
| 42 | self._lastReceivedBid = cast(Offer, action).getBid()
|
---|
| 43 | elif isinstance(info, YourTurn):
|
---|
| 44 | # This is a stupid party
|
---|
| 45 | if self._lastReceivedBid!=None:
|
---|
| 46 | self.getReporter().log(logging.INFO,"sending accept:")
|
---|
| 47 | accept = Accept(self._me, self._lastReceivedBid)
|
---|
| 48 | self.getConnection().send(accept)
|
---|
| 49 | else:
|
---|
| 50 | # We have no clue about our profile
|
---|
| 51 | offer:Offer = Offer(self._me, Bid({}))
|
---|
| 52 | self.getReporter().log(logging.INFO,"sending empty offer:")
|
---|
| 53 | self.getConnection().send(offer)
|
---|
| 54 | self.getReporter().log(logging.INFO,"sent empty offer:")
|
---|
| 55 | elif isinstance(info, Finished):
|
---|
| 56 | self.terminate()
|
---|
| 57 | else:
|
---|
| 58 | self.getReporter().log(logging.WARNING, "Ignoring unknown info "+str(info))
|
---|
| 59 |
|
---|
| 60 |
|
---|
| 61 | # Override
|
---|
| 62 | def getCapabilities(self): # -> Capabilities
|
---|
| 63 | return Capabilities( set([ "SAOP"]), set(['geniusweb.profile.utilityspace.LinearAdditive']))
|
---|
| 64 |
|
---|
| 65 | # Override
|
---|
| 66 | def getDescription(self):
|
---|
| 67 | return "Offers null bids and accept other party's first bid"
|
---|
| 68 |
|
---|
| 69 | # Override
|
---|
| 70 | def terminate(self):
|
---|
| 71 | self.getReporter().log(logging.INFO,"party is terminating:")
|
---|
| 72 | super().terminate()
|
---|
| 73 | if self._profile != None:
|
---|
| 74 | self._profile.close()
|
---|
| 75 | self._profile = None
|
---|
| 76 |
|
---|