Skip to content

Commit

Permalink
Merge pull request #5 from cuappdev/cindy/factories
Browse files Browse the repository at this point in the history
added factories
  • Loading branch information
Aayush-Agnihotri authored Nov 10, 2024
2 parents d0ed9f5 + 1d7d5d4 commit dc1e8dc
Show file tree
Hide file tree
Showing 8 changed files with 226 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

# IntelliJ
.idea/
.idea
*.iml
*.iws
*.ipr
Expand Down
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@
<artifactId>jsoup</artifactId>
<version>1.17.2</version>
</dependency>
<dependency>
<groupId>com.github.javafaker</groupId>
<artifactId>javafaker</artifactId>
<version>1.0.2</version>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
60 changes: 60 additions & 0 deletions src/test/java/com/example/allin/AllInApplicationTests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package com.example.allin;

import org.junit.jupiter.api.Test;
import static org.mockito.Mockito.mock;
import com.example.allin.contract.Contract;
import com.example.allin.contract.util.ContractGenerator;
import com.example.allin.contract.Rarity;
import com.example.allin.player.Player;
import com.example.allin.user.User;
import com.example.allin.playerData.PlayerDataRepo;
import com.example.allin.data.PlayerFactory;
import com.example.allin.data.UserFactory;



class AllInApplicationTests {
@Test
public void testGenerateContract() {

PlayerDataRepo playerDataRepo = mock(PlayerDataRepo.class);
ContractGenerator contractGenerator = new ContractGenerator(playerDataRepo);

System.out.println("Testing ContractGenerator.generateContract()");
System.out.println("Common Probability Threshold: " + ContractGenerator.COMMON_PROBABILITY_THRESHOLD);
System.out.println("Rare Probability Threshold: " + ContractGenerator.RARE_PROBABILITY_THRESHOLD);
System.out.println("Epic Probability Threshold: " + ContractGenerator.EPIC_PROBABILITY_THRESHOLD);
System.out.println("Legendary Probability Threshold: " + ContractGenerator.LEGENDARY_PROBABILITY_THRESHOLD + "\n\n");

ContractGenerator.generateBounds();

System.out.println("Printing Probability and Payout Ranges\n");
System.out.println("Common Probability: [" + ContractGenerator.COMMON_PROB_LOWER_BOUND + ", " + ContractGenerator.COMMON_PROB_UPPER_BOUND + "]");
System.out.println("Common Payout: [" + ContractGenerator.COMMON_PAYOUT_LOWER_BOUND + ", " + ContractGenerator.COMMON_PAYOUT_UPPER_BOUND + "]\n");
System.out.println("Rare Probability: [" + ContractGenerator.RARE_PROB_LOWER_BOUND + ", " + ContractGenerator.RARE_PROB_UPPER_BOUND + "]");
System.out.println("Rare Payout: [" + ContractGenerator.RARE_PAYOUT_LOWER_BOUND + ", " + ContractGenerator.RARE_PAYOUT_UPPER_BOUND + "]\n");
System.out.println("Epic Probability: [" + ContractGenerator.EPIC_PROB_LOWER_BOUND + ", " + ContractGenerator.EPIC_PROB_UPPER_BOUND + "]");
System.out.println("Epic Payout: [" + ContractGenerator.EPIC_PAYOUT_LOWER_BOUND + ", " + ContractGenerator.EPIC_PAYOUT_UPPER_BOUND + "]\n");
System.out.println("Legendary Probability: [" + ContractGenerator.LEGENDARY_PROB_LOWER_BOUND + ", " + ContractGenerator.LEGENDARY_PROB_UPPER_BOUND + "]");
System.out.println("Legendary Payout: [" + ContractGenerator.LEGENDARY_PAYOUT_LOWER_BOUND + ", " + ContractGenerator.LEGENDARY_PAYOUT_UPPER_BOUND + "]\n");

double common_alpha = (ContractGenerator.COMMON_PROB_UPPER_BOUND + ContractGenerator.COMMON_PROB_LOWER_BOUND) * (ContractGenerator.COMMON_PAYOUT_UPPER_BOUND + ContractGenerator.COMMON_PAYOUT_LOWER_BOUND) / 4.0;
double rare_alpha = (ContractGenerator.RARE_PROB_UPPER_BOUND + ContractGenerator.RARE_PROB_LOWER_BOUND) * (ContractGenerator.RARE_PAYOUT_UPPER_BOUND + ContractGenerator.RARE_PAYOUT_LOWER_BOUND) / 4.0;
double epic_alpha = (ContractGenerator.EPIC_PROB_UPPER_BOUND + ContractGenerator.EPIC_PROB_LOWER_BOUND) * (ContractGenerator.EPIC_PAYOUT_UPPER_BOUND + ContractGenerator.EPIC_PAYOUT_LOWER_BOUND) / 4.0;
double legendary_alpha = (ContractGenerator.LEGENDARY_PROB_UPPER_BOUND + ContractGenerator.LEGENDARY_PROB_LOWER_BOUND) * (ContractGenerator.LEGENDARY_PAYOUT_UPPER_BOUND + ContractGenerator.LEGENDARY_PAYOUT_LOWER_BOUND) / 4.0;

System.out.println("Alphas:");
System.out.println("Common: " + common_alpha);
System.out.println("Rare: " + rare_alpha);
System.out.println("Epic: " + epic_alpha);
System.out.println("Legendary: " + legendary_alpha + "\n");

System.out.println("Generating a Contract\n");

User user = UserFactory.createRandomUser();
Player player = PlayerFactory.createFakePlayer();
Contract contract = contractGenerator.generateContract(user, player, 100.0, Rarity.Common);
System.out.println("Contract: " + contract.toString());
}

}
42 changes: 42 additions & 0 deletions src/test/java/com/example/allin/data/ContractFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.example.allin.data;
import com.example.allin.contract.OpposingTeam;
import com.example.allin.contract.Rarity;
import com.example.allin.user.User;
import com.example.allin.player.Player;
import com.example.allin.contract.Contract;
import com.example.allin.contract.Event;
import com.example.allin.data.PlayerFactory;
import com.github.javafaker.Faker;

