Search in sources :

Example 16 with RiderStageConnection

use of ch.hsr.sa.radiotour.dataaccess.models.RiderStageConnection in project app by TourLive.

the class RiderStageConnectionRepository method updateRiderStageConnection.

@Override
public void updateRiderStageConnection(final RiderStageConnection newRiderStageConnection, final RiderStageConnection oldRiderStageConnection, OnUpdateRiderStageConnectionCallBack callback) {
    Realm realm = Realm.getInstance(RadioTourApplication.getInstance());
    realm.executeTransaction((Realm db) -> {
        RiderStageConnection res = db.where(RiderStageConnection.class).equalTo("id", oldRiderStageConnection.getId()).findFirst();
        res.setBonusPoint(newRiderStageConnection.getBonusPoint());
        res.setSprintBonusPoints(newRiderStageConnection.getSprintBonusPoints());
        res.setMountainBonusPoints(newRiderStageConnection.getMountainBonusPoints());
        res.setBonusTime(newRiderStageConnection.getBonusTime());
        res.setVirtualGap(newRiderStageConnection.getVirtualGap());
        res.setOfficialGap(newRiderStageConnection.getOfficialGap());
        res.setOfficialTime(newRiderStageConnection.getOfficialTime());
        res.setType(newRiderStageConnection.getType());
        res.setMoney(newRiderStageConnection.getMoney());
    });
}
Also used : RiderStageConnection(ch.hsr.sa.radiotour.dataaccess.models.RiderStageConnection) Realm(io.realm.Realm)

Example 17 with RiderStageConnection

use of ch.hsr.sa.radiotour.dataaccess.models.RiderStageConnection in project app by TourLive.

the class RiderStageConnectionRepository method updateRiderStageConnectionTime.

@Override
public void updateRiderStageConnectionTime(long timeBefore, long timeStamp, final RaceGroup res, OnUpdateRiderStageConnectionCallBack callback) {
    Realm realm = Realm.getInstance(RadioTourApplication.getInstance());
    realm.beginTransaction();
    if (timeBefore > 0 && !res.getRiders().isEmpty()) {
        for (Rider r : res.getRiders()) {
            if (!r.getRiderStages().isEmpty()) {
                r.getRiderStages().first().setVirtualGap(r.getRiderStages().first().getVirtualGap() - timeBefore);
            }
        }
    }
    if (!res.getRiders().isEmpty()) {
        for (Rider r : res.getRiders()) {
            if (!r.getRiderStages().isEmpty()) {
                r.getRiderStages().first().setVirtualGap(r.getRiderStages().first().getVirtualGap() + timeStamp);
            }
        }
    }
    RealmResults<RiderStageConnection> connections = realm.where(RiderStageConnection.class).findAll();
    List<RiderStageConnection> cons = realm.copyFromRealm(connections);
    cons.sort(new RiderStageConnectionComparatorVirtualGap());
    for (int i = 0; i < cons.size(); i++) {
        RiderRanking riderRanking = realm.where(RiderStageConnection.class).equalTo("id", cons.get(i).getId()).findFirst().getRiderRanking(RankingType.VIRTUAL);
        if (riderRanking != null) {
            riderRanking.setRank(i + 1);
        }
    }
    realm.commitTransaction();
    if (callback != null) {
        callback.onSuccess();
    }
}
Also used : RiderStageConnectionComparatorVirtualGap(ch.hsr.sa.radiotour.dataaccess.models.RiderStageConnectionComparatorVirtualGap) RiderStageConnection(ch.hsr.sa.radiotour.dataaccess.models.RiderStageConnection) RiderRanking(ch.hsr.sa.radiotour.dataaccess.models.RiderRanking) Realm(io.realm.Realm) Rider(ch.hsr.sa.radiotour.dataaccess.models.Rider)

Example 18 with RiderStageConnection

use of ch.hsr.sa.radiotour.dataaccess.models.RiderStageConnection in project app by TourLive.

the class RiderStageConnectionRepositoryInstrumentedTest method updateRiderStageConnection.

