forked from jitendra1998/headshot
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mod_func.py
695 lines (600 loc) · 32.8 KB
/
mod_func.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
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
from helperfunctions import *
from common import *
from mysql_configure import MYSQLConfiguration
from banner_api import BannerStats, BannerDict
from game_room_api import *
import math
import random
import redis as rd
import json
r = rd.StrictRedis('localhost',db=4)
red = rd.StrictRedis('localhost',db=4)
req = {}
obj_help = HelpingFunctions()
#call this function after every round is completed, it's supposed to initialize the pot_amount, select dealer, get question
class GameActions(object):
def start_set(self, message, clients):
round_no =2
global req
req = {}
# ans_submit =0
# print message,type(message)
if isinstance(message,str):
mass_dec = json.loads(message)
else:
mass_dec = message
idle_gameroom = json.loads(r.get('active_set'))[str(mass_dec['game_room_id'])]['idle_gameroom']
player_list =[]
usr = json.loads(r.get('user_session').encode('utf-8'))
# clients = json.loads('active_set')[str(mass_dec['game_room_id'])]['clients']
oclients = clients
clients = clients[str(mass_dec['game_room_id'])].values()
# all_in_list=[]
# check for ad banners
banner_dict = BannerDict()
banners = banner_dict.get_banner_dict()
for banner in banners:
if str(mass_dec['game_room_id']) in banners[banner]['gamerooms']:
this_banner = BannerStats(banner)
this_banner = this_banner.get_banner_stats()
# send ads position on websocket
message_for_banner = {
'game_key':'banner_check',
'position': banners[banner]['gamerooms'][str(mass_dec['game_room_id'])],
'link': this_banner['link']
}
for client in clients:
client.write_message(json.dumps(message_for_banner))
usr_ult = {}
for i, j in usr.iteritems():
usr_ult[j] = i
#create a random player sequence, with the sitted player data
sitted_clients = json.loads(r.get('sitted_clients'))[str(mass_dec['game_room_id'])]
for key in sitted_clients:
if sitted_clients[key]!='-1':
for i,j in usr.iteritems():
if i == sitted_clients[key]:
player_list.append(j)
active_set = json.loads(r.get('active_set'))[str(mass_dec['game_room_id'])]
player_list = [ele for ele in player_list if ele in active_set['total_amount'].keys()]
# if len(player_list) > 1:
print idle_gameroom, len(json.loads(r.get('active_set'))[str(mass_dec['game_room_id'])]['total_amount'])
if len(json.loads(r.get('active_set'))[str(mass_dec['game_room_id'])]['total_amount'])>1 and idle_gameroom:
# idle_gameroom = False
opp = json.loads(r.get('active_set'))
opp[str(mass_dec['game_room_id'])]['idle_gameroom'] = False
r.set('active_set', json.dumps(opp))
obj_help.select_player(player_list,str(mass_dec['game_room_id']))
player_seq = obj_help.redis_fetch('shuffled_active_players')
obj_help.start_game(str(mass_dec['game_room_id']))
for i,j in sitted_clients.iteritems():
for k,l in usr.iteritems():
if player_seq[str(mass_dec['game_room_id'])][3%len(player_seq[str(mass_dec['game_room_id'])])] == l:
if k==j :
mass_dec['dealer_seat'] = i
mass_dec['message'] = j
mass_dec['game_key'] = '4'
mass_dec['question_no'] = randint(1,1000)
usr = json.loads(r.get('user_session').encode('utf-8'))
active_set = json.loads(r.get('active_set'))[str(mass_dec['game_room_id'])]
call_amount = float(active_set['current_max_bet']) - float(active_set['player_bet'][usr[sitted_clients[mass_dec['dealer_seat']]]])
bet_amount = 2 * float(active_set['current_max_bet']) - float(active_set['player_bet'][usr[sitted_clients[mass_dec['dealer_seat']]]])
#defining total amount
total_amount = {}
for key,value in active_set['total_amount'].iteritems():
total_amount[usr_ult[key]] = value
# updating call and bet amount
mass_dec['call_amount'] = call_amount
mass_dec['bet_amount'] = bet_amount
mass_dec['next_user_amount'] = active_set['total_amount'][usr[sitted_clients[mass_dec['dealer_seat']]]]
# put profile photos for sitted clients
profile_dict = json.loads(r.get('profile'))
user_amount = {}
for ids in active_set['total_amount']:
if ids in usr_ult:
for seat in sitted_clients:
if usr_ult[ids] == sitted_clients[seat]:
user_amount[seat] = active_set['total_amount'][ids]
print user_amount ,"THis is user-amount"
if str(mass_dec['game_room_id']) in profile_dict.keys():
profile_dict = json.loads(r.get('profile'))[str(mass_dec['game_room_id'])]
else:
profile_dict = {}
active_player_list = obj_help.redis_fetch('shuffled_active_players')[str(mass_dec['game_room_id'])]
print 'This is sitted Clients'
print sitted_clients.keys()[sitted_clients.values().index(usr_ult[active_player_list[1%len(active_player_list)]])]
print 'This is sitted Clients'
sb_amount = obj_help.redis_fetch('active_set')[str(mass_dec['game_room_id'])]['small_blind']
small_blind = sitted_clients.keys()[sitted_clients.values().index(usr_ult[active_player_list[1%len(active_player_list)]])]
bb_amount = obj_help.redis_fetch('active_set')[str(mass_dec['game_room_id'])]['big_blind']
big_blind = sitted_clients.keys()[sitted_clients.values().index(usr_ult[active_player_list[2 % len(active_player_list)]])]
mass_dec['small_blind'] = small_blind
mass_dec['sb_amount'] = sb_amount
mass_dec['big_blind'] = big_blind
mass_dec['bb_amount'] = bb_amount
mass_dec['user_amount']= user_amount
mess = {
'game_key': "10",
'profile': profile_dict,
'sitted_clients': sitted_clients,
'total_amount': total_amount,
'small_blind':small_blind,
'big_blind':big_blind,
'sb_amount':sb_amount,
'bb_amount':bb_amount,
'user_amount': user_amount
}
update_call_bet_message = {
'game_key': 'update_call_bet',
'call_amount': call_amount,
'bet_amount': bet_amount
}
for client in clients:
client.write_message(json.dumps(mass_dec))
client.write_message(json.dumps(mess))
client.write_message(json.dumps(update_call_bet_message))
game_room_id = mass_dec['game_room_id']
#get a random question based on theme and sitted player data
print active_set, 'This is mass Dec'
req_success =False
while req_success == False:
req = json.loads(requests.post('http://localhost:8088/question', data = {'Game_Room_id': json.dumps({game_room_id:player_seq}), 'theme': active_set['game_category'] }).text)
# req = json.loads(req.text)
if str("success") in req:
req_success = True
opp = json.loads(r.get('active_set'))
opp[str(mass_dec['game_room_id'])]['req'] = req
opp[str(mass_dec['game_room_id'])]['all_in_list'] = []
opp[str(mass_dec['game_room_id'])]['round_no'] = round_no
opp[str(mass_dec['game_room_id'])]['ans_submit'] = 0
r.set('active_set',json.dumps(opp))
# r.set('question_data', json.dumps(req))
#write the first hint once the game starts
mass_dec['game_key'] = '6'
mass_dec['con_id'] = 'cat_1'
mass_dec['con'] = req['success']['data']['d1'][1]
active_set = json.loads(r.get('active_set'))[str(mass_dec['game_room_id'])]
mass_dec['pot_amount'] = active_set['pot_amount']
for client in clients:
client.write_message(json.dumps(mass_dec))
active_player_list = obj_help.redis_fetch('shuffled_active_players')[str(mass_dec['game_room_id'])]
small_blind_notification = json.dumps({
'game_key': '1',
'user_name': usr_ult[active_player_list[1%len(active_player_list)]],
'message': "Played Small Blind " + str(active_set['small_blind']) + u"\u20ac"
})
big_blind_notification = json.dumps({
'game_key': '1',
'user_name': usr_ult[active_player_list[2%len(active_player_list)]],
'message':"Played Big Blind " + str(active_set['big_blind']) + u"\u20ac"
})
for client in clients:
client.write_message(small_blind_notification)
client.write_message(big_blind_notification)
else:
# idle_gameroom = True
print "this is waiting"
opp = json.loads(r.get('active_set'))
opp[str(mass_dec['game_room_id'])]['idle_gameroom'] = True
r.set('active_set', json.dumps(opp))
waiting_message = json.dumps({
'game_key': '1',
'user_name': "Waiting for players",
'message':"to join"
})
for client in clients:
client.write_message(waiting_message)
if len(json.loads(r.get('active_set'))[str(mass_dec['game_room_id'])]['total_amount'])>1:
self.start_set(message, oclients)
#self.sit_out modular function
def sit_out(self, message, clients):
#self.self.sit_out init
mass_dec = json.loads(message)
# clients = json.loads('active_set')[str(mass_dec['game_room_id'])]['clients']
clients = clients[str(mass_dec['game_room_id'])].values()
global global_id
global_id = None
#when a players sits out, information is again updated in redis active_set table, balance is written in sql and total_amount and player_keys are removed for the respective user
active_set = json.loads(r.get('active_set').decode('utf-8'))
usr = json.loads(r.get('user_session').encode('utf-8'))
player_id = usr[str(mass_dec['user_name'])]
game_room_id = mass_dec['game_room_id']
if player_id in active_set[str(game_room_id)]['player_bet']:
del active_set[str(game_room_id)]['player_bet'][player_id]
# set gameroom record in last_session
session_log = json.loads(r.get('last_session'))
if str(mass_dec['game_room_id']) in session_log[player_id]['gamerooms'].values():
total_amount_dict = json.loads(r.get('active_set'))[str(mass_dec['game_room_id'])]['total_amount']
if player_id in total_amount_dict:
old_earnings = session_log[player_id]['gamerooms'][str(mass_dec['game_room_id'])]['earnings']
session_log[player_id]['gamerooms'][str(mass_dec['game_room_id'])] = {
'earnings': str(float(old_earnings) + float(total_amount_dict[player_id]))
}
r.set('last_session', json.dumps(session_log))
# update sql
ab = WriteData()
user_balance = ab.get_user_data(player_id)[0]['virtual_money']
total_amount_dict = json.loads(r.get('active_set'))[str(mass_dec['game_room_id'])]['total_amount']
if player_id in total_amount_dict:
temp_balance = float(total_amount_dict[player_id])
del active_set[str(game_room_id)]['total_amount'][player_id]
else:
temp_balance = 0
ab.append_player_balance(float(player_id), float(user_balance) + float(temp_balance))
r.set('active_set', json.dumps(active_set))
#sitted_clients
sitted_clients_tmp = json.loads(r.get('sitted_clients'))
sitted_clients = sitted_clients_tmp[str(mass_dec['game_room_id'])]
sitted_clients[mass_dec['seat_no']] = '-1'
sitted_clients_tmp[str(mass_dec['game_room_id'])] = sitted_clients
r.set('sitted_clients',json.dumps(sitted_clients_tmp))
# remove player from shuffled active players
player_seq = obj_help.redis_fetch('shuffled_active_players')
if str(game_room_id) in player_seq:
if player_id in player_seq[str(game_room_id)]:
this_sequence = player_seq[str(game_room_id)]
this_sequence.remove(player_id)
player_seq[game_room_id] = this_sequence
obj_help.redis_store('shuffled_active_players', player_seq)
# put profile photos for sitted clients
profile_all_gamerooms = json.loads(r.get('profile'))
if str(mass_dec['game_room_id']) in profile_all_gamerooms.keys():
profile_dict = json.loads(r.get('profile'))[str(mass_dec['game_room_id'])]
del profile_dict[mass_dec['seat_no']]
profile_all_gamerooms[str(mass_dec['game_room_id'])] = profile_dict
r.set('profile', json.dumps(profile_all_gamerooms))
else:
profile_dict = {}
total_amount = json.loads(r.get('active_set'))[str(mass_dec['game_room_id'])]['total_amount']
#new sitted_clients to write
sitted_clients = json.loads(r.get('sitted_clients'))[str(game_room_id)]
mess3 = {
'game_key': "10" ,
'sitted_clients': sitted_clients,
'profile': profile_dict,
'total_amount': total_amount,
# 'player_bet': active_set[str(mass_dec['game_room_id'])]['player_bet']
}
for client in clients:
try:
client.write_message(json.dumps(mass_dec))
client.write_message(mess3)
except Exception as e:
pass
def game_action(self, mass_dec, clients):
# clients = json.loads('active_set')[str(mass_dec['game_room_id'])]['clients']
oclients = clients
clients = clients[str(mass_dec['game_room_id'])].values()
game_room_id = str(mass_dec['game_room_id'])
#player_id = json.loads(r.get('user_session').encode('utf-8'))[self.get_secure_cookie('qc_user')]
usr = json.loads(r.get('user_session').encode('utf-8'))
player_id = usr[mass_dec['user_name']]
player_seq1 = obj_help.redis_fetch('shuffled_active_players')[game_room_id]
usr_ult = {}
ind1 =0 #next player id
ind2 =0 #next player index
ind3 =0 #current player index
for i,j in usr.iteritems():
usr_ult[j] = i
#naming convention
#iterate the player_seq list and give the next player the turn to play
sitted_clients = json.loads(r.get('sitted_clients'))[str(mass_dec['game_room_id'])]
for ind,val in enumerate(player_seq1):
if val == usr[mass_dec['user_name']]:
ind2 = (ind +1)%len(player_seq1)
ind1 = player_seq1[ind2]
for seat,u_name in sitted_clients.iteritems():
if u_name != '-1':
if ind1 == usr[u_name]:
mass_dec['curr_seat_no'] = seat
waiting_for = mass_dec['curr_seat_no']
ind3 = ind
# no action if player is all_in
opp = json.loads(r.get('active_set'))
all_in_list = opp[str(mass_dec['game_room_id'])]['all_in_list']
if player_id not in all_in_list:
#call the particular function based on user action
if mass_dec['action'] == 'call':
obj_help.call(player_id, game_room_id)
elif mass_dec['action'] == 'check':
obj_help.check(player_id, game_room_id)
elif mass_dec['action'] == 'bet':
obj_help.bet(player_id, game_room_id, mass_dec['bet_amount'])
elif mass_dec['action'] == 'fold':
time.sleep(1)
obj_help.fold(player_id, game_room_id)
player_seq1 = obj_help.redis_fetch('shuffled_active_players')[game_room_id]
print player_seq1, 'This is '
if len(player_seq1) == 1:
obj_help.divide_cash_len(game_room_id,player_seq1[0])
print "start set called after fold:"
self.start_set(mass_dec, oclients)
elif mass_dec['action'] == 'all_in':
obj_help.all_in(player_id, game_room_id)
if player_id not in all_in_list:
all_in_list.append(player_id)
opp = json.loads(r.get('active_set'))
opp[str(mass_dec['game_room_id'])]['all_in_list'] = all_in_list
r.set('active_set',json.dumps(opp))
# updates for front end
active_set = json.loads(r.get('active_set'))[game_room_id]
print active_set, 'This is active set'
mass_dec['pot_amount'] = active_set['pot_amount']
# print mass_dec['pot_amount'], 'This is Pot Amount'
# print mass_dec['action'], 'THis is actions'
mass_dec['user_amount'] = active_set['total_amount'][usr[mass_dec['user_name']]]
mass_dec['call_amount'] = float(active_set['current_max_bet']) - float(active_set['player_bet'][ind1])
mass_dec['bet_amount'] = 2*float(active_set['current_max_bet']) - float(active_set['player_bet'][ind1])
mass_dec['next_user_amount'] = active_set['total_amount'][ind1]
round_no = active_set['round_no']
if round_no < 5:
for client in clients:
client.write_message(json.dumps(mass_dec))
#based on the round no. write the required hints to all the clients,
#this will be true if ind3 == 0, specifying the beginning of next round
active_set = json.loads(r.get('active_set'))[game_room_id]
# check if round should continue or not
round_end = True
for player in obj_help.redis_fetch('shuffled_active_players')[game_room_id]:
if active_set['player_bet'][player] == active_set['current_max_bet']:
continue
if player in all_in_list:
continue
round_end = False
last_player = r.get('higgest_bidder')
# finish betting round
# if ind3 == 2%len(player_seq1) and len(player_seq1) >1 :
if player_id == last_player and round_end:
print "i am here again"
print round_end
req = active_set['req']
if round_no == 2:
mass_dec['game_key'] = '6'
mass_dec['con_id'] = 'cat_' + str(round_no)
mass_dec['con'] = req['success']['data']['d1'][2]
elif round_no == 3:
mass_dec['game_key'] = '6'
mass_dec['con_id'] = 'cat_' + str(round_no)
mass_dec['con'] = req['success']['data']['d1'][3]
elif round_no == 4:
mass_dec['game_key'] = '6'
mass_dec['con_id'] = 'question'
# mass_dec['question_viewers'] = redis_fetch('shuffled_active_players')[game_room_id]
# print mass_dec['question_viewers'],'<------------'
mass_dec['con'] = req['success']['data']['question'][0]
mass_dec['opt'][0] = req['success']['data']['ans'][0]
mass_dec['opt'][1] = req['success']['data']['ans'][1]
mass_dec['opt'][2] = req['success']['data']['ans'][2]
mass_dec['opt'][3] = req['success']['data']['ans'][3]
elif round_no == 5:
mass_dec['game_key'] ='6'
mass_dec['con_id'] = 'round_end'
round_no += 1
opp[str(mass_dec['game_room_id'])]['round_no'] = round_no
r.set('active_set',json.dumps(opp))
active_player_list = obj_help.redis_fetch('shuffled_active_players')[str(mass_dec['game_room_id'])]
sb_amount = obj_help.redis_fetch('active_set')[str(mass_dec['game_room_id'])]['small_blind']
small_blind = sitted_clients.keys()[sitted_clients.values().index(usr_ult[active_player_list[1%len(active_player_list)]])]
bb_amount = obj_help.redis_fetch('active_set')[str(mass_dec['game_room_id'])]['big_blind']
big_blind = sitted_clients.keys()[sitted_clients.values().index(usr_ult[active_player_list[2 % len(active_player_list)]])]
mass_dec['small_blind'] = small_blind
mass_dec['sb_amount'] = sb_amount
mass_dec['big_blind'] = big_blind
mass_dec['bb_amount'] = bb_amount
for client in clients:
print "abc"
print mass_dec
client.write_message(json.dumps(mass_dec))
def winning_amount(self, mass_dec, right_ans):
acc = json.loads(r.get('active_set'))
pot_won = str(math.floor(0.9 * (acc[str(mass_dec['game_room_id'])]['pot_amount']) / len(right_ans.keys())))
return pot_won
def submit_ans(self, mass_dec, clients):
opp = json.loads(r.get('active_set'))
print('this is opp', opp)
ans_submit = opp[str(mass_dec['game_room_id'])]['ans_submit']
ans_submit +=1
opp[str(mass_dec['game_room_id'])]['ans_submit'] = ans_submit
r.set('active_set', json.dumps(opp))
req = opp[str(mass_dec['game_room_id'])]['req']
oclients=clients
clients = clients[str(mass_dec['game_room_id'])].values()
#compare the user's ans with the correct ans, and display the required information
if int(req['success']['data']['c_ans'][0]) == int(mass_dec['ans']):
mass_dec['correct_answer'] = req['success']['data']['c_ans'][0]
mass_dec['game_key'] = '8'
answer = 1
game_room_id = str(mass_dec['game_room_id'])
kk = json.loads(r.get('right_ans').decode('utf-8'))
right_ans = kk[game_room_id]
usr = json.loads(r.get('user_session').encode('utf-8'))
right_ans[usr[mass_dec['user_name']]] = mass_dec['user_name']
# mass_dec['correct_ans'] =
kk[game_room_id] = right_ans
r.set('right_ans',json.dumps(kk))
mass_dec['pot_won'] = self.winning_amount(mass_dec, right_ans)
else:
mass_dec['game_key'] = '9'
mass_dec['correct_answer'] = req['success']['data']['c_ans'][0]
answer = 0
#post the required stats to the question_statistics api
usr = json.loads(r.get('user_session').encode('utf-8'))
player_id = usr[mass_dec['user_name']]
ans_stat = {mass_dec['game_room_id'] :{ player_id : '|'.join([str(answer), mass_dec['answer_time']]) }}
requests.post('http://localhost:8088/question_statistics', data = {'user_data':json.dumps(ans_stat)})
game_room_id = str(mass_dec['game_room_id'])
player_seq = obj_help.redis_fetch('shuffled_active_players')[game_room_id]
# clients = opp[game_room_id]['clients']
for client in clients:
client.write_message(json.dumps(mass_dec))
#call the divide_cash function once every user has submitted the ans
if ans_submit == len(player_seq):
acc = json.loads(r.get('active_set'))
obj_help.divide_cash(game_room_id)
time.sleep(3)
#send the winners of each round
mass_dec['game_key'] = '11'
kk = json.loads(r.get('right_ans').decode('utf-8'))
right_ans = kk[game_room_id]
mass_dec['right_ans_usr']= right_ans.values()
for client in clients:
client.write_message(json.dumps(mass_dec))
#writing winners and pot amount
if len(right_ans.keys()) > 0:
win_mess_lis =""
for val in right_ans.values():
win_mess_lis += val + ", "
pot_won = str(math.floor(0.9 * (acc[str(mass_dec['game_room_id'])]['pot_amount']) / len(right_ans.keys())))
rake = str(0.1 * (acc[str(mass_dec['game_room_id'])]['pot_amount']) / len(right_ans.keys()))
win_mess_1 = json.dumps({
'game_key': '1',
'user_name': "Winners",
'message': str(win_mess_lis)
})
win_mess_2 = json.dumps({
'game_key': '1',
'user_name': "Pot Amount Won",
'message': pot_won
})
win_mess_3 = json.dumps({
'game_key': '1',
'user_name': "Rake For The Round",
'message': rake
})
for client in clients:
print "HELLO::: "
print win_mess_1,win_mess_2
client.write_message(win_mess_1)
client.write_message(win_mess_2)
client.write_message(win_mess_3)
else:
loss_mess_1 = json.dumps({
'game_key': '1',
'user_name': "Pot Amount",
'message': str( (acc[str(mass_dec['game_room_id'])]['pot_amount']) )
})
for client in clients:
client.write_message(loss_mess_1)
wallet_operation_obj = WriteData()
# udate wallets after winner is displayed
total_amount_dict = json.loads(r.get('active_set'))[str(mass_dec['game_room_id'])]['total_amount']
balance = {}
for player_id in total_amount_dict:
balance[player_id] = float(wallet_operation_obj.get_user_data(player_id)[0]['virtual_money']) + float(total_amount_dict[player_id])
# send update message on websocket
msg = {}
msg['game_key'] = 'update_wallet'
msg['balance'] = balance
for client in clients:
client.write_message(json.dumps(msg))
sitted_clients = json.loads(r.get('sitted_clients'))
sitted_clients_for_gameroom = sitted_clients[str(mass_dec['game_room_id'])]
usr_ult = {}
for i, j in usr.iteritems():
usr_ult[j] = i
players_for_buyin = []
players_for_game = []
# check balances
balance_dict = json.loads(r.get('active_set'))[str(mass_dec['game_room_id'])]['total_amount']
for player in balance_dict:
if balance_dict[player] <= 0:
players_for_buyin.append(player)
# username = usr_ult[player]
active_set = json.loads(r.get('active_set').decode('utf-8'))
total_amount_dict = json.loads(r.get('active_set'))[str(mass_dec['game_room_id'])]['total_amount']
if player in total_amount_dict:
del active_set[str(game_room_id)]['total_amount'][player]
r.set('active_set', json.dumps(active_set))
else:
players_for_game.append(player)
# send buyin players list on websocket
message_for_buyin = {
'game_key': 'buyin_check',
'player_for_buyin': players_for_buyin,
'max_buy_in': json.loads(r.get('active_set'))[str(mass_dec['game_room_id'])]['max_buy_in'],
'min_buy_in': json.loads(r.get('active_set'))[str(mass_dec['game_room_id'])]['min_buy_in']
}
for client in clients:
client.write_message(json.dumps(message_for_buyin))
time.sleep(3)
# idle_gameroom = True
opp = json.loads(r.get('active_set'))
opp[str(mass_dec['game_room_id'])]['idle_gameroom'] = True
r.set('active_set', json.dumps(opp))
self.start_set(mass_dec, oclients)
def rebuyin(self, mass_dec, clients):
oclients = clients
clients = clients[str(mass_dec['game_room_id'])].values()
# change total amount of player
active_set = json.loads(r.get('active_set'))
usr = json.loads(r.get('user_session'))
player_id = usr[mass_dec['user_name']]
active_set[str(mass_dec['game_room_id'])]['total_amount'][player_id] = mass_dec['rebuyin']
r.set('active_set', json.dumps(active_set))
# set gameroom record in last_session
session_log = json.loads(r.get('last_session'))
if str(mass_dec['game_room_id']) in session_log[player_id]['gamerooms'].values():
old_buy_in = session_log[player_id]['gamerooms'][str(mass_dec['game_room_id'])]['buy-in']
old_earnings = session_log[player_id]['gamerooms'][str(mass_dec['game_room_id'])]['earnings']
session_log[player_id]['gamerooms'][str(mass_dec['game_room_id'])] = {
'buy-in': str(float(old_buy_in) + float(mass_dec['rebuyin'])),
'earnings': str(float(old_earnings) - float(mass_dec['rebuyin']))
}
r.set('last_session', json.dumps(session_log))
wallet_operation_obj = WriteData()
# deduct buy-ins
user_balance = wallet_operation_obj.get_user_data(player_id)[0]['virtual_money']
wallet_operation_obj.append_player_balance(float(player_id), float(user_balance) - mass_dec['rebuyin'])
total_amount_dict = json.loads(r.get('active_set'))[str(mass_dec['game_room_id'])]['total_amount']
balance = {}
balance[player_id] = float(wallet_operation_obj.get_user_data(player_id)[0]['virtual_money']) + float(total_amount_dict[player_id])
# send update message on websocket
msg = {}
msg['game_key'] = 'update_wallet'
msg['balance'] = balance
for client in clients:
client.write_message(json.dumps(msg))
idle_gameroom = json.loads(r.get('active_set'))[str(mass_dec['game_room_id'])]['idle_gameroom']
if len(json.loads(r.get('active_set'))[str(mass_dec['game_room_id'])]['total_amount']) > 1 and idle_gameroom:
# start the game
automated_start = {
'user_name': 'admin',
'game_room_id': str(mass_dec['game_room_id'])
}
self.start_set(json.dumps(automated_start), oclients)
# # resume on seat
# sitted_clients = json.loads(r.get('sitted_clients'))
# sitted_clients_for_gameroom = sitted_clients[str(mass_dec['game_room_id'])]
# sitted_clients_for_gameroom[mass_dec['seat_no']] = mass_dec['user_name']
# sitted_clients[str(mass_dec['game_room_id'])] = sitted_clients_for_gameroom
# r.set('sitted_clients', json.dumps(sitted_clients))
def sit_here_func(self, mass_dec, clients):
#client_gameplay.append(self)
clients = clients[str(mass_dec['game_room_id'])].values()
#when a players sits, information is updated in redis active_set table, balance is fetched from sql and player_bet is initialised to 0
active_set = json.loads(r.get('active_set').decode('utf-8'))
usr = json.loads(r.get('user_session').encode('utf-8'))
player_id = usr[str(mass_dec['user_name'])]
active_set[str(mass_dec['game_room_id'])]['player_bet'][player_id] = 0
# balance = self.get_user_data(player_id)[0]['virtual_money']
# active_set[str(mass_dec['game_room_id'])]['total_amount'][player_id] = balance
r.set('active_set', json.dumps(active_set))
#enter the users information in the sitted clients table, and set in the redis table
sitted_clients_tmp = json.loads(r.get('sitted_clients').decode('utf-8'))
sitted_clients = {}
sitted_clients = sitted_clients_tmp[mass_dec['game_room_id']]
sitted_clients[mass_dec['seat_no']] = mass_dec['user_name']
sitted_clients_tmp[str(mass_dec['game_room_id'])] = sitted_clients
r.set('sitted_clients',json.dumps(sitted_clients_tmp))
# update profile photo for seat
profile_dict = json.loads(r.get('profile'))
if str(mass_dec['game_room_id']) in profile_dict.keys():
profile_dict[str(mass_dec['game_room_id'])][mass_dec['seat_no']] = mass_dec['profile']
else:
profile_dict[str(mass_dec['game_room_id'])] = {mass_dec['seat_no']: mass_dec['profile']}
r.set('profile', json.dumps(profile_dict))
# sz = str(len(client_gameplay))
# mess2 = { 'game_key': "1" ,'user_name': "Active Clients :", 'message': sz, 'seat_no':"nil", 'action':"nil", 'dealer_seat':'nil' }
for client in clients:
client.write_message(json.dumps(mass_dec))
# client.write_message(mess2)