Newer
Older
algoLike.pull / src / main / java / algoLike / pull / ResultBySelectingA.java
Shinji on 25 Jan 2023 1 KB pullに変更
package algoLike.pull;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.stereotype.Component;
import pushPullRefactor.Getter;
import pushPullRefactor.Message;
import pushPullRefactor.Resource;
import pushPullRefactor.State;

import javax.ws.rs.*;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.core.Form;
import javax.ws.rs.core.MediaType;
import java.util.AbstractMap;
import java.util.List;
import java.util.Map;

@Path("/resultBySelectingA")
@Component
@Resource("resultBySelectingA")
public class ResultBySelectingA {
    private final Client client = ClientBuilder.newClient();
    @State
    private Map.Entry<Boolean, Map.Entry<Integer, Integer>> value;

    @PUT
    @Message({})
    public void selectAndAttackA(@FormParam("t") int t, @FormParam("g") int g, @FormParam("b") List<Map.Entry<Integer, Boolean>> b, @FormParam("a") int a) throws JsonProcessingException {
        this.value = new AbstractMap.SimpleEntry<>((b.get(t).getKey() == g), new AbstractMap.SimpleEntry<>(t, a));
        Form form = new Form();
        form.param("resultBySelectingA", new ObjectMapper().writeValueAsString(this.value));
        Entity<Form> entity = Entity.entity(form, MediaType.APPLICATION_FORM_URLENCODED);
        String result = client.target("http://localhost:8080").path("/handsA/resultBySelectingA").request().post(entity, String.class);
        form = new Form();
        form.param("resultBySelectingA", new ObjectMapper().writeValueAsString(this.value));
        entity = Entity.entity(form, MediaType.APPLICATION_FORM_URLENCODED);
        result = client.target("http://localhost:8080").path("/handsB/resultBySelectingA").request().post(entity, String.class);
    }

    @Produces(MediaType.APPLICATION_JSON)
    @GET
    @Getter
    public Map.Entry<Boolean, Map.Entry<Integer, Integer>> getValue() {
        return value;
    }
}