|
@@ -1,4 +1,4 @@
|
|
|
-package re.chasam.connector
|
|
|
|
|
|
|
+package re.chasam.connector.impl
|
|
|
|
|
|
|
|
import com.mongodb.MongoException
|
|
import com.mongodb.MongoException
|
|
|
import com.mongodb.client.*
|
|
import com.mongodb.client.*
|
|
@@ -7,41 +7,22 @@ import com.mongodb.client.model.Sorts
|
|
|
import com.mongodb.client.model.UpdateOptions
|
|
import com.mongodb.client.model.UpdateOptions
|
|
|
import com.mongodb.client.model.Updates
|
|
import com.mongodb.client.model.Updates
|
|
|
import org.bson.Document
|
|
import org.bson.Document
|
|
|
-import re.chasam.models.Player
|
|
|
|
|
|
|
+import re.chasam.connector.Connector
|
|
|
|
|
+import re.chasam.connector.Database
|
|
|
|
|
+import re.chasam.models.impl.Player
|
|
|
|
|
|
|
|
|
|
|
|
|
-class Mongo {
|
|
|
|
|
- data class Database (
|
|
|
|
|
- val scheme : String,
|
|
|
|
|
- val host : String,
|
|
|
|
|
- val port : Int = 27017,
|
|
|
|
|
- val name : String,
|
|
|
|
|
- val collection : String,
|
|
|
|
|
- )
|
|
|
|
|
- val defaultDatabase = Database("mongodb","127.0.0.1", 27017, "tournament", "users")
|
|
|
|
|
|
|
+class Mongo : Connector {
|
|
|
|
|
+ override val defaultDatabase = Database("mongodb","127.0.0.1", 27017, "tournament", "users")
|
|
|
|
|
|
|
|
private var uri = "${defaultDatabase.scheme}://${defaultDatabase.host}:${defaultDatabase.port}"
|
|
private var uri = "${defaultDatabase.scheme}://${defaultDatabase.host}:${defaultDatabase.port}"
|
|
|
-
|
|
|
|
|
private val mongoClient: MongoClient = MongoClients.create(uri)
|
|
private val mongoClient: MongoClient = MongoClients.create(uri)
|
|
|
private val database: MongoDatabase = mongoClient.getDatabase(defaultDatabase.name)
|
|
private val database: MongoDatabase = mongoClient.getDatabase(defaultDatabase.name)
|
|
|
private val mCollection: MongoCollection<Document> = database.getCollection(defaultDatabase.collection)
|
|
private val mCollection: MongoCollection<Document> = database.getCollection(defaultDatabase.collection)
|
|
|
init {
|
|
init {
|
|
|
println("init tournament")
|
|
println("init tournament")
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
-// fun insert(name: String, score: Int = 0) {
|
|
|
|
|
-// try {
|
|
|
|
|
-// val result = collection.insertOne(
|
|
|
|
|
-// Document()
|
|
|
|
|
-// .append("name", name)
|
|
|
|
|
-// .append("score", score)
|
|
|
|
|
-// )
|
|
|
|
|
-// println("Success! Inserted document id: " + result.insertedId)
|
|
|
|
|
-// } catch (me: MongoException) {
|
|
|
|
|
-// System.err.println("Unable to insert due to an error: $me")
|
|
|
|
|
-// }
|
|
|
|
|
-// }
|
|
|
|
|
- fun insertOrUpdate(name: String, score: Int = 0) {
|
|
|
|
|
|
|
+ override fun insertOrUpdate(name: String, score: Int, rank : Int) {
|
|
|
val query = Document().append("name", name)
|
|
val query = Document().append("name", name)
|
|
|
val updates = Updates.combine(
|
|
val updates = Updates.combine(
|
|
|
Updates.set("score", score),
|
|
Updates.set("score", score),
|
|
@@ -56,31 +37,28 @@ class Mongo {
|
|
|
System.err.println("Unable to update due to an error: $me")
|
|
System.err.println("Unable to update due to an error: $me")
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
- fun dropAll() {
|
|
|
|
|
|
|
+ override fun dropAll() {
|
|
|
try {
|
|
try {
|
|
|
val result = mCollection.deleteMany(Document())
|
|
val result = mCollection.deleteMany(Document())
|
|
|
- println("deleted document count: " + result.deletedCount)
|
|
|
|
|
|
|
+ println("Dropped collection : $result")
|
|
|
} catch (me: MongoException) {
|
|
} catch (me: MongoException) {
|
|
|
System.err.println("Unable to drop due to an error: $me")
|
|
System.err.println("Unable to drop due to an error: $me")
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
- fun listAll(): List<Player> {
|
|
|
|
|
|
|
+ override fun listAll(): List<Player> {
|
|
|
try {
|
|
try {
|
|
|
|
|
|
|
|
val projectionFields = Projections.fields(
|
|
val projectionFields = Projections.fields(
|
|
|
- Projections.include("name", "score"),
|
|
|
|
|
|
|
+ Projections.include("name", "score", "rank"),
|
|
|
Projections.excludeId()
|
|
Projections.excludeId()
|
|
|
)
|
|
)
|
|
|
val doc = mCollection.find()
|
|
val doc = mCollection.find()
|
|
|
.projection(projectionFields)
|
|
.projection(projectionFields)
|
|
|
.sort(Sorts.descending("score"))
|
|
.sort(Sorts.descending("score"))
|
|
|
-
|
|
|
|
|
- println("listed document count: ${doc.toList()}")
|
|
|
|
|
val players = mutableListOf<Player>()
|
|
val players = mutableListOf<Player>()
|
|
|
doc.forEach { it: Document ->
|
|
doc.forEach { it: Document ->
|
|
|
- players.add(Player(it.getString("name"), it.getInteger("score")))
|
|
|
|
|
|
|
+ players.add(Player(it.getString("name"), it.getInteger("score"), it.getInteger("rank")))
|
|
|
}
|
|
}
|
|
|
- println("listed document count: $players")
|
|
|
|
|
return players
|
|
return players
|
|
|
} catch (me: MongoException) {
|
|
} catch (me: MongoException) {
|
|
|
System.err.println("Unable to drop due to an error: $me")
|
|
System.err.println("Unable to drop due to an error: $me")
|