Skip to content

Commit

Permalink
Merge branch 'master' into staging
Browse files Browse the repository at this point in the history
  • Loading branch information
noiresonn committed May 11, 2017
2 parents 34c2716 + 2c05904 commit f3947f0
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 18 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ release.properties
dependency-reduced-pom.xml
buildNumber.properties
.mvn/timing.properties

profiles.txt
5 changes: 5 additions & 0 deletions app.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,10 @@
"test": "mvn test -B"
}
}
},
"env": {
"DB_ENDPOINT": {
"required": true
}
}
}
Binary file removed profiles.txt
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,20 @@
import java.io.ObjectOutputStream;
import java.util.TreeSet;


/**
* Simple backup register
*
* Created by Vili
*/
public class ProfileRegister {

private TreeSet<Profile> profileTreeSet;

/**
* Getting the profile treeset from a textfile.
* @return treeset with profiles
* @throws IOException
*/
public TreeSet<Profile> getProfileRegister() throws IOException {
try {
FileInputStream fi = new FileInputStream(new File("profiles.txt"));
Expand All @@ -22,7 +31,6 @@ public TreeSet<Profile> getProfileRegister() throws IOException {
fi.close();
oi.close();
System.out.println("File loaded");

return profileTreeSet;
} catch (FileNotFoundException e) {
System.out.println("File not found");
Expand All @@ -33,6 +41,11 @@ public TreeSet<Profile> getProfileRegister() throws IOException {
}
}

/**
* Profile treeset updating/creating
* @param ts
* @throws IOException
*/
public void saveProfileRegister(TreeSet<Profile> ts) throws IOException {
try {
FileOutputStream f = new FileOutputStream(new File("profiles.txt"));
Expand All @@ -45,6 +58,4 @@ public void saveProfileRegister(TreeSet<Profile> ts) throws IOException {
System.out.println("File not found");
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@
import com.gameder.app.preferences.Preferences;
import org.springframework.web.bind.annotation.*;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.TreeSet;

@RestController
public class ProfilesHandler {
private static final String dbEndpoint = System.getenv("DB_ENDPOINT");

private TreeSet<Profile> profileTreeSet = new TreeSet<>();

Expand Down Expand Up @@ -65,7 +70,6 @@ public ArrayList<Profile> getGamerListTest() {
* */
public Profile findRoot() {
int root = Preferences.getRandomPreferences();
generateProfiles();
Iterator<Profile> iterator = profileTreeSet.iterator();

while(true) {
Expand All @@ -92,14 +96,68 @@ public Profile findRoot() {
}
}

/**
* For database
*/
public void loadProfileTree() {
try {
URL url = new URL(dbEndpoint);
URLConnection con = url.openConnection();
HttpURLConnection http = (HttpURLConnection) con;
http.setRequestMethod("GET");
http.setRequestProperty("Content-Type", "text/plain;charset=UTF-8");

System.out.println(http.getResponseCode());

BufferedInputStream bis = new BufferedInputStream(http.getInputStream());
ObjectInputStream ois = new ObjectInputStream(bis);

profileTreeSet = (TreeSet) ois.readObject();
System.out.printf("Size of the tree is %d\n", profileTreeSet.size());

System.out.println("load");
} catch (Exception e) {
System.out.println("Could not make the request");
}
}

/**
* For database
*/
public void saveProfileTree(TreeSet<Profile> profiles) {
try {
URL url = new URL(dbEndpoint);
URLConnection con = url.openConnection();
HttpURLConnection http = (HttpURLConnection) con;
http.setRequestMethod("POST");
http.setRequestProperty("Content-Type", "text/plain;charset=UTF-8");
http.setDoOutput(true);

BufferedOutputStream bos = new BufferedOutputStream(http.getOutputStream());
ObjectOutputStream oos = new ObjectOutputStream(bos);

oos.writeObject(profiles);
oos.close();

System.out.printf("save: %d\n", http.getResponseCode());
} catch (Exception e) {
System.out.println("Could not make the request");
}
}

/**
* Generates 50 profiles and adds them to the tree
*/
public void generateProfiles() {
TreeSet<Profile> profiles = new TreeSet<Profile>();

for(int i = 0; i < 50; i++) {
Profile p = ProfileGenerator.getRandomProfile();
profileTreeSet.add(p);
profiles.add(p);
}

this.saveProfileTree(profiles);
this.loadProfileTree();
}

/**
Expand All @@ -119,6 +177,17 @@ public ArrayList<Profile> getFiveProfiles() {
* If either end is reached, it will skip that side
* The algorithm can be easily limited if the profile pool is huge
* */

/**
* If both ends have been reached, the roots will be reset
*/
if(maxRoot == null && minRoot == null) {
minnull = false;
maxnull = false;
maxRoot = profileTreeSet.higher(rootPreference);
minRoot = profileTreeSet.lower(rootPreference);
}

if(!minnull && !maxnull) {
/**
* Minroot is closer
Expand Down Expand Up @@ -154,22 +223,18 @@ public ArrayList<Profile> getFiveProfiles() {
minRoot = profileTreeSet.lower(minRoot);
}

/**
* If both ends have been reached, the roots will be reset
*/
if(maxRoot == null && minRoot == null) {
minnull = false;
maxnull = false;
maxRoot = profileTreeSet.higher(rootPreference);
minRoot = profileTreeSet.lower(rootPreference);
}
}
return profiles;
} else {
System.out.println("Reached else");
return null;
}
}

public Profile getRoot() {
return this.rootPreference;
}

public TreeSet<Profile> getProfilesTreeset() {
return this.profileTreeSet;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class ProfileSetTest {
public void ProfileSetIsFilled() {
ProfilesHandler p = new ProfilesHandler();

p.generateProfiles();
p.getGamerList();

TreeSet<Profile> ts = p.getProfilesTreeset();
Iterator ite = ts.iterator();
Expand All @@ -29,7 +29,7 @@ public void ProfileSetIsFilled() {
public void ProfileSetIsInOrder() {
ProfilesHandler p = new ProfilesHandler();

p.generateProfiles();
p.getGamerList();

TreeSet<Profile> ts = p.getProfilesTreeset();

Expand Down Expand Up @@ -67,7 +67,9 @@ public void ProfileListIsValid() {
public void RootIsFound() {
ProfilesHandler p = new ProfilesHandler();

Assert.assertNotNull(p.findRoot());
p.getGamerList();

Assert.assertNotNull(p.getRoot());
}

@Test
Expand Down

0 comments on commit f3947f0

Please sign in to comment.