hanze/game-client

src/main/java/nl/isygameclient/network/Match.java in main
Repositories | Summary | Log | Files

Match.java (2632B) download


  1package nl.isygameclient.network;
  2
  3import java.io.IOException;
  4import java.util.Map;
  5
  6public class Match {
  7	private final GameClient client;
  8
  9	private GameType game;
 10	private Event	 outcome;
 11	private String	 opponent;
 12	private int[] moves;
 13	private int		pointsSelf;
 14	private int		pointsOther;
 15	private boolean yourTurn;
 16
 17	public Match(GameClient client) throws IOException {
 18		this.client = client;
 19	}
 20
 21	public GameType getGame() {
 22		return game;
 23	}
 24
 25	public boolean isYourTurn() {
 26		return yourTurn;
 27	}
 28
 29	public boolean update() throws IOException {
 30		if (outcome != null)
 31			return false;
 32
 33		Event event = client.event(EventType.MATCH, EventType.MOVE, EventType.YOURTURN, EventType.WIN, EventType.DRAW, EventType.LOSS);
 34		if (event == null)
 35			return true;
 36
 37		switch (event.type) {
 38			case MATCH:
 39				@SuppressWarnings("unchecked")
 40				var data = (Map<String, String>) event.data;
 41
 42				game	 = GameType.byName(data.get("gametype").toLowerCase());
 43				moves	 = new int[game.maxMoves];
 44				opponent = data.get("opponent");
 45				return true;
 46			case YOURTURN:
 47				yourTurn = true;
 48				return true;
 49			case MOVE:
 50				@SuppressWarnings("unchecked")
 51				var moveMap = (Map<String, String>) event.data;
 52
 53				moves[Integer.parseInt(moveMap.get("move"))] = moveMap.get("player").equals(client.getName()) ? 1 : -1;
 54				return true;
 55			case WIN:
 56				pointsSelf++;
 57				outcome = event;
 58				return false;
 59			case LOSS:
 60				pointsOther++;
 61				outcome = event;
 62				return false;
 63			default:	// loss
 64				outcome = event;
 65				return false;
 66		}
 67	}
 68
 69	public boolean isStarted() {
 70		return game != null;
 71	}
 72
 73	public int getMove(int move) {
 74		return moves[move];
 75	}
 76
 77	public void abort() throws IOException {
 78		if (outcome == null && !isStarted())
 79			client.send("forfeit");
 80	}
 81
 82	public void rematch() throws IOException {
 83		if (outcome == null && !isStarted())
 84			return;
 85
 86		Event evt;
 87		if ((evt = client.event(0.5, EventType.CHALLENGE)) != null) {
 88			@SuppressWarnings("unchecked")
 89			var id = ((Map<String, String>) evt.data).get("challengenumber");
 90			client.send("challenge", "accept", id);
 91		} else {
 92			client.send("challenge", opponent, game.name);
 93			client.event(-1, EventType.MATCH);
 94		}
 95
 96		for (int i = 0; i < moves.length; i++)
 97			moves[i] = 0;
 98		outcome = null;
 99	}
100
101	public Event getOutcome() {
102		return outcome;
103	}
104
105	public int getPointsSelf() {
106		return pointsSelf;
107	}
108
109	public int getPointsOther() {
110		return pointsOther;
111	}
112
113	public String getOpponent() {
114		return opponent;
115	}
116
117	public void move(int move) throws IOException {
118		if (outcome != null && !isStarted())
119			return;
120		client.send("move " + move);
121		yourTurn = false;
122	}
123}