Newer
Older
IrisServerWebSocket / src / main / java / com / ntlab / irisserver / entities / Game.java
Kota on 11 Dec 2022 5 KB test部屋は諦めた。
package com.ntlab.irisserver.entities;

import com.fasterxml.jackson.annotation.JsonProperty;

import java.util.*;

public class Game {

    @JsonProperty("assignments")
    private Map<Member, List<Integer>> assignments = new HashMap<>();//<Member, List<dno>>
    @JsonProperty("drawingList")
    private Map<Integer, Drawing> drawingList = new HashMap<>();//<dno,Drawing>
    @JsonProperty("keywordList")
    private Map<Integer, String> keywordList = new HashMap<>();//<dno, keyword>
    @JsonProperty("cellList")
    private Map<Integer, Cell> cellList = new HashMap<>();//<cno, Cell>

    @JsonProperty("map")
    private List<Integer> map = new ArrayList<>();//cno順にdnoを管理
    @JsonProperty("colorList")
    private List<String> colorList = new ArrayList<>();//cno順にr:赤,g:灰,b:青,d:黒を管理
    @JsonProperty("turnList")
    private List<Turn> turnList = new ArrayList<>();

    @JsonProperty("nowTurn")
    private Turn nowTurn = new Turn(); //現在のターン
    @JsonProperty("drawingController")
    private DrawingController drawingController = null;
    @JsonProperty("settings")
    private Settings settings = null;


    public Game(Room room, String[] keywords) {

        room.setGame(this);
        this.settings = room.getSettings();
        drawingController = new DrawingController(room, room.getMembers(), this);

        List<Member> memberList = room.getMembers();
        int num = 16/memberList.size();//一人あたりが確実に採用されるイラスト枚数
        int cnt = 0;
        for(int i=0; i<memberList.size(); i++){
            List<Integer> dnoList = new ArrayList<>();//一人が描くイラストのdnoリスト
            for(int j=0; j<num; j++){
                dnoList.add(cnt);
                cnt++;
            }
            assignments.put(memberList.get(i), dnoList);
        }
        int remainder = 0;//残りの割り当て枚数
        if(16%memberList.size()!=0) remainder = ((num+1)* memberList.size()) - cnt;

        for(int i=0; i<remainder; i++){
            List<Integer> dnoList =  assignments.get(memberList.get(i));
            dnoList.add(cnt);
            cnt++;
            assignments.put(memberList.get(i), dnoList);
        }

        for(int i=0; i<16; i++) map.add(i);
        Collections.shuffle(map);//0~15のdnoをランダムにマップに割り振る
        System.out.println(map);
        //r:6 b:5 g:4 d:1
        for(int i=0; i<6; i++) colorList.add("r");
        for(int i=0; i<5; i++) colorList.add("b");
        for(int i=0; i<4; i++) colorList.add("g");
        colorList.add("d");
        Collections.shuffle(colorList);//r,b,g,dをランダムにマップに割り振る

        List<String> randKeys = Arrays.asList(keywords);//コンストラクタの引数で受け取ったキーワードをリストに変換
        Collections.shuffle(randKeys);//キーワードをシャッフル

        //Cellのインスタンス作成
        for(int i=0; i<16; i++){
            Cell c = new Cell();
            cellList.put(i, c);
            c.setCno(i);
            c.setColor(colorList.get(i));
            c.setDno(map.get(i));

            //今回のゲームで使用するキーワードを追加
            //keywordList.put(i, randKeys.get(i));//(dno, キーワード)
        }
        int drawNum = 16;
        if(16%memberList.size()!=0) drawNum = (num+1)*memberList.size();

        for(int i=0; i<drawNum; i++){
            keywordList.put(i, randKeys.get(i));//(dno, キーワード)
        }
        //1ターン目の作成
        nowTurn = new Turn();
        nowTurn.setTeam("r");
        this.turnList.add(nowTurn);
    }

    public DrawingController getDrawingController() {
        return drawingController;
    }

    public Drawing getDrawing(int dno) {
        Drawing d = drawingList.get(dno);
        return d;
    }

    public Map<Integer, Drawing> getDrawings(){
        return drawingList;
    }

    public Drawing putDrawing(int dno, Drawing drawing) {
        Drawing d = drawingList.put(dno, drawing);
        drawingController.update();
        return d;
    }

    public String getKeyword(int dno) {
        String keyword = keywordList.get(dno);
        return keyword;
    }

//    public List<String> getKeywords() {
//        List<String> keywords = new ArrayList<>();
//        for(int i=0; i<16; i++){
//            keywords.add(keywordList.get(map.get(i)));
//        }
//        return keywords;
//    }

    public List<String> getKeywords() {
        List<String> keywords = new ArrayList<>();
        for(List<Integer> dnoList: assignments.values()){
            for(int i=0; i<dnoList.size(); i++){
                keywords.add(keywordList.get(dnoList.get(i)));
            }
        }
        return keywords;
    }

    public List<Boolean> getOpens(){
        List<Boolean> opens= new ArrayList<>();
        for(int i=0; i<16; i++){
            Cell cell = cellList.get(i);
            boolean isOpen = cell.getIsOpen();
            opens.add(isOpen);
        }
        return opens;
    }

    public Map<Member, List<Integer>> getAssignments(){
        return assignments;
    }

    public List<Integer> getMap(){
        return map;
    }
    public List<String> getColorList(){
        return colorList;
    }

    public Settings getSettings(){
        return this.settings;
    }

    public void createTurn(){
        nowTurn = new Turn();
        if(turnList.size()%2==0){//偶数ターンなら青色,奇数ターンは赤色
            nowTurn.setTeam("r");
        }else{
            nowTurn.setTeam("b");
        }
        this.turnList.add(nowTurn);
    }

    public Turn getTurn(Integer num){
        return turnList.get(num);
    }

    public Integer getNowTurn(){//現在何ターン目か取得
        return turnList.size()-1;
    }

    public Cell getCell(Integer cno){
        return cellList.get(cno);
    }

    public Map<Integer, Drawing> getDrawingList() {return drawingList;}
}