-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #118 from scionaltera/character-attributes
Character Stats
- Loading branch information
Showing
17 changed files
with
267 additions
and
19 deletions.
There are no files selected for viewing
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
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
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
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
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
118 changes: 118 additions & 0 deletions
118
agonyforge-mud-demo/src/main/java/com/agonyforge/mud/demo/cli/CharacterStatQuestion.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,118 @@ | ||
package com.agonyforge.mud.demo.cli; | ||
|
||
import com.agonyforge.mud.core.cli.Color; | ||
import com.agonyforge.mud.core.cli.Question; | ||
import com.agonyforge.mud.core.cli.Response; | ||
import com.agonyforge.mud.core.web.model.Input; | ||
import com.agonyforge.mud.core.web.model.Output; | ||
import com.agonyforge.mud.core.web.model.WebSocketContext; | ||
import com.agonyforge.mud.demo.cli.menu.MenuItem; | ||
import com.agonyforge.mud.demo.cli.menu.MenuPane; | ||
import com.agonyforge.mud.demo.cli.menu.MenuPrompt; | ||
import com.agonyforge.mud.demo.cli.menu.MenuTitle; | ||
import com.agonyforge.mud.models.dynamodb.constant.Stat; | ||
import com.agonyforge.mud.models.dynamodb.impl.MudCharacter; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.context.ApplicationContext; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.Arrays; | ||
import java.util.Locale; | ||
|
||
@Component | ||
public class CharacterStatQuestion extends BaseQuestion { | ||
private static final Logger LOGGER = LoggerFactory.getLogger(CharacterStatQuestion.class); | ||
private static final int STARTING_STATS = 6; | ||
|
||
private final MenuPane menuPane = new MenuPane(); | ||
|
||
public CharacterStatQuestion(ApplicationContext applicationContext, | ||
RepositoryBundle repositoryBundle) { | ||
super(applicationContext, repositoryBundle); | ||
|
||
menuPane.setTitle(new MenuTitle("Allocate Stat Points")); | ||
menuPane.setPrompt(new MenuPrompt()); | ||
} | ||
|
||
@Override | ||
public Output prompt(WebSocketContext wsContext) { | ||
Output output = new Output(); | ||
MudCharacter ch = getCharacter(wsContext, output).orElseThrow(); | ||
|
||
populateMenuItems(ch); | ||
|
||
return menuPane.render(Color.WHITE, Color.BLACK); | ||
} | ||
|
||
@Override | ||
public Response answer(WebSocketContext webSocketContext, Input input) { | ||
String nextQuestion = "characterStatQuestion"; | ||
Output output = new Output(); | ||
MudCharacter ch = getCharacter(webSocketContext, output).orElseThrow(); | ||
String choice = input.getInput().toUpperCase(Locale.ENGLISH); | ||
int totalPoints = computeStatPoints(ch); | ||
|
||
if (choice.contains("+")) { | ||
if (totalPoints >= STARTING_STATS) { | ||
output.append("[red]You don't have any more points to allocate!"); | ||
} else { | ||
try { | ||
int i = Integer.parseInt(choice.substring(0, choice.length() - 1)) - 1; | ||
ch.addStat(Stat.values()[i], 1); | ||
} catch (NumberFormatException | ArrayIndexOutOfBoundsException e) { | ||
output.append("[red][red]Oops! Try a number with a plus or minus!"); | ||
} | ||
} | ||
} else if (choice.contains("-")) { | ||
if (totalPoints <= 0) { | ||
output.append("[red]You haven't assigned any of your points yet!"); | ||
} else { | ||
try { | ||
int i = Integer.parseInt(choice.substring(0, choice.length() - 1)) - 1; | ||
ch.addStat(Stat.values()[i], -1); | ||
} catch (NumberFormatException | ArrayIndexOutOfBoundsException e) { | ||
output.append("[red][red]Oops! Try a number with a plus or minus!"); | ||
} | ||
} | ||
} else { | ||
if (choice.equals("S")) { | ||
if (totalPoints == STARTING_STATS) { | ||
output.append("[green]Character stats saved!"); | ||
nextQuestion = "characterMenuQuestion"; | ||
} else { | ||
output.append("[red]Please allocate exactly 6 points for your stats."); | ||
} | ||
} else { | ||
output.append("[red]Oops! Try a number with a plus or minus!"); | ||
} | ||
} | ||
|
||
getRepositoryBundle().getCharacterRepository().save(ch); | ||
|
||
Question next = getQuestion(nextQuestion); | ||
|
||
return new Response(next, output); | ||
} | ||
|
||
private int computeStatPoints(MudCharacter ch) { | ||
return Arrays.stream(Stat.values()) | ||
.map(ch::getStat) | ||
.reduce(0, Integer::sum); | ||
} | ||
|
||
private void populateMenuItems(MudCharacter ch) { | ||
menuPane.getItems().clear(); | ||
|
||
int points = STARTING_STATS - computeStatPoints(ch); | ||
|
||
menuPane.getItems().add(new MenuItem(" ", "[default]Enter the menu number and a plus (+) or minus (-) to add or subtract from a stat.")); | ||
menuPane.getItems().add(new MenuItem(" ", "[default]For example, '3+' to raise CON or '6-' to lower CHA.")); | ||
menuPane.getItems().add(new MenuItem(" ", String.format("[default]Please allocate [white]%d more points [default]for your stats.", points))); | ||
|
||
Arrays.stream(Stat.values()) | ||
.forEachOrdered(stat -> menuPane.getItems().add(new MenuItem((stat.ordinal() + 1) + "[+/-]", String.format("%15s (%d)", stat.getName(), ch.getStat(stat))))); | ||
|
||
menuPane.getItems().add(new MenuItem("S", "Save")); | ||
} | ||
} |
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
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
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
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
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
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
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
26 changes: 26 additions & 0 deletions
26
...e-mud-models-dynamodb/src/main/java/com/agonyforge/mud/models/dynamodb/constant/Stat.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,26 @@ | ||
package com.agonyforge.mud.models.dynamodb.constant; | ||
|
||
public enum Stat { | ||
STR("Strength", "STR"), | ||
DEX("Dexterity", "DEX"), | ||
CON("Constitution", "CON"), | ||
INT("Intelligence", "INT"), | ||
WIS("Wisdom", "WIS"), | ||
CHA("Charisma", "CHA"); | ||
|
||
private final String name; | ||
private final String abbreviation; | ||
|
||
Stat(String name, String abbreviation) { | ||
this.name = name; | ||
this.abbreviation = abbreviation; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public String getAbbreviation() { | ||
return abbreviation; | ||
} | ||
} |
Oops, something went wrong.