Skip to content

Commit

Permalink
Merged with skiwi's changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Zomis committed Sep 8, 2014
2 parents f13af17 + b536700 commit b91d767
Show file tree
Hide file tree
Showing 31 changed files with 531 additions and 434 deletions.
22 changes: 11 additions & 11 deletions .travis.yml
@@ -1,12 +1,12 @@
language: java

jdk:
- oraclejdk8

notifications:
webhooks:
urls:
- secure: "SPXpxdUqqEMqGJK1WbNOo4RxS9Sx2CKvmIUaadwFsSg34qR0KSOhXMnWEo6s+uFLv8OB+Q/nYtWvD9Xg6n+QfgkgmyOLiYsN8cGTG4B8KFKn3Kbl6mfG8IzRP+nQYzXCqcuQ6gf+eurAX3LIvYWF+D2qcHedbZHQfVZ12ai2dTU="
on_success: always
on_failure: always
language: java

jdk:
- oraclejdk8

notifications:
webhooks:
urls:
- secure: "SPXpxdUqqEMqGJK1WbNOo4RxS9Sx2CKvmIUaadwFsSg34qR0KSOhXMnWEo6s+uFLv8OB+Q/nYtWvD9Xg6n+QfgkgmyOLiYsN8cGTG4B8KFKn3Kbl6mfG8IzRP+nQYzXCqcuQ6gf+eurAX3LIvYWF+D2qcHedbZHQfVZ12ai2dTU="
on_success: always
on_failure: always
on_start: false
@@ -1,7 +1,7 @@
package com.cardshifter.ai;

import com.cardshifter.core.Player;
import com.cardshifter.core.UsableAction;
import com.cardshifter.core.actions.UsableAction;

