Package net.cloudcodex.server.data.Data

Examples of net.cloudcodex.server.data.Data.Scene


          tx.commit();

          /*
           * Init some scenes and messages.
           */
          Scene scene = messageService.startScene(context, "You're talking to the Guard chief", ulrich, inigo, guardChief);
          scene.setAlias(guardChief.getKey().getId() + "", "Global Alias for Guard Chief");
          scene.setAlias(ulrich.getKey().getId() + "", "Global Alias for Ulrich");
          scene.setAlias(ulrich.getKey().getId() + "-" + inigo.getKey().getId(), "My friend");
          dao.save(context, scene);
          messageService.postSpeech(context, null, ulrich, "Have you seen the mayor ?");
          messageService.postSpeech(context, null, ulrich, "Guy, I'm talking to you !");
          messageService.postSpeech(context, scene.getKey(), guardChief, "No, Not the last days. I'm worried.");
          messageService.postSpeech(context, null, inigo, "I'm sure he's gone with the town gold !");
          messageService.postSpeech(context, scene.getKey(), guardChief, "I'm going right now to check the bank !");
          scene = messageService.startScene(context, "Guard chief is gone, you're alone in the street", ulrich, inigo);
          messageService.postSpeech(context, null, inigo, "OK, wat a stupid guy ...");
          messageService.postSpeech(context, null, ulrich, "Yes, we can now deliver our friend from the jail.");
          messageService.postSpeech(context, null, inigo, "Let's go ! I'm going to search Hektor");
          scene = messageService.startScene(context, "Hektor, Inigo is coming to you at the inn", hektor, inigo);
          messageService.postSpeech(context, null, inigo, "You ! Come with me !");
          messageService.postSpeech(context, null, hektor, "Huh ?!? OK ...");
         
          // now ulrich scene
          messageService.postOFF(context, null, ulrich.getKey(), null, "Is it to late to go to the market ?");
          messageService.postOFF(context, null, null, ulrich.getKey(), "No, you can.");
          messageService.postOFF(context, null, ulrich.getKey(), null, "So I go to the market");
          messageService.postOFF(context, null, null, ulrich.getKey(), "When arriving to market, you see Olukun.");
          messageService.postOFF(context, null, ulrich.getKey(), null, "I go talk to him");
          scene = messageService.startScene(context, "Olukun, Ulrich is here.", ulrich, olukun);
          messageService.postSpeech(context, null, olukun, "Hey ! How do you do ?");
         
          // ...
          scene = messageService.startScene(context, "Everybody is here ...", ulrich, olukun, hektor, inigo);
          messageService.postSpeech(context, null, inigo, "OK Let's go to the jail !");
          messageService.postSpeech(context, null, olukun, "Huh ... wait a minute ...");
         
          {
            List<Scene> scenes = dao.asListOfScenes(null, dao.queryScene(campaign.getKey()), null);
            if(scenes != null) {
              for(Scene seq : scenes) {
                System.err.println(seq.getEntity());
                final List<Message> messages =
                  dao.asListOfMessages(null,
                    dao.queryMessage(seq.getKey()), null,
                    Message.Properties.TIMESTAMP);
                if(messages != null) {
                  for(Message msg : messages) {
                    System.err.println(msg.getEntity());
                  }
                }
              }
            }
          }
         
          {
            System.err.println("----------- Inigo messages");
            final Context context2 = new Context(romain);
            final List<SceneSDO> scenes = messageService.getMessages(context2,
                campaign.getKey().getId(),
                inigo.getKey().getId(), null, null);
           
            if(scenes != null) {
              for(SceneSDO sdo : scenes) {
                final Scene seq = sdo.getScene();
                System.err.println(seq.getEntity());
                final List<Message> messages = sdo.getMessages();
                if(messages != null) {
                  for(Message msg : messages) {
                    System.err.println(msg.getEntity());
                  }
                }
              }
            }
          }

          {
            System.err.println("----------- Ulrich messages");
            final Context context2 = new Context(xavier);
            final List<SceneSDO> scenes = messageService.getMessages(context2,
                campaign.getKey().getId(),
                ulrich.getKey().getId(), null, null);
           
            if(scenes != null) {
              for(SceneSDO sdo : scenes) {
                final Scene seq = sdo.getScene();
                System.err.println(seq.getEntity());
                final List<Message> messages = sdo.getMessages();
                if(messages != null) {
                  for(Message msg : messages) {
                    System.err.println(msg.getEntity());
                  }
                }
              }
            }
          }
         
          {
            System.err.println("----------- Olukun messages");
            final Context context2 = new Context(arnaud);
            final List<SceneSDO> scenes = messageService.getMessages(context2,
                campaign.getKey().getId(),
                olukun.getKey().getId(), null, null);
           
            if(scenes != null) {
              for(SceneSDO sdo : scenes) {
                final Scene seq = sdo.getScene();
                System.err.println(seq.getEntity());
                final List<Message> messages = sdo.getMessages();
                if(messages != null) {
                  for(Message msg : messages) {
                    System.err.println(msg.getEntity());
                  }
                }
              }
            }
          }

          {
            System.err.println("----------- Hektor messages");
            final Context context2 = new Context(daniel);
            final List<SceneSDO> scenes = messageService.getMessages(context2,
                campaign.getKey().getId(),
                hektor.getKey().getId(), null, null);
           
            if(scenes != null) {
              for(SceneSDO sdo : scenes) {
                final Scene seq = sdo.getScene();
                System.err.println(seq.getEntity());
                final List<Message> messages = sdo.getMessages();
                if(messages != null) {
                  for(Message msg : messages) {
                    System.err.println(msg.getEntity());
                  }
                }
              }
            }
          }

        } finally {
          if(tx.isActive()) {
            tx.rollback();
          }
        }
      }
      {
        final Transaction tx = datastore.beginTransaction();
        try {
          final Campaign campaign = campaignService.createCampaign(null, "another thomas' campaign", "Some game", "Some desc", null, thomas);
          campaign.setIcon("http://4.bp.blogspot.com/_SniTwfm5BwE/StS30qA7boI/AAAAAAAABpc/1kDwWjGglHI/s320/D%26D4E.jpg");
          createNotification(dao,  "this is another Notification", campaign.getKey());
          dao.save(null, campaign);
          tx.commit();
        } finally {
          if(tx.isActive()) {
            tx.rollback();
          }
        }
      }
     
      {
        final Transaction tx = datastore.beginTransaction();
        try {
          final Campaign campaign = campaignService.createCampaign(null, "Campaign 3", "Some game", "Some desc", null, daniel);
          campaign.setIcon("http://www.wizards.com/dnd/images/rhod_gallery/95863.jpg");
          createNotification(dao, "Please," + daniel.getNickname()+ " make OFF session to define your character", campaign.getKey());
         
          dao.save(null, campaign);
          tx.commit();
        } finally {
          if(tx.isActive()) {
            tx.rollback();
          }
        }
      }

     
      {
        final Transaction tx = datastore.beginTransaction();
        try {
          final Campaign campaign = campaignService.createCampaign(null, "Campaign 2", "Some game", "Some desc", null, daniel);
          campaign.setIcon("http://4.bp.blogspot.com/_SniTwfm5BwE/StS30qA7boI/AAAAAAAABpc/1kDwWjGglHI/s320/D%26D4E.jpg");
          createNotification(dao,  "this is another Notification", campaign.getKey());
         
          dao.save(null, campaign);
          tx.commit();
        } finally {
          if(tx.isActive()) {
            tx.rollback();
          }
        }
      }

     
      {
        final Transaction tx = datastore.beginTransaction();
        try {
          final Campaign campaign = campaignService.createCampaign(null, "Daniel's Campaign", "Some game", "Some desc", null, daniel);
          createNotification(dao,  "this is a Notification", campaign.getKey());
          final Data.Character joe = createCharacter(dao, campaign, "Joe", thomas);
          final Data.Character jack = createCharacter(dao, campaign, "Jack", arnaud);
          createNote(dao, jack, joe, "Why this guy is always following me ?!?");
          final Data.Character eric = createCharacter(dao, campaign, "Eric", null);
          tx.commit();
        } finally {
          if(tx.isActive()) {
            tx.rollback();
          }
        }
      }
    }

   
    /*
     * Tests for Home
     */
    {
      final Data.User user1 = createUser(dao, "user1@home.com", "User1");
      final Data.User user2 = createUser(dao, "user2@home.com", "User2");
      final Data.User user3 = createUser(dao, "user3@home.com", "User3");
     
      final Campaign campaign = campaignService.createCampaign(null, "My Home !", "Some game", "Some desc", null, user3);

      final Data.Character char1 = campaignService.createCharacter(null, campaign, "Char1", user1, null, null);
      final Data.Character char2 = campaignService.createCharacter(null, campaign, "Char2", user2, null, null);
      final Data.Character char3 = campaignService.createCharacter(null, campaign, "Char3", user3, null, null);
     
      Scene scene = messageService.startScene(context, "Intro", char1, char2, char3);
      messageService.postSpeech(context, null, char1, "Message 1");
      messageService.postSpeech(context, null, char2, "Message 2");
      messageService.postSpeech(context, null, char3, "Message 3");
    }
   
