forked from vkolagotla/DevOpsPoker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdplayer.py
130 lines (109 loc) · 4.09 KB
/
dplayer.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# -----------------------------------------------------------
# dodPoker: a poker server to run automated texas hold'em
# poker rounds with bots
# Copyright (C) 2017 wobe-systems GmbH
# -----------------------------------------------------------
# -----------------------------------------------------------
# Configuration
# You need to change the setting according to your environment
gregister_url='http://localhost:5001'
glocalip_adr='127.0.0.1'
# -----------------------------------------------------------
from flask import Flask, request
from flask_restful import Resource, Api
import sys
from requests import put
import json
app = Flask(__name__)
api = Api(app)
# Web API to be called from the poker manager
class PokerPlayerAPI(Resource):
## return bid to caller
#
# Depending on the cards passed to this function in the data parameter,
# this function has to return the next bid.
# The following rules are applied:
# -- fold --
# bid < min_bid
# bid > max_bid -> ** error **
# (bid > min_bid) and (bid < (min_bid+big_blind)) -> ** error **
#
# -- check --
# (bid == 0) and (min_bid == 0) -> check
#
# -- call --
# (bid == min_bid) and (min_bid > 0)
#
# -- raise --
# min_bid + big_blind + x
# x is any value to increase on top of the Big blind
#
# -- all in --
# bid == max_bid -> all in
#
# @param data : a dictionary containing the following values - example: data['pot']
# min_bid : minimum bid to return to stay in the game
# max_bid : maximum possible bid
# big_blind : the current value of the big blind
# pot : the total value of the current pot
# board : a list of board cards on the table as string '<rank><suit>'
# hand : a list of individual hand cards as string '<rank><suit>'
#
# <rank> : 23456789TJQKA
# <suit> : 's' : spades
# 'h' : hearts
# 'd' : diamonds
# 'c' : clubs
#
# @return a dictionary containing the following values
# bid : a number between 0 and max_bid
def __get_bid(self, data):
return 0
# dispatch incoming get commands
def get(self, command_id):
data = request.form['data']
data = json.loads(data)
if command_id == 'get_bid':
return {'bid': self.__get_bid(data)}
else:
return {}, 201
# dispatch incoming put commands (if any)
def put(self, command_id):
return 201
api.add_resource(PokerPlayerAPI, '/dpoker/player/v1/<string:command_id>')
# main function
def main():
# run the player bot with parameters
if len(sys.argv) == 4:
team_name = sys.argv[1]
api_port = int(sys.argv[2])
api_url = 'http://%s:%s' % (glocalip_adr, api_port)
api_pass = sys.argv[3]
else:
print("""
DevOps Poker Bot - usage instruction
------------------------------------
python3 dplayer.py <team name> <port> <password>
example:
python3 dplayer bazinga 40001 x407
""")
return 0
# register player
r = put("%s/dpoker/v1/enter_game"%gregister_url, data={'team': team_name, \
'url': api_url,\
'pass':api_pass}).json()
if r != 201:
raise Exception('registration failed: probably wrong team name or password')
else:
print('registration successful')
try:
app.run(host='0.0.0.0', port=api_port, debug=False)
finally:
put("%s/dpoker/v1/leave_game"%gregister_url, data={'team': team_name, \
'url': api_url,\
'pass': api_pass}).json()
# run the main function
if __name__ == '__main__':
main()