public interface CardshifterAI {
UsableAction getAction(Player player);
Expand Down
Expand Up @@ -6,9 +6,9 @@
import java.util.stream.Stream;

import com.cardshifter.core.Player;
import com.cardshifter.core.TargetAction;
import com.cardshifter.core.Targetable;
import com.cardshifter.core.UsableAction;
import com.cardshifter.core.actions.TargetAction;
import com.cardshifter.core.actions.UsableAction;

public class CompleteIdiot implements CardshifterAI {

Expand Down
Expand Up @@ -9,9 +9,13 @@
import org.luaj.vm2.LuaTable;
import org.luaj.vm2.LuaValue;

import com.cardshifter.core.actions.CardAction;
import com.cardshifter.core.actions.TargetAction;
import com.cardshifter.core.actions.UsableAction;

public class Card implements Targetable, IdEntity {
public final LuaTable data = new ExtLuaTable(this::onChange);

private final Map<String, UsableAction> actions = new HashMap<>();

private Optional<Zone> currentZone;
Expand Down
27 changes: 15 additions & 12 deletions cardshifter-core/src/main/java/com/cardshifter/core/Events.java
Expand Up @@ -7,6 +7,7 @@
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;

import org.luaj.vm2.Globals;
Expand All @@ -18,7 +19,7 @@
import org.luaj.vm2.luajc.LuaJC;

public class Events {

//TODO refactor to enums
public static final String ACTION_USED = "actionUsed";

public static final String TURN_END = "turnEnd";
Expand All @@ -27,18 +28,19 @@ public class Events {
private final Globals globals = JsePlatform.standardGlobals();
private final Map<String, List<LuaFunction>> eventListeners = new ConcurrentHashMap<>();

public Events(InputStream stream) {
InputStreamReader reader = new InputStreamReader(stream);
public Events(final InputStream inputStream) {
Objects.requireNonNull(inputStream, "inputStream");
InputStreamReader reader = new InputStreamReader(inputStream);
globals.load(reader, "mainScript").call();
LuaJC.install(globals);
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
e.printStackTrace(); //TODO logging?
}
}

public Zone zoneMove(Card card, Zone source, Zone destination) {
public Zone zoneMove(final Card card, final Zone source, final Zone destination) {
// TODO: Execute some event for when cards get moved, should perhaps also be possible to cancel it
// To support things like: "Whenever a creature enters the battlefield, you gain 1 life"
// for now this only returns the new destination zone.
Expand All @@ -53,7 +55,7 @@ public Zone zoneMove(Card card, Zone source, Zone destination) {
*
* @param game Game to setup
*/
public void startGame(Game game) {
public void startGame(final Game game) {
globals.set("gadsame", CoerceJavaToLua.coerce(game));

// globals.load(new StringReader(codeTextArea.getText()), "interopTest").call();
Expand All @@ -63,14 +65,15 @@ public void startGame(Game game) {
game.setCurrentPlayer(game.getFirstPlayer());
}

public void callEvent(String eventName, LuaValue source, LuaValue table) {
eventListeners.getOrDefault(eventName, Collections.emptyList()).forEach(func -> func.call(source, table));
public void callEvent(final String eventName, final LuaValue source, final LuaValue table) {
Objects.requireNonNull(eventName, "eventName");
Objects.requireNonNull(source, "source");
eventListeners.getOrDefault(eventName, Collections.emptyList())
.forEach(func -> func.call(source, table));
}

public void registerListener(String eventName, LuaValue function) {
function.checkfunction();
public void registerListener(final String eventName, final LuaValue function) {
eventListeners.putIfAbsent(eventName, Collections.synchronizedList(new ArrayList<>()));
eventListeners.get(eventName).add((LuaFunction) function);
eventListeners.get(eventName).add(function.checkfunction());
}

}
44 changes: 25 additions & 19 deletions cardshifter-core/src/main/java/com/cardshifter/core/Game.java
Expand Up @@ -7,22 +7,27 @@
import java.util.Random;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.luaj.vm2.LuaFunction;
import org.luaj.vm2.LuaValue;
import org.luaj.vm2.lib.jse.CoerceJavaToLua;

import com.cardshifter.core.actions.UsableAction;

public class Game implements IdEntity {

public static interface StateChangeListener {
void onChange(IdEntity what, Object key, Object value);
}

private final List<Zone> zones;
private final List<Player> players;
public final LuaValue data = new ExtLuaTable((key, value) -> this.broadcastChange(this, key, value));

private final List<Zone> zones = new ArrayList<>();
private final List<Player> players = new ArrayList<>();
private final Events events;
private final Random random;
public final LuaValue data = new ExtLuaTable((key, value) -> this.broadcastChange(this, key, value));

private boolean gameOver = false;
private final AtomicInteger ids;
private int turnNumber;
Expand All @@ -36,22 +41,20 @@ public Game(InputStream file, Random random) {
}

public Game(InputStream file, Random random, StateChangeListener listener) {
Objects.requireNonNull(random);
Objects.requireNonNull(file);
Objects.requireNonNull(file, "file");
Objects.requireNonNull(random, "random");
this.ids = new AtomicInteger(0);
this.id = nextId();
this.zones = new ArrayList<>();
this.players = new ArrayList<>();
this.events = new Events(file);
this.random = random;

this.players.add(new Player(this, "Player1", nextId()));
this.players.add(new Player(this, "Player2", nextId()));
this.random = random;
this.listener = listener;
this.turnNumber = 1;
}

public Game(InputStream file) {
public Game(final InputStream file) {
this(file, new Random());
}

Expand Down Expand Up @@ -87,26 +90,29 @@ public Events getEvents() {
return events;
}

public Zone createZone(Player owner, String name) {
public Zone createZone(final Player owner, final String name) {
Zone zone = new Zone(owner, name, this.nextId());
this.zones.add(zone);
return zone;
}

public List<UsableAction> getAllActions() {
List<UsableAction> actions = new ArrayList<>();
actions.addAll(getPlayers().stream().flatMap(player -> player.getActions().values().stream()).collect(Collectors.toList()));
actions.addAll(getZones().stream().flatMap(zone -> zone.getCards().stream())
.flatMap(card -> card.getActions().values().stream())
.collect(Collectors.toList()));
return actions;
Stream<UsableAction> playerActions = getPlayers().stream()
.flatMap(player -> player.getActions().values().stream());

Stream<UsableAction> cardActions = getZones().stream()
.flatMap(zone -> zone.getCards().stream())
.flatMap(card -> card.getActions().values().stream());

return Stream.concat(playerActions, cardActions).collect(Collectors.toList());
}

public void on(String eventName, LuaFunction function) {
public void on(final String eventName, final LuaFunction function) {
this.events.registerListener(eventName, function);
}

public void nextTurn() {
//TODO is it not bad if currentPlayer == null?
if (this.currentPlayer != null) {
this.events.callEvent(Events.TURN_END, CoerceJavaToLua.coerce(this.currentPlayer), null);
}
Expand All @@ -121,15 +127,15 @@ public void nextTurn() {
}
}

public int randomInt(int count) {
public int randomInt(final int count) {
return this.random.nextInt(count);
}

public Random getRandom() {
return this.random;
}

public void setCurrentPlayer(Player currentPlayer) {
public void setCurrentPlayer(final Player currentPlayer) {
this.currentPlayer = currentPlayer;
}

Expand Down
27 changes: 14 additions & 13 deletions cardshifter-core/src/main/java/com/cardshifter/core/Player.java
@@ -1,6 +1,5 @@
package com.cardshifter.core;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand All @@ -9,19 +8,22 @@
import org.luaj.vm2.LuaTable;
import org.luaj.vm2.LuaValue;

import com.cardshifter.core.actions.PlayerAction;
import com.cardshifter.core.actions.UsableAction;

public class Player implements Targetable, IdEntity {

public final LuaTable data = new ExtLuaTable(this::onChange);

private final Game game;
private final String name;
private final Map<String, UsableAction> actions;
public final LuaTable data = new ExtLuaTable(this::onChange);

private final Map<String, UsableAction> actions = new HashMap<>();
private final int id;

public Player(Game game, String name, int id) {
Objects.requireNonNull(game);
this.game = game;
this.name = name;
this.actions = new HashMap<>();
this.game = Objects.requireNonNull(game, "game");
this.name = Objects.requireNonNull(name, "name");;
this.id = id;
}

Expand All @@ -47,14 +49,13 @@ public Player getNextPlayer() {
public List<Player> getOpponents() {
List<Player> players = game.getPlayers();
int index = players.indexOf(this);

List<Player> before = players.subList(0, index);
List<Player> after = players.subList(index + 1, players.size());

List<Player> result = new ArrayList<Player>(after.size() + before.size());
result.addAll(after);
result.addAll(before);
return result;
}

after.addAll(before);
return after;
}

public Game getGame() {
return game;
Expand Down

This file was deleted.

Expand Up @@ -3,7 +3,5 @@
import org.luaj.vm2.LuaTable;

public interface Targetable {

LuaTable getData();

}

0 comments on commit b91d767

Please sign in to comment.