diff --git a/src/main/java/com/ntlab/irisserver/resources/MembersRest.java b/src/main/java/com/ntlab/irisserver/resources/MembersRest.java index 41533dc..1239ede 100644 --- a/src/main/java/com/ntlab/irisserver/resources/MembersRest.java +++ b/src/main/java/com/ntlab/irisserver/resources/MembersRest.java @@ -1,11 +1,97 @@ package com.ntlab.irisserver.resources; +import com.ntlab.irisserver.models.RoomManager; +import com.ntlab.irisserver.entities.Room; +import com.ntlab.irisserver.entities.Member; import org.springframework.stereotype.Component; -import javax.ws.rs.Path; +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("MembersRest") + + +@Path("/rooms") public class MembersRest { -} + public static void membershuffle(List m) { + + //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 || i==1){ + info.setMaster(true); + } + + //Listの偶数番目を"r"(赤)に、奇数番目を"b"(青)にする + if(i % 2 == 0){ + info.setBelongs("r"); + }else{ + info.setBelongs("b"); + } + } + + } + + //ランダムにチーム編成を送り、全員の役職を変更する + @PUT + @Path("/{rid}/members") + public void putMembers(@PathParam("rid") String rid, @FormParam("nickname") String nickname) { + RoomManager rm = RoomManager.getInstance(); + Room room = rm.getRoom(rid); + List gm = room.getMembers(); + + //引数で取ってきたnicknameがownerかどうか確認 +/* + if(nickname != room.ownerName) { + var response = Response.status(401).entity("あなたはオーナーではありません"); + throw new WebApplicationException(response.build()); + } +*/ + + //全員の役職を変更 + membershuffle(gm); + + + } + + //自分のnicknameを送り、Roomに追加 + @POST + @Path("/{rid}/members") + public void postMembers(@PathParam("rid") String rid, @FormParam("nickname") String nickname) { + RoomManager rm = RoomManager.getInstance(); + Room room = rm.getRoom(rid); + List m = room.getMembers(); + + //引数で取ってきたnicknameがListに存在しているか確認 +/* + for(int i=0; i<10; i++){ + Member info = m.get(i); + if(info.getNickname() == nickname) { + var response = Response.status(401).entity("入力したnicknameは既に存在しています"); + throw new WebApplicationException(response.build()); + } + } +*/ + room.addMember(nickname); + } + + + + + +} \ No newline at end of file