Skip to content

Commit

Permalink
Merge pull request #31 from youngmonkeys/version-1.1.2
Browse files Browse the repository at this point in the history
remove Readonly Collections
  • Loading branch information
tvd12 authored Dec 5, 2021
2 parents 035e96f + 2776e26 commit f170303
Show file tree
Hide file tree
Showing 57 changed files with 2,119 additions and 435 deletions.
9 changes: 4 additions & 5 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,17 @@
<parent>
<groupId>com.tvd12</groupId>
<artifactId>ezyfox-server</artifactId>
<version>1.0.1</version>
<version>1.0.2</version>
</parent>

<artifactId>game-box</artifactId>
<version>1.1.1</version>
<packaging>jar</packaging>
<version>1.1.2</version>

<name>game-box</name>
<url>https://youngmonkeys.org/</url>

<properties>
<ezy.server.version>1.2.0</ezy.server.version>
<ezy.server.version>1.2.1</ezy.server.version>
</properties>

<dependencies>
Expand Down Expand Up @@ -49,7 +48,7 @@
<configuration>
<encoding>UTF-8</encoding>
<consoleOutput>true</consoleOutput>
<violationSeverity>error</violationSeverity>
<violationSeverity>warn</violationSeverity>
<configLocation>checkstyle.xml</configLocation>
</configuration>
<executions>
Expand Down
1 change: 0 additions & 1 deletion src/main/java/com/tvd12/gamebox/constant/PlayerRole.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,4 @@ public String getName() {
public static PlayerRole valueOf(int id) {
return ROLE_BY_ID.getOrDefault(Integer.valueOf(id), NULL);
}

}
12 changes: 11 additions & 1 deletion src/main/java/com/tvd12/gamebox/constant/PlayerStatus.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package com.tvd12.gamebox.constant;

import java.util.Map;

import com.tvd12.ezyfox.util.EzyEnums;

import lombok.Getter;

