mirror of
https://github.com/2OOP/pism.git
synced 2026-02-05 03:14:50 +00:00
Added pairs
This commit is contained in:
@@ -1,15 +1,14 @@
|
||||
package org.toop.framework.networking.server;
|
||||
|
||||
import org.toop.framework.game.players.ServerPlayer;
|
||||
|
||||
import java.util.Map;
|
||||
import org.toop.framework.utils.Pair;
|
||||
|
||||
public interface ServerUser {
|
||||
long id();
|
||||
String name();
|
||||
Game game();
|
||||
ServerPlayer serverPlayer();
|
||||
void addGame(Game game, ServerPlayer player);
|
||||
void addGame(Pair<Game, ServerPlayer> gamePair);
|
||||
void removeGame();
|
||||
void setName(String name);
|
||||
void sendMessage(String message);
|
||||
|
||||
@@ -2,14 +2,12 @@ package org.toop.framework.networking.server;
|
||||
|
||||
import io.netty.channel.ChannelHandlerContext;
|
||||
import org.toop.framework.game.players.ServerPlayer;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import org.toop.framework.utils.Pair;
|
||||
|
||||
public class User implements ServerUser {
|
||||
final private long id;
|
||||
private String name;
|
||||
private final Map<Game, ServerPlayer> game = new HashMap<>();
|
||||
private Pair<Game, ServerPlayer> gamePair;
|
||||
private ChannelHandlerContext connectionContext;
|
||||
|
||||
public User(long userId, String name) {
|
||||
@@ -28,24 +26,24 @@ public class User implements ServerUser {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addGame(Game game, ServerPlayer player) {
|
||||
if (this.game.isEmpty()) {
|
||||
this.game.put(game, player);
|
||||
public void addGame(Pair<Game, ServerPlayer> gamePair) {
|
||||
if (this.gamePair == null) {
|
||||
this.gamePair = gamePair;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void removeGame() {
|
||||
this.game.clear();
|
||||
this.gamePair = null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Game game() {
|
||||
return this.game.keySet().iterator().next();
|
||||
return this.gamePair.getLeft();
|
||||
}
|
||||
|
||||
public ServerPlayer serverPlayer() {
|
||||
return this.game.values().iterator().next();
|
||||
return this.gamePair.getRight();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
package org.toop.framework.utils;
|
||||
|
||||
public class ImmutablePair <T, K> implements Pair<T,K> {
|
||||
final T left;
|
||||
final K right;
|
||||
|
||||
public ImmutablePair(T left, K right) {
|
||||
this.left = left;
|
||||
this.right = right;
|
||||
}
|
||||
|
||||
@Override
|
||||
public T getLeft() {
|
||||
return left;
|
||||
}
|
||||
|
||||
@Override
|
||||
public K getRight() {
|
||||
return right;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
package org.toop.framework.utils;
|
||||
|
||||
public class MutablePair<T, K> implements Pair<T,K> {
|
||||
T left;
|
||||
K right;
|
||||
|
||||
public MutablePair(T left, K right) {
|
||||
this.left = left;
|
||||
this.right = right;
|
||||
}
|
||||
|
||||
@Override
|
||||
public T getLeft() {
|
||||
return left;
|
||||
}
|
||||
|
||||
public void setLeft(T left) {
|
||||
this.left = left;
|
||||
}
|
||||
|
||||
@Override
|
||||
public K getRight() {
|
||||
return right;
|
||||
}
|
||||
|
||||
public void setRight(K right) {
|
||||
this.right = right;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
package org.toop.framework.utils;
|
||||
|
||||
public interface Pair<T, K> {
|
||||
T getLeft();
|
||||
K getRight();
|
||||
}
|
||||
Reference in New Issue
Block a user