gapoera.api

View Source
from gapoera.envs.surakarta import Surakarta
from gapoera.envs.mancala import Mancala
import requests

__all__ = ['Game']
    
class Game(object):
    """
        Digunakan untuk memudahkan Python user ketika akan menggunakan Gapoera API
        Berisikan perintah-perintah dasar environment yang diterjemahkan ke `request`
    """
    def __init__(self, game_name, host_url="http://api.gapoera.com"):
        self.url = host_url
        self.game_name = game_name

    def start(self):
        resp = requests.get(url=self.url+"/"+self.game_name)
        print(resp)
        self.data = resp.json()
        self.game_id = self.data["game_id"]
        self.state = self.data["state"]
        return self.game_id
    
    def step(self, player_id=0, action=0):
        response = requests.get(url=self.url+"/"+self.game_name+"/"+self.game_id+"/step/"+str(player_id)+"/" + str(action)).json()
        if response["message"]=="OK":
            self.state = response["state"]
        return response

    def bot_step(self):
        response = requests.get(url=self.url+"/"+self.game_name+"/"+self.game_id+"/bot", timeout=None).json()
        if response["message"]=="OK":
            self.state = response["state"]
        return response

    def valid_act(self, player_id=0):
        response = requests.get(url=self.url+"/"+self.game_name+"/"+self.game_id+"/valid/"+str(player_id)).json()
        return list(response["return"])

    def get_state(self):
        response = requests.get(url=self.url+"/"+self.game_name+"/"+self.game_id+"/state").json()
        if response["message"]=="OK":
            self.state = response["state"]
        return self.state
    
    def observe(self, player_id=0):
        response = requests.get(url=self.url+"/"+self.game_name+"/"+self.game_id+"/observe/"+str(player_id)).json()
        if response["message"]=="OK":
            self.state = response["state"]
        return self.state

    def reset_state(self):
        response = requests.get(url=self.url+"/"+self.game_name+"/"+self.game_id+"/reset_state").json()
        if response["message"]=="OK":
            self.state = response["state"]
        return response 

    def score(self):
        return self.state["score"]
    
    def is_over(self):
        return self.state["is_over"]

    def current_player(self):
        return self.state["current_player"]

    def simulate_start(self):
        requests.get(url=self.url+"/"+self.game_name+"/"+self.game_id+"/sim_start")
    
    def simulate_stop(self):
        requests.get(url=self.url+"/"+self.game_name+"/"+self.game_id+"/sim_stop")


class __OfflineGame(object):
    """ 
        Digunakan untuk memudahkan Flask server mengakses environment
        Bisa digunakan juga bagi yang mau memainkan game ini lokal
    """
    def __init__(self, game_name, *args):
        self.__states = []
        if game_name.lower()=="surakarta":
            self.env = Surakarta()
        elif game_name.lower()=="mancala":
            if args:
                self.env = Mancala(len_board=args[0], stone_each=args[1])
            else:
                self.env = Mancala(len_board=7, stone_each=7)
    
    def step(self, player_id=0, action=0):
        status, reward = self.env.step(player_id, action)
        return {"status":status, "reward":reward}

    def valid_act(self, player_id=0):
        return self.env.valid_act(player_id)

    def simulate_start(self):
        self.__states.append(self.env.game_state())

    def simulate_stop(self):
        if self.in_simulation():
            self.env.set_game_state(self.__states.pop())

    def in_simulation(self):
        return len(self.__states)!=0

    def get_state(self):
        return self.env.game_state()

    def observe(self, player_id):
        return self.env.observe(player_id)

    def set_game_state(self, state):
        return self.env.set_game_state(state)

    def render(self, state=None):
        if state==None:
            state = self.get_state()
        self.env.render(state)

    def reset_state(self):
        self.env.reset()

    def score(self):
        return self.env.get_score()
    
    def is_over(self):
        return self.env.gameover()

    def describe(self):
        return self.env.describe()

    def current_player(self):
        return self.env.get_current_player()
