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."; } }
public static void main(String[] args) { transactions = new ArrayList<Transaction>(); transfers = new ArrayList<Transfer>(); banks = new HashMap<String, Collection<Account>>(); Collection<Account> accountList = new ArrayList<Account>(); BanksService banksService = new BanksService(); Place place = new Place("boom"); Player player1 = new Player("test", "", "", place, 1); Player player2 = new Player("test2", "", "", place, 1); Account account1 = new Account(player1, 350); Account account2 = new Account(player2, 100); accountList.add(account1); accountList.add(account2); banks.put("test", accountList); System.out.println("test banks: " + gson.toJson(banks.get("test"))); try { Service service = new Service( "banks", "gruppe ingrid", "banks", "https://vs-docker.informatik.haw-hamburg.de/ports/11431/banks"); System.out.println("service wird registriert..."); String tmp = gson.toJson(service); Unirest.post("http://vs-docker.informatik.haw-hamburg.de:8053/services") .header("Content-Type", "application/json") .body(tmp) .asString(); } catch (UnirestException e) { System.out.println("Service konnte nicht registriert werden."); e.printStackTrace(); } get( "/banks", (request, response) -> { ArrayList<String> uris = new ArrayList<>(); for (String uri : banks.keySet()) { uris.add(localUri + uri); } String json = gson.toJson(uris); response.status(200); return json; }); post( "/banks", (request, response) -> { String gameid = UniqueId++ + ""; Collection<Account> gameacc = new ArrayList<Account>(); Game json = gson.fromJson(request.body(), Game.class); json.setGameid(gameid); for (Player player : json.getPlayers()) { Account acc = new Account(); acc.setPlayer(player); acc.setSaldo(0); acc.setUri(localUri + gameid + "/players/" + player.getId()); gameacc.add(acc); } banks.put(gameid, gameacc); response.status(200); response.header("location", localUri + gameid); return "ok"; }); get( "/banks/transfers", (request, response) -> { return gson.toJson(transfers); }); get( "/banks/transfers/:transferid", (request, response) -> { String uri = localUri + "transfers/" + request.params(":transferid"); for (Transfer trans : transfers) { if (trans.getUri().equals(uri)) { response.status(200); return gson.toJson(trans); } } response.status(404); return "Transfer not found."; }); put( "/banks/:gameid", (request, response) -> { Game json = gson.fromJson(request.body(), Game.class); String gameid = request.params(":gameid"); Collection<Account> gameacc = banks.getOrDefault(gameid, new ArrayList<Account>()); for (Player player : json.getPlayers()) { Account acc = new Account(); acc.setPlayer(player); acc.setSaldo(0); acc.setUri(localUri + gameid + "/players/" + player.getId()); gameacc.add(acc); } banks.put(gameid, gameacc); response.header("uri", localUri + gameid + "/players"); return ""; }); get( "/banks/:gameid", (request, response) -> { String gameid = request.params(":gameid"); String json = gson.toJson(banks.getOrDefault(gameid, new ArrayList<Account>())); return json; }); post( "/banks/:gameid/players", (request, response) -> { Type collectionType = new TypeToken<ArrayList<Account>>() {}.getType(); ArrayList<Account> json = gson.fromJson(request.body(), collectionType); Collection<Account> current = banks.getOrDefault(request.params(":gameid"), new ArrayList<Account>()); Set<Account> set = new HashSet<Account>(current); for (Account acc : json) { if (set.contains(acc)) { response.status(409); return "Player " + acc.getPlayer().getId() + " already exists."; } else { set.add(acc); current.add(acc); } } banks.put(request.params(":gameid"), (ArrayList<Account>) current); response.status(201); return "bank account has been created"; }); get( "/banks/:gameid/players/:playerid", (request, response) -> { 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(":playerid"))) { response.status(200); return account.getSaldo(); } } response.status(404); return "Player not found."; } else { response.status(404); return "Game not found."; } }); post( "/banks/:gameid/transfer/to/:to/:amount", (request, response) -> { return banksService.transferFromBankToPlayer(request, response); }); post( "/banks/:gameid/transfer/from/:from/:amount", (request, response) -> { return banksService.transferFromPlayerToBank(request, response); }); post( "/banks/:gameid/transfer/from/:from/to/:to/:amount", (request, response) -> { return banksService.transferFromPlayerToPlayer(request, response); }); post( "/banks/sync/:gameid", (request, response) -> { return banksService.updateAccounts(request, response); }); post( "/banks/sync/all", (request, response) -> { return banksService.replaceBanks(request, response); }); get( "/banks/sync/all", (request, response) -> { String tmp = gson.toJson(banksService.banks); Unirest.post(replication + "/sync/all") .header("Content-Type", "application/json") .body(tmp) .asString(); return "success"; }); post( "/banks/change/replication", (request, response) -> { replication = request.body(); return replication; }); get( "/stop/service", (request, response) -> { stop(); return ""; }); }
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."; } } }