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