2021-06-04 05:47:54 +00:00
|
|
|
package uulm.teamname.marvelous.gamelibrary.gamelogic;
|
|
|
|
|
|
|
|
import org.junit.jupiter.api.BeforeAll;
|
|
|
|
import org.junit.jupiter.api.Test;
|
2021-06-05 02:19:58 +00:00
|
|
|
import uulm.teamname.marvelous.gamelibrary.ai.AIClient;
|
2021-06-04 05:47:54 +00:00
|
|
|
import uulm.teamname.marvelous.gamelibrary.entities.EntityType;
|
|
|
|
import uulm.teamname.marvelous.gamelibrary.events.Event;
|
|
|
|
import uulm.teamname.marvelous.gamelibrary.json.JSON;
|
|
|
|
import uulm.teamname.marvelous.gamelibrary.messages.*;
|
|
|
|
import uulm.teamname.marvelous.gamelibrary.messages.client.*;
|
|
|
|
import uulm.teamname.marvelous.gamelibrary.messages.server.*;
|
|
|
|
import uulm.teamname.marvelous.gamelibrary.requests.*;
|
|
|
|
|
|
|
|
import java.util.List;
|
|
|
|
import java.util.Optional;
|
|
|
|
import java.util.concurrent.*;
|
|
|
|
|
|
|
|
class AITest extends BaseGameLogicTest {
|
|
|
|
private static JSON json;
|
|
|
|
private static ExecutorService executor;
|
|
|
|
|
|
|
|
private static GameInstance server;
|
2021-06-05 02:19:58 +00:00
|
|
|
private static AIClient clientA;
|
|
|
|
private static AIClient clientB;
|
2021-06-04 05:47:54 +00:00
|
|
|
|
|
|
|
@BeforeAll
|
|
|
|
static void setUp() {
|
|
|
|
generate();
|
|
|
|
|
|
|
|
json = new JSON(characterConfig);
|
|
|
|
executor = new SimpleErrorSensitiveThreadPoolExecutor();
|
|
|
|
|
|
|
|
server = new GameInstance(partyConfig, characterConfig, scenarioConfig);
|
2021-06-05 02:19:58 +00:00
|
|
|
clientA = new AIClient(EntityType.P1, partyConfig, characterConfig, scenarioConfig);
|
|
|
|
clientB = new AIClient(EntityType.P2, partyConfig, characterConfig, scenarioConfig);
|
2021-06-04 05:47:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
void main() throws InterruptedException {
|
|
|
|
serverSend(server.startGame(player1Selection, player2Selection).toArray(new Event[0]));
|
|
|
|
|
2021-06-05 02:00:25 +00:00
|
|
|
Thread.sleep(10000);
|
2021-06-04 05:47:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void clientSend(Request[] requests) {
|
|
|
|
RequestMessage message = new RequestMessage();
|
|
|
|
message.messages = requests;
|
|
|
|
|
|
|
|
Optional<String> data = json.stringify(message);
|
|
|
|
|
|
|
|
if(data.isPresent()) {
|
|
|
|
executor.submit(() -> serverReceive(data.get()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void clientReceive(String data) {
|
|
|
|
Optional<BasicMessage> message = json.parse(data);
|
|
|
|
|
|
|
|
if(message.isPresent()) {
|
|
|
|
Optional<List<Request>> resultA = clientA.handle(((EventMessage)message.get()).messages);
|
|
|
|
Optional<List<Request>> resultB = clientB.handle(((EventMessage)message.get()).messages);
|
|
|
|
|
|
|
|
if(resultA.isPresent()) {
|
|
|
|
executor.submit(() -> clientSend(resultA.get().toArray(new Request[0])));
|
|
|
|
}
|
|
|
|
|
|
|
|
if(resultB.isPresent()) {
|
|
|
|
executor.submit(() -> clientSend(resultB.get().toArray(new Request[0])));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void serverSend(Event[] events) {
|
|
|
|
EventMessage message = new EventMessage();
|
|
|
|
message.messages = events;
|
|
|
|
|
|
|
|
Optional<String> data = json.stringify(message);
|
|
|
|
|
|
|
|
if(data.isPresent()) {
|
|
|
|
executor.submit(() -> clientReceive(data.get()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private void serverReceive(String data) {
|
|
|
|
Optional<BasicMessage> message = json.parse(data);
|
|
|
|
|
|
|
|
if(message.isPresent()) {
|
|
|
|
Optional<List<Event>> result = server.checkRequestsAndApply(((RequestMessage)message.get()).messages);
|
|
|
|
|
|
|
|
if(result.isPresent()) {
|
|
|
|
executor.submit(() -> serverSend(result.get().toArray(new Event[0])));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|