Skip to content

Commit

Permalink
Tanks v1.4.1b - nfixed spawning network glitch
Browse files Browse the repository at this point in the history
  • Loading branch information
aehmttw committed Sep 5, 2022
1 parent a97b60f commit 300c1b4
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/main/java/tanks/Game.java
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public enum Framework {lwjgl, libgdx}
public static double[][] tilesDepth = new double[28][18];

//Remember to change the version in android's build.gradle and ios's robovm.properties
public static final String version = "Tanks v1.4.1a";
public static final String version = "Tanks v1.4.1b";
public static final int network_protocol = 46;
public static boolean debug = false;
public static boolean traceAllRays = false;
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/tanks/Panel.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import tanks.hotbar.Hotbar;
import tanks.network.Client;
import tanks.network.ClientHandler;
import tanks.network.MessageReader;
import tanks.obstacle.Obstacle;
import tanks.tank.*;

Expand Down Expand Up @@ -866,6 +867,13 @@ public void drawBar(double offset)
Drawing.drawing.setColor(col[0], col[1], col[2]);
Game.game.window.fontRenderer.drawString(boundary + 150, offset + (int) (Panel.windowHeight - 40 + 6), 0.4, 0.4, "Latency: " + ClientHandler.lastLatencyAverage + "ms");
}

if (ScreenPartyLobby.isClient || ScreenPartyHost.isServer)
{
Drawing.drawing.setColor(255, 227, 186);
Game.game.window.fontRenderer.drawString(boundary + 400, offset + (int) (Panel.windowHeight - 40 + 6), 0.4, 0.4, "Upstream: " + MessageReader.upstreamBytesPerSec / 1024 + "KB/s");
Game.game.window.fontRenderer.drawString(boundary + 400, offset + (int) (Panel.windowHeight - 40 + 22), 0.4, 0.4, "Downstream: " + MessageReader.downstreamBytesPerSec / 1024 + "KB/s");
}
}

public double[] getLatencyColor(long l)
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/tanks/bullet/Bullet.java
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,7 @@ public Ray getRay()
@Override
public void update()
{
if (!this.isRemote && (this.vX != this.lastVX || this.lastVY != this.lastVY))
if (!this.isRemote && (this.vX != this.lastVX || this.vY != this.lastVY))
Game.eventsOut.add(new EventBulletUpdate(this));

super.update();
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/tanks/gui/screen/ScreenChangelog.java
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,13 @@ public static void setupLogs()
"*What's new in Tanks v1.4.1a:\n\n" +
"Fixed a memory leak with custom tank music\n"
});

new Changelog("v1.4.1b", new String[]
{
"*What's new in Tanks v1.4.1b:\n\n" +
"Added bandwidth usage to info bar\n" +
"Fixed a bug with spawning tanks in multiplayer\n"
});
}
}
}
2 changes: 2 additions & 0 deletions src/main/java/tanks/network/ClientHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ public synchronized void sendEvent(INetworkEvent e, boolean flush)

ByteBuf b2 = ctx.channel().alloc().buffer();
b2.writeInt(b.readableBytes());
MessageReader.upstreamBytes += b.readableBytes() + 4;
MessageReader.updateLastMessageTime();
b2.writeBytes(b);

if (flush)
Expand Down
23 changes: 23 additions & 0 deletions src/main/java/tanks/network/MessageReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ public class MessageReader
{
public static final int max_event_size = 1048576;

public static int downstreamBytes;
public static int upstreamBytes;
public static long lastMessageTime;

public static int upstreamBytesPerSec;
public static int downstreamBytesPerSec;

public boolean useQueue = true;
public ByteBuf queue;
protected boolean reading = false;
Expand Down Expand Up @@ -42,6 +49,8 @@ public synchronized boolean queueMessage(ServerHandler s, ByteBuf m, UUID client
if (!reading)
{
endpoint = queue.readInt();
downstreamBytes += endpoint + 4;
updateLastMessageTime();

if (endpoint > max_event_size)
{
Expand Down Expand Up @@ -167,4 +176,18 @@ else if (e instanceof IServerThreadEvent)

return false;
}

public static void updateLastMessageTime()
{
long time = System.currentTimeMillis() / 1000;

if (lastMessageTime < time)
{
lastMessageTime = time;
upstreamBytesPerSec = upstreamBytes;
downstreamBytesPerSec = downstreamBytes;
upstreamBytes = 0;
downstreamBytes = 0;
}
}
}
10 changes: 10 additions & 0 deletions src/main/java/tanks/network/ServerHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import tanks.gui.ChatMessage;
import tanks.gui.screen.ScreenPartyHost;

import java.util.HashMap;
import java.util.UUID;

public class ServerHandler extends ChannelInboundHandlerAdapter
Expand Down Expand Up @@ -82,6 +83,8 @@ public void channelInactive(ChannelHandlerContext ctx)
ScreenPartyHost.chat.add(0, new ChatMessage("\u00A7000127255255" + this.username + " has left the party\u00A7000000000255"));
}
}

System.out.println(eventFrequencies);
}

@Override
Expand Down Expand Up @@ -144,8 +147,13 @@ public synchronized void sendEvent(INetworkEvent e)
this.sendEvent(e, true);
}

public HashMap<String, Integer> eventFrequencies = new HashMap<>();

public synchronized void sendEvent(INetworkEvent e, boolean flush)
{
eventFrequencies.putIfAbsent(e.getClass().getSimpleName(), 0);
eventFrequencies.put(e.getClass().getSimpleName(), eventFrequencies.get(e.getClass().getSimpleName()) + 1);

if (steamID != null)
{
SteamNetworking.P2PSend sendType = SteamNetworking.P2PSend.ReliableWithBuffering;
Expand All @@ -168,6 +176,8 @@ public synchronized void sendEvent(INetworkEvent e, boolean flush)

ByteBuf b2 = ctx.channel().alloc().buffer();
b2.writeInt(b.readableBytes());
MessageReader.upstreamBytes += b.readableBytes() + 4;
MessageReader.updateLastMessageTime();
b2.writeBytes(b);

if (flush)
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/tanks/tank/TankAIControlled.java
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,12 @@ public SpawnedTankEntry(TankAIControlled t, double weight)
{
this.tank = t;
this.weight = weight;

if (ScreenPartyHost.isServer)
{
Tank.freeIDs.add(t.networkID);
Tank.idMap.remove(t.networkID);
}
}

public String toString()
Expand Down

0 comments on commit 300c1b4

Please sign in to comment.