| 1 | package io.github.some_example_name; | |
| 2 | ||
| 3 | import com.badlogic.gdx.Gdx; | |
| 4 | import com.badlogic.gdx.files.FileHandle; | |
| 5 | import com.badlogic.gdx.utils.Array; | |
| 6 | import com.badlogic.gdx.utils.Json; | |
| 7 | ||
| 8 | /** <code>JSONHandler</code> handles all JSON input (position, score) ready to read and write from leaderboard.json | |
| 9 | * @see com.badlogic.gdx.utils.Json */ | |
| 10 | public class JSONHandler { | |
| 11 | ||
| 12 | private final Json json; | |
| 13 | private final FileHandle file; | |
| 14 | ||
| 15 | public JSONHandler() { | |
| 16 | json = new Json(); | |
| 17 | file = Gdx.files.local("leaderboard.json"); | |
| 18 | } | |
| 19 | ||
| 20 | /** <code>readLeaderboard()</code> reads leaderboard from leaderboard.json */ | |
| 21 | public Array<LeaderboardEntry> readLeaderboard() { | |
| 22 |
2
1. readLeaderboard : negated conditional → KILLED 2. readLeaderboard : negated conditional → KILLED |
if (!file.exists() || file.length() == 0) { |
| 23 |
1
1. readLeaderboard : replaced return value with null for io/github/some_example_name/JSONHandler::readLeaderboard → NO_COVERAGE |
return new Array<>(); |
| 24 | } | |
| 25 |
1
1. readLeaderboard : replaced return value with null for io/github/some_example_name/JSONHandler::readLeaderboard → KILLED |
return json.fromJson(Array.class, LeaderboardEntry.class, file); |
| 26 | } | |
| 27 | ||
| 28 | /** | |
| 29 | * <code>writeLeaderboard(int finalScore)</code> reads leaderboard from an array of leaderboard entries, | |
| 30 | * adds new finalScore as entry to leaderboard.json, | |
| 31 | * sorts the scores into descending order, keeping only the top 5 in the file, | |
| 32 | * updates the following scores positions if finalScore is within top 5, | |
| 33 | * save file containing new entries | |
| 34 | * @param finalScore | |
| 35 | */ | |
| 36 | public void writeLeaderboard(String name, int finalScore) { | |
| 37 | Array<LeaderboardEntry> entries = readLeaderboard(); | |
| 38 |
1
1. writeLeaderboard : removed call to com/badlogic/gdx/utils/Array::add → KILLED |
entries.add(new LeaderboardEntry(0, name, finalScore)); |
| 39 |
3
1. lambda$writeLeaderboard$0 : replaced int return with 0 for io/github/some_example_name/JSONHandler::lambda$writeLeaderboard$0 → KILLED 2. writeLeaderboard : removed call to com/badlogic/gdx/utils/Array::sort → KILLED 3. lambda$writeLeaderboard$0 : Replaced integer subtraction with addition → KILLED |
entries.sort((a, b) -> b.score - a.score); // sort descending |
| 40 | ||
| 41 |
2
1. writeLeaderboard : changed conditional boundary → SURVIVED 2. writeLeaderboard : negated conditional → KILLED |
if (entries.size > 5) { // keep only top 5 |
| 42 |
1
1. writeLeaderboard : removed call to com/badlogic/gdx/utils/Array::truncate → KILLED |
entries.truncate(5); |
| 43 | } | |
| 44 |
2
1. writeLeaderboard : negated conditional → KILLED 2. writeLeaderboard : changed conditional boundary → KILLED |
for (int i = 0; i < entries.size; i++) { // update following entries |
| 45 |
1
1. writeLeaderboard : Replaced integer addition with subtraction → KILLED |
entries.get(i).position = i + 1; |
| 46 | } | |
| 47 |
1
1. writeLeaderboard : removed call to com/badlogic/gdx/files/FileHandle::writeString → KILLED |
file.writeString(json.prettyPrint(entries), false); // save file |
| 48 | } | |
| 49 | ||
| 50 | /** <code>readLeaderboardAsStrings()</code> returns leaderboard as strings for drawing */ | |
| 51 | public String[] readLeaderboardAsStrings() { | |
| 52 | Array<LeaderboardEntry> entries = readLeaderboard(); | |
| 53 | String[] result = new String[5]; | |
| 54 | ||
| 55 |
2
1. readLeaderboardAsStrings : negated conditional → KILLED 2. readLeaderboardAsStrings : changed conditional boundary → KILLED |
for (int i = 0; i < entries.size; i++) { |
| 56 | LeaderboardEntry entry = entries.get(i); | |
| 57 | result[i] = entry.position + ") " + entry.name + ": " + entry.score; | |
| 58 | } | |
| 59 |
1
1. readLeaderboardAsStrings : replaced return value with null for io/github/some_example_name/JSONHandler::readLeaderboardAsStrings → KILLED |
return result; |
| 60 | } | |
| 61 | ||
| 62 | /** <code>resetFile()</code> clears leaderboard, for testing purposes only */ | |
| 63 | public void resetFile() { | |
| 64 |
1
1. resetFile : removed call to com/badlogic/gdx/files/FileHandle::writeString → KILLED |
file.writeString("[]", false); |
| 65 | } | |
| 66 | } | |
Mutations | ||
| 22 |
1.1 2.2 |
|
| 23 |
1.1 |
|
| 25 |
1.1 |
|
| 38 |
1.1 |
|
| 39 |
1.1 2.2 3.3 |
|
| 41 |
1.1 2.2 |
|
| 42 |
1.1 |
|
| 44 |
1.1 2.2 |
|
| 45 |
1.1 |
|
| 47 |
1.1 |
|
| 55 |
1.1 2.2 |
|
| 59 |
1.1 |
|
| 64 |
1.1 |