Newer
Older
algoLike.pull / src / main / java / algoLike / pull / ResultByDrawingB.java
Shinji on 25 Jan 2023 2 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.ArrayList;
import java.util.List;
import java.util.Map;

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

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

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