View Full Code Here


  ) {
    if(sdo == null) {
      return null;
    }
   
    final Scene scene = sdo.getScene();
    final List<Message> messages = sdo.getMessages();
    final List<Data.Character> characters = sdo.getCharacters();
   
    final SceneDTO dto = new SceneDTO();
    dto.setId(scene.getKey().getId());
    dto.setCharacters(mapCharacterHeader(characters, scene, viewCharacterKey));
    dto.setMessages(mapMessage(messages));
   
    // use introduction as message
    final String introduction = scene.getIntroduction();
    if(introduction != null) {
      if(dto.getMessages() == null) {
        dto.setMessages(new ArrayList<MessageDTO>());
      }
      final MessageDTO messageDTO = new MessageDTO();
      messageDTO.setId(null);
      messageDTO.setAction(null);
      messageDTO.setAuthor(null);
      messageDTO.setContent(introduction);
      messageDTO.setIndex(0);
      messageDTO.setType(null);
      dto.getMessages().add(0, messageDTO);
    }
    dto.setTimestamp(scene.getTimestamp());
   
    if(viewCharacterKey != null) {
      // for a character, only its own informations
      final String viewCharacterKeyString = viewCharacterKey.toString();
      final Key next = scene.getNext(viewCharacterKeyString);
      if (next != null) {
        dto.getNext().put(viewCharacterKey, next.getId());
      }
      final Key previous = scene.getPrevious(viewCharacterKeyString);
      if (previous != null) {
        dto.getPrevious().put(viewCharacterKey, previous.getId());
      }
    } else {
      // for GM : all informations
      for(Map.Entry<String, Key> entry : scene.getNext().entrySet()) {
        try {
          final Long id = Long.valueOf(entry.getKey());
          dto.getNext().put(id, entry.getValue().getId());
        } catch (NumberFormatException e) {
          // ...
        }
      }
      for(Map.Entry<String, Key> entry : scene.getPrevious().entrySet()) {
        try {
          final Long id = Long.valueOf(entry.getKey());
          dto.getPrevious().put(id, entry.getValue().getId());
        } catch (NumberFormatException e) {
          // ...
View Full Code Here

          final List<Data.Character> otherPCs = removeNPCs(others);

          if(otherPCs != null && !otherPCs.isEmpty()) {

            // ... create a new alternative scene for "the others" ...
            final Scene newseq = new Scene(campaign);
            newseq.setDate(new Date());
            newseq.setCharacters(dao.toKeysFromData(others)); // includes NPC

            // ... link it to the odlseq for each PC ...
            for(Data.Character otherPC : otherPCs) {
              final String index = String.valueOf(otherPC.getKey().getId());
              newseq.setPrevious(index, oldseq.getKey());
            }

            dao.save(context, newseq);
            logger.info(toStringKeys(others) + " go to " + newseq.getKey() + " alternative scene");
           
            // ... and link the oldseq to this new alternative scene
            // In 2 steps because newseq.key was not set before
            for(Data.Character otherPC : otherPCs) {
              final String index = String.valueOf(otherPC.getKey().getId());
              oldseq.setNext(index, newseq.getKey());
             
              // save the new alternative scene as current "other characters"'s scene
              otherPC.setScene(newseq);
              dao.save(context, otherPC);
            }
          } else {
            logger.info("there was no others playable characters");
          }
        }
      }

      // create the new scene with characters and link to old scenes.
      final Scene newseq = new Scene(campaign);
      newseq.setDate(new Date());
      newseq.setIntroduction(introduction);
      newseq.setCharacters(dao.toKeysFromData(characters)); // includes NPCs
      for(Data.Character character : characters) {
        if(character.getOwner() != null) { // !NPC
          final String index = String.valueOf(character.getKey().getId());
          newseq.setPrevious(index, character.getScene());
        }
      }
      dao.save(context, newseq);
     
      // create the forward link from old scenes to the new scene.
      if(oldseqs != null) {
        for(Scene oldseq : oldseqs) {
          final List<Data.Character> oldseqChars =
            charactersByScene.get(oldseq.getKey());
          if(oldseqChars != null) {
            for(Data.Character oldseqChar : oldseqChars) {
              // here we iterate only on PC associated to this old scene
              final String index = String.valueOf(oldseqChar.getKey().getId());
              oldseq.setNext(index, newseq.getKey());
            }
          }

          // useless but ... for the future ?
          oldseq.setClosed(true);
         
          dao.save(context, oldseq);
        }       
      }
     
      // save the new scene as current characters's scene
      for(Data.Character character : characters) {
        if(character.getOwner() != null) { // !NPC
          character.setScene(newseq);
          dao.save(context, character);
        }
      }
     
      tx.commit();

      logger.info("scene " + newseq.getKey() + " created for " + toStringKeys(characters));
      return newseq;
     
    } finally {
      if(tx.isActive()) {
        tx.rollback();
View Full Code Here

    // Create a list of PCs's current scenes
    final List<Scene> currentScenes = new ArrayList<Scene>();
    for(Data.Character pc : pcs.values()) {
      final Key sceneKey = pc.getScene();
      if(sceneKey != null) {
        final Scene currentScene = dao.readScene(context, sceneKey);
        if(currentScene == null) {
          logger.severe("invalid scene " + sceneKey);
          context.addError(Errors.NOT_FOUND_SCENE, sceneKey);
          return false;
        }
       
        lastScenes.put(pc.getKey(), currentScene);
       
        if(!currentScenes.contains(currentScene)) {
          currentScenes.add(currentScene);
         
          // check the scene doesn't contains another PC not listed
          if(currentScene.getCharacters() != null) {
            for(Key characterKey : currentScene.getCharacters()) {
              if(!allCharacters.containsKey(characterKey)) {
                final Data.Character character =
                  dao.readCharacter(context, characterKey);
                if(character == null) {
                  logger.severe("invalid character " + characterKey);
                } else {
                  if(character.getOwner() != null) {
                    logger.severe("try to create a scene but "
                        + characterKey + " not dispatched");
                    context.addError(Errors.IMPOSSIBLE_PC_NOT_DISPATCHED,
                        characterKey);
                    return false;
                  }
                }
              }
            }
          }
        }
      }
    }
   
    // At this points all PCs of impacted scenes are listed and used
    // only one time, we are sure ... so create the new scenes

    // Start a TX, all entities remains to Campaign 
    final Transaction tx = dao.getStore().beginTransaction();
    try {
      for(SceneToCreateSDO sceneToCreate : scenes) {
        final List<Key> charactersKeys = scenesCharactersKeys.get(sceneToCreate);
        final List<Data.Character> characters = scenesCharacters.get(sceneToCreate);
       
        final Scene scene = new Scene(campaign);
        scene.setDate(new Date());
        scene.setCharacters(charactersKeys);
        scene.setIntroduction(sceneToCreate.getIntroduction());

        final Map<Long, Map<Long, String>> allAliases = sceneToCreate.getAliases();
        for(Data.Character character : characters) {
          final Key characterKey = character.getKey();

          // set the aliases
          if(allAliases != null) {
            final Map<Long, String> charAliases = allAliases.get(characterKey.getId());
            if(charAliases != null) {
              for(Map.Entry<Long, String> entry : charAliases.entrySet()) {
                final Long charId = entry.getKey();
                final String alias = entry.getValue();
                if(charId == null) {
                  // global alias
                  scene.setAlias(String.valueOf(characterKey.getId()), alias);
                } else {
                  // specific alias
                  scene.setAlias(String.valueOf(characterKey.getId())
                      + "-" + String.valueOf(charId), alias);
                }
              }
            }
          }
        }

        // set the "previous" property of the new scene, for each PC
        for(Data.Character character : characters) {
          if(character.getOwner() != null) {
            final Key characterKey = character.getKey();
            final Scene lastScene = lastScenes.get(characterKey);
            if(lastScene != null) {
              scene.setPrevious(String.valueOf(characterKey.getId()), lastScene.getKey());
            }
          }
        }
       
        // after that we have a scene key
        dao.save(context, scene);

        // update the PCs and the last scene
        final List<Scene> updatedScenes = new ArrayList<Scene>();
        for(Data.Character character : characters) {
          if(character.getOwner() != null) {

            // set the PC current scene
            character.setScene(scene.getKey());
            dao.save(context, character);
           
            // set the last scene's "next" scene.
            final Key characterKey = character.getKey();
            final Scene lastScene = lastScenes.get(characterKey);
            if(lastScene != null) {
              lastScene.setNext(String.valueOf(characterKey.getId()), scene.getKey());
              if(!updatedScenes.contains(lastScene)) {
                updatedScenes.add(lastScene);
              }
            }
          }
View Full Code Here

    if(sceneKey == null) {
      return false;
    }

    final Scene scene = dao.readScene(context, sceneKey);
   
    if(scene == null) {
      return false;
    }
View Full Code Here

      context.addError(Errors.OUTOFDATE);
      return false;
    }

    // check the scene
    final Scene scene = dao.readScene(context, sceneKey);
    if(scene == null) {
      logger.severe("invalid scene " + sceneKey);
      context.addError(Errors.NOT_FOUND_SCENE, sceneKey);
      return false;
    }
   
    // check the scene is not closed
    if(Boolean.TRUE.equals(scene.getClosed())) {
      logger.severe("Scene " + sceneKey + " is closed");
      context.addError(Errors.IMPOSSIBLE_CLOSED, sceneKey);
      return false;
    }

    // check the scene is not paused
    if(Boolean.TRUE.equals(scene.getPaused())) {
      logger.severe("Scene " + sceneKey + " is paused");
      context.addError(Errors.IMPOSSIBLE_PAUSED, sceneKey);
      return false;
    }

    // Check the client is up-to-date
    if(scene.getTimestamp().after(clientSceneTimestamp)) {
      logger.severe("client is out-of-date");
      context.addError(Errors.OUTOFDATE);
      return false;
    }
   
View Full Code Here

      context.addError(Errors.OUTOFDATE);
      return false;
    }

    // check the scene
    final Scene scene = dao.readScene(context, sceneKey);
    if(scene == null) {
      logger.severe("invalid scene " + sceneKey);
      context.addError(Errors.NOT_FOUND_SCENE, sceneKey);
      return false;
    }
   
    // check the scene is not closed
    if(Boolean.TRUE.equals(scene.getClosed())) {
      logger.severe("Scene " + sceneKey + " is closed");
      context.addError(Errors.IMPOSSIBLE_CLOSED, sceneKey);
      return false;
    }

    // check the scene is not paused
    if(Boolean.TRUE.equals(scene.getPaused())) {
      logger.severe("Scene " + sceneKey + " is paused");
      context.addError(Errors.IMPOSSIBLE_PAUSED, sceneKey);
      return false;
    }

    // Check the client is up-to-date
    if(scene.getTimestamp().after(clientSceneTimestamp)) {
      logger.severe("client is out-of-date");
      context.addError(Errors.OUTOFDATE);
      return false;
    }
View Full Code Here

     
      // PC can only post to GM.
      toKey = null;
    }
   
    final Scene scene = dao.readScene(context, sceneKey);
   
    if(scene == null) {
      return false;
    }
View Full Code Here

         
          SceneSDO sceneSDO = mapScenes.get(sceneKey);
         
          if(sceneSDO == null) {
            // scene was not already loaded, so ...
            final Scene scene = dao.readScene(context, sceneKey);
            if(scene == null) {
              logger.severe("Invalid scene " + sceneKey);
              continue;
            }
            sceneSDO = readSceneSDO(context, scene, null, character);
            mapScenes.put(sceneKey, sceneSDO);
          }

          // add the message to the scene.
          if(sceneSDO.getMessages() == null) {
            sceneSDO.setMessages(new ArrayList<Message>());
          }
         
          sceneSDO.getMessages().add(message);
        }
      }
     
      scenes.addAll(mapScenes.values());
     
    } else {
      final String characterIndex = String.valueOf(characterId);
     
      final Key sceneKey;
      if(lastSceneId == null) {
        // use current scene as start
        sceneKey = character.getScene();
      } else {
        // get the "last scene"'s previous scene
        final Key lastSceneKey =  Scene.createKey(campaignKey, lastSceneId);
        final Scene lastScene = dao.readScene(context, lastSceneKey);
        if(lastScene == null) {
          logger.severe("Invalid scene " + lastSceneKey);
          context.addError(Errors.NOT_FOUND_SCENE, lastSceneKey);
          return null;
        }
        if(!lastScene.getNonNullCharacters().contains(characterKey)) {
          logger.severe("LastScene " + lastSceneKey
              + " is not associated with " + characterKey);
          context.addError(Errors.IMPOSSIBLE);
          return null;
        }
       
        // get the previous scene "for the current character"
        sceneKey = lastScene.getPrevious(characterIndex);
      }

      if(sceneKey == null) {
        logger.severe("cannot find a starting scene for " + characterKey);
        context.addError(Errors.IMPOSSIBLE);
        return null;
      }
     
      // Load the scene.
      Scene scene = dao.readScene(context, sceneKey);
      if(scene == null) {
        logger.severe("Invalid scene " + sceneKey);
        context.addError(Errors.NOT_FOUND_SCENE, sceneKey);
        return null;
      }
     
      // Iterate over the scenes to reach (if possible), 25 messages
      int count = 0;
      while(scene != null && count < PAGINATION_MESSAGES) {
       
        // select all the messages of the scene, always, and order them.
        final List<Message> messages = order(dao.asListOfMessages(
          context, dao.queryMessage(scene.getKey()), null), scene.getFirstMessage());
       
        // keep only visible messages
        keepOnlyVisibleMessages(messages, characterKey, false);

        // note : even scenes with 0 messages must be returned because off the intro !
        final SceneSDO sceneSDO = readSceneSDO(context, scene, messages, character);

        scenes.add(0, sceneSDO);
       
        if(messages != null) {
          count += messages.size();
        }
       
        if(count < PAGINATION_MESSAGES) {
          final Key previousSceneKey = scene.getPrevious(characterIndex);
          scene = previousSceneKey == null ? null : dao.readScene(context, previousSceneKey);
        }
      }
    }
   
View Full Code Here

      context.addError(Errors.OUTOFDATE);
      return false;
    }

    // check the scene
    final Scene scene = dao.readScene(context, sceneKey);
    if(scene == null) {
      logger.severe("invalid scene " + sceneKey);
      context.addError(Errors.NOT_FOUND_SCENE, sceneKey);
      return false;
    }
   
    // check the scene is not closed
    if(Boolean.TRUE.equals(scene.getClosed())) {
      logger.severe("Scene " + sceneKey + " is closed");
      context.addError(Errors.IMPOSSIBLE_CLOSED, sceneKey);
      return false;
    }

    // check the scene is not paused
    if(Boolean.TRUE.equals(scene.getPaused())) {
      logger.severe("Scene " + sceneKey + " is paused");
      context.addError(Errors.IMPOSSIBLE_PAUSED, sceneKey);
      return false;
    }

    // Check the client is up-to-date
    if(scene.getTimestamp().after(clientSceneTimestamp)) {
      logger.severe("client is out-of-date");
      context.addError(Errors.OUTOFDATE);
      return false;
    }
   
View Full Code Here

TOP

Related Classes of net.cloudcodex.server.data.Data.Scene

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.