@Test
public void updateRiderStageConnection() {
    initalData();
    String id;
    RiderStageConnection res = realm.where(RiderStageConnection.class).findAll().first();
    id = res.getId();
    RiderStageConnection newStageConnection = new RiderStageConnection();
    newStageConnection.setType(RiderStateType.AKTIVE);
    newStageConnection.setBonusPoint(100);
    newStageConnection.setBonusTime(2020);
    newStageConnection.setMountainBonusPoints(4000);
    newStageConnection.setSprintBonusPoints(5000);
    newStageConnection.setVirtualGap(res.getVirtualGap());
    newStageConnection.setOfficialTime(res.getOfficialTime());
    newStageConnection.setOfficialGap(res.getOfficialGap());
    synchronized (this) {
        riderStageConnectionRepository.updateRiderStageConnection(newStageConnection, res, onUpdateRiderStageConnectionCallBack);
    }
    RiderStageConnection endRes = realm.where(RiderStageConnection.class).equalTo("id", id).findAll().first();
    assertEquals(100, endRes.getOfficialGap());
    assertEquals(100, endRes.getVirtualGap());
    assertEquals(100, endRes.getOfficialTime());
    assertEquals(100, endRes.getBonusPoint());
    assertEquals(2020, endRes.getBonusTime());
    assertEquals(4000, endRes.getMountainBonusPoints());
    assertEquals(5000, endRes.getSprintBonusPoints());
    assertEquals(RiderStateType.AKTIVE, endRes.getType());
}
Also used : RiderStageConnection(ch.hsr.sa.radiotour.dataaccess.models.RiderStageConnection) Test(org.junit.Test)

Example 19 with RiderStageConnection

use of ch.hsr.sa.radiotour.dataaccess.models.RiderStageConnection in project app by TourLive.

the class RiderStageConnectionRepositoryInstrumentedTest method addRiderStageConnection.

@Test
public void addRiderStageConnection() {
    RiderStageConnection riderStageConnection = new RiderStageConnection();
    riderStageConnection.setBonusPoint(10);
    riderStageConnection.setBonusTime(20);
    riderStageConnection.setSprintBonusPoints(100);
    riderStageConnection.setMountainBonusPoints(200);
    riderStageConnection.setOfficialGap(100);
    riderStageConnection.setOfficialTime(100);
    riderStageConnection.setType(RiderStateType.DNC);
    riderStageConnection.setVirtualGap(100);
    synchronized (this) {
        riderStageConnectionRepository.addRiderStageConnection(riderStageConnection, onSaveRiderStageConnectionCallback);
    }
    RealmResults<RiderStageConnection> res = realm.where(RiderStageConnection.class).findAll();
    assertEquals(1, res.size());
    assertEquals(100, res.get(0).getOfficialGap());
    assertEquals(100, res.get(0).getVirtualGap());
    assertEquals(100, res.get(0).getOfficialTime());
    assertEquals(10, res.get(0).getBonusPoint());
    assertEquals(20, res.get(0).getBonusTime());
    assertEquals(100, res.get(0).getSprintBonusPoints());
    assertEquals(200, res.get(0).getMountainBonusPoints());
    assertEquals(RiderStateType.DNC, res.get(0).getType());
}
Also used : RiderStageConnection(ch.hsr.sa.radiotour.dataaccess.models.RiderStageConnection) Test(org.junit.Test)

Example 20 with RiderStageConnection

use of ch.hsr.sa.radiotour.dataaccess.models.RiderStageConnection in project app by TourLive.

the class Parser method parseRidersAndPersist.

