Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
plugins {
id 'java'
}

group 'rahnzo.kata.solid'
version '1.0-SNAPSHOT'

repositories {
mavenCentral()
}

dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
}
21 changes: 21 additions & 0 deletions src/main/java/game/domain/core/DynamoDB.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package game.domain.core;

import game.domain.player.PlayerRecord;
import game.domain.player.PlayerInfo;

import java.util.Set;

public class DynamoDB {
public Set<PlayerInfo> getUserInfo(long userId) {
// Get a db connection and query
return Set.of(new PlayerInfo("current-level", "level_1"));
}

public void saveOrUpdate(PlayerRecord playerRecord) {
// Save or update player to db
}

public void delete(long userId) {
// Delete player from db
}
}
19 changes: 19 additions & 0 deletions src/main/java/game/domain/core/GameCore.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package game.domain.core;

import game.domain.input.UserAction;

public class GameCore {

public void initializeAllGameObjects() {
// Load all objects
// Init game parameters with complex and large operations
}

public void processInput(UserAction userAction) {
// Apply logic on event, calling hero actions
}

public void handleHardwareEvents(Object hardwareEvent) {
// Apply certain actions based on hardware events
}
}
32 changes: 32 additions & 0 deletions src/main/java/game/domain/core/GameEventObserver.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package game.domain.core;

import game.domain.hero.HeroInteract;

public class GameEventObserver {

private final World world;
private final HeroInteract heroInteract;

// This works by injection
public GameEventObserver(World world, HeroInteract heroInteract) {
this.world = world;
this.heroInteract = heroInteract;
}

public void observeHeroPunch(Object obj) {
// Do some task with punch event
world.processEvent(obj);
heroInteract.processEvent(obj);
}

public void observeHeroMagicCasting(Object obj) {
// Do some task with magic casting
world.processEvent(obj);
heroInteract.processEvent(obj);
}

public void observeHeroJump(Object obj) {
// Do some task with jump event
world.processEvent(obj);
}
}
16 changes: 16 additions & 0 deletions src/main/java/game/domain/core/World.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package game.domain.core;

import game.domain.music.SpotifyPlugin;

public class World {

private final SpotifyPlugin spotifyPlugin = new SpotifyPlugin();

public void runWorld() {
spotifyPlugin.playMusic();
}

public void processEvent(Object obj) {
// Process a world event
}
}
6 changes: 6 additions & 0 deletions src/main/java/game/domain/hero/Hero.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package game.domain.hero;

public interface Hero {
void punch();
void jump();
}
16 changes: 16 additions & 0 deletions src/main/java/game/domain/hero/HeroInteract.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package game.domain.hero;

import java.util.Set;

public class HeroInteract {

private final Set<Hero> heroes;

public HeroInteract(Set<Hero> heroes) {
this.heroes = heroes;
}

public void processEvent(Object obj) {
// Process a event between heroes
}
}
14 changes: 14 additions & 0 deletions src/main/java/game/domain/hero/MagicianHero.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package game.domain.hero;

public class MagicianHero extends MeleeHero {

public void castMagic() {
gameEventObserver.observeHeroMagicCasting(this);
// Do some casting action
}

@Override
public void punch() {
// This hero is a magician. Does not punch.
}
}
22 changes: 22 additions & 0 deletions src/main/java/game/domain/hero/MeleeHero.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package game.domain.hero;

import game.domain.core.GameEventObserver;

public class MeleeHero implements Hero {
private int punchPower;
protected GameEventObserver gameEventObserver;

@Override
public void punch() {
gameEventObserver.observeHeroPunch(this);
// process hero actions for hero
}

@Override
public void jump() {
gameEventObserver.observeHeroJump(this);
// process jump action for hero
}

//TODO: Next week this class will change in order to support a new turbo punch feature
}
6 changes: 6 additions & 0 deletions src/main/java/game/domain/input/ButtonAction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package game.domain.input;

public enum ButtonAction {
JUMP,
PUNCH
}
32 changes: 32 additions & 0 deletions src/main/java/game/domain/input/GamepadInput.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package game.domain.input;

import game.domain.core.GameCore;

public class GamepadInput implements PlayerInput {

public final GameCore gameCore;

public GamepadInput(GameCore gameCore) {
this.gameCore = gameCore;
}

@Override
public void captureKeyPressed(KeyAction keyAction) {
// Log warning, this is not supported
}

@Override
public void captureBottomPressed(ButtonAction keyAction) {
gameCore.processInput(convertTo(keyAction));
}

@Override
public void captureStickPressed(ButtonAction keyAction) {
gameCore.processInput(convertTo(keyAction));
}

private UserAction convertTo(ButtonAction keyAction) {
// Map to user action
return UserAction.GENERIC;
}
}
10 changes: 10 additions & 0 deletions src/main/java/game/domain/input/KeyAction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package game.domain.input;

