77import com .mongodb .client .MongoCollection ;
88import com .mongodb .client .MongoDatabase ;
99import com .mongodb .client .model .Filters ;
10+ import com .mongodb .client .model .ReplaceOptions ;
1011import com .mongodb .client .model .Updates ;
1112import net .swofty .service .generic .MongoDB ;
1213import org .bson .Document ;
1314
15+ import java .util .HashMap ;
16+ import java .util .List ;
17+ import java .util .Map ;
18+
1419public record ElectionDatabase (String key ) implements MongoDB {
1520 public static MongoClient client ;
1621 public static MongoDatabase database ;
1722 public static MongoCollection <Document > electionCollection ;
23+ public static MongoCollection <Document > votesCollection ;
24+ public static MongoCollection <Document > talliesCollection ;
1825
1926 @ Override
2027 public MongoDB connect (String connectionString ) {
@@ -24,6 +31,8 @@ public MongoDB connect(String connectionString) {
2431
2532 database = client .getDatabase ("Minestom" );
2633 electionCollection = database .getCollection ("elections" );
34+ votesCollection = database .getCollection ("election-votes" );
35+ talliesCollection = database .getCollection ("election-tallies" );
2736 return this ;
2837 }
2938
@@ -42,33 +51,25 @@ public boolean exists() {
4251 @ Override
4352 public Object get (String key , Object def ) {
4453 Document doc = electionCollection .find (Filters .eq ("_id" , this .key )).first ();
45- if (doc == null ) {
46- return def ;
47- }
54+ if (doc == null ) return def ;
4855 return doc .get (key );
4956 }
5057
5158 @ Override
5259 public void insertOrUpdate (String key , Object value ) {
53- if (exists ()) {
54- Document query = new Document ("_id" , this .key );
55- Document found = electionCollection .find (query ).first ();
56- assert found != null ;
57- electionCollection .updateOne (found , Updates .set (key , value ));
58- return ;
59- }
60- Document newDoc = new Document ("_id" , this .key );
61- newDoc .append (key , value );
62- electionCollection .insertOne (newDoc );
60+ Document doc = new Document ("_id" , this .key ).append (key , value );
61+ electionCollection .replaceOne (
62+ Filters .eq ("_id" , this .key ),
63+ doc ,
64+ new ReplaceOptions ().upsert (true )
65+ );
6366 }
6467
6568 @ Override
6669 public boolean remove (String id ) {
6770 Document query = new Document ("_id" , id );
6871 Document found = electionCollection .find (query ).first ();
69- if (found == null ) {
70- return false ;
71- }
72+ if (found == null ) return false ;
7273 electionCollection .deleteOne (query );
7374 return true ;
7475 }
@@ -80,14 +81,88 @@ public static String loadElectionData() {
8081 }
8182
8283 public static void saveElectionData (String serializedData ) {
83- Document query = new Document ("_id" , "election_data" );
84- Document existing = electionCollection .find (query ).first ();
85- if (existing != null ) {
86- electionCollection .updateOne (query , Updates .set ("data" , serializedData ));
87- } else {
88- Document newDoc = new Document ("_id" , "election_data" );
89- newDoc .append ("data" , serializedData );
90- electionCollection .insertOne (newDoc );
84+ Document doc = new Document ("_id" , "election_data" ).append ("data" , serializedData );
85+ electionCollection .replaceOne (
86+ Filters .eq ("_id" , "election_data" ),
87+ doc ,
88+ new ReplaceOptions ().upsert (true )
89+ );
90+ }
91+
92+ public static void castVote (String playerId , String candidateName , int electionYear ) {
93+ String talliesDocId = "tallies_" + electionYear ;
94+
95+ Document existingVote = votesCollection .find (
96+ Filters .and (Filters .eq ("_id" , playerId ), Filters .eq ("electionYear" , electionYear ))
97+ ).first ();
98+
99+ if (existingVote != null ) {
100+ String oldCandidate = existingVote .getString ("candidate" );
101+ if (oldCandidate != null && !oldCandidate .equals (candidateName )) {
102+ talliesCollection .updateOne (
103+ Filters .eq ("_id" , talliesDocId ),
104+ Updates .inc (oldCandidate , -1L )
105+ );
106+ }
107+ }
108+
109+ Document voteDoc = new Document ("_id" , playerId )
110+ .append ("candidate" , candidateName )
111+ .append ("electionYear" , electionYear );
112+ votesCollection .replaceOne (
113+ Filters .eq ("_id" , playerId ),
114+ voteDoc ,
115+ new ReplaceOptions ().upsert (true )
116+ );
117+
118+ if (existingVote == null || !candidateName .equals (existingVote .getString ("candidate" ))) {
119+ talliesCollection .updateOne (
120+ Filters .eq ("_id" , talliesDocId ),
121+ Updates .inc (candidateName , 1L ),
122+ new com .mongodb .client .model .UpdateOptions ().upsert (true )
123+ );
91124 }
92125 }
126+
127+ public static String getPlayerVote (String playerId , int electionYear ) {
128+ Document doc = votesCollection .find (
129+ Filters .and (Filters .eq ("_id" , playerId ), Filters .eq ("electionYear" , electionYear ))
130+ ).first ();
131+ if (doc == null ) return null ;
132+ return doc .getString ("candidate" );
133+ }
134+
135+ public static Map <String , Long > getTallies (int electionYear ) {
136+ String talliesDocId = "tallies_" + electionYear ;
137+ Document doc = talliesCollection .find (Filters .eq ("_id" , talliesDocId )).first ();
138+ if (doc == null ) return new HashMap <>();
139+
140+ Map <String , Long > tallies = new HashMap <>();
141+ for (String key : doc .keySet ()) {
142+ if (key .equals ("_id" )) continue ;
143+ Object val = doc .get (key );
144+ if (val instanceof Number num ) {
145+ tallies .put (key , num .longValue ());
146+ }
147+ }
148+ return tallies ;
149+ }
150+
151+ public static void initTallies (int electionYear , List <String > candidateNames ) {
152+ String talliesDocId = "tallies_" + electionYear ;
153+ Document doc = new Document ("_id" , talliesDocId );
154+ for (String name : candidateNames ) {
155+ doc .append (name , 0L );
156+ }
157+ talliesCollection .replaceOne (
158+ Filters .eq ("_id" , talliesDocId ),
159+ doc ,
160+ new ReplaceOptions ().upsert (true )
161+ );
162+ }
163+
164+ public static void clearVotesForYear (int electionYear ) {
165+ votesCollection .deleteMany (Filters .eq ("electionYear" , electionYear ));
166+ talliesCollection .deleteMany (Filters .eq ("_id" , "tallies_" + electionYear ));
167+ }
93168}
0 commit comments