-
Notifications
You must be signed in to change notification settings - Fork 0
/
pygame_trade.py
551 lines (482 loc) · 20.9 KB
/
pygame_trade.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
#! /usr/bin/env python
from collections import defaultdict
import pygame
import events
import catan
from pygame_utils import *
#------------------------------------------------------------------------------
class TradeButton(SimpleTextButton):
def __init__(self):
SimpleTextButton.__init__(self, (150,50), 'TRADE')
#----------------------------------------------------------------------
def onMouseLeftDown(self, pos):
if self.rect.collidepoint(pos):
if catan.game.state.stage != catan.Stages.playerTurn:
print "Can only trade during active player's turn"
return
events.post('ShowTrade')
#------------------------------------------------------------------------------
class QuitTradeButton(SimpleTextButton):
def __init__(self, pos):
SimpleTextButton.__init__(self, (100,12), 'QUIT TRADE')
self.rect.topleft = pos
def click(self):
events.post('HideTrade')
#------------------------------------------------------------------------------
class ProposalMatchButton(SimpleTextButton):
def __init__(self, parent, pos):
EasySprite.__init__(self)
Highlightable.__init__(self)
events.registerListener(self)
self.parent = parent
self.opponent = None
self.hidden = True
self.text = 'Match'
self.rect = pygame.Rect(pos[0], pos[1], 60,12)
self.image = EasySurface(self.rect.size)
self.draw()
def click(self):
if self.hidden:
return
if not (self.opponent and self.opponent.proposal):
return
self.parent.matchProposal(self.opponent.proposal)
#------------------------------------------------------------------------------
class MaritimeMatchButton(SimpleTextButton):
def __init__(self, parent, pos):
EasySprite.__init__(self)
Highlightable.__init__(self)
events.registerListener(self)
self.parent = parent
self.proposal = None
self.hidden = True
self.text = 'Match'
self.rect = pygame.Rect(pos[0], pos[1], 60,12)
self.image = EasySurface(self.rect.size)
self.draw()
def click(self):
print 'maritime match clicked'
if self.hidden:
return
if not self.proposal:
print 'maritime match no proposal'
return
print 'maritime match proposal match.'
self.parent.matchProposal(self.proposal)
#------------------------------------------------------------------------------
class ProposeConfirmButton(SimpleTextButton):
def __init__(self, pos):
EasySprite.__init__(self)
Highlightable.__init__(self)
events.registerListener(self)
self.opponent = None
self.proposal = None
self.hidden = True
self.text = 'Confirm'
self.rect = pygame.Rect(pos[0], pos[1], 60,12)
self.image = EasySurface(self.rect.size)
self.draw()
def click(self):
if self.hidden:
return
if not (self.opponent and self.proposal):
return
events.post('ConfirmProposalRequest', self.opponent, self.proposal)
#------------------------------------------------------------------------------
class MaritimeConfirmButton(SimpleTextButton):
def __init__(self, pos):
EasySprite.__init__(self)
Highlightable.__init__(self)
events.registerListener(self)
self.proposal = None
self.hidden = True
self.text = 'Confirm'
self.rect = pygame.Rect(pos[0], pos[1], 60,12)
self.image = EasySurface(self.rect.size)
self.draw()
def click(self):
if self.hidden:
return
if not self.proposal:
return
events.post('MaritimeTradeRequest', self.proposal)
#------------------------------------------------------------------------------
class TradeGiveButton(CardAddButton):
def __init__(self, parent, pos, cardClass):
CardAddButton.__init__(self, parent, pos, cardClass, symbol='^')
def click(self):
playerCardsOfClass = self.cardSubset()
self.parent.addCard(self.cardClass, playerCardsOfClass)
#------------------------------------------------------------------------------
class TradeTakeButton(CardAddButton):
def __init__(self, parent, pos, cardClass):
CardAddButton.__init__(self, parent, pos, cardClass, symbol='v')
def click(self):
self.parent.takeCard(self.cardClass)
#------------------------------------------------------------------------------
class TradeDisplay(EasySprite):
singleton_guard = False
def __init__(self, player):
assert TradeDisplay.singleton_guard == False, 'TODO: make this be safely ignored, not blow up'
TradeDisplay.singleton_guard = True
EasySprite.__init__(self)
events.registerListener(self)
self.image = EasySurface( (380,180) )
self.rect = self.image.get_rect()
self.player = player
# TODO: i'm not really liking these collections living here. I should
# probably just switch to inspecting human Player's .proposal
# {cardClass: [card1, card2, ...], ...}
self._cardsToGive = {}
# {cardClass1: 3, cardClass2: 1, ...}
self._cardClassesToTake = defaultdict(lambda:0)
self.giveButtons = {}
self.takeButtons = {}
# TODO: only show maritime buttons in the trade display if self.player
# is the activePlayer. Also, if this isn't the active player,
# only show activePlayer as a tradable opponent.
self.textButtons = [QuitTradeButton((190,160))]
self.matchButtons = [ ProposalMatchButton(self, (0,0)),
ProposalMatchButton(self, (0,0)),
ProposalMatchButton(self, (0,0)),]
self.confirmButtons = [ ProposeConfirmButton((0,0)),
ProposeConfirmButton((0,0)),
ProposeConfirmButton((0,0)),]
self.maritimeMatchButtons = [
MaritimeMatchButton(self, (0,0)),
MaritimeMatchButton(self, (0,0)),
MaritimeMatchButton(self, (0,0)),
MaritimeMatchButton(self, (0,0)),
MaritimeMatchButton(self, (0,0)),
MaritimeMatchButton(self, (0,0)),
]
self.maritimeConfirmButtons = [
MaritimeConfirmButton((0,0)),
MaritimeConfirmButton((0,0)),
MaritimeConfirmButton((0,0)),
MaritimeConfirmButton((0,0)),
MaritimeConfirmButton((0,0)),
MaritimeConfirmButton((0,0)),
MaritimeConfirmButton((0,0)),
]
self.drawBg()
self.drawCards()
self.drawOpponents()
self.drawButtons()
self.dirty = True
#----------------------------------------------------------------------
def reset(self):
# {cardClass: [card1, card2, ...], ...}
self._cardsToGive = {}
# {cardClass1: 3, cardClass2: 1, ...}
self._cardClassesToTake = defaultdict(lambda:0)
self.dirty = True
#----------------------------------------------------------------------
def propose(self):
cardClassesToGive = {}
for cardClass, cardList in self._cardsToGive.items():
cardClassesToGive[cardClass] = len(cardList)
print 'to give', cardClassesToGive
print 'to take', self._cardClassesToTake
events.post('ProposeTrade', self.player,
cardClassesToGive, self._cardClassesToTake)
#----------------------------------------------------------------------
def matchProposal(self, proposal):
'''Human player matches the proposal put forward by an opponent
(or by a maritime trade port)
'''
toGive, toTake = proposal
newCardsToGive = {}
newCardClassesToTake = defaultdict(lambda:0)
# TODO: asDict returns a dict of class=>list items because doing
# dict(group_cards(...)) makes all the items empty for some reason
cardDict = group_cards(self.player.cards, asDict=True)
print 'togive', toGive
print 'totake', toTake
print cardDict
try:
for cls in toTake:
matchingCards = cardDict[cls]
card = matchingCards.pop()
cardList = newCardsToGive.setdefault(cls, [])
cardList.append(card)
except (StopIteration, KeyError), ex:
print 'there werent enough cards of that class'
print 'ex', ex
print 'exargs', ex.args
return # there weren't enough cards of that class
for x in toGive:
#TODO: this is a hack until i standardize on proposal datastruct
if type(x) == type:
#it's a class
newCardClassesToTake[x] += 1
else:
newCardClassesToTake[x.__class__] += 1
self._cardsToGive = newCardsToGive
self._cardClassesToTake = newCardClassesToTake
self.propose()
self.dirty = True
#----------------------------------------------------------------------
def matchesProposal(self, proposal):
opponentGive, opponentTake = proposal
opponentGive = opponentGive[:] #copy
opponentTake = opponentTake[:] #copy
print '----'
#print 'op give', opponentGive
#print 'self take', self._cardClassesToTake
#print 'op take', opponentTake
#print 'self give', self._cardsToGive
try:
for cardClass in self._cardsToGive:
for card in self._cardsToGive[cardClass]:
opponentTake.remove(cardClass)
for cardClass, howMany in self._cardClassesToTake.items():
for i in range(howMany):
#TODO: this is a hack until i standardize on proposal datastruct
if type(opponentGive[0]) == type:
#it's a class
opponentGive.remove(cardClass)
else:
card = [x for x in opponentGive
if x.__class__ == cardClass][0]
opponentGive.remove(card)
except (ValueError, IndexError), ex:
print 'ValueError', ex, ex.args
return False
#print 'op give', opponentGive
#print 'op take', opponentTake
if opponentGive != [] or opponentTake != []:
return False
return True
#----------------------------------------------------------------------
def addCard(self, cardClass, playerCardsOfClass):
if self._cardClassesToTake.get(cardClass):
self._cardClassesToTake[cardClass] -= 1
else:
given = self._cardsToGive.get(cardClass, [])
ungiven = playerCardsOfClass.difference(set(given))
if not ungiven:
return
cardList = self._cardsToGive.setdefault(cardClass, [])
cardList.append(ungiven.pop())
self.propose()
self.dirty = True
#----------------------------------------------------------------------
def takeCard(self, cardClass):
if self._cardsToGive.get(cardClass):
self._cardsToGive[cardClass].pop()
if not self._cardsToGive[cardClass]:
del self._cardsToGive[cardClass]
else:
self._cardClassesToTake[cardClass] += 1
self.propose()
self.dirty = True
#----------------------------------------------------------------------
def drawBg(self):
self.image.fill( (0,0,20) )
r = self.rect.move(-self.x,-self.y)
pygame.draw.rect(self.image, blue, r, 8)
pygame.draw.rect(self.image, (200,200,255), r, 1)
#----------------------------------------------------------------------
def drawButtons(self):
for button in self.textButtons:
button.update()
self.image.blit(button.image, button.rect)
for button in self.giveButtons.values():
self.image.blit(button.image, button.rect)
cards = self._cardsToGive.get(button.cardClass, [])
howMany = len(cards)
txtImg = font_render(str(howMany))
pos = vect_add(button.rect.midtop, (0,-5))
self.image.blit(txtImg, pos)
for button in self.takeButtons.values():
self.image.blit(button.image, button.rect)
howMany = self._cardClassesToTake[button.cardClass]
txtImg = font_render(str(howMany))
pos = vect_add(button.rect.midbottom, (0,5))
self.image.blit(txtImg, pos)
#----------------------------------------------------------------------
def drawCards(self):
classes = [catan.Stone, catan.Brick, catan.Grain, catan.Sheep,
catan.Wood]
x = 40
y = 100
for cls in classes:
givePos = vect_add((x,y), (0, -25))
self.giveButtons[cls] = TradeGiveButton(self, givePos, cls)
takePos = vect_add((x,y), (0, 30))
self.takeButtons[cls] = TradeTakeButton(self, takePos, cls)
givenCards = self._cardsToGive.get(cls, [])
group = [card for card in self.player.cards
if isinstance(card, cls)
and card not in givenCards]
draw_cards(group, self.image, x, y, 2, 3, number=True)
x += 30
#----------------------------------------------------------------------
def drawOpponents(self):
opponents = catan.game.players[:]
opponents.remove(self.player)
padX, padY = 15, 5
for i, opponent in enumerate(opponents):
# draw the opponent's identifier
x = padX + 60*i
y = padY
txtImg = font_render(str(opponent.identifier),
color=opponent.color)
self.image.blit(txtImg, (x, y))
# draw the opponent's proposed trade
cButton = self.confirmButtons[i]
mButton = self.matchButtons[i]
if not (opponent.offer or opponent.wants):
cButton.hidden = True
mButton.hidden = True
continue
classes = [catan.Stone, catan.Brick, catan.Grain, catan.Sheep,
catan.Wood]
# draw "give" cards
cards = opponent.offer
cardGroups = group_cards(cards)
for cls, group in cardGroups:
xoffset = x + classes.index(cls)*8
draw_cards(group, self.image, xoffset, y+10, 0,0, number=True)
# draw "want" cards
cardClasses = opponent.wants
cards = [cls() for cls in cardClasses]
cardGroups = group_cards(cards)
for cls, group in cardGroups:
xoffset = x + classes.index(cls)*8
draw_cards(group, self.image, xoffset, y+5, 0,0, number=True)
# draw proposematch button
mButton.hidden = False
mButton.opponent = opponent
mButton.x = x
mButton.y = y+40
mButton.update()
self.image.blit(mButton.image, mButton.rect)
# draw proposeconfirm button
if self.matchesProposal(opponent.proposal):
cButton.hidden = False
cButton.opponent = opponent
cButton.proposal = opponent.proposal
cButton.x = x
cButton.y = y+52
cButton.update()
self.image.blit(cButton.image, cButton.rect)
#----------------------------------------------------------------------
def drawMaritime(self):
padX, padY = 215, 5
for i, port in enumerate(self.player.ports):
portCardClass, portAmt = port
x = padX + 60*i
y = padY
identifier = str(portAmt)+':1' # '4:1' or '2:1'
if portCardClass:
identifier = portCardClass.__name__[0] + identifier
txtImg = font_render(identifier, color=blue)
self.image.blit(txtImg, (x, y))
def findPlayerOffer(cards):
cardGroups = group_cards(cards)
for cls, group in cardGroups:
if portCardClass and cls != portCardClass:
continue
group = list(group)
if len(group) >= portAmt:
return group[:portAmt]
return []
# if player has offered some cards take the first group of "4"
playerOffer = findPlayerOffer(self.player.offer)
# if player has cards in their hand, take the first group of "4"
if not playerOffer:
playerOffer = findPlayerOffer(self.player.cards)
# draw the trader's proposed trade
cButton = self.maritimeConfirmButtons[i]
mButton = self.maritimeMatchButtons[i]
if not playerOffer:
cButton.hidden = True
continue
classes = [catan.Stone, catan.Brick, catan.Grain, catan.Sheep,
catan.Wood]
playerOfferClass = playerOffer[0].__class__
xoffset = x + classes.index(playerOfferClass)*8
draw_cards(playerOffer, self.image, xoffset, y+5, 0,0, number=True)
if not self.player.wants:
cButton.hidden = True
continue
traderOffer = []
for cls, howMany in self.player.wants.items():
if howMany:
traderOfferClass = cls
traderOffer = [cls()]
if traderOffer:
xoffset = x + classes.index(traderOfferClass)*8
draw_cards(traderOffer, self.image,
xoffset, y+10, 0,0, number=True)
proposal = [traderOffer, [card.__class__ for card in playerOffer]]
# draw proposematch button
mButton.hidden = False
mButton.proposal = proposal
mButton.x = x
mButton.y = y+40
mButton.update()
self.image.blit(mButton.image, mButton.rect)
# draw proposeconfirm button
if self.matchesProposal(proposal):
cButton.hidden = False
cButton.proposal = proposal
cButton.x = x
cButton.y = y+52
cButton.update()
self.image.blit(cButton.image, cButton.rect)
#----------------------------------------------------------------------
def update(self):
self.drawBg()
self.drawCards()
self.drawOpponents()
self.drawMaritime()
self.drawButtons()
self.dirty = False
#----------------------------------------------------------------------
def onProposeTrade(self, player, toGive, toTake):
self.dirty = True
#----------------------------------------------------------------------
def onConfirmProposal(self, *args):
self.reset()
#----------------------------------------------------------------------
def onMaritimeTrade(self, *args):
self.reset()
#----------------------------------------------------------------------
def onHideTrade(self):
TradeDisplay.singleton_guard = False
self.kill()
events.unregisterListener(self)
self.giveButtons = None
self.takeButtons = None
#----------------------------------------------------------------------
def onMouseMotion(self, pos, buttons):
if not self.rect.collidepoint(pos):
return
innerPos = vect_diff(pos, self.topleft)
for button in (self.giveButtons.values()
+ self.takeButtons.values()
+ self.textButtons
+ self.confirmButtons
+ self.maritimeConfirmButtons
+ self.maritimeMatchButtons
+ self.matchButtons):
if hasattr(button, 'onMouseMotion'):
button.onMouseMotion(innerPos, buttons)
#----------------------------------------------------------------------
def onMouseLeftDown(self, pos):
if not self.rect.collidepoint(pos):
return
self.dirty = True
innerPos = vect_diff(pos, self.topleft)
for button in (self.giveButtons.values()
+ self.takeButtons.values()
+ self.textButtons
+ self.confirmButtons
+ self.maritimeConfirmButtons
+ self.maritimeMatchButtons
+ self.matchButtons):
if button.rect.collidepoint(innerPos):
button.click()