All Downloads are FREE. Search and download functionalities are using the official Maven repository.

io.minecloud.models.server.ServerRepository Maven / Gradle / Ivy

The newest version!
/*
 * Copyright (c) 2015, Mazen Kotb 
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */
package io.minecloud.models.server;

import com.mongodb.BasicDBObject;
import io.minecloud.db.mongo.AbstractMongoRepository;
import io.minecloud.models.network.Network;
import io.minecloud.models.player.PlayerData;
import io.minecloud.models.server.type.ServerType;
import org.mongodb.morphia.Datastore;

import java.util.Collections;
import java.util.List;
import java.util.UUID;

public class ServerRepository extends AbstractMongoRepository {
    private ServerRepository(Datastore datastore) {
        super(Server.class, datastore);
    }

    public static ServerRepository create(Datastore datastore) {
        return new ServerRepository(datastore);
    }

    public int nextNumberFor(ServerType type) {
        List servers = find(createQuery().filter("type", type)).asList();
        int lastNumber = 0;

        Collections.sort(servers, (s1, s2) -> s1.number() - s2.number());

        for (Server server : servers) {
            if (lastNumber != (server.number() - 1))
                return server.number() - 1;

            lastNumber = server.number();
        }

        return lastNumber + 1;
    }

    public Server serverBy(ServerType type, int number) {
        return find(createQuery()
                .field("type").equal(type)
                .field("number").equal(number))
                .get();
    }

    public List serverBy(ServerType type) {
        return find(createQuery().field("type").equal(type))
                .asList();
    }

    public List serversFor(Network network) {
        return find(createQuery().field("network").equal(network))
                .asList();
    }

    public Server serverFor(UUID id) {
        return find(createQuery().field("onlinePlayers").hasThisElement(new BasicDBObject("id", id.toString())))
                .get();
    }

    public Server serverFor(String name) {
        return find(createQuery().field("onlinePlayers").hasThisElement(new BasicDBObject("name", name)))
                .get();
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy