source: exampleparties/stupidparty/stupidparty/StupidParty.py@ 88

Last change on this file since 88 was 88, checked in by Bart Vastenhouw, 3 years ago

Added python SimpleRunner GUI

File size: 2.8 KB
Line 
1import json
2import logging
3import sys
4from typing import Dict, Any
5from typing import cast
6
7from geniusweb.actions.Accept import Accept
8from geniusweb.actions.Action import Action
9from geniusweb.actions.Offer import Offer
10from geniusweb.actions.PartyId import PartyId
11from geniusweb.inform.ActionDone import ActionDone
12from geniusweb.inform.Finished import Finished
13from geniusweb.inform.Inform import Inform
14from geniusweb.inform.Settings import Settings
15from geniusweb.inform.YourTurn import YourTurn
16from geniusweb.issuevalue.Bid import Bid
17from geniusweb.party.Capabilities import Capabilities
18from geniusweb.party.DefaultParty import DefaultParty
19from geniusweb.utils import val
20
21
22class 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
Note: See TracBrowser for help on using the repository browser.