diff --git a/src/Paxos/Acceptor.java b/src/Paxos/Acceptor.java new file mode 100644 index 0000000000000000000000000000000000000000..a93718d923b37b6ee5f6363abc2a803144993021 --- /dev/null +++ b/src/Paxos/Acceptor.java @@ -0,0 +1,25 @@ +import java.util.HashMap; + +public class Acceptor { + ProposalID promisedProposalID = null; + Integer lastAcceptedValue = null; + + public ProposalID getpromisedProposalID(){ + return promisedProposalID; + } + + public Integer getLastAcceptedValue(){ + return lastAcceptedValue; + } + + public HashMap<ProposalID, Integer> receivePromise(ProposalID proposalID){ + if (promisedProposalID == null) { + + } + } + + public Boolean receiveAccept(ProposalID proposalID, Integer acceptValue){ + + } + +} \ No newline at end of file diff --git a/src/server/GamePlay.java b/src/server/GamePlay.java new file mode 100644 index 0000000000000000000000000000000000000000..8cc919a19e18ec85e1768cce329ac35da57c6420 --- /dev/null +++ b/src/server/GamePlay.java @@ -0,0 +1,127 @@ +import java.util.HashSet; +import java.util.Iterator; + +public class GamePlay { + Integer dayCount; + Boolean isDay; + Boolean isRunning; + Boolean isStarted; + + static HashSet<player> playerHashSet; + + public GamePlay() { + if (playerHashSet == null){ + playerHashSet = new HashSet<>(); + } + + if (isRunning == null) { + isRunning = false; + } + + if (isDay == null) { + isDay = true; + } + + if (isStarted == null){ + isStarted = false; + } + } + + public void changePhase() { + if (!isDay) { + dayCount++; + } + isDay = !isDay; + } + + + public static HashSet<Player> getPlayerHashSet() { + return playerHashSet; + } + + public Integer getPlayerId(String udpIpAddress) { + Iterator<Player> playerIterator = playerHashSet.iterator(); + Player currentPlayerInIterator; + + while (playerIterator.hasNext()) { + currentPlayerInIterator = playerIterator.next(); + if (currentPlayerInIterator.getUdpIpAddress().equals(udpIpAddress)){ + return currentPlayerInIterator.getPlayerId(); + } + } + + return -1; + } + + public Integer joinGame(String username, String udpIpAddress, Integer udpPortNumber) { + Player player = new Player(username, udpIpAddress, udpPortNumber); + playerHashSet.add(player); + return player.getPlayerId(); + } + + public Boolean readyGame(String udpIpAddress) { + Integer playerId = getPlayerIdFromUdpIpAddress(udpIpAddress); + if (playerId < 0) { + return false; + } + else { + Iterator<Player> playerIterator = playerHashSet.iterator(); + Player playerInIterator; + while (playerIterator.hasNext()) { + playerInIterator = playerIterator.next(); + if (playerInIterator.getPlayerId() == playerId) { + playerInIterator.readyGame(); + // CHECK IF GAME IS AVAILABLE TO BE STARTED + if (isReadyStart()) { + startGame(); + } + return true; + } + } + return false; + } + } + + public Boolean isReadyStart() { + // To check whether or not the game is available to start + // Checking procedure: Making sure all Players are CONNECTED AND THEN NOT LEAVING AND THEN READY. + Boolean found = false; + Iterator<Player> playerIterator = playerHashSet.iterator(); + Player currentPlayerInIterator; + while (!found && playerIterator.hasNext()) { + currentPlayerInIterator = playerIterator.next(); + if (currentPlayerInIterator.getIsConnected() && !currentPlayerInIterator.getIsLeft() && currentPlayerInIterator.getIsReady()) { + // PASS (doing nothing) and check next + } + else { + found = true; + } + } + + return (!found); + } + + public Boolean leaveGame(String udpIpAddress) { + Integer playerId = getPlayerIdFromUdpIpAddress(udpIpAddress); + if (playerId < 0) { + return false; + } + else { + Iterator<Player> playerIterator = playerHashSet.iterator(); + Player playerInIterator; + Boolean found = false; + while (!found && playerIterator.hasNext()) { + playerInIterator = playerIterator.next(); + if (playerInIterator.getPlayerId() == playerId) { + playerIterator.remove(); + found = true; + } + } + return found; + } + } + + public void startGame() { + isStarted = true; + } +} \ No newline at end of file diff --git a/src/server/SisterServer.java b/src/server/SisterServer.java index 9b6e96ddb554872a99101aa27638d8883151ea8a..c9e81b6da59d4cba74dec9991948be02eaed73a8 100644 --- a/src/server/SisterServer.java +++ b/src/server/SisterServer.java @@ -51,12 +51,6 @@ public class SisterServer { Client c = new Client(1,9999); c.receiveMessageFromClient("localhost", 8888); - - // creating consensus - public void consensus(){ - - } - } }