public enum PlayerStatus implements IPlayerStatus {
Expand All @@ -11,6 +15,9 @@ public enum PlayerStatus implements IPlayerStatus {

@Getter
private int id;

private static final Map<Integer, PlayerStatus> STATUS_BY_ID =
EzyEnums.enumMapInt(PlayerStatus.class);

private PlayerStatus(int id) {
this.id = id;
Expand All @@ -20,5 +27,8 @@ private PlayerStatus(int id) {
public String getName() {
return toString();
}


public static PlayerStatus valueOf(int id) {
return STATUS_BY_ID.getOrDefault(Integer.valueOf(id), NULL);
}
}
8 changes: 6 additions & 2 deletions src/main/java/com/tvd12/gamebox/constant/RoomStatus.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.tvd12.gamebox.constant;

import java.util.Map;

import com.tvd12.ezyfox.util.EzyEnums;

import lombok.Getter;
Expand All @@ -14,6 +16,9 @@ public enum RoomStatus implements IRoomStatus {

@Getter
private int id;

private static final Map<Integer, RoomStatus> STATUS_BY_ID =
EzyEnums.enumMapInt(RoomStatus.class);

private RoomStatus(int id) {
this.id = id;
Expand All @@ -25,7 +30,6 @@ public String getName() {
}

public static RoomStatus valueOf(int id) {
return EzyEnums.valueOf(values(), id);
return STATUS_BY_ID.get(Integer.valueOf(id));
}

}
8 changes: 5 additions & 3 deletions src/main/java/com/tvd12/gamebox/entity/LocatedRoom.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,16 @@ public int addPlayer(LocatedPlayer player) {
throw new NoSlotException("has no available slot");
}
int location = slots.poll();
player.setLocation(location);
playerManager.addPlayer(player, location);
return location;
}

public void removePlayer(LocatedPlayer player) {
playerManager.removePlayer(player.getLocation());
slots.add(player.getLocation());
if (playerManager.containsPlayer(player.getName())) {
playerManager.removePlayer(player.getLocation());
slots.add(player.getLocation());
}

}

protected Queue<Integer> newSlots(int maxSlots) {
Expand Down
18 changes: 10 additions & 8 deletions src/main/java/com/tvd12/gamebox/entity/MMOPlayer.java
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
package com.tvd12.gamebox.entity;

import com.tvd12.gamebox.math.Vec3;
import com.tvd12.gamebox.util.ReadOnlySet;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.Setter;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicBoolean;

import com.tvd12.gamebox.math.Vec3;

import lombok.AccessLevel;
import lombok.Getter;
import lombok.Setter;

@Getter
@SuppressWarnings("AbbreviationAsWordInName")
public class MMOPlayer extends Player {

protected Vec3 position = new Vec3();
protected Vec3 rotation = new Vec3();
@Getter(AccessLevel.NONE)
protected AtomicBoolean stateChanged = new AtomicBoolean(false);
@Setter
protected int clientTimeTick;
Expand Down Expand Up @@ -75,8 +77,8 @@ public void getNearbyPlayerNames(List<String> buffer) {
buffer.addAll(nearbyPlayers.keySet());
}

public ReadOnlySet<String> getNearbyPlayerNames() {
return new ReadOnlySet<>(nearbyPlayers.keySet());
public List<String> getNearbyPlayerNames() {
return new ArrayList<String>(nearbyPlayers.keySet());
}

protected MMOPlayer(Builder builder) {
Expand Down
58 changes: 36 additions & 22 deletions src/main/java/com/tvd12/gamebox/entity/MMORoom.java
Original file line number Diff line number Diff line change
@@ -1,32 +1,37 @@
package com.tvd12.gamebox.entity;

import java.util.ArrayList;
import java.util.List;

import com.tvd12.gamebox.handler.MMORoomUpdatedHandler;
import com.tvd12.gamebox.manager.PlayerManager;
import com.tvd12.gamebox.manager.SynchronizedPlayerManager;
import com.tvd12.gamebox.util.ReadOnlyCollection;
import lombok.Getter;

import java.util.ArrayList;
import java.util.List;
import lombok.Getter;
import lombok.Setter;

@SuppressWarnings("AbbreviationAsWordInName")
public class MMORoom extends NormalRoom {

protected final List<MMORoomUpdatedHandler> roomUpdatedHandlers;
@Getter
protected final double distanceOfInterest;
@Getter
@Setter
protected MMOPlayer master;

@Getter
protected int maxPlayer;

protected final double distanceOfInterest;

protected final List<MMOPlayer> playerBuffer;

protected final List<MMORoomUpdatedHandler> roomUpdatedHandlers;

public MMORoom(Builder builder) {
super(builder);
this.playerBuffer = new ArrayList<>();
this.roomUpdatedHandlers = builder.roomUpdatedHandlers;
this.distanceOfInterest = builder.distanceOfInterest;
this.maxPlayer = builder.maxPlayer;
}

@SuppressWarnings("unchecked")
@Override
public void addPlayer(Player player) {
if (!(player instanceof MMOPlayer)) {
Expand All @@ -53,21 +58,28 @@ public void removePlayer(Player player) {

synchronized (this) {
super.removePlayer(player);
if (master == player && !playerManager.isEmpty()) {
master = (MMOPlayer) playerManager.getPlayerCollection().getFirst();
if (master == player) {
master = (playerManager.isEmpty())
? null
: (MMOPlayer) playerManager.getFirstPlayer();
}
}
}

public boolean isEmpty() {
return this.getPlayerManager().isEmpty();
}

public int getMaxPlayer() {
return this.getPlayerManager().getMaxPlayer();
}

@SuppressWarnings("unchecked")
public void update() {
ReadOnlyCollection<MMOPlayer> playerCollection = playerManager.getPlayerCollection();
playerCollection.forEach(player -> {
List<MMOPlayer> playerList = playerManager.getPlayerList();
playerList.forEach(player -> {
player.clearNearByPlayers();
playerCollection.forEach(other -> {
playerList.forEach(other -> {
double distance = player.getPosition().distance(other.getPosition());
if (distance <= this.distanceOfInterest) {
player.addNearbyPlayer(other);
Expand All @@ -80,9 +92,12 @@ public void update() {
notifyUpdatedHandlers();
}

@SuppressWarnings("unchecked")
private void notifyUpdatedHandlers() {
playerBuffer.clear();
playerManager.getPlayerList(playerBuffer);
for (MMORoomUpdatedHandler handler : this.roomUpdatedHandlers) {
handler.onRoomUpdated(this);
handler.onRoomUpdated(this, playerBuffer);
}
}

Expand All @@ -91,12 +106,10 @@ public static Builder builder() {
}

public static class Builder extends NormalRoom.Builder<Builder> {
protected List<MMORoomUpdatedHandler> roomUpdatedHandlers = new ArrayList<>();
protected double distanceOfInterest;

protected int maxPlayer = 999;

public Builder() {
}
protected double distanceOfInterest;
protected List<MMORoomUpdatedHandler> roomUpdatedHandlers = new ArrayList<>();

public Builder addRoomUpdatedHandler(MMORoomUpdatedHandler handler) {
this.roomUpdatedHandlers.add(handler);
Expand All @@ -119,6 +132,7 @@ public Builder defaultPlayerManager(int maxPlayer) {
return this;
}

@SuppressWarnings("rawtypes")
@Override
public Builder playerManager(PlayerManager playerManager) {
if (playerManager instanceof SynchronizedPlayerManager) {
Expand Down
15 changes: 10 additions & 5 deletions src/main/java/com/tvd12/gamebox/entity/MMORoomGroup.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package com.tvd12.gamebox.entity;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

import com.tvd12.ezyfox.builder.EzyBuilder;
import com.tvd12.ezyfox.util.EzyLoggable;
import com.tvd12.gamebox.manager.RoomManager;
import com.tvd12.gamebox.manager.SynchronizedRoomManager;

import java.util.concurrent.atomic.AtomicInteger;

@SuppressWarnings("AbbreviationAsWordInName")
class MMORoomGroup extends EzyLoggable {

Expand All @@ -29,10 +31,11 @@ private void start() {

private void loop() {
this.active = true;
List<MMORoom> roomBuffer = new ArrayList<>();
while (active) {
try {
long start = System.currentTimeMillis();
this.updateRooms();
this.updateRooms(roomBuffer);
long end = System.currentTimeMillis();
long timeElapsed = end - start;
if (timeElapsed < timeTickMillis) {
Expand All @@ -44,8 +47,10 @@ private void loop() {
}
}

private void updateRooms() {
this.roomManager.getRoomList().forEach(room -> {
private void updateRooms(List<MMORoom> roomBuffer) {
roomBuffer.clear();
roomManager.getRoomList(roomBuffer);
roomBuffer.forEach(room -> {
try {
room.update();
} catch (Exception e) {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/tvd12/gamebox/entity/NormalRoom.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import lombok.Setter;

@Getter
@SuppressWarnings({"unchecked"})
@SuppressWarnings({"unchecked", "rawtypes"})
public class NormalRoom extends Room {

@Setter(AccessLevel.NONE)
Expand All @@ -19,6 +19,7 @@ public NormalRoom(Builder<?> builder) {
}

public void addPlayer(Player player) {
player.setCurrentRoomId(id);
playerManager.addPlayer(player);
}

Expand All @@ -30,7 +31,6 @@ public <T extends PlayerManager> T getPlayerManager() {
return (T) playerManager;
}

@SuppressWarnings("rawtypes")
public static Builder builder() {
return new Builder<>();
}
Expand Down
16 changes: 8 additions & 8 deletions src/main/java/com/tvd12/gamebox/entity/Player.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.tvd12.gamebox.entity;

import com.tvd12.ezyfox.builder.EzyBuilder;
import com.tvd12.ezyfox.util.EzyEquals;
import com.tvd12.ezyfox.util.EzyHashCodes;
import com.tvd12.gamebox.constant.IPlayerRole;
import com.tvd12.gamebox.constant.IPlayerStatus;
import com.tvd12.gamebox.constant.PlayerRole;
Expand Down Expand Up @@ -32,16 +30,18 @@ protected Player(Builder<?> builder) {

@Override
public boolean equals(Object obj) {
return new EzyEquals<Player>()
.function(t -> t.name)
.isEquals(this, obj);
if (obj == null) {
return false;
}
if (this == obj) {
return true;
}
return name.equals(((Player) obj).name);
}

@Override
public int hashCode() {
return new EzyHashCodes()
.append(name)
.toHashCode();
return name.hashCode();
}

@Override
Expand Down
Loading

0 comments on commit f170303

Please sign in to comment.