Package net.yura.lobby.database

Examples of net.yura.lobby.database.User


       
        boolean resignFromAllGames = false;
       
        try {
            database.startTransaction();
            User user = database.getUser(username);
            if (user==null) {
                throw new IllegalStateException("can not find user "+username);
            }
            String registrationId = user.getAndroidId();
           
            if (registrationId!=null) {
                Sender sender = new Sender(myApiKey);
                Message.Builder message = new Message.Builder();
                for (Map.Entry<String, String> entry : data.entrySet()) {
                    message.addData(entry.getKey(), entry.getValue());
                }
                Result result = sender.send(message.build(), registrationId, 5);

                if (result.getMessageId() != null) {
                    String canonicalRegId = result.getCanonicalRegistrationId();
                    if (canonicalRegId != null) {
                        // same device has more than on registration ID: update database
                        user.setAndroidId(canonicalRegId);
                        database.saveUser(user);
                    }
                }
                else {
                    String error = result.getErrorCodeName();
                    if (error.equals(Constants.ERROR_NOT_REGISTERED)) {
                        // application has been removed from device - unregister database
                        user.setAndroidId(null);
                        database.saveUser(user);
                       
                        resignFromAllGames = true;
                    }
                }
View Full Code Here


   
    @Override
    public User getUser(String username) {
        Interaction in = interactions.get();
        if (username.toUpperCase().startsWith("GUEST")) {
            User user = in.em.find(User.class, Integer.parseInt(username.substring("GUEST".length())) );
            // user.getName() always returns the correct name, so we need to make sure it matches
            if (username.equalsIgnoreCase( user.getName() )) {
                return user;
            }
        }
        else {
            Query q1 = in.em.createQuery("SELECT u FROM User u WHERE u.name = ?1");
View Full Code Here

    }

    private void assertAmMod(LobbySession session) {
        try {
            lobby.database.startTransaction();
            User me = lobby.database.getUser(session.getUsername());
            if (me.getType() < Player.PLAYER_MODERATOR) {
                throw new SecurityException("what do you think you are doing!");
            }
        }
        finally {
            lobby.database.endTransaction();
View Full Code Here

            try {
                database.startTransaction();
                // just in case the client does not have one
                //if (uuid!=null) { uuid = UUID.randomUUID().toString(); }

                User client = database.getUUIDGuestName(uuid);
                if (client==null) {
                    client = new User();
                    client.setUid(uuid);
                    client.setType(Player.PLAYER_GUEST);
                    database.saveUser(client);
                }

                Map responce = new HashMap();
                responce.put("key", key );
                responce.put("username", client.getName() );
                responce.put("usertype", client.getType() );

                session.setUser( client.getName() );
                session.setClientUID( uuid );

                send( session,ProtoAccess.COMMAND_KEY,responce );

                if (welcomeMessage!=null) {
View Full Code Here

    }

    @Override
    public void setNick(String oldname, String username) {

        User user;

        try {
            database.startTransaction();

            // maybe this should not "fix" the errors but throw an exception!
            // then again we send the username back to the client, so is prob ok
            username = LobbyCom.checkUsername(username);

            if (oldname.equals(username) || (oldname.toLowerCase().startsWith("guest") && username.length()==0)) {
                // should never happen
                throw new IllegalArgumentException("new name same as old name: "+oldname);
            }
            if (username.toLowerCase().startsWith("guest")) {
                throw new IllegalArgumentException("can not start your nick with guest: "+username );
            }
            if (trimToLetters(username.toLowerCase()).endsWith("bot")) {
                throw new IllegalArgumentException("can not end your nick with bot: "+username );
            }
            if (username.toLowerCase().endsWith("resigned")) {
                throw new IllegalArgumentException("can not end your nick with resigned: "+username );
            }
            if (username.toLowerCase().equals("null")) {
                throw new IllegalArgumentException("you can not call yourself: "+username );
            }
            if (username.indexOf('\n') >= 0 || username.indexOf('\r') >= 0 || username.indexOf('\t') >= 0) {
                throw new IllegalArgumentException("you can not have a that symbol in your name: "+username );
            }
            if (username.length()!=0 && username.length() < 4) {
                throw new IllegalArgumentException("username must be at least 4 chars: "+username);
            }
            if (username.length()!=0 && username.length() > 45) { // 45 is length of field in JPADB
                throw new IllegalArgumentException("username is too long, must be less then 45 chars: "+username);
            }
            if (username.length()!=0 && database.getUser(username)!=null) {
                // error: an account is already registered with this name
                // error: or a guest has joined a game with this name
                // error: a guest is connected with this name
                throw new IllegalArgumentException("this user is already in the database: "+username);
            }

            user = database.getUser(oldname);
            if (user==null) {
                throw new IllegalArgumentException("user with name \""+oldname+"\" not found");
            }
            user.setName(username.length()==0?null:username);
            database.saveUser(user);
        }
        finally {
            database.endTransaction();
        }

        // everything went ok with renaming, now we need to send out 3 commands
        // 1) COMMAND_LOGIN_OK/COMMAND_LOGOUT_OK
        // 2) COMMAND_ADD_OR_UPDATE_GAME and COMMAND_RENAME_PLAYER
        // and we need to make sure it is in this order otherise the client game action buttons may get wrong option

        Map responce = new HashMap();
        responce.put("username", user.getName() ); // username sent server->client not encrypted
        responce.put("userType", user.getType() );

        Collection<LobbySession> sessions = server.getLobbySession(oldname);
        for (LobbySession session:sessions) {
            session.setUser(user.getName());

            // TODO what exactly is the difference between these 2 commands???
            if (username.length()==0) {
                send( session,ProtoAccess.COMMAND_LOGOUT_OK, responce );
            }
            else {
                send( session,ProtoAccess.COMMAND_LOGIN_OK, responce );
            }
        }

        // these are the games that the user is currently in or is watching
        Set<LobbySession> others = new HashSet();
        // this is the games that the user has joined
        for (GameRoom gm: user.getGames() ) {
            ServerGame serverGame = runningGames.get(gm.getId());
            if (serverGame!=null) {
                serverGame.midgameLogin( oldname, user.getName() );
                others.addAll( serverGame.getAllClients() );
            }
            gameChanged(gm);
        }
        for (LobbySession session:sessions) for (ServerGame gm: session.playingOrWatchingGame ) {
            // put together a list of everyone watching any of this users games
            others.addAll( gm.getAllClients() );
        }
        Map send = new HashMap();
        send.put("oldName", oldname );
        send.put("newName", user.getName() );
        send.put("userType", user.getType() );
        for (LobbySession other: others) {
            // we need to send this even to ourselves as we need to be renamed there too
            send(other, ProtoAccess.COMMAND_RENAME_PLAYER, send);
        }
    }
View Full Code Here

    public void resignFromAllGames(String username) {
        List<Integer> games = new ArrayList();

        try {
            database.startTransaction();
            User user = database.getUser(username);
            for (GameRoom game:user.getGames()) {
                games.add(game.getId());
            }
        }
        finally {
            database.endTransaction();
View Full Code Here

            send(name, ProtoAccess.COMMAND_CHAT_MESSAGE, map);
        }
    }

    private User getUser(LobbySession session) {
        User user = database.getUUIDGuestName( session.getClientUID() );
        if (user==null) {
            throw new IllegalArgumentException("no user for "+session.getClientUID()+" "+session.getUsername());
        }
        return user;
    }
View Full Code Here

        }

        try {
            database.startTransaction();
            game = getGame( id );
            User user = database.getUser(username);

            // as this game is already started and serverGame.playerJoined was successfull
            // we need to increase the number of available places in this game
            // to allow this new player to be added to it with game.joinGame
            if (serverGame!=null) {
View Full Code Here

            if (game.getNumOfPlayers() == game.getMaxPlayers() && serverGame==null) {
                throw new IllegalStateException("game has not been initialised yet");
            }

            User user = database.getUser(username);
            username = user.getName(); // as the case may have not been exactly correct when this method was called

            game.leaveGame( user );

            if (serverGame==null) {
                gameRemoved = destroyGameCheck(serverGame,gameId);
View Full Code Here

    public void registerGCM(LobbySession session,String key) {
        try {
            database.startTransaction();

            User user = getUser(session);
            user.setAndroidId( key );
            database.saveUser(user);
            send(session, ProtoLobby.COMMAND_ANDROID_REGISTER_DONE, null);
        }
        finally {
            database.endTransaction();
View Full Code Here

TOP

Related Classes of net.yura.lobby.database.User

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.