import java.time.LocalDate;
import java.util.Random;

//needs tests
public class ContractFactory {
private final Faker faker = new Faker();
private final Random random = new Random();

private final PlayerFactory playerFactory = new PlayerFactory();
private final UserFactory userFactory = new UserFactory();


public Contract createRandomContract() {
Player player = playerFactory.createFakePlayer();
User owner = userFactory.createRandomUser();
Double buyPrice = faker.number().randomDouble(2, 100, 5000); // Random buy price between 100 and 5000
Rarity rarity = Rarity.getRandomRarity(); // Random rarity
OpposingTeam opposingTeam = OpposingTeam.getRandomOpposingTeam();
String opposingTeamImage = faker.internet().avatar(); // Random image URL
Event event = Event.getRandomEvent();
Integer eventThreshold = faker.number().numberBetween(1, 100); // Random event threshold between 1 and 100
LocalDate creationTime = LocalDate.now().minusDays(faker.number().numberBetween(1, 365)); // Within the past year
Double value = faker.number().randomDouble(2, 100, 5000); // Random value between 100 and 5000
Boolean expired = faker.bool().bool();
Boolean forSale = faker.bool().bool();
Double sellPrice = forSale ? faker.number().randomDouble(2, 100, 5000) : null; // If for sale, random sell price

return new Contract(player, owner, buyPrice, rarity, opposingTeam, opposingTeamImage, event, eventThreshold,
creationTime, value, expired, forSale, sellPrice);
}
}

32 changes: 32 additions & 0 deletions src/test/java/com/example/allin/data/PlayerDataFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.example.allin.data;
import com.example.allin.player.Player;
import com.example.allin.contract.OpposingTeam;
import com.example.allin.playerData.PlayerData;
import com.github.javafaker.Faker;

import java.time.LocalDate;
import java.util.Random;

