-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
36 changes: 36 additions & 0 deletions
36
server/src/main/java/com/colonelpanic/n64all/server/service/ControllerService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
4 changes: 4 additions & 0 deletions
4
server/src/main/java/com/colonelpanic/n64all/server/service/PlayerNotFoundException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 { | ||
} |
53 changes: 53 additions & 0 deletions
53
server/src/test/java/com/colonelpanic/n64all/server/service/ControllerServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
|
||
} |