Newer
Older
IrisServer / src / main / java / com / ntlab / irisserver / resources / MembersRest.java
package com.ntlab.irisserver.resources;

import com.ntlab.irisserver.entities.RoomJson;
import com.ntlab.irisserver.models.RoomManager;
import com.ntlab.irisserver.entities.Room;
import com.ntlab.irisserver.entities.Member;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Collections;
import javax.ws.rs.*;
import java.util.List;
import java.util.Random;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

@Component
@Path("/rooms")
public class MembersRest {

    public static void membershuffle(String rid, List<Member> m) {
        RoomManager rm = RoomManager.getInstance();
        Room room = rm.getRoom(rid);

        //List<Member>をshuffleする
        Collections.shuffle(m);

        for(int i=0; i < 10; i++){

            //ListのMember人数以上になればbreakする
            if(i >= m.size())break;

            //各Memberの情報取ってくる
            Member info = m.get(i);

            //Listの0番目と1番目をチームマスターにする
            if(i==0){
                info.setMaster(true);
                room.changeTeamAndMaster(info.getNickname(), "r", true);
            }else if(i==1){
                info.setMaster(true);
                room.changeTeamAndMaster(info.getNickname(), "b", true);
            }else{
                info.setMaster(false);
            }

            //Listの偶数番目を"r"(赤)に、奇数番目を"b"(青)にする
            if(i % 2 == 0){
                info.setBelongs("r");
            }else{
                info.setBelongs("b");
            }

        }

    }

    //ランダムにチーム編成を送り、全員の役職を変更する
    @PUT
    @Path("/{rid}/members")
    @Produces(MediaType.APPLICATION_JSON)
    public void putMembers(@PathParam("rid") String rid, @FormParam("nickname") String nickname) {
        RoomManager rm = RoomManager.getInstance();
        Room room = rm.getRoom(rid);
        List<Member> gm = room.getMembers();

        //引数で取ってきたnicknameがownerかどうか確認
        String owner = room.getOwnerName();
        if(nickname.equals(owner)) {
            //全員の役職を変更
            membershuffle(rid, gm);
        }else{
            var response = Response.status(401).entity("あなたはオーナーではありません");
            throw new WebApplicationException(response.build());
        }


    }

    //自分のnicknameを送り、Roomに追加
    @POST
    @Path("/{rid}/members")
    @Produces(MediaType.APPLICATION_JSON)
    public RoomJson postMembers(@PathParam("rid") String rid, @FormParam("nickname") String nickname) {
        RoomManager rm = RoomManager.getInstance();
        Room room = rm.getRoom(rid);
        List<Member> gm = room.getMembers();
        RoomJson rj = new RoomJson(room);

        //引数で取ってきたnicknameがListに存在しているか確認
        if(room.getMember(nickname)==null && gm.size() < 10 ){
            room.addMember(nickname);
        } else {
            var response = Response.status(401).entity("満席です");
            throw new WebApplicationException(response.build());
        }

        return rj;

    }





}