Contract con_poker_game_controller_v2


Contract Code

1 # con_poker_game_controller_v2
2 # owner: con_poker_card_games_v4
3
4 I = importlib
5
6
7 ONE_CARD_POKER = 0
8 BLIND_POKER = 1
9 STUD_POKER = 2
10 HOLDEM_POKER = 3
11 OMAHA_POKER = 4
12 ALL_GAME_TYPES = [ONE_CARD_POKER, BLIND_POKER, STUD_POKER, HOLDEM_POKER, OMAHA_POKER]
13 NO_LIMIT = 0
14 POT_LIMIT = 1
15 ALL_BETTING_TYPES = [NO_LIMIT, POT_LIMIT]
16
17
18 def get_players_and_assert_exists(game_id: str, games: Any) -> dict:
19 players = games[game_id, 'players']
20 assert players is not None, f'Game {game_id} does not exist.'
21 return players
22
23
24 def create_game_id(name: str) -> str:
25 return hashlib.sha3(":".join([name, str(now)]))
26
27
28 @export
29 def add_chips_to_game(game_id: str, amount: float, player: str, games: Any):
30 assert amount > 0, 'Amount must be a positive number'
31 players = get_players_and_assert_exists(game_id, games)
32 assert player in players, 'You do not belong to this game.'
33 games[game_id, player] = (games[game_id, player] or 0.0) + amount
34 I.import_module(ctx.owner).force_transfer(player=player, amount=amount)
35
36
37 @export
38 def withdraw_chips_from_game(game_id: str, amount: float, player: str, games: Any):
39 assert amount > 0, 'Amount must be a positive number'
40 current_chip_count = games[game_id, player] or 0
41 assert current_chip_count >= amount, 'You cannot withdraw more than you have.'
42 games[game_id, player] = current_chip_count - amount
43 I.import_module(ctx.owner).force_withdraw(player=player, amount=amount)
44
45
46 @export
47 def respond_to_invite(game_id: str, accept: bool, player: str, games: Any, players_invites: Any, players_games: Any):
48 player_invites = players_invites[player] or []
49 players = get_players_and_assert_exists(game_id, games)
50 assert player not in players, 'You are already a part of this game.'
51 declined = players_invites[player, 'declined'] or []
52 public = games[game_id, 'public']
53 assert game_id in player_invites or game_id in declined or public, 'You have not been invited to this game.'
54 if public:
55 # check banned players
56 assert player not in (games[game_id, 'banned'] or []), 'You have been banned from this public game.'
57 if game_id in player_invites:
58 player_invites.remove(game_id)
59 players_invites[player] = player_invites
60 players_invites[player, game_id] = accept
61 if accept:
62 if game_id in declined:
63 declined.remove(game_id)
64 players_invites[player, 'declined'] = declined
65 players.append(player)
66 games[game_id, 'players'] = players
67 players_games[player] = (players_games[player] or []) + [game_id]
68 else:
69 if game_id not in declined:
70 declined.append(game_id)
71 players_invites[player, 'declined'] = declined
72
73 @export
74 def decline_all_invites(player: str, players_invites: Any):
75 invites = players_invites[player] or []
76 for invite in invites:
77 players_invites[player, invite] = False
78 players_invites[player] = []
79
80 def send_invite_requests(game_id: str, others: list, players_invites: Any):
81 for other in others:
82 player_invites = players_invites[other] or []
83 player_invites.append(game_id)
84 players_invites[other] = player_invites
85
86 def validate_game_name(name: str):
87 assert name is not None and len(name) > 0, 'Game name cannot be null or empty'
88 assert isinstance(name, str), 'Game name must be a string.'
89 assert len(name) <= 36, 'Game name cannot be longer than 36 characters.'
90 assert all([c.isalnum() or c in ('_', '-') for c in name]), 'Game name has invalid characters. Each character must be alphanumeric, a hyphen, or an underscore.'
91 assert name[0] not in ('-', '_') and name[-1] not in ('-', '_'), 'Game name cannot start or end with a hyphen or underscore.'
92
93 @export
94 def start_game(name: str,
95 other_players: list,
96 creator: str,
97 game_config: dict,
98 games: Any,
99 game_names: Any,
100 players_games: Any,
101 players_invites: Any,
102 public: bool = False) -> str:
103
104 game_type = game_config['game_type']
105 bet_type = game_config['bet_type']
106 assert game_type in ALL_GAME_TYPES, f'Invalid game type: {game_type}.'
107 assert bet_type in ALL_BETTING_TYPES, f'Invalid betting type: {bet_type}.'
108
109 ante = game_config['ante']
110 assert ante >= 0, 'Ante must be non-negative.'
111 assert creator not in other_players, f'Caller can\'t be in other_players input.'
112
113 game_id = create_game_id(name=name)
114
115 assert games[game_id, 'creator'] is None, f'Game {game_id} has already been created.'
116
117 validate_game_name(name)
118 assert game_names[name] is None, f'Game {name} has already been created.'
119 game_names[name] = game_id
120
121 games[game_id, 'players'] = [creator]
122 games[game_id, 'name'] = name
123 games[game_id, 'ante'] = ante
124 games[game_id, 'creator'] = creator
125 games[game_id, 'invitees'] = other_players
126 games[game_id, 'public'] = public
127 games[game_id, 'game_type'] = game_type
128 games[game_id, 'bet_type'] = bet_type
129
130 if game_type == STUD_POKER:
131 n_cards_total = game_config['n_cards_total']
132 n_hole_cards = game_config['n_hole_cards']
133 assert n_cards_total is not None, 'Must specify n_cards_total for stud poker.'
134 assert n_hole_cards is not None, 'Must specify n_hole_cards for stud poker.'
135 assert n_cards_total == 5 or n_cards_total == 7, 'n_cards_total must equal 5 or 7.'
136 assert n_hole_cards > 0, 'n_hole_cards must be positive.'
137 assert n_hole_cards <= n_cards_total, 'n_hole_cards must be less than or equal to n_cards_total.'
138 games[game_id, 'n_cards_total'] = n_cards_total
139 games[game_id, 'n_hole_cards'] = n_hole_cards
140
141 players_games[creator] = (players_games[creator] or []) + [game_id]
142 send_invite_requests(game_id, other_players, players_invites)
143
144 return game_id
145
146 @export
147 def add_player_to_game(game_id: str, player_to_add: str, player: str, games: Any, players_invites: Any):
148 assert player != player_to_add, 'You cannot add yourself to a game.'
149 creator = games[game_id, 'creator']
150 assert player == creator, 'Only the game creator can add players.'
151 players = get_players_and_assert_exists(game_id, games)
152 assert player_to_add not in players, 'Player is already in the game.'
153 invitees = games[game_id, 'invitees']
154 assert player_to_add not in invitees, 'Player has already been invited.'
155 invitees.append(player_to_add)
156 games[game_id, 'invitees'] = invitees
157 send_invite_requests(game_id, [player_to_add], players_invites)
158
159 @export
160 def leave_game(game_id: str, player: str, force: bool, games: Any, players_games: Any):
161 players = get_players_and_assert_exists(game_id, games)
162 assert player in players, 'You are not in this game.'
163
164 chips = games[game_id, player]
165
166 hand_id = games[game_id, 'current_hand']
167
168 if hand_id is not None:
169 I.import_module(ctx.owner).leave_hand(
170 game_id=game_id,
171 hand_id=hand_id,
172 player=player,
173 force=force,
174 )
175
176 if chips is not None and chips > 0:
177 # Withdraw their chips
178 withdraw_chips_from_game(game_id, chips, player, games)
179
180 player_games = players_games[player]
181 player_games.remove(game_id)
182 players_games[player] = player_games
183 players.remove(player)
184 games[game_id, 'players'] = players
185
186
187 @export
188 def kick_from_game(game_id: str, creator: str, player: str, games: Any, players_games: Any):
189 assert creator == games[game_id, 'creator'], 'Only the game creator can remove players from the game.'
190 assert creator != player, 'You cannot force kick yourself out of the game.'
191 # Kick them out
192 leave_game(game_id, player, True, games, players_games)
193
194
195 @export
196 def ban_player(game_id: str, creator: str, player: str, games: Any, players_games: Any):
197 assert games[game_id, 'public'], 'You can only ban people from public games.'
198 kick_from_game(game_id, creator, player, games, players_games)
199 banned = games[game_id, 'banned'] or []
200 banned.append(player)
201 games[game_id, 'banned'] = banned
202
203
204 @export
205 def unban_player(game_id: str, creator: str, player: str, games: Any):
206 assert games[game_id, 'public'], 'You can only unban people from public games.'
207 assert creator == games[game_id, 'creator'], 'Only the game creator can remove players from the game.'
208 assert creator != player, 'You cannot unban yourself from the game.'
209 banned = games[game_id, 'banned'] or []
210 if player in banned:
211 banned.remove(player)
212 games[game_id, 'banned'] = banned
213

