Example #1
0
 @Override
 public JsonElement serialize(
     Transaction transaction, Type type, JsonSerializationContext jsonSerializationContext) {
   final JsonObject jo = new JsonObject();
   jo.addProperty("amount", transaction.getAmount());
   jo.addProperty("type", transaction.getType());
   if (transaction.getParent() != null) {
     jo.addProperty("parent", transaction.getParent().getId());
   }
   return jo;
 }
Example #2
0
  public synchronized String updateAccounts(Request request, Response response) {

    Transaction json = gson.fromJson(request.body(), Transaction.class);

    if (json.getTimestamp() > lamport.getTime()) {
      lamport.Update(json.getTimestamp());
      lamport.Add();
      Unirest.get(replication + "/sync/all");
      return "replacing database.";
    } else {
      lamport.Add();
      for (Transaction transaction : transactions) {
        if (transaction.getTransfer().equals(json.getTransfer())
            && transaction.getTimestamp() == json.getTimestamp()) {
          return "failure, transfer already exists.";
        }
      }
      if (banks.containsKey(request.params(":gameid"))) {
        Collection<Account> accounts = banks.get(request.params(":gameid"));
        for (Account account : accounts) {
          if (account.getPlayer().getId().equals(request.params(":from"))) {
            int am = Integer.parseInt(request.params(":amount"));
            if (account.getSaldo() >= am) {
              account.SubtractAmount(am);

              banks.put(request.params(":gameid"), accounts);
              String uri = localUri + "transfers/" + UniqueId++;
              Transfer trans =
                  new Transfer(
                      request.params(":from"), request.params(":to"), am, request.body(), "", uri);
              Event event =
                  new Event(
                      "bank transfer",
                      request.body(),
                      request.body(),
                      uri,
                      account.getPlayer().getId());
              transfers.add(trans);
              Transaction transaction = new Transaction(trans, lamport.getTime());
              transactions.add(transaction);
              BanksService.sendUpdate(trans, request.params(":gameid"));

              response.status(201);
              return gson.toJson(event);
            } else {
              response.status(403);
              return "Insufficent funds.";
            }
          }
        }
        response.status(404);
        return "Player not found.";
      }

      // from Bank to Player
      else if (json.getTransfer().getFrom().equals("bank")) {
        if (banks.containsKey(request.params(":gameid"))) {
          Collection<Account> accounts = banks.get(request.params(":gameid"));
          for (Account account : accounts) {
            if (account.getPlayer().getId().equals(request.params(":to"))) {
              int am = Integer.parseInt(request.params(":amount"));
              account.AddAmount(am);
              banks.put(request.params(":gameid"), accounts);

              String uri = localUri + "transfers/" + UniqueId++;
              Transfer trans =
                  new Transfer(
                      request.params(":from"), request.params(":to"), am, request.body(), "", uri);
              Event event =
                  new Event(
                      "bank transfer",
                      request.body(),
                      request.body(),
                      uri,
                      account.getPlayer().getId());
              transfers.add(trans);
              BanksService.sendUpdate(trans, request.params(":gameid"));

              response.status(201);
              return gson.toJson(event);
            }
          }
          response.status(404);
          return "Player not found.";
        }

        // from Player to Player
        else {
          Collection<Event> events = new ArrayList<Event>();
          boolean transactionSuccessful = false;
          Transfer trans = null;
          Collection<Account> accounts = banks.get(request.params(":gameid"));
          Collection<Account> copy = new ArrayList<>();
          copy.addAll(accounts);

          for (Account account : copy) {
            if (account.getPlayer().getId().equals(request.params(":from"))) {
              int am = Integer.parseInt(request.params(":amount"));
              if (account.getSaldo() >= am) {
                account.SubtractAmount(am);
                break;
              } else {
                response.status(403);
                return "Insufficent funds.";
              }
            }
          }

          for (Account account : copy) {
            if (account.getPlayer().getId().equals(request.params(":to"))) {
              int am = Integer.parseInt(request.params(":amount"));
              account.AddAmount(am);
              String uri = localUri + "transfers/" + UniqueId++;
              trans =
                  new Transfer(
                      request.params(":from"), request.params(":to"), am, request.body(), "", uri);
              Event event =
                  new Event(
                      "bank transfer",
                      request.body(),
                      request.body(),
                      uri,
                      account.getPlayer().getId());
              events.add(event);
              transactionSuccessful = true;
            }
          }

          if (transactionSuccessful) {
            // transfer was successful, time to "commit" the changes!
            banks.put(request.params(":gameid"), copy);
            transfers.add(trans);
            Transaction transaction = new Transaction(trans, lamport.getTime());
            transactions.add(transaction);
            BanksService.sendUpdate(trans, request.params(":gameid"));

            response.status(201);
            return gson.toJson(events);
          } else {
            // OH OH! Something went wrong! But we actually didn't change anything, so we're good.
            response.status(404);
            return "Player not found.";
          }
        }
      } else {
        return "Game not found.";
      }
    }
  }