189 lines
5.1 KiB
Python
189 lines
5.1 KiB
Python
|
from flask import Flask, request, jsonify
|
||
|
from flask_json import FlaskJSON, as_json_p
|
||
|
from flask_cors import CORS
|
||
|
from board import Board
|
||
|
import tensorflow as tf
|
||
|
from eval import Eval
|
||
|
import argparse
|
||
|
import main
|
||
|
import random
|
||
|
from network import Network
|
||
|
|
||
|
parser = argparse.ArgumentParser(description="Backgammon games")
|
||
|
|
||
|
parser.add_argument('--model', action='store', dest='model',
|
||
|
default='player_testings',
|
||
|
help='name of Tensorflow model to use')
|
||
|
|
||
|
parser.add_argument('--board-rep', action='store', dest='board_rep',
|
||
|
default='tesauro',
|
||
|
help='name of board representation to use as input to neural network')
|
||
|
|
||
|
|
||
|
args = parser.parse_args()
|
||
|
|
||
|
|
||
|
app = Flask(__name__)
|
||
|
|
||
|
|
||
|
app.config['JSON_ADD_STATUS'] = False
|
||
|
app.config['JSON_JSONP_OPTIONAL'] = False
|
||
|
|
||
|
json = FlaskJSON(app)
|
||
|
CORS(app)
|
||
|
|
||
|
config = main.config.copy()
|
||
|
config['model'] = args.model
|
||
|
config['board_representation'] = args.board_rep
|
||
|
|
||
|
network = Network(config, config['model'])
|
||
|
|
||
|
sess = tf.Session()
|
||
|
network.restore_model(sess)
|
||
|
sess.run(tf.global_variables_initializer())
|
||
|
|
||
|
|
||
|
|
||
|
def calc_move_sets(from_board, roll, player):
|
||
|
board = from_board
|
||
|
sets = []
|
||
|
total = 0
|
||
|
for r in roll:
|
||
|
# print("Value of r:", r)
|
||
|
sets.append([Board.calculate_legal_states(board, player, [r, 0]), r])
|
||
|
total += r
|
||
|
sets.append([Board.calculate_legal_states(board, player, roll), total])
|
||
|
return sets
|
||
|
|
||
|
|
||
|
def tmp_name(from_board, to_board, roll, player, total_moves, is_quad=False):
|
||
|
sets = calc_move_sets(from_board, roll, player)
|
||
|
return_board = from_board
|
||
|
print("To board:\n",to_board)
|
||
|
print("All sets:\n",sets)
|
||
|
for idx, board_set in enumerate(sets):
|
||
|
board_set[0] = list(board_set[0])
|
||
|
# print(to_board)
|
||
|
# print(board_set)
|
||
|
if to_board in board_set[0]:
|
||
|
# print("To board:", to_board)
|
||
|
# print(board_set[0])
|
||
|
# print(board_set[1])
|
||
|
total_moves -= board_set[1]
|
||
|
# if it's not the sum of the moves
|
||
|
if idx < (4 if is_quad else 2):
|
||
|
roll[idx] = 0
|
||
|
else:
|
||
|
roll = [0, 0]
|
||
|
return_board = to_board
|
||
|
break
|
||
|
|
||
|
# print("Return board!:\n",return_board)
|
||
|
return total_moves, roll, return_board
|
||
|
|
||
|
def calc_move_stuff(from_board, to_board, roll, player, total_roll, is_quad):
|
||
|
|
||
|
total_moves, roll, board = tmp_name(from_board, to_board, list(roll), player, total_roll, is_quad)
|
||
|
return board, total_moves, roll
|
||
|
|
||
|
|
||
|
@app.route('/get_board', methods=['GET'])
|
||
|
@as_json_p
|
||
|
def get_board():
|
||
|
return {'board':'0, 2, 0, 0, 0, 0, -5, 0, -3, 0, 0, 0, 5, -5, 0, 0, 0, 3, 0, 5, 0, 0, 0, 0, -2, 0'}
|
||
|
|
||
|
|
||
|
|
||
|
def check_move(prev, curr):
|
||
|
|
||
|
# TODO: Decide on player system and implement roll properly
|
||
|
legal_states = Board.calculate_legal_states(tuple(prev), -1, [1,2])
|
||
|
|
||
|
truth_list = [list(curr) == list(ele) for ele in legal_states]
|
||
|
|
||
|
return any(truth_list)
|
||
|
|
||
|
|
||
|
@app.route('/pubeval_move', methods=['POST'])
|
||
|
def pubeval_move():
|
||
|
|
||
|
data = request.get_json(force=True)
|
||
|
board = [int(x) for x in data['board'].split(',')]
|
||
|
player = int(data['player'])
|
||
|
|
||
|
roll = (random.randrange(1,7), random.randrange(1,7))
|
||
|
|
||
|
board, value = Eval.make_pubeval_move(tuple(board), player, roll)
|
||
|
print("Doing pubeval move")
|
||
|
return ",".join([str(x) for x in list(board)])
|
||
|
|
||
|
|
||
|
@app.route('/network_move', methods=['POST'])
|
||
|
def network_move():
|
||
|
|
||
|
data = request.get_json(force=True)
|
||
|
board = [int(x) for x in data['board'].split(',')]
|
||
|
player = int(data['player'])
|
||
|
|
||
|
roll = (random.randrange(1,7), random.randrange(1,7))
|
||
|
|
||
|
board, value = network.make_move(sess, tuple(board), roll, player)
|
||
|
|
||
|
print("Doing network move")
|
||
|
return ",".join([str(x) for x in list(board)])
|
||
|
|
||
|
|
||
|
@app.route('/bot_move', methods=['POST'])
|
||
|
def bot_move():
|
||
|
data = request.get_json(force=True)
|
||
|
|
||
|
board = [int(x) for x in data['board'].split(',')]
|
||
|
use_pubeval = bool(data['pubeval'])
|
||
|
|
||
|
roll = (random.randrange(1, 7), random.randrange(1, 7))
|
||
|
|
||
|
if use_pubeval:
|
||
|
board, value = Eval.make_pubeval_move(tuple(board), 1, roll)
|
||
|
else:
|
||
|
board, _ = network.make_move(sess, tuple(board), roll, 1)
|
||
|
|
||
|
# print("Board!:",board)
|
||
|
|
||
|
return ",".join([str(x) for x in list(board)])
|
||
|
|
||
|
|
||
|
|
||
|
@app.route('/post_board', methods=['POST'])
|
||
|
def post_board():
|
||
|
data = request.get_json(force=True)
|
||
|
|
||
|
# TODO: Fix hardcoded player
|
||
|
player = -1
|
||
|
|
||
|
board = [int(x) for x in data['board'].split(',')]
|
||
|
prev_board = [int(x) for x in data['prevBoard'].split(',')]
|
||
|
print(data['roll'])
|
||
|
roll = [int(x) for x in data['roll'].split(',')]
|
||
|
print(roll)
|
||
|
quad = data['quad'] == "true"
|
||
|
|
||
|
|
||
|
# print(board)
|
||
|
|
||
|
total_roll = int(data['totalRoll'])
|
||
|
print("total roll is:", total_roll)
|
||
|
return_board, total_moves, roll = calc_move_stuff(tuple(prev_board), tuple(board), tuple(roll), player, total_roll, quad)
|
||
|
|
||
|
str_board = ",".join([str(x) for x in return_board])
|
||
|
str_roll = ",".join([str(x) for x in roll])
|
||
|
|
||
|
|
||
|
return_string = str_board + "#" + str(total_moves) + "#" + str_roll
|
||
|
|
||
|
print(return_string)
|
||
|
|
||
|
return return_string
|
||
|
|
||
|
if __name__ == '__main__':
|
||
|
app.run(host = '0.0.0.0', port=35270)
|