Byte Code

e30000000000000000000000000d0000004000000073aa01000065005a0164005a0264015a0364025a0464035a0564045a066502650365046505650667055a0764005a0864015a096508650967025a0a650b650c650d64059c036406640784045a0e650b650b64089c026409640a84045a0f6510640b8301650b6511650b650c640c9c04640d640e840483015a126510640b8301650b6511650b650c640c9c04640f6410840483015a136510640b8301650b6514650b650c650c650c64119c0664126413840483015a156510640b8301650b650c64149c0264156416840483015a16650b6517650c64179c036418641984045a18650b641a9c01641b641c84045a196510640b83016430650b6517650b650d650c650c650c650c6514650b641e9c0a641f6420840583015a1a6510640b8301650b650b650b650c650c64219c0564226423840483015a1b6510640b8301650b650b6514650c650c64249c0564256426840483015a1c6510640b8301650b650b650b650c650c64279c0564286429840483015a1d6510640b8301650b650b650b650c650c64279c05642a642b840483015a1e6510640b8301650b650b650b650c642c9c04642d642e840483015a1f642f53002931e900000000e901000000e902000000e903000000e9040000002903da0767616d655f6964da0567616d6573da0672657475726e63020000000000000003000000040000004300000073280000007c017c006401660219007d027c0264006b097324740064027c009b0064039d03830182017c02530029044eda07706c61796572737a0547616d65207a1020646f6573206e6f742065786973742e2901da0e417373657274696f6e4572726f722903720600000072070000007209000000a900720b000000da00da1f5f5f6765745f706c61796572735f616e645f6173736572745f6578697374730e000000730600000000010c011801720d0000002902da046e616d657208000000630100000000000000010000000500000043000000731800000074006a0164016a027c00740374048301670283018301530029024efa013a2905da07686173686c6962da0473686133da046a6f696eda03737472da036e6f772901720e000000720b000000720b000000720c000000da105f5f6372656174655f67616d655f696414000000730200000000017215000000da1c636f6e5f706f6b65725f67616d655f636f6e74726f6c6c65725f763229047206000000da06616d6f756e74da06706c61796572720700000063040000000000000005000000040000004300000073640000007c0164016b047310740064028301820174017c007c0383027d047c027c046b06732a74006403830182017c037c007c0266021900703c7402640483017c0117007c037c007c0266023c0074036a0474056a0683016a077c027c0164058d0201006400530029064e72010000007a20416d6f756e74206d757374206265206120706f736974697665206e756d6265727a1f596f7520646f206e6f742062656c6f6e6720746f20746869732067616d652e7a03302e302902721800000072170000002908720a000000720d000000da07646563696d616cda0149da0d696d706f72745f6d6f64756c65da03637478da056f776e6572da0e666f7263655f7472616e73666572290572060000007217000000721800000072070000007209000000720b000000720b000000720c000000da116164645f63686970735f746f5f67616d6518000000730c000000000210010a01100112010e01721f000000630400000000000000050000000400000043000000735a0000007c0164016b04731074006402830182017c037c007c0266021900701e64017d047c047c016b05733074006403830182017c047c0118007c037c007c0266023c0074016a0274036a0483016a057c027c0164048d0201006400530029054e72010000007a20416d6f756e74206d757374206265206120706f736974697665206e756d6265727a27596f752063616e6e6f74207769746864726177206d6f7265207468616e20796f7520686176652e2902721800000072170000002906720a000000721a000000721b000000721c000000721d000000da0e666f7263655f776974686472617729057206000000721700000072180000007207000000da1263757272656e745f636869705f636f756e74720b000000720b000000720c000000da1877697468647261775f63686970735f66726f6d5f67616d6522000000730a00000000031001100110011001722200000029067206000000da0661636365707472180000007207000000da0f706c61796572735f696e7669746573da0d706c61796572735f67616d65736306000000000000000a000000040000004300000073180100007c047c021900700a67007d0674007c007c0383027d077c027c076b07732674016401830182017c047c02640266021900703467007d087c037c006403660219007d097c007c066b06735e7c007c086b06735e7c09735e74016404830182017c09727e7c027c037c00640566021900707267006b07737e74016406830182017c007c066b0672987c066a027c00830101007c067c047c023c007c017c047c027c0066023c007c0172f47c007c086b0672c67c086a027c00830101007c087c047c02640266023c007c076a037c02830101007c077c037c00640766023c007c057c02190070e667007c00670117007c057c023c006e207c007c086b07900172147c086a037c00830101007c087c047c02640266023c006400530029084e7a24596f752061726520616c726561647920612070617274206f6620746869732067616d652eda086465636c696e6564da067075626c69637a27596f752068617665206e6f74206265656e20696e766974656420746f20746869732067616d652eda0662616e6e65647a2b596f752068617665206265656e2062616e6e65642066726f6d2074686973207075626c69632067616d652e72090000002904720d000000720a000000da0672656d6f7665da06617070656e64290a720600000072230000007218000000720700000072240000007225000000da0e706c617965725f696e7669746573720900000072260000007227000000720b000000720b000000720c000000da11726573706f6e645f746f5f696e766974652c000000732e00000000030c010a01100110010c011c0104011601060108010a0108010c01040108010a010c010a010c0118010a010a01722c00000029027218000000722400000063020000000000000004000000050000004300000073320000007c017c001900700a67007d0278187c0244005d107d0364017c017c007c0366023c007112570067007c017c003c006400530029024e46720b000000290472180000007224000000da07696e7669746573da06696e76697465720b000000720b000000720c000000da136465636c696e655f616c6c5f696e766974657348000000730800000000020c010a011001722f00000029037206000000da066f746865727372240000006303000000000000000500000004000000430000007330000000782a7c0144005d227d037c027c031900701467007d047c046a007c00830101007c047c027c033c00710657006400530029014e2901722a0000002905720600000072300000007224000000da056f74686572722b000000720b000000720b000000720c000000da165f5f73656e645f696e766974655f726571756573747350000000730800000000010a010c010a0172320000002901720e00000063010000000000000001000000030000004300000073800000007c0064006b09721474007c00830164016b04731c740164028301820174027c0074038302732e740164038301820174007c00830164046b017342740164058301820174046406640784007c00440083018301735c74016408830182017c0064011900640d6b0772747c00640e1900640f6b07737c7401640c830182016400530029104e72010000007a2147616d65206e616d652063616e6e6f74206265206e756c6c206f7220656d7074797a1b47616d65206e616d65206d757374206265206120737472696e672ee9240000007a2e47616d65206e616d652063616e6e6f74206265206c6f6e676572207468616e20333620636861726163746572732e630100000000000000020000000500000053000000731c00000067007c005d147d017c016a00830070167c0164026b069102710453002903da015ffa012d2902723400000072350000002901da076973616c6e756d2902da022e30da0163720b000000720b000000720c000000fa0a3c6c697374636f6d703e5c000000730200000006007a285f5f76616c69646174655f67616d655f6e616d652e3c6c6f63616c733e2e3c6c697374636f6d703e7a6247616d65206e616d652068617320696e76616c696420636861726163746572732e204561636820636861726163746572206d75737420626520616c7068616e756d657269632c20612068797068656e2c206f7220616e20756e64657273636f72652e7235000000723400000072020000007a3a47616d65206e616d652063616e6e6f74207374617274206f7220656e64207769746820612068797068656e206f7220756e64657273636f72652e290272350000007234000000e9ffffffff2902723500000072340000002905da036c656e720a000000da0a6973696e7374616e63657213000000da03616c6c2901720e000000720b000000720b000000720c000000da145f5f76616c69646174655f67616d655f6e616d6557000000731000000000010e010e0112011401140106011a01723e00000046290a720e000000da0d6f746865725f706c6179657273da0763726561746f72da0b67616d655f636f6e6669677207000000da0a67616d655f6e616d657372250000007224000000722700000072080000006309000000000000000f000000040000004300000073de0100007c03640119007d097c03640219007d0a7c0974006b067328740164037c099b0064049d03830182017c0a74026b067340740164057c0a9b0064049d03830182017c03640619007d0b7c0b64076b05735874016408830182017c027c016b077368740164098301820174037c00640a8d017d0c7c047c0c640b6602190064006b0873927401640c7c0c9b00640d9d038301820174047c00830101007c057c00190064006b0873b67401640c7c009b00640d9d03830182017c0c7c057c003c007c0267017c047c0c640e66023c007c007c047c0c640f66023c007c0b7c047c0c640666023c007c027c047c0c640b66023c007c017c047c0c641066023c007c087c047c0c641166023c007c097c047c0c640166023c007c0a7c047c0c640266023c007c0974056b02900172b67c03641219007d0d7c03641319007d0e7c0d64006b099001734c74016414830182017c0e64006b099001735e74016415830182017c0d64166b029001737a7c0d64176b029001737a74016418830182017c0e64076b049001738c74016419830182017c0e7c0d6b019001739e7401641a830182017c0d7c047c0c641266023c007c0e7c047c0c641366023c007c067c021900900170c267007c0c670117007c067c023c0074067c0c7c017c07830301007c0c5300291b4eda0967616d655f74797065da086265745f747970657a13496e76616c69642067616d6520747970653a20da012e7a16496e76616c69642062657474696e6720747970653a20da04616e746572010000007a1a416e7465206d757374206265206e6f6e2d6e656761746976652e7a2743616c6c65722063616e277420626520696e206f746865725f706c617965727320696e7075742e2901720e00000072400000007a0547616d65207a1a2068617320616c7265616479206265656e20637265617465642e7209000000720e000000da08696e7669746565737227000000da0d6e5f63617264735f746f74616cda0c6e5f686f6c655f63617264737a2a4d7573742073706563696679206e5f63617264735f746f74616c20666f72207374756420706f6b65722e7a294d7573742073706563696679206e5f686f6c655f636172647320666f72207374756420706f6b65722ee905000000e9070000007a206e5f63617264735f746f74616c206d75737420657175616c2035206f7220372e7a1e6e5f686f6c655f6361726473206d75737420626520706f7369746976652e7a396e5f686f6c655f6361726473206d757374206265206c657373207468616e206f7220657175616c20746f206e5f63617264735f746f74616c2e2907da0e414c4c5f47414d455f5459504553720a000000da11414c4c5f42455454494e475f54595045537215000000723e000000da0a535455445f504f4b45527232000000290f720e000000723f0000007240000000724100000072070000007242000000722500000072240000007227000000724300000072440000007246000000720600000072480000007249000000720b000000720b000000720c000000da0a73746172745f67616d65620000007344000000000408010801180118010801100110010a010a01160108011c0108010e010c010c010c010c010c010c010c010a0108010801120112011c01120112010c010c0118010c01724f00000029057206000000da0d706c617965725f746f5f616464721800000072070000007224000000630500000000000000080000000400000043000000738a0000007c027c016b03731074006401830182017c037c006402660219007d057c027c056b02732c740064038301820174017c007c0383027d067c017c066b07734674006404830182017c037c006405660219007d077c017c076b07736274006406830182017c076a027c01830101007c077c037c00640566023c0074037c007c0167017c04830301006400530029074e7a22596f752063616e6e6f742061646420796f757273656c6620746f20612067616d652e72400000007a264f6e6c79207468652067616d652063726561746f722063616e2061646420706c61796572732e7a1e506c6179657220697320616c726561647920696e207468652067616d652e72470000007a20506c617965722068617320616c7265616479206265656e20696e76697465642e2904720a000000720d000000722a0000007232000000290872060000007250000000721800000072070000007224000000724000000072090000007247000000720b000000720b000000720c000000da126164645f706c617965725f746f5f67616d658a0000007314000000000310010c0110010a0110010c0110010a010c017251000000290572060000007218000000da05666f7263657207000000722500000063050000000000000009000000060000004300000073a600000074007c007c0383027d057c017c056b06731a74016401830182017c037c007c01660219007d067c037c006402660219007d077c0764006b09725474026a0374046a0583016a067c007c077c017c0264038d0401007c0664006b0972727c0664046b04727274077c007c067c017c03830401007c047c0119007d087c086a087c00830101007c087c047c013c007c056a087c01830101007c057c037c00640566023c006400530029064e7a19596f7520617265206e6f7420696e20746869732067616d652eda0c63757272656e745f68616e6429047206000000da0768616e645f696472180000007252000000720100000072090000002909720d000000720a000000721a000000721b000000721c000000721d000000da0a6c656176655f68616e64722200000072290000002909720600000072180000007252000000720700000072250000007209000000da0563686970737254000000da0c706c617965725f67616d6573720b000000720b000000720c000000da0a6c656176655f67616d6599000000731c00000000030a0110010c010c0108010e010c0110010e0108010a0108010a017258000000290572060000007240000000721800000072070000007225000000630500000000000000050000000600000043000000733c0000007c017c037c006401660219006b02731874006402830182017c017c026b037328740064038301820174017c007c0264047c037c04830501006400530029054e72400000007a374f6e6c79207468652067616d652063726561746f722063616e2072656d6f766520706c61796572732066726f6d207468652067616d652e7a2f596f752063616e6e6f7420666f726365206b69636b20796f757273656c66206f7574206f66207468652067616d652e542902720a0000007258000000290572060000007240000000721800000072070000007225000000720b000000720b000000720c000000da0e6b69636b5f66726f6d5f67616d65ac000000730800000000031201060110017259000000630500000000000000060000000600000043000000734e0000007c037c006401660219007314740064028301820174017c007c017c027c037c04830501007c037c00640366021900703267007d057c056a027c02830101007c057c037c00640366023c006400530029044e72270000007a2a596f752063616e206f6e6c792062616e2070656f706c652066726f6d207075626c69632067616d65732e72280000002903720a0000007259000000722a0000002906720600000072400000007218000000720700000072250000007228000000720b000000720b000000720c000000da0a62616e5f706c61796572b5000000730c00000000030e010601100110010a01725a00000029047206000000724000000072180000007207000000630400000000000000050000000400000043000000736e0000007c037c00640166021900731474006402830182017c017c037c006403660219006b02732c74006404830182017c017c026b03733c74006405830182017c037c00640666021900704a67007d047c027c046b06726a7c046a017c02830101007c047c037c00640666023c006400530029074e72270000007a2c596f752063616e206f6e6c7920756e62616e2070656f706c652066726f6d207075626c69632067616d65732e72400000007a374f6e6c79207468652067616d652063726561746f722063616e2072656d6f766520706c61796572732066726f6d207468652067616d652e7a28596f752063616e6e6f7420756e62616e20796f757273656c662066726f6d207468652067616d652e72280000002902720a0000007229000000290572060000007240000000721800000072070000007228000000720b000000720b000000720c000000da0c756e62616e5f706c61796572c0000000731200000000020e010601120106011001100108010a01725b0000004e2901462920da09696d706f72746c6962721a000000da0e4f4e455f434152445f504f4b4552da0b424c494e445f504f4b4552724e000000da0c484f4c44454d5f504f4b4552da0b4f4d4148415f504f4b4552724c000000da084e4f5f4c494d4954da09504f545f4c494d4954724d0000007213000000da03416e79da0464696374720d0000007215000000da085f5f6578706f7274da05666c6f6174721f0000007222000000da04626f6f6c722c000000722f000000da046c6973747232000000723e000000724f000000725100000072580000007259000000725a000000725b000000720b000000720b000000720b000000720c000000da083c6d6f64756c653e01000000735000000004010401040104010401040108010601040104010803120610040601160906010601100806010801121a0601120712070e0b0603242506010601120d0601080110110601080110070601080110090601