Skip to content
Browse files

Few logic and bug fixes. NPE fix on random, item names.

  • Loading branch information...
1 parent 125759a commit ec81fa843e8e1dc80cc9076417e312e3a4869e1b @ingenthr ingenthr committed Jul 27, 2011
View
49 src/com/couchbase/demo/gamesim/GameSimDriver.java
@@ -59,18 +59,16 @@
Player player;
private final String bucketname;
private String bucketpass;
- byte[] buffer = new byte[8192];
private static MemcachedClient gamesimStore;
- private final int ACTORMULT = 100;
+ private final int ACTORMULT = 500000 / players.length;
private static final String[] players = {"Matt", "Steve", "Dustin",
"James", "Trond", "Melinda",
- "Bob", "Perry",
+ "Bob", "Perry", "Sharon",
"Leila", "Tony", "Damien", "Jan", "JChris",
"Volker", "Dale", "Aaron", "Aliaksey", "Frank",
"Mike", "Claire", "Benjamin", "Tony", "Keith",
"Bin", "Chiyoung", "Jens", "Srini"
};
-
// See http://en.wikipedia.org/wiki/Category:Celtic_legendary_creatures
private static final String[] monsters = {"Bauchan", "Fachen", "Fuath", "Joint-eater", "Kelpie",
"Knocker", "Merrow", "Morgen", "Pictish-beast", "Wild-man"};
@@ -124,22 +122,11 @@ public GameSimDriver() throws XPathExpressionException, IOException {
}
@OnceBefore
- public void setup() {
- Integer playerseq = (Integer) gamesimStore.get("playerseq");
- if (playerseq == null || playerseq < ACTORMULT) {
- logger.info("System has no player sequence, populating with players");
- populatePlayers(ACTORMULT);
- } else {
- logger.info("Populated with players, no population done.");
- }
+ public void setup() throws InterruptedException {
+
+ populatePlayers(ACTORMULT);
+ populateMonsters(ACTORMULT);
- Integer monsterseq = (Integer) gamesimStore.get("monsterseq");
- if (monsterseq == null || monsterseq < ACTORMULT) {
- logger.info("System has no monster sequence, populating with monsters");
- populateMonsters(ACTORMULT);
- } else {
- logger.info("Populated with monsters, no population done.");
- }
}
@@ -223,9 +210,22 @@ public void doAttackRandom() throws IOException, InterruptedException, Execution
doLogin();
String attackerName = getRandomMonster();
ctx.recordTime();
- Monster attacker = gson.fromJson((String)gamesimStore.get(attackerName), Monster.class);
+ Monster attacker = gson.fromJson((String) gamesimStore.get(attackerName), Monster.class);
+ assert (attacker != null);
- Double playerWinProbable = new Double(player.getHitpoints()) / new Double(player.getHitpoints()) + new Double(attacker.getHitpoints());
+
+ Double ahpd = null;
+ Double phpd = null;
+ try {
+ phpd = new Double(player.getHitpoints());
+ ahpd = new Double(attacker.getHitpoints());
+ } catch (NullPointerException e) {
+ }
+
+ assert (ahpd != null);
+ assert (phpd != null);
+
+ Double playerWinProbable = phpd / (phpd + ahpd);
if (playerWinProbable > 0.5d) {
Double itemProb = random.drandom(0.0d, 1.0d);
if (itemProb <= attacker.getItemProbability()) {
@@ -274,16 +274,16 @@ private String getRandomPlayerName() {
}
private String getRandomMonsterName() {
- int i = random.random(0, monsters.length -1);
+ int i = random.random(0, monsters.length - 1);
return monsters[i];
}
private String getRandomMonster() {
- return getRandomMonsterName() + random.random(0, ACTORMULT);
+ return getRandomMonsterName() + random.random(0, ACTORMULT - 1);
}
private String getRandomPlayer() {
- return getRandomPlayerName() + random.random(0, ACTORMULT);
+ return getRandomPlayerName() + random.random(0, ACTORMULT - 1);
}
/**
@@ -292,5 +292,4 @@ private String getRandomPlayer() {
public static Random getRandom() {
return random;
}
-
}
View
3 src/com/couchbase/demo/gamesim/Item.java
@@ -21,6 +21,7 @@
public Item(UUID itemOwner) {
ownerUuid = itemOwner;
uuid = UUID.randomUUID();
+ name = weapons[GameSimDriver.getRandom().random(0, weapons.length-1)] + itemOwner;
}
protected Item() {
@@ -35,6 +36,6 @@ public UUID getUuid() {
}
public String getItemName() {
- return "item" + uuid;
+ return name;
}
}
View
9 src/com/couchbase/demo/gamesim/Monster.java
@@ -7,15 +7,12 @@
import java.util.UUID;
-import com.sun.faban.driver.util.Random;
-
/**
*
* @author ingenthr
*/
public class Monster {
- private Random random = new Random();
private String jsonType = "monster";
private String name;
private UUID uuid;
@@ -26,9 +23,9 @@
public Monster(String monstername) {
name = monstername;
uuid = UUID.randomUUID();
- experienceWhenKilled = random.random(10, 100);
- hitpoints = random.random(10, 100);
- itemProbability = random.drandom(0.1d, 0.6d);
+ experienceWhenKilled = GameSimDriver.getRandom().random(10, 100);
+ hitpoints = GameSimDriver.getRandom().random(10, 5000);
+ itemProbability = GameSimDriver.getRandom().drandom(0.1d, 0.6d);
}
protected Monster() {
View
8 src/com/couchbase/demo/gamesim/Player.java
@@ -2,7 +2,6 @@
import java.util.UUID;
-import com.sun.faban.driver.util.Random;
import java.util.ArrayList;
/**
@@ -17,15 +16,14 @@
private int experience;
private int level;
private boolean loggedIn;
- private final Random random = new Random();
private ArrayList<String> playerItems;
public Player(String playerName) {
name = playerName;
uuid = UUID.randomUUID();
- hitpoints = random.random(70, 150);
- level = random.random(1, 5);
- experience = random.random(100*2^level, (100*2^(level+1)-1));
+ hitpoints = GameSimDriver.getRandom().random(70, 150);
+ level = GameSimDriver.getRandom().random(1, 5);
+ experience = GameSimDriver.getRandom().random(100*2^level, (100*2^(level+1)-1));
}
protected Player() {

0 comments on commit ec81fa8

Please sign in to comment.
Something went wrong with that request. Please try again.