#   class Game:
View Source
class Game(object):
    """
        Digunakan untuk memudahkan Python user ketika akan menggunakan Gapoera API
        Berisikan perintah-perintah dasar environment yang diterjemahkan ke `request`
    """
    def __init__(self, game_name, host_url="http://api.gapoera.com"):
        self.url = host_url
        self.game_name = game_name

    def start(self):
        resp = requests.get(url=self.url+"/"+self.game_name)
        print(resp)
        self.data = resp.json()
        self.game_id = self.data["game_id"]
        self.state = self.data["state"]
        return self.game_id
    
    def step(self, player_id=0, action=0):
        response = requests.get(url=self.url+"/"+self.game_name+"/"+self.game_id+"/step/"+str(player_id)+"/" + str(action)).json()
        if response["message"]=="OK":
            self.state = response["state"]
        return response

    def bot_step(self):
        response = requests.get(url=self.url+"/"+self.game_name+"/"+self.game_id+"/bot", timeout=None).json()
        if response["message"]=="OK":
            self.state = response["state"]
        return response

    def valid_act(self, player_id=0):
        response = requests.get(url=self.url+"/"+self.game_name+"/"+self.game_id+"/valid/"+str(player_id)).json()
        return list(response["return"])

    def get_state(self):
        response = requests.get(url=self.url+"/"+self.game_name+"/"+self.game_id+"/state").json()
        if response["message"]=="OK":
            self.state = response["state"]
        return self.state
    
    def observe(self, player_id=0):
        response = requests.get(url=self.url+"/"+self.game_name+"/"+self.game_id+"/observe/"+str(player_id)).json()
        if response["message"]=="OK":
            self.state = response["state"]
        return self.state

    def reset_state(self):
        response = requests.get(url=self.url+"/"+self.game_name+"/"+self.game_id+"/reset_state").json()
        if response["message"]=="OK":
            self.state = response["state"]
        return response 

    def score(self):
        return self.state["score"]
    
    def is_over(self):
        return self.state["is_over"]

    def current_player(self):
        return self.state["current_player"]

    def simulate_start(self):
        requests.get(url=self.url+"/"+self.game_name+"/"+self.game_id+"/sim_start")
    
    def simulate_stop(self):
        requests.get(url=self.url+"/"+self.game_name+"/"+self.game_id+"/sim_stop")

Digunakan untuk memudahkan Python user ketika akan menggunakan Gapoera API Berisikan perintah-perintah dasar environment yang diterjemahkan ke request

#   Game(game_name, host_url='http://api.gapoera.com')
View Source
    def __init__(self, game_name, host_url="http://api.gapoera.com"):
        self.url = host_url
        self.game_name = game_name
#   def start(self):
View Source
    def start(self):
        resp = requests.get(url=self.url+"/"+self.game_name)
        print(resp)
        self.data = resp.json()
        self.game_id = self.data["game_id"]
        self.state = self.data["state"]
        return self.game_id
#   def step(self, player_id=0, action=0):
View Source
    def step(self, player_id=0, action=0):
        response = requests.get(url=self.url+"/"+self.game_name+"/"+self.game_id+"/step/"+str(player_id)+"/" + str(action)).json()
        if response["message"]=="OK":
            self.state = response["state"]
        return response
#   def bot_step(self):
View Source
    def bot_step(self):
        response = requests.get(url=self.url+"/"+self.game_name+"/"+self.game_id+"/bot", timeout=None).json()
        if response["message"]=="OK":
            self.state = response["state"]
        return response
#   def valid_act(self, player_id=0):
View Source
    def valid_act(self, player_id=0):
        response = requests.get(url=self.url+"/"+self.game_name+"/"+self.game_id+"/valid/"+str(player_id)).json()
        return list(response["return"])
#   def get_state(self):
View Source
    def get_state(self):
        response = requests.get(url=self.url+"/"+self.game_name+"/"+self.game_id+"/state").json()
        if response["message"]=="OK":
            self.state = response["state"]
        return self.state
#   def observe(self, player_id=0):
View Source
    def observe(self, player_id=0):
        response = requests.get(url=self.url+"/"+self.game_name+"/"+self.game_id+"/observe/"+str(player_id)).json()
        if response["message"]=="OK":
            self.state = response["state"]
        return self.state
#   def reset_state(self):
View Source
    def reset_state(self):
        response = requests.get(url=self.url+"/"+self.game_name+"/"+self.game_id+"/reset_state").json()
        if response["message"]=="OK":
            self.state = response["state"]
        return response 
#   def score(self):
View Source
    def score(self):
        return self.state["score"]
#   def is_over(self):
View Source
    def is_over(self):
        return self.state["is_over"]
#   def current_player(self):
View Source
    def current_player(self):
        return self.state["current_player"]
#   def simulate_start(self):
View Source
    def simulate_start(self):
        requests.get(url=self.url+"/"+self.game_name+"/"+self.game_id+"/sim_start")
#   def simulate_stop(self):
View Source
    def simulate_stop(self):
        requests.get(url=self.url+"/"+self.game_name+"/"+self.game_id+"/sim_stop")