public static void parseRidersAndPersist(JSONArray riders) throws InterruptedException {
    final JSONArray ridersJson = riders;
    Runnable runnable = new Runnable() {

        public void run() {
            for (int i = 0; i < ridersJson.length(); i++) {
                try {
                    JSONObject jsonRider = ridersJson.getJSONObject(i);
                    Rider rider = new Rider();
                    rider.setStartNr(jsonRider.getInt(startNr));
                    rider.setCountry(jsonRider.getString(country));
                    rider.setName(jsonRider.getString(name));
                    rider.setTeamName(jsonRider.getString(team));
                    rider.setTeamShortName(jsonRider.getString(teamShort));
                    rider.setRiderID(jsonRider.getInt(riderID));
                    synchronized (this) {
                        Context.addRider(rider);
                    }
                    RiderStageConnection riderStageConnection = new RiderStageConnection();
                    riderStageConnection.setBonusPoint(0);
                    riderStageConnection.setBonusTime(0);
                    riderStageConnection.setOfficialGap(jsonRider.getLong("timeRueckLong"));
                    riderStageConnection.setOfficialTime(jsonRider.getLong("timeOffLong"));
                    riderStageConnection.setVirtualGap(jsonRider.getLong("timeVirtLong"));
                    String state = jsonRider.getString("active");
                    if (state.equals("true")) {
                        riderStageConnection.setType(RiderStateType.AKTIVE);
                    } else {
                        riderStageConnection.setType(RiderStateType.DNC);
                    }
                    RiderStageConnection riderStageConnectionOne;
                    synchronized (this) {
                        riderStageConnectionOne = Context.addRiderStageConnection(riderStageConnection);
                    }
                    RealmList<RiderStageConnection> riderStageConnections = new RealmList<>();
                    riderStageConnections.add(riderStageConnectionOne);
                    Context.updateRiderStageConnection(rider, riderStageConnections);
                } catch (JSONException e) {
                    Log.d(Parser.class.getSimpleName(), "APP - PARSER - RIDERS - " + e.getMessage());
                }
            }
        }
    };
    Thread threadRiders = new Thread(runnable);
    threadRiders.start();
    threadRiders.join();
    Thread threadGroup = createDefaultGroup();
    threadGroup.start();
    threadGroup.join();
    updateRiderConnectionRankByOfficalGap();
}
Also used : RiderStageConnection(ch.hsr.sa.radiotour.dataaccess.models.RiderStageConnection) RealmList(io.realm.RealmList) JSONObject(org.json.JSONObject) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) Rider(ch.hsr.sa.radiotour.dataaccess.models.Rider)

Aggregations

RiderStageConnection (ch.hsr.sa.radiotour.dataaccess.models.RiderStageConnection)21 Realm (io.realm.Realm)9 Test (org.junit.Test)7 RealmList (io.realm.RealmList)6 Rider (ch.hsr.sa.radiotour.dataaccess.models.Rider)5 RiderRanking (ch.hsr.sa.radiotour.dataaccess.models.RiderRanking)3 Judgement (ch.hsr.sa.radiotour.dataaccess.models.Judgement)2 RiderStageConnectionComparatorMountainPoints (ch.hsr.sa.radiotour.dataaccess.models.RiderStageConnectionComparatorMountainPoints)2 RiderStageConnectionComparatorPoints (ch.hsr.sa.radiotour.dataaccess.models.RiderStageConnectionComparatorPoints)2 RiderStageConnectionComparatorVirtualGap (ch.hsr.sa.radiotour.dataaccess.models.RiderStageConnectionComparatorVirtualGap)2 Date (java.util.Date)2 JSONException (org.json.JSONException)2 JudgmentRiderConnection (ch.hsr.sa.radiotour.dataaccess.models.JudgmentRiderConnection)1 Reward (ch.hsr.sa.radiotour.dataaccess.models.Reward)1 RiderStageConnectionComparatorMoney (ch.hsr.sa.radiotour.dataaccess.models.RiderStageConnectionComparatorMoney)1 RiderStageConnectionComparatorOfficalGap (ch.hsr.sa.radiotour.dataaccess.models.RiderStageConnectionComparatorOfficalGap)1 RiderStageConnectionComparatorTimeInLead (ch.hsr.sa.radiotour.dataaccess.models.RiderStageConnectionComparatorTimeInLead)1 Stage (ch.hsr.sa.radiotour.dataaccess.models.Stage)1 JSONArray (org.json.JSONArray)1 JSONObject (org.json.JSONObject)1