~rick-rickspencer3/+junk/flash2

« back to all changes in this revision

Viewing changes to components/setDataWorker.js

  • Committer: Rick Spencer
  • Date: 2015-07-29 07:56:10 UTC
  • Revision ID: rick.spencer@canonical.com-20150729075610-0q7vl1qqc2gqcxzg
added comments and formatting to workerscript, tweaked layout of rundialog.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
WorkerScript.onMessage = function (message) {
2
2
    var set = message.set
3
 
    var numCorrect = 0.0
4
 
    var numTotal = 0.0
 
3
    var correctTrials = 0.0
 
4
    var totalTrials = 0.0
5
5
    var hoursSinceRun = -1
6
6
    var maxTimestamp = -1
7
7
    var today = new Date().getTime() / (1000 * 60 * 60 * 24)
12
12
        var cardCorrect = 0
13
13
        var consecutiveCorrect = 0
14
14
        var mostRecentIncorrect = ""
 
15
 
 
16
        //handle if the card has bever been run
15
17
        if(set["cards"][c]["trials"].length === 0) {
16
18
            set["cards"][c]["visible"] = true
 
19
            //in order to calculate the percentage accuracy,
 
20
            //assume that a card that has never run has one incorrect trial
 
21
            totalTrials += 1
 
22
        }
 
23
        else {
 
24
            totalTrials += set["cards"][c]["trials"].length
17
25
        }
18
26
 
19
 
        if(set["cards"][c]["trials"].length < 1)numTotal += 1
 
27
        //find latest streak for each card,
 
28
        //find the most recent correct trial
20
29
        for(var t in set["cards"][c]["trials"]) {
21
30
            var ts = set["cards"][c]["trials"][t]["timestamp"]
22
31
            if(set["cards"][c]["trials"][t]["result"]) {            
23
 
                numCorrect += 1
 
32
                correctTrials += 1
24
33
                consecutiveCorrect += 1
25
34
                cardCorrect += 1
26
35
            }
30
39
                mostRecentIncorrect = ts
31
40
            }
32
41
            if(ts > maxTimestamp) maxTimestamp = ts
33
 
            numTotal += 1
 
42
            totalTrials += 1
34
43
        }
35
44
        set["cards"][c]["streak"] = consecutiveCorrect
 
45
 
 
46
        //calculate the cards rate of success
36
47
        if(set["cards"][c]["trials"].length === 0) {
37
48
            set["cards"][c]["rate"] = 0
38
49
        }
40
51
            set["cards"][c]["rate"] = cardCorrect / set["cards"][c]["trials"].length
41
52
        }
42
53
 
 
54
        //calculate the latest trial run
43
55
        if(set["cards"][c]["trials"].length > 0){
44
56
            var mostRecentTrial = sinceString(set["cards"][c]["trials"][set["cards"][c]["trials"].length - 1]["timestamp"] )
45
57
            set["cards"][c]["hoursSinceRun"] = mostRecentTrial
47
59
            set["cards"][c]["hoursSinceRun"] = -1
48
60
        }
49
61
 
 
62
        //apply rules to determine if card should be shown when run
50
63
        if((today - (mostRecentIncorrect / (1000 * 60 * 60 * 24)) < showAfterDays) & consecutiveCorrect >= hideAfterConsecutiveCorrect)
51
64
        {
52
65
            set["cards"][c]["visible"] = false
54
67
        else {
55
68
            set["cards"][c]["visible"] = true
56
69
        }
 
70
 
57
71
        //this case is likely only to happen in testing
58
72
        if(consecutiveCorrect > 4 & mostRecentIncorrect == "")
59
73
        {
61
75
        }
62
76
    }
63
77
 
 
78
    //calculate when any card in the set was last run
64
79
    if(maxTimestamp > -1) {
65
80
        hoursSinceRun =  (new Date().getTime() / (1000 * 60 * 60)) -(maxTimestamp / (1000 * 60 * 60))
66
81
    }
67
82
 
68
83
    var sc = -1
69
 
    if(numTotal !== 0) sc = numCorrect/numTotal
 
84
    //print(totalTrials)
 
85
    if(totalTrials !== 0) sc = correctTrials/totalTrials
70
86
 
71
87
    WorkerScript.sendMessage({'score':sc, "hours": Math.floor(hoursSinceRun), "set": set})
72
88
    }