How to write the first achievement

At the end of this tutorial you have created an achievement for working on files in Eclipse on Mondays after 6 PM.

Make sure that CodeCaterpillar with at least version 0.9.0 is installed. Create a Plug-in “de.unibonn.myachievements” and add a dependency to “de.unibonn.iai.se.codecaterpillar.codeachiever” in the Manifest. Afterwards you should be able to add an Extension Points to the extension “de.unibonn.iai.se.codecaterpillar.codeachiever.achievements” (see Figure 1 below). Achievements that are registered via this extension point are only read during the start-up of Eclipse.

Figure 1 The extension point to register achievements.

One can add three different elements to this extension point by right-clicking on the extension (see Figure 2):

  • Achievement
  • LeveledAchievement
  • Category

An Achievement can only be gained one time, whereas on the other side the LeveledAchievement is an achievement that increases in level based on a progress, which is tracked based on the behaviour. The Category is used to group the achievements together in the Achievement Profile View of CodeCaterpillar. In this tutorial we will use a LeveledAchievement, without much difficulty, the achievement could also be added as a one-time achievement.

Figure 2 One can create an Achievement, LevelAchivement or Category at the extension point.

An achievement (one-time and leveled) has a unique id, a name that should be used to display the achievement, a 40×40 picture (make sure that it is 40×40) and a behaviour class. The behaviour class is the controller for the achievement. As we want to do a leveled achievement, we need to select a ILevelStrategy as well. Either reuse an existing one or create one on your own. This level strategy defines what amount of progress is needed to gain a specific level.

We would like to define an achievement for those manic mondays that don't seem to end and you end up working still after 6 P.M.

Write the following values in the LeveledAchievement element (we selected a 40×40 image an put it to “resources/images.jpg” in the plugin):

id="myfirstachievement.mondayevening"
Name="Ah those Manic Mondays"
Behaviour="achievevement.funny.MondayEveningBehaviour"
LevelStrategy="de.unibonn.iai.se.codecaterpillar.codeachiever.api.leveled.strategy.MildLevelStrategy"
UIPicture="resources/images.jpg"

After you have added all those values, your view should look like Figure 3. The name and picture are used in the extension view.

Figure 3
<?xml version="1.0" encoding="UTF-8"?>
<?eclipse version="3.4"?>
<plugin>
   <extension
         point="de.unibonn.iai.se.codecaterpillar.codeachiever.achievements">
      <LeveledAchievement
            Behaviour="achievevement.funny.MondayEveningBehaviour"
            LevelStrategy="de.unibonn.iai.se.codecaterpillar.codeachiever.api.leveled.strategy.MildLevelStrategy"
            Name="Ah those Manic Mondays"
            UIPicture="resources/images.jpg"
            id="myfirstachievement.mondayevening">
      </LeveledAchievement>
   </extension>
 
</plugin>
Complete content of the Plugin.xml

To have not only a name but also a description that should be used in the UI, you need to set the description of the label in the behaviour class, this is done in the initLabel(AchievementLabel) method (see Figure below).

Now we want to implement that our achievement has the behaviour to make progress once we change a file on a Monday after 6 PM. CodeCaterpillar already provides an event for file changes with a timestamp, thus we register ourselves for those events by defining a method with a single parameter of type FileChangeEvent and annotate it with Subscribe (CodeCaterpillar uses the Google Guava Eventbus . A FileChangeEvent has a timestamp, with the help of a CalenderUtil class in the CodeAchiever plugin, we can simply write our requirement down. In the case that we have a file change event on Mondays after 6 PM we would like to increase our progress and (as we don't want to increase the progress for this session in that case) untrack the achievement for this session.

The full code:

package achievevement.funny;
 
import com.google.common.eventbus.Subscribe;
 
import de.unibonn.iai.se.codecaterpillar.codeachiever.api.leveled.LeveledAchievementBehaviour;
import de.unibonn.iai.se.codecaterpillar.codeachiever.helper.CalendarUtil;
import de.unibonn.iai.se.codecaterpillar.codeachiever.helper.CalendarUtil.Day;
import de.unibonn.iai.se.codecaterpillar.codeachiever.label.AchievementLabel;
import de.unibonn.iai.se.codecaterpillar.notification.events.FileChangeEvent;
 
public class MondayEveningBehaviour extends LeveledAchievementBehaviour {
 
 
	@Override
	public void initLabel(AchievementLabel label) {
	  label.setDescription("Working on Mondays after 6 P.M.");
	}
 
	@Subscribe
	public void handleFileChangeEvent(FileChangeEvent fileChange){
	  long timestamp = fileChange.getTimestamp();
 
	  Day day = CalendarUtil.getWeekDay(timestamp);
	  int hour = CalendarUtil.getLocalHour(timestamp);
 
	  if(day == Day.MONDAY && hour > 17){
	    progressUp();
	    untrackForSession();
	  }
	}
 
}

After you started a runtime Eclipse with your fresh implemented achievement, you should see your achievement being shown in the CodeCaterpillar>“Achievement Profile” view as an achievement card (see Figure 4).

Figure 4 The achievement card of the final achievement in the profile view.
Last modified: 2017/08/30 02:13
*