-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModel_Objects.js
107 lines (93 loc) · 2.82 KB
/
Model_Objects.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
MAX_IN_CENTER = 3;
//The main bookkeeper of the current model
Model = function() {
this.ondeck = null;
this.queue = [];
this.incenter = [];
this.rotate_time = null;
}
//Set rotate_time to this
setRotationTime = function(model, seconds) {
model.rotate_time = seconds;
}
//Return the index of the author's opinion
//in the queue, or -1 if it does not exist
queueIndex = function(array, author) {
for(var i = 0; i < array.length; i++) {
if(array[i].author == author) {
return i;
}
}
//Not found
return -1;
}
//Add this opinion to the back (bottom) of the queue
//if the author is not already in the queue
//Returns: false for already in the queue, true for added
addOpinion = function(model, author, text) {
if(queueIndex(model.queue, author) == -1 && queueIndex(model.incenter, author) == -1 && (model.ondeck == null || model.ondeck.author != author)) { //Not already in fishbowl/queue
model.queue.push(new Opinion(author, text));
return true;
}
//Already in queue, so no opinion added
return false;
}
//Upvote this author with the given userId
//Returns: false if they had already upvoted this opinion,
//or the opinion does not even exist. True otherwise
addVote = function(model, author, userId) {
var index = queueIndex(model.queue, author);
if(index == -1) { //No such author
return false;
}
var record = model.queue[index];
var voters = record.voters;
index = voters.indexOf(userId);
if(index != -1) { //Already voted for model
return false;
}
voters.push(userId);
record.upvotes++;
return true;
}
//Remove the userId's vote from author.
//Returns false if the opinion did not exist,
//or userId had not voted for this opinion. True otherwise
removeVote = function(model, author, userId) {
var index = queueIndex(model.queue, author);
if(index == -1) { //No such author
return false;
}
var record = model.queue[index];
var voters = record.voters;
index = voters.indexOf(userId);
if(index == -1) {
return false;
}
voters.splice(index, 1);
record.upvotes--;
return true;
}
//Pop the first opinion off the queue, and place it on deck.
//The previous opinion on deck is removed.
//Returns: false if there were no opinions to remove. True otherwise
removeMax = function(model) {
model.queue.sort(function(a, b) { return b.upvotes - a.upvotes; });
var old_ondeck = model.ondeck;
if(old_ondeck) {
model.incenter.push(old_ondeck);
if(model.incenter.length > MAX_IN_CENTER) {
model.incenter.shift(); //Throw person who has been in longest out
}
}
if(model.queue.length) {
model.ondeck = model.queue.shift(); //Top of the queue comes on deck
}
}
//A class to represent an "opinion" in the fishbowl world
Opinion = function(author, text) {
this.author = author;
this.text = text;
this.upvotes = 0;
this.voters = [];
}