Skip to content

Commit

Permalink
Adding switch role, green tests and refactoring messages. They are no…
Browse files Browse the repository at this point in the history
…t the right ones, but they'll do for now
  • Loading branch information
Sebastian Hermida committed May 9, 2011
1 parent 715a113 commit 91bfe69
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 38 deletions.
31 changes: 20 additions & 11 deletions src/com/happyprog/pairhero/game/Game.java
Expand Up @@ -7,9 +7,14 @@

public class Game {

public static final int _1X_MULTIPLIER = 1;
public static final int _2_MULTIPLIER = 2;
public static final int _4X_MULTIPLIER = 4;

public static final int GREEN_TEST_POINTS = 10;
public static final int REFACTORING_POINTS = 2;
public static final int ROLE_SWITCH_POINTS = 1;

private int score;
private int timeSinceSwitch;

Expand Down Expand Up @@ -38,8 +43,6 @@ public void start() {
rightProgrammer.observe();

timer.start(this);

view.updateScore(score);
}

public void stop() {
Expand All @@ -63,26 +66,32 @@ public void onTimeChange(int seconds) {

public void onSwitchRole() {
score += ROLE_SWITCH_POINTS;
if (timeSinceSwitch < 30) {
score *= 4;
} else if (timeSinceSwitch >= 30 && timeSinceSwitch < 120) {
score *= 2;
}

int multiplier = getMultiplier();
score *= multiplier;

leftProgrammer.switchRole();
rightProgrammer.switchRole();
view.onSwitchRole();
view.updateScore(score);
view.onSwitchRole(score, multiplier);
}

public void onGreenTest() {
score += GREEN_TEST_POINTS;
view.updateScore(score);
view.onGreenTest(score);
}

public void onRefactoring() {
score += REFACTORING_POINTS;
view.updateScore(score);
view.onRefactoring(score);
}

private int getMultiplier() {
if (timeSinceSwitch < 30) {
return _4X_MULTIPLIER;
} else if (timeSinceSwitch >= 30 && timeSinceSwitch < 120) {
return _2_MULTIPLIER;
}
return _1X_MULTIPLIER;
}

}
44 changes: 30 additions & 14 deletions src/com/happyprog/pairhero/views/MainView.java
Expand Up @@ -162,11 +162,6 @@ public void run() {
});
}

public void updateScore(int score) {
updateScore(scoreLabel, String.format("%d", score));
updateMessage();
}

public void onStop() {
boolean response = MessageDialog.openConfirm(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(),
"Pair Hero", "Are you sure you want to stop this session?");
Expand All @@ -178,30 +173,51 @@ public void onStop() {
}
}

public void onSwitchRole(int score, int multiplier) {
showMessageAndUpdateScore(getSwitchRoleImage(multiplier), score);
}

public void onRefactoring(int score) {
showMessageAndUpdateScore("icons/great.png", score);
}

public void onGreenTest(int score) {
showMessageAndUpdateScore("icons/okay.png", score);
}

private void showMessageAndUpdateScore(String imagePath, int score) {
updateMessage(messageLabel, Activator.getImageDescriptor(imagePath).createImage());
updateScore(score);
messageDelayCounter = 3;
}

private void updateScore(int score) {
updateScore(scoreLabel, String.format("%d", score));
}

private void updateMessageToDefault() {
if (messageDelayCounter < 0) {
updateMessage(messageLabel, Activator.getImageDescriptor("icons/blank.png").createImage());
}
messageDelayCounter--;
}

private void updateMessage() {
updateMessage(messageLabel, Activator.getImageDescriptor("icons/great.png").createImage());
messageDelayCounter = 3;
}

private void updateMessage(final Label label, final Image image) {
PlatformUI.getWorkbench().getDisplay().asyncExec(new Runnable() {

@Override
public void run() {
label.setImage(image);
}
});
}

public void onSwitchRole() {
updateMessage(messageLabel, Activator.getImageDescriptor("icons/insane.png").createImage());
messageDelayCounter = 3;
private String getSwitchRoleImage(int multiplier) {
if (multiplier == Game._4X_MULTIPLIER) {
return "icons/insane.png";
} else if (multiplier == Game._2_MULTIPLIER) {
return "icons/great.png";
} else {
return "icons/okay.png";
}
}
}
20 changes: 7 additions & 13 deletions test/com/happyprog/pairhero/game/GameTest.java
@@ -1,5 +1,6 @@
package com.happyprog.pairhero.game;

import static org.mockito.Matchers.*;
import static org.mockito.Mockito.*;

import org.junit.Before;
Expand Down Expand Up @@ -62,13 +63,6 @@ public void onGameStart_subscribeToRefactorings() throws Exception {
verify(refactoringSubscriber).subscribe(game);
}

@Test
public void onGameStart_updateScore() throws Exception {
game.start();

verify(view).updateScore(0);
}

@Test
public void onGameStop_stopTimer() throws Exception {
game.stop();
Expand Down Expand Up @@ -153,7 +147,7 @@ public void onSwitchRole_tellViewThatProgrammerShouldSwitch() throws Exception {

game.onSwitchRole();

verify(view).onSwitchRole();
verify(view).onSwitchRole(anyInt(), anyInt());
}

@Test
Expand All @@ -164,7 +158,7 @@ public void whenSwitchingRolesTakesLessThan30Seconds_add4xMultiplierToScore() th

game.onSwitchRole();

verify(view).updateScore(4);
verify(view).onSwitchRole(4, Game._4X_MULTIPLIER);
}

@Test
Expand All @@ -177,7 +171,7 @@ public void whenSwitchingRoleTakesBetween30And120Seconds_add2xMultiplierToScore(

game.onSwitchRole();

verify(view).updateScore(2);
verify(view).onSwitchRole(2, Game._2_MULTIPLIER);
}

@Test
Expand All @@ -190,7 +184,7 @@ public void whenSwitchingRoleTakesMoreThan120Seconds_noMuliplierIsAddedToScore()

game.onSwitchRole();

verify(view).updateScore(1);
verify(view).onSwitchRole(1, Game._1X_MULTIPLIER);

}

Expand All @@ -200,7 +194,7 @@ public void onGreenTest_updateScore() throws Exception {

game.onGreenTest();

verify(view).updateScore(Game.GREEN_TEST_POINTS);
verify(view).onGreenTest(Game.GREEN_TEST_POINTS);
}

@Test
Expand All @@ -209,6 +203,6 @@ public void onRefactoring_updateScore() throws Exception {

game.onRefactoring();

verify(view).updateScore(Game.REFACTORING_POINTS);
verify(view).onRefactoring(Game.REFACTORING_POINTS);
}
}

0 comments on commit 91bfe69

Please sign in to comment.