Newer
Older
SproutServerMicro / src / main / java / org / ntlab / SproutServer / battles / BattlesRest.java
s-bekki on 30 Nov 2017 2 KB initial commit
package org.ntlab.SproutServer.battles;


import net.arnx.jsonic.JSON;
import org.ntlab.SproutServer.accounts.Accounts;
import org.ntlab.SproutServer.rooms.Rooms;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import java.util.HashMap;

/**
 * http://nitta-lab-www2.is.konan-u.ac.jp/battles
 * 上記URIへの各種リクエストを処理するクラス
 *
 * @author matsumoto_k
 */
@Path("battles")
public class BattlesRest {
    public BattlesRest(){

    }

    @Path("/test")
    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String hoge() {
        return "deploy jenkins from tomcat";
    }

    /**
     * バトル一覧取得API
     * @return バトル一覧
     */
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public HashMap<Integer,Battle> getBattle(){
        return Battles.getInstance().getBattleMap();
    }

    /**
     * 指定したbattleIdのバトルを取得する
     * @param battleId
     * @return
     */
    @Path("/{battleId}")
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public Battle getBattle(@PathParam("battleId") int battleId) {
        return  Battles.getInstance().getBattleMap().get(battleId);
    }

    /**
     * 指定したパラメータのバトルを更新する
     * @param battleId
     * @param roomId
     * @param userId
     * @param stringPlayerData
     * @return
     */
    @Path("/{battleId}")
    @PUT
    public Battle putBattle(@PathParam("battleId") int battleId, @FormParam("roomId") int roomId,
                            @FormParam("userId") int userId, @FormParam("playerData") String stringPlayerData) {
        PlayerData playerData = JSON.decode(stringPlayerData, PlayerData.class);
        Battles.getInstance().updatePlayer(battleId, roomId, userId, playerData);
        return Battles.getInstance().findBattleById(battleId);
    }

    /**
     * ダミーデータの作成
     */
    @Path("/develop")
    @GET
    public void createDummyBattle() {
        Accounts accounts = Accounts.getInstance();
        if (!accounts.checkCreatedUser(1000)) {
            for (int i = 1000; i < 1008; i++) {
                accounts.createAccount(i);
            }
        }
        Rooms rooms = Rooms.getInstance();
        rooms.makeRoomBattle(1000, 1000, "room1", "hoge");

        rooms.makeRoomBattle(1001, 1004, "room2", "hoge");

        int battleId = 1000;

        Battles.getInstance().createDummyBattle(battleId, rooms.getRoomList().get(1000), rooms.getRoomList().get(1001));
    }
}