Skip to content

Commit

Permalink
Server funktioniert, AI auch.
Browse files Browse the repository at this point in the history
Let's merge!
  • Loading branch information
Julian-Wollersberger committed Jun 12, 2018
1 parent 2d77daa commit 4c4b240
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 22 deletions.
9 changes: 6 additions & 3 deletions AI/src/at/dropical/wolliAI/AiMain.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

import at.dropical.wolliAI.bestPossibility.BestPossibilityAI;
import at.dropical.wolliAI.types.AI;
import at.dropical.wolliAI.types.AlwaysLeftAI;
import at.dropical.wolliAI.types.TryToLoseAI;

/**
* Starts the AI and connects to the default
Expand All @@ -12,16 +14,17 @@ public class AiMain {
public static void main(String[] args) throws InterruptedException {
AI ai = new BestPossibilityAI(new ServerAdapter());

//Temp
//AI ai2 = new BestPossibilityAI(new ServerAdapter());
//Temp to occupy full game
//AI ai2 = new TryToLoseAI(new ServerAdapter());

while(true) {
Thread.sleep(100);
try {
ai.process();
//ai2.process();

} catch(IndexOutOfBoundsException e) {
System.out.println(e.getMessage());
System.err.println(e.getMessage());
}
}
}
Expand Down
1 change: 0 additions & 1 deletion AI/src/at/dropical/wolliAI/ServerAdapter.kt
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,6 @@ class ServerAdapter(
}

fun sendInput(input: PlayerAction) {
//inputQueue.add(input)
server.writeToServer(HandleInputRequest(playerName, input))
}

Expand Down
21 changes: 21 additions & 0 deletions AI/src/at/dropical/wolliAI/types/TryToLoseAI.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package at.dropical.wolliAI.types;
// Created by julian on 12.06.18.

import at.dropical.shared.PlayerAction;
import at.dropical.wolliAI.ServerAdapter;

/**
* Just drops to lose as fast as possible.
*/
public class TryToLoseAI implements AI {

private ServerAdapter serverAdapter;

public TryToLoseAI(ServerAdapter serverAdapter) {
this.serverAdapter = serverAdapter;
}

public void process() {
serverAdapter.sendInput(PlayerAction.DROP);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public void writeRequest(SendableItem r) {
try {
outputStream.writeObject(r);
} catch(IOException e) {
e.printStackTrace();
System.out.println(e.getLocalizedMessage());
}
}

Expand Down
2 changes: 1 addition & 1 deletion server/src/at/dropical/server/GameManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ private Game autoJoinOrCreateGame(String playerName, Transmitter trans) {
}));
// No waiting game found.
if(game[0] == null) {
game[0] = new Game();
game[0] = new Game(name);
gamesMap.put(name, game[0]);
}

Expand Down
29 changes: 17 additions & 12 deletions server/src/at/dropical/server/game/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@
import at.dropical.shared.net.abstracts.Transmitter;
import at.dropical.shared.net.requests.HandleInputRequest;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.*;
import java.util.concurrent.locks.ReentrantLock;
import java.util.logging.Level;

Expand All @@ -30,11 +27,13 @@ public class Game extends Thread implements AutoCloseable {
private boolean updateClientsNextTime = false;

//Classic
public Game() {
public Game(String name) {
setName(name);
necessaryPlayers =2;
}
//Variable Players
public Game(int playercount) {
public Game(int playercount, String name) {
setName(name);
this.necessaryPlayers =playercount;
}

Expand Down Expand Up @@ -70,8 +69,10 @@ private void gameLoop() {

// Process inputs
for(ServerToClientAdapter player : players) {
for(HandleInputRequest inputRequest : player.getInputQueue()) {
handleInput(inputRequest);
HandleInputRequest request = player.pollInput();
while(request != null) {
handleInput(request);
request = player.pollInput();
}
}

Expand Down Expand Up @@ -146,26 +147,29 @@ public void updateClients() {

if(!playerLock.tryLock())
return;
for (ServerToClientAdapter player : players) {
for(Iterator<ServerToClientAdapter> iterator = players.iterator(); iterator.hasNext(); ) {
ServerToClientAdapter player = iterator.next();
if(player.stillConnected())
player.writeRequest(container);
else {
players.remove(player);
iterator.remove();
}

}
for (ServerToClientAdapter viewer : viewers) {
for(Iterator<ServerToClientAdapter> iterator = viewers.iterator(); iterator.hasNext(); ) {
ServerToClientAdapter viewer = iterator.next();
if(viewer.stillConnected())
viewer.writeRequest(container);
else
viewers.remove(viewer);
iterator.remove();
}
playerLock.unlock();
}

/** End all connections and terminate Threads. */
public void close() {
this.interrupt();
playerLock.lock();
try {
for(ServerToClientAdapter player : players) {
player.close();
Expand All @@ -174,6 +178,7 @@ public void close() {
viewer.close();
}
} catch(Exception ignored) { }
playerLock.unlock();
}

public Map<String, OnePlayer> getGames() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ private void recieveRequests() {

/** Returns null when no request is in the queue. */
@Override
public SendableItem readRequest() throws IOException {
public SendableItem readRequest() {
return requestQueue.poll();
}

Expand All @@ -68,9 +68,8 @@ public boolean stillConnected() {
return !transmitterDied;
}

/** So kann gleich ein foreach gemacht werden. */
public ConcurrentLinkedQueue<HandleInputRequest> getInputQueue() {
return inputQueue;
public HandleInputRequest pollInput() {
return inputQueue.poll();
}

@Override
Expand Down

0 comments on commit 4c4b240

Please sign in to comment.