From 87c5be2a26576b0bbc58a00578d1eb719fd38c71 Mon Sep 17 00:00:00 2001 From: Daochen Zha Date: Wed, 22 Jul 2020 00:53:44 -0500 Subject: [PATCH] Add page splits --- README.md | 26 +++++++++++++------------- server/tournament/views.py | 21 ++++++++++++++++++--- 2 files changed, 31 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index e905a8c..d1898b9 100644 --- a/README.md +++ b/README.md @@ -30,23 +30,23 @@ The definitions of the fields are as follows: | type | Resource | Parameters | Description | |------|---------------------------|------------------------------------------------------|--------------------------------------------------------------------------------------------------------------------| -| GET | tournament/launch | `eval_num`, `name` | Launch tournment on the game. Each pair of models will play `eval_num` times. Results will be saved in database. | -| GET | tournament/query\_game | `name`, `index`, `agent0`, `agent1`, `win`, `payoff` | Query the games with the given parameters | -| GET | tournament/query\_payoff | `name`, `agent0`, `agent1`, `payoff` | Query the payoffs with the given parameters | -| GET | tournament/replay | `name`, `agent0`, `agent1`, `index` | Return the replay data | -| POST | tournament/upload\_agent | `model`(Python file), `name`, `game`, `entry` | Upload a model file. `name` is model ID, `entry` is the class name of the model | -| GET | tournament/delete\_agent | `name` | Delete the agent of the given name | -| GET | tournament/list\_agents | | list all the agents | +| GET | tournament/launch | `eval_num`, `name` | Launch tournment on the game. Each pair of models will play `eval_num` times. Results will be saved in database. | +| GET | tournament/query\_game | `name`, `index`, `agent0`, `agent1`, `win`, `payoff`, `elements_every_page`, `page_index` | Query the games with the given parameters | +| GET | tournament/query\_payoff | `name`, `agent0`, `agent1`, `payoff` | Query the payoffs with the given parameters | +| GET | tournament/replay | `name`, `agent0`, `agent1`, `index` | Return the replay data | +| POST | tournament/upload\_agent | `model`(Python file), `name`, `game`, `entry` | Upload a model file. `name` is model ID, `entry` is the class name of the model | +| GET | tournament/delete\_agent | `name` | Delete the agent of the given name | +| GET | tournament/list\_agents | | list all the agents | ## Example API | API | Description | |-----------------------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------| -| http://127.0.0.1:8000/tournament/launch?eval_num=200&name=leduc-holdem | Evaluate on Leduc Holdem with 200 games for each pair of models | -| http://127.0.0.1:8000/tournament/replay?name=leduc-holdem&agent0=leduc-holdem-rule-v1&agent1=leduc-holdem-cfr&index=3 | Obtain the replay data between rule model and CFR model. Obtain the data of the 3rd game | -| http://127.0.0.1:8000/tournament/query_game | Get all the game data | -| http://127.0.0.1:8000/tournament/query_game?name=leduc-holdem | Get all the game data of Leduc Holdem | -| http://127.0.0.1:8000/tournament/query_payoff | Get all the payoffs | -| http://127.0.0.1:8000/tournament/query_payoff?agent0=leduc-holdem-cfr&agent1=leduc-holdem-rule-v1 | Get all the payoffs between rule and CFR models | +| http://127.0.0.1:8000/tournament/launch?eval_num=200&name=leduc-holdem | Evaluate on Leduc Holdem with 200 games for each pair of models | +| http://127.0.0.1:8000/tournament/replay?name=leduc-holdem&agent0=leduc-holdem-rule-v1&agent1=leduc-holdem-cfr &index=3 | Obtain the replay data between rule model and CFR model. Obtain the data of the 3rd game | +| http://127.0.0.1:8000/tournament/query_game&elements_every_page=10&page_index=0 | Get all the game data | +| http://127.0.0.1:8000/tournament/query_game?name=leduc-holdem&elements_every_page=10&page_index=0 | Get all the game data of Leduc Holdem | +| http://127.0.0.1:8000/tournament/query_payoff | Get all the payoffs | +| http://127.0.0.1:8000/tournament/query_payoff?agent0=leduc-holdem-cfr&agent1=leduc-holdem-rule-v1 | Get all the payoffs between rule and CFR models | ## Registered Models Some models have been pre-registered as baselines diff --git a/server/tournament/views.py b/server/tournament/views.py index 4f9cc5f..3b8ea5c 100644 --- a/server/tournament/views.py +++ b/server/tournament/views.py @@ -1,6 +1,7 @@ import json import os import importlib.util +import math from django.shortcuts import render from django.http import HttpResponse @@ -41,6 +42,17 @@ def _reset_model_ids(): rlcard.models.registration.model_registry.model_specs[name] = ModelSpec() MODEL_IDS[game].append(name) +PAGE_FIELDS = ['elements_every_page', 'page_index'] + +def _get_page(result, elements_every_page, page_index): + elements_every_page = int(elements_every_page) + page_index = int(page_index) + total_page = math.ceil(len(result) / float(elements_every_page)) + begin = page_index * elements_every_page + end = min((page_index+1) * elements_every_page, total_page) + result = result[begin:end] + return result, total_page + def replay(request): if request.method == 'GET': name = request.GET['name'] @@ -53,10 +65,13 @@ def replay(request): def query_game(request): if request.method == 'GET': - filter_dict = {key: request.GET.get(key) for key in dict(request.GET).keys()} - result = Game.objects.filter(**filter_dict) + if not PAGE_FIELDS[0] in request.GET or not PAGE_FIELDS[1] in request.GET: + return HttpResponse(json.dumps({'value': -1, 'info': 'elements_every_page and page index should be given'})) + filter_dict = {key: request.GET.get(key) for key in dict(request.GET).keys() if key not in PAGE_FIELDS} + result = Game.objects.filter(**filter_dict).order_by('index') + result, total_page = _get_page(result, request.GET['elements_every_page'], request.GET['page_index']) result = serializers.serialize('json', result, fields=('name', 'index', 'agent0', 'agent1', 'win', 'payoff')) - return HttpResponse(result) + return HttpResponse(json.dumps({'value': 0, 'data': json.loads(result), 'total_page': total_page})) def query_payoff(request): if request.method == 'GET':