Skip to content

Commit

Permalink
#18: add a basic command service
Browse files Browse the repository at this point in the history
  • Loading branch information
OhmGeek committed Oct 19, 2019
1 parent fbd7214 commit b9c06a6
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,8 +1,44 @@
package com.colonelpanic.n64all.server.service;

import com.colonelpanic.n64all.server.model.ControllerProperty;
import com.colonelpanic.n64all.server.model.ControllerState;
import com.google.common.collect.Maps;
import com.google.inject.Singleton;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;

@Singleton
public class ControllerService {
private static final Logger logger = LoggerFactory.getLogger(ControllerService.class);
private Map<Integer, PlayerController> players;

public ControllerService() {
players = Maps.newConcurrentMap();
}

public void addPlayer(int playerId) {

if(players.containsKey(playerId))
return;

// TODO use a Guice supplied Factory to get the appropriate state.
players.put(playerId, new ControllerState());
}
public void updatePlayerState(int playerId, ControllerProperty propToUpdate, int valueToSet) {
if(this.players.get(playerId) == null) {
logger.error("Player with id: {} not found.", playerId);
throw new PlayerNotFoundException();
}
this.players.get(playerId).setState(propToUpdate, valueToSet);
}

public int getPlayerState(int playerId, ControllerProperty property) {
if(this.players.get(playerId) == null) {
logger.error("Player with id: {} not found.", playerId);
throw new PlayerNotFoundException();
}
return this.players.get(playerId).getState(property);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package com.colonelpanic.n64all.server.service;

public class PlayerNotFoundException extends RuntimeException {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package com.colonelpanic.n64all.server.service;

import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static com.colonelpanic.n64all.server.model.ControllerProperty.ANALOG_X;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;

public class ControllerServiceTest {

private ControllerService sut;

@BeforeEach
public void setUp() {
sut = new ControllerService();
}

@AfterEach
public void tearDown() {
}

@Test
public void shouldBarfIfStateUpdatedForUncreatedPlayer() {
//given
int playerId = 15;

assertThrows(PlayerNotFoundException.class,
() -> sut.updatePlayerState(playerId, ANALOG_X, 5));
}

@Test
public void shouldBarfOnGetStateForUncreatedPlayer() {
//given
int playerId = 15;

assertThrows(PlayerNotFoundException.class,
() -> sut.getPlayerState(playerId, ANALOG_X));
}

@Test
public void shouldUpdateStateForPrecreatedPlayer() {
//given
int playerId = 1;
int stateValue = 5;
sut.addPlayer(playerId);
sut.updatePlayerState(playerId, ANALOG_X, 5);

assertEquals(stateValue, sut.getPlayerState(playerId, ANALOG_X));
}

}

0 comments on commit b9c06a6

Please sign in to comment.