001/* 002 * PlotSquared, a land and world management plugin for Minecraft. 003 * Copyright (C) IntellectualSites <https://intellectualsites.com> 004 * Copyright (C) IntellectualSites team and contributors 005 * 006 * This program is free software: you can redistribute it and/or modify 007 * it under the terms of the GNU General Public License as published by 008 * the Free Software Foundation, either version 3 of the License, or 009 * (at your option) any later version. 010 * 011 * This program is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 014 * GNU General Public License for more details. 015 * 016 * You should have received a copy of the GNU General Public License 017 * along with this program. If not, see <https://www.gnu.org/licenses/>. 018 */ 019package com.plotsquared.core.plot; 020 021import com.plotsquared.core.configuration.Settings; 022 023import java.util.ArrayList; 024import java.util.HashMap; 025import java.util.List; 026import java.util.stream.IntStream; 027 028public class Rating { 029 030 private static final String LIKE_INTERNAL = "__LIKES__"; 031 032 /** 033 * This is a map of the rating category to the rating value 034 */ 035 private final HashMap<String, Integer> ratingMap; 036 private final int initial; 037 private boolean changed; 038 039 public Rating(int value) { 040 this.ratingMap = new HashMap<>(); 041 if (Settings.Ratings.USE_LIKES) { 042 this.initial = value == 10 ? 10 : 1; 043 this.ratingMap.put(LIKE_INTERNAL, this.initial == 10 ? 10 : 1); 044 } else { 045 this.initial = value; 046 if (Settings.Ratings.CATEGORIES != null && Settings.Ratings.CATEGORIES.size() > 1) { 047 if (value < 10) { 048 for (String ratingCategory : Settings.Ratings.CATEGORIES) { 049 this.ratingMap.put(ratingCategory, value); 050 } 051 this.changed = true; 052 return; 053 } 054 for (String ratingCategory : Settings.Ratings.CATEGORIES) { 055 this.ratingMap.put(ratingCategory, value % 10 - 1); 056 value = value / 10; 057 } 058 } else { 059 this.ratingMap.put(null, value); 060 } 061 } 062 } 063 064 public List<String> getCategories() { 065 if (this.ratingMap.size() == 1) { 066 return new ArrayList<>(0); 067 } 068 return new ArrayList<>(this.ratingMap.keySet()); 069 } 070 071 public double getAverageRating() { 072 if (Settings.Ratings.USE_LIKES) { 073 return getLike() ? 10 : 1; 074 } 075 double total = this.ratingMap.values().stream().mapToDouble(v -> v).sum(); 076 return total / this.ratingMap.size(); 077 } 078 079 public boolean getLike() { 080 final Integer rating = this.getRating(LIKE_INTERNAL); 081 return rating != null && rating == 10; 082 } 083 084 public Integer getRating(String category) { 085 return this.ratingMap.get(category); 086 } 087 088 public boolean setRating(String category, int value) { 089 this.changed = true; 090 if (!this.ratingMap.containsKey(category)) { 091 return false; 092 } 093 return this.ratingMap.put(category, value) != null; 094 } 095 096 public int getAggregate() { 097 if (!this.changed) { 098 return this.initial; 099 } 100 if (Settings.Ratings.USE_LIKES) { 101 return this.ratingMap.get(LIKE_INTERNAL); 102 } 103 if (Settings.Ratings.CATEGORIES != null && Settings.Ratings.CATEGORIES.size() > 1) { 104 return IntStream.range(0, Settings.Ratings.CATEGORIES.size()).map( 105 i -> (int) ((i + 1) * Math 106 .pow(10, this.ratingMap.get(Settings.Ratings.CATEGORIES.get(i))))).sum(); 107 } else { 108 return this.ratingMap.get(null); 109 } 110 111 } 112 113}