Skip to content

Commit

Permalink
added method for extracting statistics from a replay file
Browse files Browse the repository at this point in the history
  • Loading branch information
Zomis committed Mar 20, 2015
1 parent 4f4a5cf commit 4d7c946
Showing 1 changed file with 61 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,12 @@
import com.cardshifter.ai.AIs;
import com.cardshifter.ai.ScoringAI;
import com.cardshifter.api.CardshifterConstants;
import com.cardshifter.core.game.FakeClient;
import com.cardshifter.core.game.ModCollection;
import com.cardshifter.core.game.TCGGame;
import com.cardshifter.core.replays.ReplayAction;
import com.cardshifter.core.replays.ReplayPlaybackSystem;
import com.cardshifter.core.replays.ReplayRecordSystem;
import com.cardshifter.modapi.actions.ActionPerformEvent;
import com.cardshifter.modapi.actions.attack.AttackEvent;
import com.cardshifter.modapi.actions.attack.DamageEvent;
Expand All @@ -20,13 +25,19 @@
import com.cardshifter.modapi.phase.PhaseStartEvent;
import com.cardshifter.modapi.players.Players;
import com.cardshifter.modapi.resources.ResourceValueChange;
import com.cardshifter.server.commands.ReplayCommand;
import com.cardshifter.server.main.ServerMain;
import com.cardshifter.server.model.GameFactory;
import net.zomis.cardshifter.ecs.config.ConfigComponent;
import net.zomis.cardshifter.ecs.usage.CardshifterIO;
import net.zomis.fight.statextract.Extractor;
import net.zomis.fight.statextract.IndexableResults;
import net.zomis.fight.statextract.InstancePoster;
import org.apache.log4j.PropertyConfigurator;

import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

/**
Expand All @@ -46,17 +57,66 @@ public static void main(String[] args) {

private void run() {
for (int i = 0; i < GAMES; i++) {
playGame();
// playGame();
replayGame("extract-replay.json");
}
IndexableResults results = extractor.collectIndexable();
System.out.println(results.unindexed().getData());
}

private void replayGame(String filename) {
ReplayRecordSystem replay;
File file = new File(filename);
try {
replay = CardshifterIO.mapper().readValue(file, ReplayRecordSystem.class);
} catch (IOException e1) {
throw new RuntimeException("Error loading replay: " + e1.getMessage(), e1);
}

String actualMod = replay.getModName() != null ? replay.getModName() : null;
ECSMod mod = mods.getModFor(actualMod);
final InstancePoster instance = extractor.postPrimary();

ECSGame game = new ECSGame();
ReplayPlaybackSystem playback = new ReplayPlaybackSystem(game, replay);
game.addSystem(playback);
registerEvents(game, instance);

mod.declareConfiguration(game);
playback.setPlayerConfigs(game);
// is decks configured correctly?
mod.setupGame(game);

game.startGame();
// while (!game.isGameOver()) {
while (!playback.isReplayFinished()) {
// AISystem.call(game);
ReplayAction step = playback.nextStep();
System.out.println("next step: " + step);
}
}

private void playGame() {
ECSMod mod = mods.getModFor(CardshifterConstants.VANILLA);
final InstancePoster instance = extractor.postPrimary();
ECSGame game = new ECSGame();
game.setRandomSeed(42);
registerEvents(game, instance);
mod.declareConfiguration(game);
List<Entity> players = Players.getPlayersInGame(game);
players.get(0).addComponent(new AIComponent(new ScoringAI(AIs.medium())));
players.get(1).addComponent(new AIComponent(new ScoringAI(AIs.fighter())));
for (Entity entity : players) {
entity.getComponent(AIComponent.class).getAI().configure(entity, entity.getComponent(ConfigComponent.class));
}
mod.setupGame(game);
game.startGame();
while (!game.isGameOver()) {
AISystem.call(game);
}
}

private void registerEvents(ECSGame game, InstancePoster instance) {
game.getEvents().registerHandlerAfter(this, GameOverEvent.class, e -> instance.post(e));
game.getEvents().registerHandlerAfter(this, StartGameEvent.class, e -> instance.post(e));
game.getEvents().registerHandlerAfter(this, ActionPerformEvent.class, instance::post);
Expand All @@ -70,18 +130,6 @@ private void playGame() {
game.getEvents().registerHandlerAfter(this, PhaseEndEvent.class, instance::post);
game.getEvents().registerHandlerAfter(this, ResourceValueChange.class, instance::post);
game.getEvents().registerHandlerAfter(this, ZoneChangeEvent.class, instance::post);
mod.declareConfiguration(game);
List<Entity> players = Players.getPlayersInGame(game);
players.get(0).addComponent(new AIComponent(new ScoringAI(AIs.medium())));
players.get(1).addComponent(new AIComponent(new ScoringAI(AIs.fighter())));
for (Entity entity : players) {
entity.getComponent(AIComponent.class).getAI().configure(entity, entity.getComponent(ConfigComponent.class));
}
mod.setupGame(game);
game.startGame();
while (!game.isGameOver()) {
AISystem.call(game);
}
}

}

0 comments on commit 4d7c946

Please sign in to comment.