Skip to content

Commit

Permalink
pulled client-server changes
Browse files Browse the repository at this point in the history
  • Loading branch information
bazola committed Sep 18, 2014
1 parent b9c47a5 commit 1920f1f
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.cardshifter.server.incoming.StartGameRequest;
import com.cardshifter.server.incoming.UseAbilityMessage;
import com.cardshifter.server.outgoing.CardInfoMessage;
import com.cardshifter.server.outgoing.EntityRemoveMessage;
import com.cardshifter.server.outgoing.GameMessage;
import com.cardshifter.server.outgoing.NewGameMessage;
import com.cardshifter.server.outgoing.PlayerMessage;
Expand All @@ -17,6 +18,7 @@
import com.cardshifter.server.outgoing.UseableActionMessage;
import com.cardshifter.server.outgoing.WaitMessage;
import com.cardshifter.server.outgoing.WelcomeMessage;
import com.cardshifter.server.outgoing.ZoneChangeMessage;
import com.cardshifter.server.outgoing.ZoneMessage;
import com.fasterxml.jackson.annotation.JsonTypeInfo.Id;
import com.fasterxml.jackson.databind.JavaType;
Expand All @@ -33,6 +35,8 @@ public class MessageTypeIdResolver implements TypeIdResolver {
clazzes.put("startgame", StartGameRequest.class);
clazzes.put("use", UseAbilityMessage.class);
clazzes.put("requestTargets", RequestTargetsMessage.class);
clazzes.put("zoneChange", ZoneChangeMessage.class);
clazzes.put("entityRemoved", EntityRemoveMessage.class);

clazzes.put("resetActions", ResetAvailableActionsMessage.class);
clazzes.put("game", GameMessage.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.cardshifter.server.outgoing;

import com.cardshifter.server.messages.Message;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;

public class EntityRemoveMessage extends Message {

private final int entity;

@JsonCreator
public EntityRemoveMessage(@JsonProperty("entity") int entity) {
super("entityRemoved");
this.entity = entity;
}

public int getEntity() {
return entity;
}

@Override
public String toString() {
return "EntityRemoveMessage [entity=" + entity + "]";
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package com.cardshifter.server.outgoing;

import com.cardshifter.server.messages.Message;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;

public class ZoneChangeMessage extends Message {

private final int entity;
private final int sourceZone;
private final int destinationZone;

@JsonCreator
public ZoneChangeMessage(@JsonProperty("entity") int entity,
@JsonProperty("sourceZone") int sourceZone, @JsonProperty("destinationZone") int destinationZone) {
super("zoneChange");
this.entity = entity;
this.sourceZone = sourceZone;
this.destinationZone = destinationZone;
}

public int getEntity() {
return entity;
}

public int getSourceZone() {
return sourceZone;
}

public int getDestinationZone() {
return destinationZone;
}

@Override
public String toString() {
return "ZoneChangeMessage [entity=" + entity + ", sourceZone="
+ sourceZone + ", destinationZone=" + destinationZone + "]";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,13 @@
import java.util.function.Consumer;
import java.util.function.Predicate;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

public class EventExecutor {

private static final Logger logger = LogManager.getLogger(EventExecutor.class);

protected final Map<Class<? extends IEvent>, Collection<EventHandler<?>>> bindings;

public EventExecutor() {
Expand All @@ -26,10 +31,12 @@ private <T extends IEvent> T executeEvent(T event, Predicate<EventHandler<?>> pr
}

public <T extends IEvent> T executePostEvent(T event) {
logger.debug("Execute pre event " + event);
return executeEvent(event, eh -> eh.isAfter());
}

public <T extends IEvent> T executePreEvent(T event) {
logger.debug("Execute post event " + event);
return executeEvent(event, eh -> !eh.isAfter());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -316,10 +316,8 @@ private void processUpdateMessage(UpdateMessage message) {
}
private void processUpdateMessageForPlayer(Pane statBox, UpdateMessage message, Map playerMap) {
String key = (String)message.getKey();
System.out.println(String.format("The old value = %d", playerMap.get(key)));
Integer value = (Integer)message.getValue();
playerMap.put(key, value);
System.out.println(String.format("new map value = %d", playerMap.get(key)));

this.repaintStatBox(statBox, playerMap);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
import net.zomis.cardshifter.ecs.base.ComponentRetriever;
import net.zomis.cardshifter.ecs.base.ECSGame;
import net.zomis.cardshifter.ecs.base.Entity;
import net.zomis.cardshifter.ecs.base.EntityRemoveEvent;
import net.zomis.cardshifter.ecs.cards.CardComponent;
import net.zomis.cardshifter.ecs.cards.ZoneChangeEvent;
import net.zomis.cardshifter.ecs.cards.ZoneComponent;
import net.zomis.cardshifter.ecs.components.PlayerComponent;
import net.zomis.cardshifter.ecs.phase.PhaseController;
Expand All @@ -31,10 +33,12 @@
import com.cardshifter.server.incoming.UseAbilityMessage;
import com.cardshifter.server.main.FakeAIClientTCG;
import com.cardshifter.server.outgoing.CardInfoMessage;
import com.cardshifter.server.outgoing.EntityRemoveMessage;
import com.cardshifter.server.outgoing.PlayerMessage;
import com.cardshifter.server.outgoing.ResetAvailableActionsMessage;
import com.cardshifter.server.outgoing.UpdateMessage;
import com.cardshifter.server.outgoing.UseableActionMessage;
import com.cardshifter.server.outgoing.ZoneChangeMessage;
import com.cardshifter.server.outgoing.ZoneMessage;

public class TCGGame extends ServerGame {
Expand All @@ -53,10 +57,26 @@ public TCGGame(Server server, int id) {
super(server, id);
game = PhrancisGame.createGame();
game.getEvents().registerHandlerAfter(ResourceValueChange.class, this::broadcast);
game.getEvents().registerHandlerAfter(ZoneChangeEvent.class, this::zoneChange);
game.getEvents().registerHandlerAfter(EntityRemoveEvent.class, this::remove);
aiPerform.scheduleWithFixedDelay(this::aiPerform, 0, AI_DELAY_SECONDS, TimeUnit.SECONDS);
phases = ComponentRetriever.singleton(game, PhaseController.class);
}

private void zoneChange(ZoneChangeEvent event) {
for (ClientIO io : this.getPlayers()) {
Entity player = playerFor(io);
io.sendToClient(new ZoneChangeMessage(event.getCard().getId(), event.getSource().getZoneId(), event.getDestination().getZoneId()));
if (event.getDestination().isKnownTo(player) && event.getSource().isKnownTo(player)) {
sendCard(io, event.getCard());
}
}
}

private void remove(EntityRemoveEvent event) {
this.send(new EntityRemoveMessage(event.getEntity().getId()));
}

private void broadcast(ResourceValueChange event) {
if (getState() == GameState.NOT_STARTED) {
// let the most information be sent when actually starting the game
Expand Down

0 comments on commit 1920f1f

Please sign in to comment.