public enum KeyAction {
UP,
DOWN,
LEFT,
RIGHT,
Z,
X
}
32 changes: 32 additions & 0 deletions src/main/java/game/domain/input/KeyboardInput.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package game.domain.input;

import game.domain.core.GameCore;

public class KeyboardInput implements PlayerInput {

public final GameCore gameCore;

public KeyboardInput(GameCore gameCore) {
this.gameCore = gameCore;
}

@Override
public void captureKeyPressed(KeyAction keyAction) {
gameCore.processInput(convertTo(keyAction));
}

@Override
public void captureBottomPressed(ButtonAction keyAction) {
// Log warning, this is not supported
}

@Override
public void captureStickPressed(ButtonAction keyAction) {
// Log warning, this is not supported
}

private UserAction convertTo(KeyAction keyAction) {
// Map to user action
return UserAction.GENERIC;
}
}
7 changes: 7 additions & 0 deletions src/main/java/game/domain/input/PlayerInput.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package game.domain.input;

public interface PlayerInput {
void captureKeyPressed(KeyAction keyAction);
void captureBottomPressed(ButtonAction keyAction);
void captureStickPressed(ButtonAction keyAction);
}
8 changes: 8 additions & 0 deletions src/main/java/game/domain/input/StickAction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package game.domain.input;

public enum StickAction {
UP,
DOWN,
LEFT,
RIGHT,
}
11 changes: 11 additions & 0 deletions src/main/java/game/domain/input/UserAction.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package game.domain.input;

public enum UserAction {
DIRECTION_UP,
DIRECTION_DOWN,
DIRECTION_LEFT,
DIRECTION_RIGHT,
PUNCH_PRESSED,
JUMP_PRESSED,
GENERIC
}
7 changes: 7 additions & 0 deletions src/main/java/game/domain/music/SpotifyPlugin.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package game.domain.music;

public class SpotifyPlugin {
public void playMusic() {
// Do some management of the plugin and play music
}
}
25 changes: 25 additions & 0 deletions src/main/java/game/domain/player/Player.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package game.domain.player;

import game.domain.core.DynamoDB;

import java.util.Map;
import java.util.Set;

public class Player {

private static final DynamoDB DYNAMO_DB = new DynamoDB();

public void createPlayer(String name, Map<String, String> attributes) {
// Do some parsing work
DYNAMO_DB.saveOrUpdate(new PlayerRecord());
}

public Process getProcess(long userId) {
return calculateProcess(DYNAMO_DB.getUserInfo(userId));
}

private Process calculateProcess(Set<PlayerInfo> playerInfo) {
// Do some expensive computations in order to calculate process
return new Process("level_1", 10, 30, 3);
}
}
19 changes: 19 additions & 0 deletions src/main/java/game/domain/player/PlayerInfo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package game.domain.player;

public class PlayerInfo {
public String key;
public String value;

public PlayerInfo(String key, String value) {
this.key = key;
this.value = value;
}

public String getKey() {
return key;
}

public String getValue() {
return value;
}
}
4 changes: 4 additions & 0 deletions src/main/java/game/domain/player/PlayerRecord.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package game.domain.player;

public class PlayerRecord {
}
49 changes: 49 additions & 0 deletions src/main/java/game/domain/player/Process.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
package game.domain.player;

public class Process {
private String stage;
private int level;
private int currentPunchPower;
private int powerLevelMultiplier;

// A lot of process indicators

public Process(String stage, int level, int currentPunchPower, int powerLevelMultiplier) {
this.stage = stage;
this.level = level;
this.currentPunchPower = currentPunchPower;
this.powerLevelMultiplier = powerLevelMultiplier;
}

public String getStage() {
return stage;
}

public void setStage(String stage) {
this.stage = stage;
}

public int getLevel() {
return level;
}

public void setLevel(int level) {
this.level = level;
}

public int getCurrentPunchPower() {
return currentPunchPower;
}

public void setCurrentPunchPower(int currentPunchPower) {
this.currentPunchPower = currentPunchPower;
}

public int getPowerLevelMultiplier() {
return powerLevelMultiplier;
}

public void setPowerLevelMultiplier(int powerLevelMultiplier) {
this.powerLevelMultiplier = powerLevelMultiplier;
}
}