Skip to content

Commit

Permalink
Implement voting
Browse files Browse the repository at this point in the history
Fixes #200
  • Loading branch information
1-alex98 committed Jun 2, 2018
1 parent 0101c8f commit 45ef310
Show file tree
Hide file tree
Showing 19 changed files with 877 additions and 39 deletions.
72 changes: 36 additions & 36 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -16,30 +16,30 @@ buildscript {
tasks.withType(Test) {

boolean TRAVIS_FOLDING = project.hasProperty('TRAVIS_FOLDING') ? project.TRAVIS_FOLDING : true
String ANSI_BOLD_WHITE = "\u001B[0;1m";
String ANSI_RESET = "\u001B[0m";
String ANSI_BLACK = "\u001B[30m";
String ANSI_RED = "\u001B[31m";
String ANSI_GREEN = "\u001B[32m";
String ANSI_YELLOW = "\u001B[33m";
String ANSI_BLUE = "\u001B[34m";
String ANSI_PURPLE = "\u001B[35m";
String ANSI_CYAN = "\u001B[36m";
String ANSI_WHITE = "\u001B[37m";
String CHECK_MARK = "\u2713";
String NEUTRAL_FACE = "\u0CA0_\u0CA0";
String X_MARK = "\u274C";
String ANSI_BOLD_WHITE = "\u001B[0;1m"
String ANSI_RESET = "\u001B[0m"
String ANSI_BLACK = "\u001B[30m"
String ANSI_RED = "\u001B[31m"
String ANSI_GREEN = "\u001B[32m"
String ANSI_YELLOW = "\u001B[33m"
String ANSI_BLUE = "\u001B[34m"
String ANSI_PURPLE = "\u001B[35m"
String ANSI_CYAN = "\u001B[36m"
String ANSI_WHITE = "\u001B[37m"
String CHECK_MARK = "\u2713"
String NEUTRAL_FACE = "\u0CA0_\u0CA0"
String X_MARK = "\u274C"

beforeSuite { suite ->
if (suite.name.startsWith("Test Run") || suite.name.startsWith("Gradle Worker")) {
return;
return
}

if (suite.parent != null && suite.className != null) {
if (TRAVIS_FOLDING) {
out.println("travis_fold:start:" + suite.name + "\r");
out.println("travis_fold:start:" + suite.name + "\r")
}
out.println(ANSI_BOLD_WHITE + suite.name + ANSI_RESET);
out.println(ANSI_BOLD_WHITE + suite.name + ANSI_RESET)
}

}
Expand All @@ -48,57 +48,57 @@ tasks.withType(Test) {
def indicator = ANSI_WHITE

if (result.failedTestCount > 0) {
indicator = ANSI_RED + X_MARK;
indicator = ANSI_RED + X_MARK
} else if (result.skippedTestCount > 0) {
indicator = ANSI_YELLOW + NEUTRAL_FACE;
indicator = ANSI_YELLOW + NEUTRAL_FACE
} else {
indicator = ANSI_GREEN + CHECK_MARK;
indicator = ANSI_GREEN + CHECK_MARK
}

out.println(' ' + indicator + ANSI_RESET + " " + descriptor.name);
out.println(' ' + indicator + ANSI_RESET + " " + descriptor.name)

if (result.failedTestCount > 0) {
out.println(' ');
out.println(' ')
}
}

afterSuite { desc, result ->
if (desc.parent != null && desc.className != null) {

if (TRAVIS_FOLDING && result.failedTestCount == 0) {
out.println("travis_fold:end:" + desc.name + "\r");
out.println("travis_fold:end:" + desc.name + "\r")
}
out.println("");
out.println("")
}

if (!desc.parent) { // will match the outermost suite
def failStyle = ANSI_RED;
def skipStyle = ANSI_YELLOW;
def summaryStyle = ANSI_WHITE;
def failStyle = ANSI_RED
def skipStyle = ANSI_YELLOW
def summaryStyle = ANSI_WHITE
if (result.failedTestCount > 0) {
failStyle = ANSI_RED;
failStyle = ANSI_RED
}
if (result.skippedTestCount > 0) {
skipStyle = ANSI_YELLOW;
skipStyle = ANSI_YELLOW
}

switch (result.resultType) {
case TestResult.ResultType.SUCCESS:
summaryStyle = ANSI_GREEN;
break;
summaryStyle = ANSI_GREEN
break
case TestResult.ResultType.FAILURE:
summaryStyle = ANSI_RED;
break;
summaryStyle = ANSI_RED
break
}

out.println("--------------------------------------------------------------------------");
out.println("--------------------------------------------------------------------------")
out.println("Results: " + summaryStyle + "${result.resultType}" + ANSI_RESET
+ " (${result.testCount} tests, "
+ ANSI_GREEN + "${result.successfulTestCount} passed" + ANSI_RESET
+ ", " + failStyle + "${result.failedTestCount} failed" + ANSI_RESET
+ ", " + skipStyle + "${result.skippedTestCount} skipped" + ANSI_RESET
+ ")");
out.println("--------------------------------------------------------------------------");
+ ")")
out.println("--------------------------------------------------------------------------")
}
}

Expand All @@ -110,7 +110,7 @@ apply plugin: 'propdeps'
apply plugin: 'idea'

group = 'faforever'
version = '1.4.1'
version = 'voting'

sourceCompatibility = 1.8
targetCompatibility = 1.8
Expand Down
12 changes: 11 additions & 1 deletion src/main/java/com/faforever/api/data/domain/Player.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package com.faforever.api.data.domain;

import com.faforever.api.data.checks.IsEntityOwner;
import com.github.jasminb.jsonapi.annotations.Type;
import com.yahoo.elide.annotation.Include;
import com.yahoo.elide.annotation.ReadPermission;
import com.yahoo.elide.annotation.SharePermission;
import com.yahoo.elide.annotation.UpdatePermission;
import lombok.Setter;
import org.hibernate.annotations.BatchSize;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.OneToMany;
Expand All @@ -22,14 +25,15 @@
@SharePermission(expression = "Prefab.Role.All")
@Setter
@Type(Player.TYPE_NAME)
public class Player extends Login {
public class Player extends Login implements OwnableEntity {

public static final String TYPE_NAME = "player";
private Ladder1v1Rating ladder1v1Rating;
private GlobalRating globalRating;
private List<ClanMembership> clanMemberships;
private List<NameRecord> names;
private List<AvatarAssignment> avatarAssignments;
private List<Vote> votes;

@OneToOne(mappedBy = "player", fetch = FetchType.LAZY)
@BatchSize(size = 1000)
Expand Down Expand Up @@ -78,4 +82,10 @@ public List<AvatarAssignment> getAvatarAssignments() {
public String toString() {
return "Player(" + getId() + ", " + getLogin() + ")";
}

@ReadPermission(expression = IsEntityOwner.EXPRESSION)
@OneToMany(mappedBy = "player", cascade = CascadeType.ALL, orphanRemoval = true)
public List<Vote> getVotes() {
return votes;
}
}
54 changes: 54 additions & 0 deletions src/main/java/com/faforever/api/data/domain/Vote.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package com.faforever.api.data.domain;

import com.faforever.api.data.checks.IsEntityOwner;
import com.yahoo.elide.annotation.Include;
import com.yahoo.elide.annotation.ReadPermission;
import com.yahoo.elide.annotation.UpdatePermission;
import lombok.Setter;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;
import java.util.List;

@Entity
@Table(name = "vote")
@Include(type = Vote.TYPE_NAME, rootLevel = true)
@ReadPermission(expression = IsEntityOwner.EXPRESSION)
@UpdatePermission(expression = "Prefab.Role.None")
@Setter
public class Vote extends AbstractEntity implements OwnableEntity {
public static final String TYPE_NAME = "vote";

private Player player;
private VotingSubject votingSubject;
private List<VotingAnswer> votingAnswers;

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "player_id")
public Player getPlayer() {
return player;
}

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "voting_subject_id")
public VotingSubject getVotingSubject() {
return votingSubject;
}

@OneToMany(mappedBy = "vote", cascade = CascadeType.ALL, orphanRemoval = true)
public List<VotingAnswer> getVotingAnswers() {
return votingAnswers;
}

@Transient
@Override
public Login getEntityOwner() {
return getPlayer();
}
}
45 changes: 45 additions & 0 deletions src/main/java/com/faforever/api/data/domain/VotingAnswer.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.faforever.api.data.domain;

import com.faforever.api.data.checks.IsEntityOwner;
import com.yahoo.elide.annotation.Include;
import com.yahoo.elide.annotation.ReadPermission;
import com.yahoo.elide.annotation.UpdatePermission;
import lombok.Setter;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

@Entity
@Table(name = "voting_answer")
@Include(type = VotingAnswer.TYPE_NAME)
@ReadPermission(expression = IsEntityOwner.EXPRESSION)
@UpdatePermission(expression = "Prefab.Role.None")
@Setter
public class VotingAnswer extends AbstractEntity {
public static final String TYPE_NAME = "votingAnswer";

private Vote vote;
private int alternativeOrdinal;
private VotingChoice votingChoice;

@Column(name = "alternative_ordinal")
public int getAlternativeOrdinal() {
return alternativeOrdinal;
}

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "vote_id")
public Vote getVote() {
return vote;
}

@ManyToOne(fetch = FetchType.LAZY)
@JoinColumn(name = "voting_choice_id")
public VotingChoice getVotingChoice() {
return votingChoice;
}
}
99 changes: 99 additions & 0 deletions src/main/java/com/faforever/api/data/domain/VotingChoice.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package com.faforever.api.data.domain;

