Skip to content

Commit

Permalink
Merge 9fcd65d into a2a687d
Browse files Browse the repository at this point in the history
  • Loading branch information
superernie77 committed Feb 20, 2017
2 parents a2a687d + 9fcd65d commit 3a42dab
Show file tree
Hide file tree
Showing 12 changed files with 205 additions and 78 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,12 @@ public List<WorldEvent> getEventsForPlayer(int playerId) {
return events.stream().filter( e -> e.getPlayerId() == playerId).collect(Collectors.toList());
}

public SpacePlayer getPlayerById(int playerId){
return null;
@Override
public String toString(){
String result = "";
for( Segment seg : segments) {
result += " Segment: "+seg.getContent();
}
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ public interface GameEngine {
*/
void startGame(Integer playerId, Integer worldId);

void addPlayer2World(Integer playerId, Integer worldId);

/**
* Stops a space game for a given player and world.
* @param playerId
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.space.server.engine.api;

import com.space.server.core.World;
import com.space.server.domain.api.Item;

import java.util.List;

/**
* interface for worldEvent to transport event information into the game world.
Expand All @@ -24,4 +27,8 @@ public interface WorldEvent {

void setWorld(World world);

void setInventory(List<Item> items);

List<Item> getInventory();

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@

import com.space.server.core.World;
import com.space.server.domain.api.Segment;
import com.space.server.domain.api.SpacePlayer;
import com.space.server.domain.api.SpaceWorld;
import com.space.server.engine.api.GameEngine;
import com.space.server.engine.api.WorldEvent;
import com.space.server.web.util.JsonUtil;
import org.eclipse.jetty.websocket.api.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.Set;
Expand All @@ -18,9 +21,9 @@
*/
public class Broadcaster {

private int worldId;
private static final Logger LOG = LoggerFactory.getLogger(Broadcaster.class);

private int playerId;
private int worldId;

private GameEngine engine;

Expand All @@ -38,29 +41,29 @@ public void setWorldId(int worldId) {
this.worldId = worldId;
}

public int getPlayerId() {
return playerId;
}

public void setPlayerId(int playerId) {
this.playerId = playerId;
}

public WorldEvent createWorldEvent(){
public WorldEvent createWorldEvent(Integer playerId){
SpaceWorld world = engine.getWorld(this.getWorldId());
Segment segmentwithplayer = world.getSegments().stream().filter( s -> s.containsPlayer(playerId)).findFirst().get();
World gameWorld = new World(segmentwithplayer.getContent());

WorldEvent resultEvent = new WorldEventImpl();
resultEvent.setPlayerId(this.getPlayerId());
resultEvent.setWorldId(this.getWorldId());
resultEvent.setType(UPDATE);
resultEvent.setWorld(gameWorld);

return resultEvent;
if (world.getSegments().stream().anyMatch( s -> s.containsPlayer(playerId))){
Segment segmentwithplayer = world.getSegments().stream().filter( s -> s.containsPlayer(playerId)).findFirst().get();

World gameWorld = new World(segmentwithplayer.getContent());

SpacePlayer player = engine.getPlayer(playerId);

WorldEvent resultEvent = new WorldEventImpl();
resultEvent.setWorldId(this.getWorldId());
resultEvent.setPlayerId(playerId);
resultEvent.setType(UPDATE);
resultEvent.setWorld(gameWorld);
resultEvent.setInventory(player.getInventory());
return resultEvent;
}
return null;
}

public void broadcast(Session playerSession, WorldEvent resultEvent) throws IOException {
playerSession.getRemote().sendString(JsonUtil.toJson(resultEvent));
String result = JsonUtil.toJson(resultEvent);
playerSession.getRemote().sendString(result);
LOG.debug(result);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,43 @@ public class GameEngineImpl implements GameEngine {

private Map<Integer,Integer> playerWorldmapping = new HashMap<>();


private void startGameInternal(SpacePlayer player, SpaceWorld world){
activePlayer.put(player.getPlayerId(),player);
LOG.debug("Active player:" + activePlayer.toString());

// set player into world and connect player with step
Segment segment = world.getSegment(world.getStartSegment());
Step step = segment.getStep(world.getStartStep());
step.addOverlay(player);
player.setActiveStep(step);
LOG.debug("Added player {} to world {} ", player.getPlayerId(), world.getWorldId());
LOG.debug(world.toString());

playerWorldmapping.put(player.getPlayerId(),world.getWorldId());
LOG.debug("Player-world mapping: "+playerWorldmapping.toString());

LOG.info("Player {} added to world {}",player.getPlayerId(), world.getWorldId());
}

public void addPlayer2World(Integer playerId, Integer worldId){
// load player
SpacePlayer player = playerDao.getPlayer(playerId);
if (player == null){
LOG.warn("Player No. {} not found. game not started", playerId);
return;
}

// get world from active worlds
SpaceWorld world = activeWorlds.get(worldId);
if (world == null){
LOG.warn("World No. {} not found. game not started", worldId);
return;
}

startGameInternal(player,world);
}

@Override
public void startGame(Integer playerId, Integer worldId) {
LOG.info("Starting game for playerId {} and worldId {}",playerId, worldId);
Expand All @@ -68,21 +105,11 @@ public void startGame(Integer playerId, Integer worldId) {
return;
}

activePlayer.put(playerId,player);

// set player into world and connect player with step
Segment segment = world.getSegment(world.getStartSegment());
Step step = segment.getStep(world.getStartStep());
step.addOverlay(player);
player.setActiveStep(step);

// activate world
activeWorlds.put(worldId,world);
LOG.debug("Active worlds: "+ activeWorlds.toString());

// map player to world
playerWorldmapping.put(playerId,worldId);

LOG.info("Game started for playerId {} and worldId {}",playerId, worldId);
startGameInternal(player,world);
}

@Override
Expand Down Expand Up @@ -153,7 +180,9 @@ public SpaceWorld getWorld(Integer worldId) {
SpaceWorld world = activeWorlds.get(worldId);
if (world == null){
world = worldDao.getWorld(worldId);
LOG.debug("World (worldId {}) has been loaded.", world.getWorldId());
if (world != null){
LOG.debug("World (worldId {}) has been loaded.", world.getWorldId());
}
}
return world;
}
Expand Down Expand Up @@ -183,10 +212,6 @@ void setPlayerDao(PlayerDao dao){
playerDao = dao;
}

public WorldEventProcessorImpl getWorldEventProcessor(){
return processor;
}

void setWorldEventProcessor(WorldEventProcessorImpl proc) {
processor = proc;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import static com.space.server.engine.api.WorldEventType.UPDATE;

/**
* Start and Stops games. Steps running games ever second and sends result to client
* Start and Stops games. Steps running games every second and sends result to client(s)
* Created by superernie77 on 02.02.2017.
*/
@Component
Expand Down Expand Up @@ -63,20 +63,25 @@ public void run(){
// step world one step
b.getEngine().stepWorld(b.getWorldId());

// create result for client
WorldEvent resultEvent = b.createWorldEvent();

// broadcast to all players
Set<Integer> playerSetRunnable = playerWorldMap.get(b.getWorldId());
try {
for (Integer playerIdRunnable : playerSetRunnable ) {
LOG.debug("Broadcasting world for playerId "+playerIdRunnable);
Session playerSession = playerSessionMap.get(playerIdRunnable);
b.broadcast(playerSession,resultEvent);
LOG.debug(JsonUtil.toJson(resultEvent));
if (playerSetRunnable != null) {
try {
LOG.debug("Broadcasting for {} players", playerSetRunnable.size());
for (Integer playerIdRunnable : playerSetRunnable ) {
LOG.debug("Broadcasting world for playerId "+playerIdRunnable);
WorldEvent resultEvent = b.createWorldEvent(playerIdRunnable);
if (resultEvent != null) {
Session playerSession = playerSessionMap.get(playerIdRunnable);
LOG.debug("Player {} session {}", playerIdRunnable, playerSession.toString());
b.broadcast(playerSession,resultEvent);
} else {
LOG.debug("No result-event available for player {}", playerIdRunnable);
}
}
} catch (IOException e) {
LOG.error(e.getMessage(), e);
}
} catch (IOException e) {
LOG.error(e.getMessage(), e);
}
}
}
Expand All @@ -85,18 +90,25 @@ public void run(){
public void startGame(int worldId,int playerId, Session session) {

if (checkGameStartedAlready(worldId,playerId)){
LOG.debug("World {} already started for player {}", worldId, playerId);
return;
}

Set<Integer> players = playerWorldMap.get(worldId);
if (players != null) {
// world started already
LOG.debug("World {} running already.",worldId);

engine.addPlayer2World(playerId,worldId);

// add player to world
LOG.debug("Adding player {} to playerWorldMap",playerId);
players.add(playerId);
LOG.debug("player-world map:" +playerWorldMap.toString());

// save session
LOG.debug("Adding session for player {}", playerId);
playerSessionMap.put(playerId,session);
LOG.debug("player-session map:" +playerSessionMap.toString());


} else {
// start new world

Expand All @@ -110,14 +122,24 @@ public void startGame(int worldId,int playerId, Session session) {
runner.setBroadCaster(b);


LOG.debug("Starting new Runner for worldId {}",worldId );
ScheduledFuture future = scheduledExecutorService.scheduleAtFixedRate(runner , 3, 1000L, TimeUnit.MILLISECONDS);

// register world, player, future and session
Set<Integer> newPlayerSet = new HashSet<>();
newPlayerSet.add(playerId);

LOG.debug("Adding player {} to world",playerId);
playerWorldMap.put(worldId, newPlayerSet );
LOG.debug("player-world map:" +playerWorldMap.toString());

LOG.debug("Adding session for player {}", playerId);
playerSessionMap.put(playerId,session);
LOG.debug("player-session map:" +playerSessionMap.toString());

LOG.debug("Adding future for world {}", worldId);
worldFutureMap.put(worldId, future);
LOG.debug("future-world map:" +worldFutureMap.toString());
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package com.space.server.engine.impl;

import com.space.server.core.World;
import com.space.server.domain.api.Item;
import com.space.server.engine.api.WorldEvent;
import com.space.server.engine.api.WorldEventType;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* Action of a player in a sppace world.
* Created by superernie77 on 08.12.2016.
Expand All @@ -20,6 +23,8 @@ public class WorldEventImpl implements WorldEvent {

private World world;

private List<Item> items;

@Override
public int getWorldId() {
return worldId;
Expand Down Expand Up @@ -59,4 +64,14 @@ public World getWorld() {
public void setWorld(World world) {
this.world = world;
}

@Override
public void setInventory(List<Item> items) {
this.items = items;
}

@Override
public List<Item> getInventory() {
return items;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,23 @@ public void processEvents(List<WorldEvent> events, SpacePlayer player){
}
break;
// SpaceEvent if Monster on the next step
} else if (overlay instanceof SpacePlayer) {
SpacePlayer secondplayer = (SpacePlayer)overlay;

Item activeItem = player.getActiveItem();
if (activeItem instanceof Weapon) {
LOG.debug("Player hit by player {}", player.getPlayerId());

secondplayer.getHealth().processHit();

break;
}
} else if (overlay instanceof Monster) {
Monster monster = (Monster)overlay;

Item activeItem = player.getActiveItem();
if (activeItem instanceof Weapon) {
LOG.debug("Monster killed for playerId {}", player.getPlayerId());
LOG.debug("Monster hit by player {}", player.getPlayerId());

monster.getHealth().processHit();

Expand Down
Loading

0 comments on commit 3a42dab

Please sign in to comment.