示例#1
0
  public synchronized String transferFromBankToPlayer(Request request, Response response) {
    lamport.Add();
    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.";
    } else {
      response.status(404);
      return "Game not found.";
    }
  }
示例#2
0
 public static synchronized void sendUpdate(Transfer trans, String gameid) {
   lamport.Add();
   try {
     Transaction transaction = new Transaction(trans, lamport.getTime());
     transfers.add(trans);
     transactions.add(transaction);
     String tmp = gson.toJson(trans);
     Unirest.post(replication + "/sync/" + gameid)
         .header("Content-Type", "application/json")
         .body(tmp)
         .asString();
   } catch (UnirestException e) {
     System.out.println("Konnte Dienste nicht synchronisieren.");
     e.printStackTrace();
   }
 }
示例#3
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.";
      }
    }
  }