import com.faforever.api.data.checks.permission.IsModerator;
import com.faforever.api.data.listeners.VotingChoiceEnricher;
import com.fasterxml.jackson.annotation.JsonBackReference;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.yahoo.elide.annotation.ComputedAttribute;
import com.yahoo.elide.annotation.CreatePermission;
import com.yahoo.elide.annotation.DeletePermission;
import com.yahoo.elide.annotation.Exclude;
import com.yahoo.elide.annotation.Include;
import com.yahoo.elide.annotation.ReadPermission;
import com.yahoo.elide.annotation.SharePermission;
import com.yahoo.elide.annotation.UpdatePermission;
import lombok.Setter;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.validation.constraints.NotNull;
import java.util.List;

@Entity
@Table(name = "voting_choice")
@ReadPermission(expression = "Prefab.Role.All")
@SharePermission(expression = IsModerator.EXPRESSION)
@DeletePermission(expression = IsModerator.EXPRESSION)
@UpdatePermission(expression = IsModerator.EXPRESSION)
@CreatePermission(expression = IsModerator.EXPRESSION)
@Include(rootLevel = true, type = VotingChoice.TYPE_NAME)
@Setter
@EntityListeners(VotingChoiceEnricher.class)
public class VotingChoice extends AbstractEntity {
public static final String TYPE_NAME = "votingChoice";

private String choiceTextKey;
private String choiceText;
private String descriptionKey;
private String description;
private int numberOfAnswers;
private int ordinal;
private VotingQuestion votingQuestion;
private List<VotingAnswer> votingAnswers;

@Column(name = "choice_text_key")
@NotNull
public String getChoiceTextKey() {
return choiceTextKey;
}

@ComputedAttribute
@Transient
public String getChoiceText() {
return choiceText;
}

@Column(name = "description_key")
public String getDescriptionKey() {
return descriptionKey;
}

@ComputedAttribute
@Transient
public String getDescription() {
return description;
}

@Column(name = "ordinal")
@NotNull
public int getOrdinal() {
return ordinal;
}

@Transient
@ComputedAttribute
public int getNumberOfAnswers() {
return numberOfAnswers;
}

@JsonBackReference
@JoinColumn(name = "voting_question_id")
@ManyToOne()
public VotingQuestion getVotingQuestion() {
return votingQuestion;
}

@JsonIgnore
@Exclude
@OneToMany(mappedBy = "votingChoice", cascade = CascadeType.ALL, orphanRemoval = true)
public List<VotingAnswer> getVotingAnswers() {
return votingAnswers;
}
}
Loading

0 comments on commit 45ef310

Please sign in to comment.