//need to test this
public class PlayerDataFactory {
private static final Faker faker = new Faker();
private final Random random = new Random();

public static PlayerData createRandomPlayerData(Player player, OpposingTeam opposingTeam) {
LocalDate gameDate = LocalDate.now().minusDays(faker.number().numberBetween(1, 365)); // Random date within the past year
Integer points = faker.number().numberBetween(0, 50); // Points scored
Integer minutes = faker.number().numberBetween(10, 48); // Minutes played
Integer fieldGoals = faker.number().numberBetween(0, points / 2); // Field goals made (estimate based on points)
Integer threePointers = faker.number().numberBetween(0, fieldGoals / 3); // Three-pointers made
Integer freeThrows = faker.number().numberBetween(0, points - fieldGoals * 2); // Free throws
Integer rebounds = faker.number().numberBetween(0, 15);
Integer assists = faker.number().numberBetween(0, 15);
Integer steals = faker.number().numberBetween(0, 5);
Integer blocks = faker.number().numberBetween(0, 5);
Integer turnovers = faker.number().numberBetween(0, 7);
Integer fouls = faker.number().numberBetween(0, 5);

return new PlayerData(player, gameDate, opposingTeam, points, minutes, fieldGoals, threePointers, freeThrows,
rebounds, assists, steals, blocks, turnovers, fouls);
}
}
41 changes: 41 additions & 0 deletions src/test/java/com/example/allin/data/PlayerFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.example.allin.data;
import com.example.allin.player.Player;
import com.example.allin.player.Position;
import com.github.javafaker.Faker;
import java.util.Random;


public class PlayerFactory {
private static final Faker faker = new Faker();
private static final Random random = new Random();


public static Player createFakePlayer() {
String firstName = faker.name().firstName();
String lastName = faker.name().lastName();
Position[] positions = generateRandomPositions();
Integer number = faker.number().numberBetween(1, 99); // Jersey numbers typically range from 1-99
String height = generateRandomHeight();
Integer weight = faker.number().numberBetween(150, 250); // Weight in pounds, adjustable as needed
String hometown = faker.address().city() + ", " + faker.address().state();
String highSchool = faker.educator().secondarySchool();
return new Player(firstName, lastName, positions, number, height, weight, hometown, highSchool);
}

private static Position[] generateRandomPositions() {
Position[] allPositions = Position.values();
int count = random.nextInt(2) + 1; // Players can have 1 or 2 positions
Position[] positions = new Position[count];
for (int i = 0; i < count; i++) {
positions[i] = allPositions[random.nextInt(allPositions.length)];
}
return positions;
}

private static String generateRandomHeight() {
int feet = faker.number().numberBetween(5, 7); // Heights between 5 and 7 feet
int inches = faker.number().numberBetween(0, 11);
return feet + "'" + inches + "\"";
}

}
29 changes: 29 additions & 0 deletions src/test/java/com/example/allin/data/TransactionFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.example.allin.data;
import com.example.allin.transaction.Transaction;
import com.example.allin.user.User;
import com.example.allin.contract.Contract;
import com.example.allin.data.ContractFactory;
import com.github.javafaker.Faker;

import java.time.LocalDate;

//needs tests
public class TransactionFactory {
private final Faker faker = new Faker();
private final UserFactory userFactory = new UserFactory();
private final ContractFactory contractFactory = new ContractFactory();

public Transaction createRandomTransaction() {
User seller = userFactory.createRandomUser();
User buyer = userFactory.createRandomUser();
while (buyer.equals(seller)) { // Ensure buyer and seller are different
buyer = userFactory.createRandomUser();
}

Contract contract = contractFactory.createRandomContract();
LocalDate transactionDate = LocalDate.now().minusDays(faker.number().numberBetween(1, 365)); // Date within the past year
Double price = faker.number().randomDouble(2, 100, 10000); // Random price between 100 and 10000

return new Transaction(seller, buyer, contract, transactionDate, price);
}
}
16 changes: 16 additions & 0 deletions src/test/java/com/example/allin/data/UserFactory.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.example.allin.data;
import com.example.allin.user.User;
import com.github.javafaker.Faker;

public class UserFactory {
private static final Faker faker = new Faker();

public static User createRandomUser() {
String username = faker.name().username();
String email = faker.internet().emailAddress();
String image = faker.internet().avatar();
Double balance = faker.number().randomDouble(2, 10, 1000);

return new User(username, email, image, balance);
}
}

0 comments on commit dc1e8dc

Please sign in to comment.