-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOzWeb500Server.py
executable file
·679 lines (624 loc) · 27.5 KB
/
OzWeb500Server.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
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
#!/usr/bin/python
# -*- coding: utf-8 -*-
import signal
import json
import time
import sys
import traceback
import threading
import re
import socket
import os
import random
print("Python v" + sys.version)
import logger
from SimpleWebSocketServer import WebSocket, SimpleWebSocketServer
import SimpleHTTPServer, SocketServer
from transitions import Machine
class Object(object): pass;
""" SETTINGS """
dealer_lag = 0.25
ws_port = 8000
http_port = 8001
http_root = "www/"
server_version = 0.1
server_hostname = socket.gethostname()
""" CARD RELATED CONSTANTS """
# SUIT index ranges from 1 to 5.
SUIT_str = ["Misere","Spades","Clubs","Diamonds","Hearts","No Trumps"]
SUIT_disp = [None,"♠","♣","♦","♥",""] # not displayable for python output.
SUIT_leftBower = [None,2,1,4,3,None] # left bower's suit
# RANK index ranges from 3 to 15.
RANK_str = [None,None,None,"3","4","5","6","7","8","9","10","Jack","Queen","King","Ace","Joker"]
RANK_disp = [None,None,None,"3","4","5","6","7","8","9","10","J","Q","K","A","Jok"]
NO_CARDS = False
FULL_DECK = True
class Client(object):
def __init__(self, conn):
self.connection = conn
self.username = None
self.latency = 0.0000
self.seat = None
self.hand = Deck(NO_CARDS)
def toJSONobj(self):
JSONobj = Object()
if self.connection:
JSONobj.connection = Object()
JSONobj.connection.host = self.connection.address[0]
JSONobj.connection.instance = self.connection.address[1]
if self.username:
JSONobj.username = self.username
if self.latency != 0.0000:
JSONobj.latency = self.latency
if self.seat:
JSONobj.seat = self.seat
return JSONobj
def logID(self):
if self.username:
return self.username
else:
return str(self.connection.address[0]) + '-' + str(self.connection.address[1])
class WS_Handler(WebSocket):
def sendData(self, message_type, message_data = None):
toSend = Object()
toSend.header = message_type
if message_data != None:
if isinstance(message_data, Object):
toSend.data = json.dumps(message_data, default=lambda o: o.__dict__, sort_keys=True)
else:
toSend.data = message_data
string = unicode(json.dumps(toSend.__dict__))
string = cleanJSONstring(string)
# print "outgoing", string
self.sendMessage(string)
def getClient(self):
for client in allClients():
if client.connection == self:
return client
return False
def getUsername(self):
for client in allClients():
if client.connection == self:
return client.username
return False
def setUsername(self, username):
for client in allClients():
if client.connection == self:
client.username = username
def handleMessage(self):
# print "incoming", self.data
try:
message = json.loads(self.data)
if message['header'] == "usernameRequest":
usernameRequest(self, message['data'])
elif message['header'] == "seatRequest":
seatRequest(self, message['data'])
elif message['header'] == "pong":
pong(self, message['data'])
elif message['header'] == "chat":
sendChatOut(self, message['data'])
else:
print "Unknown Message:", self.data
except: # catch *all* exceptions
e = traceback.format_exc()
f = sys.exc_info()
print "Error: " + str(e)
def handleConnected(self):
try:
newUser = Client(self)
clients.append(newUser)
logger.sockEntry(str(self.address[0]) + '-' + str(self.address[1]) + ': New socket connection.')
sendSeatsAvailability(self)
self.sendData("loginRequest")
self.sendData("serverVersion",server_version)
self.sendData("ping", logger.datetime_str())
except: # catch *all* exceptions
e = traceback.format_exc()
f = sys.exc_info()
print "Error: " + str(e)
def handleClose(self):
logger.sockEntry(str(self.address[0]) + '-' + str(self.address[1]) + ': Socket connection disconnected.')
sendLeftNotification(self)
if self.getClient().seat != None:
self.getClient().seat = None
clients.remove(self.getClient())
for client in allClients():
sendSeatsAvailability(client.connection)
sendUserList()
class HTTP_Handler(SimpleHTTPServer.SimpleHTTPRequestHandler):
def log_message(self, format, *args):
# override logging output
if "index.html" in str(format%args):
logger.httpEntry(str(self.address_string()) + " - " + str(format%args));
def translate_path(self, path):
path = http_root + path
return path
class Card(object):
def __init__(self, suit=1, rank=3):
self.suit = suit
self.rank = rank
def __str__(self):
if self.suit == 5:
return "The Joker"
else:
return RANK_str[self.rank] + " of " + SUIT_str[self.suit]
def __cmp__(self, other):
#print "self",str(self.suit),str(self.rank)
#print "other",str(other.suit),str(other.rank)
if self.rank == 15: return 1 # 1st is the Joker
if other.rank == 15: return -1 # 2nd is the Joker
if (self.suit == trumps and self.rank == 11): return 1 # 1st is the right bower
if (other.suit == trumps and other.rank == 11): return -1 # 2nd is the right bower
if (self.suit == SUIT_leftBower[trumps] and self.rank == 11): return 1 # 1st is the left bower
if (other.suit == SUIT_leftBower[trumps] and other.rank == 11): return -1 # 2nd is the left bower
if (self.suit == trumps and other.suit != trumps): return 1 # 1st only is on suit
if (other.suit == trumps and self.suit != trumps): return -1 # 2nd only is on suit
if cmp(self.suit, other.suit) == 0: # same suit -> highest wins
return cmp(self.rank, other.rank)
else:
return 0
def jsonStr(self):
return str(self.toJSONobj().__dict__)
def toJSONobj(self):
JSONobj = Object()
JSONobj.suit = self.suit
JSONobj.rank = self.rank
return JSONobj
class Deck(object):
def __init__(self, fill=True):
self.cards=[]
if fill:
for suit in range(1,3): # Create the black cards
for rank in range(4, 15):
new_card = Card(suit, rank)
self.cards.append(new_card)
for suit in range(3,5): # Create the red cards
for rank in range(5, 15):
new_card = Card(suit, rank)
self.cards.append(new_card)
new_card = Card(5, 15) # create the Joker
self.cards.append(new_card)
def __str__(self):
string = ""
for card in self.cards:
string = string + str(card) + "\n"
return string[:-1]
def addCard(self, card):
self.cards.append(card)
def removeCard(self, card):
self.cards.remove(card)
def removeCardIndex(self, index):
del self.cards[index]
def popCard(self, i=-1):
return self.cards.pop(i)
def moveCards(self, otherDeck, num):
for i in range(num):
otherDeck.addCard(self.popCard())
def shuffle(self):
random.shuffle(self.cards)
def getIndex(self, findcard):
for index,card in enumerate(self.cards):
if card == findcard:
return index
return -1
def winningIndex(self):
tempCards = self.cards
tempCards.sort()
if tempCards[-1] > tempCards[-2]:
return self.getIndex(tempCards[-1])
return -1
def jsonStr(self):
str(self.toJSONobj().cards.__dict__)
def toJSONobj(self):
JSONobj = Object()
JSONobj.cards = []
for card in self.cards:
JSONobj.cards.insert(0,card.toJSONobj())
return JSONobj
def countCards(self):
return len(self.cards)
def sort(self, trumps):
debug = 0
if debug: print "'" + SUIT_str[trumps] + "' is trumps.\n"
for reducing_range in range(len(self.cards)):
for card_index in range(len(self.cards)-1-reducing_range):
c1 = self.cards[card_index]
c2 = self.cards[card_index + 1]
if debug: print "card " + str(card_index) + ": " + str(c1) + " vs. card " + str(card_index+1) + ": " + str(c2)
if trumps == 5: # No trumps
if c2.rank == 15: # Joker needs bumping up
self.cards[card_index], self.cards[card_index + 1] = self.cards[card_index + 1], self.cards[card_index]
if debug: print str(c1) + " pushed down (Joker wins)."
elif c1.rank == 15:
None # keep the Joker safe
elif c2.suit > c1.suit: # second card's suit is larger, push first down
self.cards[card_index], self.cards[card_index + 1] = self.cards[card_index + 1], self.cards[card_index]
if debug: print str(c1) + " pushed down (Suit wins)."
elif c2.suit == c1.suit and c2.rank > c1.rank: # second card's suit is same but rank is higher, push first down
self.cards[card_index], self.cards[card_index + 1] = self.cards[card_index + 1], self.cards[card_index]
if debug: print str(c1) + " pushed down (Rank wins)."
elif trumps == 4: # Hearts is trumps.
if c2.rank == 15: # Joker needs bumping up
self.cards[card_index], self.cards[card_index + 1] = self.cards[card_index + 1], self.cards[card_index]
if debug: print str(c1) + " pushed down (Joker wins)."
elif c1.rank == 15:
None # keep the Joker safe
elif c2.rank == 11 and c2.suit == trumps: # second card is the right bower, push first down
self.cards[card_index], self.cards[card_index + 1] = self.cards[card_index + 1], self.cards[card_index]
if debug: print str(c1) + " pushed down (Right Bower wins)."
elif c1.rank == 11 and c1.suit == trumps:
None
elif c2.rank == 11 and c2.suit == 3: # second card is the left bower, push first down
self.cards[card_index], self.cards[card_index + 1] = self.cards[card_index + 1], self.cards[card_index]
if debug: print str(c1) + " pushed down (Left Bower wins)."
elif c1.rank == 11 and c1.suit == 3:
None
elif c2.suit == trumps and c1.suit != trumps: # second card's suit is a trump, but first's isn't, push first down
self.cards[card_index], self.cards[card_index + 1] = self.cards[card_index + 1], self.cards[card_index]
if debug: print str(c1) + " pushed down (Trumps Suit wins)."
elif c2.suit > c1.suit and c1.suit != trumps: # second card's suit is larger, push first down
self.cards[card_index], self.cards[card_index + 1] = self.cards[card_index + 1], self.cards[card_index]
if debug: print str(c1) + " pushed down (Suit wins)."
elif c2.suit == c1.suit and c2.rank > c1.rank: # second card's suit is same but rank is higher, push first down
self.cards[card_index], self.cards[card_index + 1] = self.cards[card_index + 1], self.cards[card_index]
if debug: print str(c1) + " pushed down (Rank wins)."
elif trumps == 3: # Diamonds is trumps.
if c2.rank == 15: # Joker needs bumping up
self.cards[card_index], self.cards[card_index + 1] = self.cards[card_index + 1], self.cards[card_index]
if debug: print str(c1) + " pushed down (Joker wins)."
elif c1.rank == 15:
None # keep the Joker safe
elif c2.rank == 11 and c2.suit == trumps: # second card is the right bower, push first down
self.cards[card_index], self.cards[card_index + 1] = self.cards[card_index + 1], self.cards[card_index]
if debug: print str(c1) + " pushed down (Right Bower wins)."
elif c1.rank == 11 and c1.suit == trumps:
None
elif c2.rank == 11 and c2.suit == 4: # second card is the left bower, push first down
self.cards[card_index], self.cards[card_index + 1] = self.cards[card_index + 1], self.cards[card_index]
if debug: print str(c1) + " pushed down (Left Bower wins)."
elif c1.rank == 11 and c1.suit == 4:
None
elif c2.suit == trumps and c1.suit != trumps: # second card's suit is a trump, but first's isn't, push first down
self.cards[card_index], self.cards[card_index + 1] = self.cards[card_index + 1], self.cards[card_index]
if debug: print str(c1) + " pushed down (Trumps Suit wins)."
elif c2.suit > c1.suit and c1.suit != trumps: # second card's suit is larger, push first down
self.cards[card_index], self.cards[card_index + 1] = self.cards[card_index + 1], self.cards[card_index]
if debug: print str(c1) + " pushed down (Suit wins)."
elif c2.suit == c1.suit and c2.rank > c1.rank: # second card's suit is same but rank is higher, push first down
self.cards[card_index], self.cards[card_index + 1] = self.cards[card_index + 1], self.cards[card_index]
if debug: print str(c1) + " pushed down (Rank wins)."
elif trumps == 2: # Clubs is trumps.
if c2.rank == 15: # Joker needs bumping up
self.cards[card_index], self.cards[card_index + 1] = self.cards[card_index + 1], self.cards[card_index]
if debug: print str(c1) + " pushed down (Joker wins)."
elif c1.rank == 15:
None # keep the Joker safe
elif c2.rank == 11 and c2.suit == trumps: # second card is the right bower, push first down
self.cards[card_index], self.cards[card_index + 1] = self.cards[card_index + 1], self.cards[card_index]
if debug: print str(c1) + " pushed down (Right Bower wins)."
elif c1.rank == 11 and c1.suit == trumps:
None ## keep the
elif c2.rank == 11 and c2.suit == 1: # second card is the left bower, push first down
self.cards[card_index], self.cards[card_index + 1] = self.cards[card_index + 1], self.cards[card_index]
if debug: print str(c1) + " pushed down (Left Bower wins)."
elif c1.rank == 11 and c1.suit == 1:
None
elif c2.suit == trumps and c1.suit != trumps: # second card's suit is a trump, but first's isn't, push first down
self.cards[card_index], self.cards[card_index + 1] = self.cards[card_index + 1], self.cards[card_index]
if debug: print str(c1) + " pushed down (Trumps Suit wins)."
elif c2.suit > c1.suit and c1.suit != trumps: # second card's suit is larger, push first down
self.cards[card_index], self.cards[card_index + 1] = self.cards[card_index + 1], self.cards[card_index]
if debug: print str(c1) + " pushed down (Suit wins)."
elif c2.suit == c1.suit and c2.rank > c1.rank: # second card's suit is same but rank is higher, push first down
self.cards[card_index], self.cards[card_index + 1] = self.cards[card_index + 1], self.cards[card_index]
if debug: print str(c1) + " pushed down (Rank wins)."
elif trumps == 1: # Spades is trumps.
if c2.rank == 15: # Joker needs bumping up
self.cards[card_index], self.cards[card_index + 1] = self.cards[card_index + 1], self.cards[card_index]
if debug: print str(c1) + " pushed down (Joker wins)."
elif c1.rank == 15:
None # keep the Joker safe
elif c2.rank == 11 and c2.suit == trumps: # second card is the right bower, push first down
self.cards[card_index], self.cards[card_index + 1] = self.cards[card_index + 1], self.cards[card_index]
if debug: print str(c1) + " pushed down (Right Bower wins)."
elif c1.rank == 11 and c1.suit == trumps:
None
elif c2.rank == 11 and c2.suit == 2: # second card is the left bower, push first down
self.cards[card_index], self.cards[card_index + 1] = self.cards[card_index + 1], self.cards[card_index]
if debug: print str(c1) + " pushed down (Left Bower wins)."
elif c1.rank == 11 and c1.suit == 2:
None
elif c2.suit == trumps and c1.suit != trumps: # second card's suit is a trump, but first's isn't, push first down
self.cards[card_index], self.cards[card_index + 1] = self.cards[card_index + 1], self.cards[card_index]
if debug: print str(c1) + " pushed down (Trumps Suit wins)."
elif c2.suit > c1.suit and c1.suit != trumps: # second card's suit is larger, push first down
self.cards[card_index], self.cards[card_index + 1] = self.cards[card_index + 1], self.cards[card_index]
if debug: print str(c1) + " pushed down (Suit wins)."
elif c2.suit == c1.suit and c2.rank > c1.rank: # second card's suit is same but rank is higher, push first down
self.cards[card_index], self.cards[card_index + 1] = self.cards[card_index + 1], self.cards[card_index]
if debug: print str(c1) + " pushed down (Rank wins)."
elif trumps == 0: # Misere
None
# TO BE IMPLEMENTED
if debug: print " "
# ## TEST ##
# trumps = 0
# deck_1 = Deck(FULL_DECK)
# deck_1.shuffle()
# deck_2 = Deck(NO_CARDS)
# deck_1.moveCards(deck_2, 43)
# # print " "
# # print deck_2
# print " "
# deck_2.sort(trumps)
# print deck_2
# ## END TEST ##
# This is the game state machine.
# It will be updated as each element of the game is implemented.
# Not currently integrated with the WS logic.
# Uses the transitions library
# https://github.com/tyarkoni/transitions
# pip install transitions
class GameMachine(Machine):
# DEFINITIONS
# hand: one game (10 tricks), at the end of which the score is awarded
# bids: the potential contract that the player is agreeing to achieve (eg: 6 Hearts)
# kitty: three cards, the contract winner receives before the hand starts
# trick: a round of the game, where each player places a card on the table
dealerButton = 0 # player index who is the current 'dealer'. Initially random.
dealerFocus = 0 # this is the player the dealer is waiting on or is dealing to.
game_deck = Deck(FULL_DECK)
kitty = Deck(NO_CARDS)
states = [
'accepting players',
'accepting bids',
'winners lead',
'finishing trick',
'assessing the trick',
'scoring the hand'
]
transitions = [
{'trigger':'deal', 'source':['accepting players','accepting bids'], 'dest':'accepting bids', 'conditions':'four_players_seated', 'before': 'deal_cards'},
{'trigger':'award_kitty', 'source':'accepting bids', 'dest':'winners lead', 'conditions':'winning_bid', 'before': 'award_kitty', 'after': 'ask_for_discard'},
]
## Conditions ##
def four_players_seated(self):
if seat2client(1) and seat2client(2) and seat2client(3) and seat2client(4):
logger.gameEntry("Four players are sitting.")
return True
else:
logger.gameEntry("Waiting for more players.")
return False
def winning_bid(self): return self.transit
def kittyThrown(self): return self.transit
def scoreOver500(self): return self.transit
## Utilities ##
def incrementDealerFocus(self):
self.dealerFocus = ((self.dealerFocus) % 4) + 1
## Doers ##
def ask_for_discard(self):
None
def stop_playing(self):
logger.gameEntry("A player left :(")
def deal_cards(self):
# this is threaded as there are blocking delays involved.
# The state machine will progress on completion of the thread.
def deal_thread():
# if a player drops during this, it hurts.
logger.gameEntry("Emptying the player's hands and kitty.")
for seat in xrange(1, 5):
seat2client(seat).hand = Deck(NO_CARDS)
self.kitty = Deck(NO_CARDS)
logger.gameEntry("Initialising and shuffling the game deck.")
self.game_deck = Deck(FULL_DECK)
sendCardUpdate()
self.game_deck.shuffle()
# if the first deal, randomise the dealer
if self.dealerButton == 0:
self.dealerButton = random.randint(1, 4)
# dealer focus is next player for deal
self.dealerFocus = self.dealerButton
self.incrementDealerFocus()
sendDealerNotification("Dealing for " + seat2client(self.dealerButton).username + " to " + seat2client(self.dealerFocus).username + ".")
sendCardUpdate()
for cards_to_deal in [3, 4, 3]:
for i in range(4):
for j in range(cards_to_deal):
self.game_deck.moveCards(seat2client(self.dealerFocus).hand, 1)
sendCardUpdate()
logger.gameEntry("Just dealt to " + seat2client(self.dealerFocus).username + ".")
time.sleep(0.25)
self.incrementDealerFocus()
self.game_deck.moveCards(self.kitty, 1)
sendCardUpdate()
time.sleep(0.25)
for i in range(1,5):
seat2client(i).hand.sort(trumps)
sendCardUpdate()
self.ask_for_bid()
t = threading.Thread(target=deal_thread, args=[])
t.start()
def ask_for_bid(self):
sendDealerNotification(seat2client(self.dealerFocus).username +"'s bid.")
# Need to rearrange the table depending on player position
# Need to check the order of dealing
def __init__(self):
dealerButton = random.randint(1, 4)
self.transit = False
Machine.__init__(self, states=self.states, transitions=self.transitions, initial=self.states[0])
def __str__(self): return "GameMachine state is '" + self.state + "'."
def allClients():
clientlist = []
for client in list(clients):
clientlist.append(client)
return clientlist
def allUsers():
userlist = []
for client in list(clients):
if client.username:
userlist.append(client)
return list(userlist)
def seatedUsers():
userlist = []
for client in list(clients):
if client.seat != None:
userlist.append(client)
return list(userlist)
def seat2client(seat):
for client in seatedUsers():
if client.seat == seat:
return client
return None
def username2client(username):
for client in list(clients):
if client.username == username:
return client
return None
def usernameRequest(conn, username):
if username2client(username):
conn.sendData("usernameExists")
logger.sockEntry(conn.getClient().logID() + ': Username request denied - exists (' + username + ').')
else:
logger.sockEntry(conn.getClient().logID() + ': Username accepted ('+username +').')
conn.setUsername(username)
conn.sendData("loginAccepted", username)
sendClientData(conn)
sendUserList()
sendSeatsAvailability(conn)
sendLoginNotification(conn)
sendCardUpdate()
def seatRequest(conn, seat):
if seat2client(seat):
conn.sendData("seatTaken")
logger.sockEntry(conn.getClient().username + ': Seat '+ str(seat) +' request denied - already taken.')
sendSeatsAvailability(conn)
else:
conn.getClient().seat = seat
conn.sendData("seatAccepted", seat)
sendClientData(conn)
logger.sockEntry(conn.getClient().username + ' sat in seat '+ str(seat) +'.')
for client in allClients():
sendSeatsAvailability(client.connection)
thisGame.deal()
def pong(conn, pingStampStr):
conn.getClient().latency = logger.secondsSinceDateTimeStr(pingStampStr)
sendClientData(conn)
def sendChatOut(conn, msg):
chatObject = Object()
chatObject.fromUser = conn.getUsername()
chatObject.message = msg
if conn.getUsername():
# logger.sockEntry(conn.getUsername() + ' said: ' + chatObject.message)
for client in allClients():
client.connection.sendData("chatMessage",chatObject)
def sendLoginNotification(conn):
notificationObject = Object()
notificationObject.str = conn.getUsername() + " logged in."
for client in allClients():
if client.connection != conn:
client.connection.sendData("notification",notificationObject)
def sendLeftNotification(conn):
notificationObject = Object()
notificationObject.str = conn.getUsername() + " left."
for client in allClients():
if client.connection != conn:
client.connection.sendData("notification",notificationObject)
def sendSeatsAvailability(conn):
seats = [None,None,None,None,None]
for client in seatedUsers():
seats[client.seat] = client.username
conn.sendData("seatsAvailability", seats)
def sendCardUpdate():
previousSeat = [None,4,1,2,3]
nextSeat = [None,2,3,4,1]
partnerSeat = [None,3,4,1,2]
for seat in range(1,5):
cardUpdateObject = Object()
if len(seatedUsers()) < 4:
cardUpdateObject.myHand = Object()
cardUpdateObject.myHand.hand = []
cardUpdateObject.previousHand = 0
cardUpdateObject.nextHand = 0
cardUpdateObject.partnerHand = 0
cardUpdateObject.kitty = 0
cardUpdateObject.deck = 0
else:
cardUpdateObject.myHand = seat2client(seat).hand.toJSONobj()
cardUpdateObject.previousHand = seat2client(previousSeat[seat]).hand.countCards()
cardUpdateObject.nextHand = seat2client(nextSeat[seat]).hand.countCards()
cardUpdateObject.partnerHand = seat2client(partnerSeat[seat]).hand.countCards()
cardUpdateObject.kitty = thisGame.kitty.countCards()
cardUpdateObject.deck = thisGame.game_deck.countCards()
#cardUpdateObject.previousHandPeak = seat2client(previousSeat[seat]).hand.toJSONobj()
#cardUpdateObject.nextHandPeak = seat2client(nextSeat[seat]).hand.toJSONobj()
#cardUpdateObject.partnerHandPeak = seat2client(partnerSeat[seat]).hand.toJSONobj()
#cardUpdateObject.kittyPeak = thisGame.kitty.toJSONobj()
#cardUpdateObject.deckPeak = thisGame.game_deck.toJSONobj()
seat2client(seat).connection.sendData("cardUpdate",cardUpdateObject)
def sendClientData(conn):
userdata = conn.getClient().toJSONobj()
conn.sendData("clientData", userdata)
def cleanJSONstring(string):
""" These are regular expressions to correct the format for send client objects """
string = re.sub(r'"{', r"{", string);
string = re.sub(r'}"', r"}", string);
string = re.sub(r' \\"', r' "', string);
string = re.sub(r'{\\"', r'{"', string);
string = re.sub(r'\\"}', r'"}', string);
string = re.sub(r'\\":', r'":', string);
string = re.sub(r'\\",', r'",', string);
#string = re.sub(r"': u'", r"': '", string);
#string = re.sub(r"(<__[^>]*>)", r"'\1'", string);
#string = re.sub(r"'", r'"', string)
#string = re.sub(r'": "{"', r'": {"', string)
#string = re.sub(r'}"', "}", string)
#string = re.sub(r': None', ": null", string)
return string
def sendUserList():
userList = []
for client in list(clients):
if client.username != "":
userList.append(client.username)
for client in list(clients):
if client.username != "":
client.connection.sendData("userList", sorted(userList))
def sendDealerNotification(msg):
notificationObject = Object()
notificationObject.str = msg
for client in allClients():
client.connection.sendData("notification",notificationObject)
logger.gameEntry("Dealer: " + msg)
ws_handler = WS_Handler
ws_server = SimpleWebSocketServer('', ws_port, ws_handler)
ws_thread = threading.Thread(target = ws_server.serveforever)
ws_thread.deamon = True
http_handler = HTTP_Handler
SocketServer.TCPServer.allow_reuse_address = True
http_server = SocketServer.TCPServer(("", http_port), http_handler)
http_thread = threading.Thread(target = http_server.serve_forever)
http_thread.deamon = True
trumps = 5 # no trumps
clients = []
thisGame = GameMachine()
if __name__ == "__main__":
logger.mainEntry("OzWeb500Server launched.")
logger.mainEntry("Client available at: http://" + server_hostname + ":" + str(http_port))
ws_thread.start()
logger.sockEntry("Web Socket Server started.")
http_thread.start()
logger.httpEntry("HTTP Server started.")
def close_sig_handler(signal, frame):
print " "
logger.mainEntry("OzWeb500Server terminating.")
http_server.shutdown()
logger.httpEntry("HTTP Server stopped.")
ws_server.close()
logger.sockEntry("Web Socket Server stopped.")
sys.exit()
signal.signal(signal.SIGINT, close_sig_handler)
while True:
logger.gameEntry("Game State is '" + thisGame.state + "'")
logger.mainEntry("Pinging Clients.")
for client in list(clients):
client.connection.sendData("ping", logger.datetime_str())
time.sleep(300)