Skip to content

Commit

Permalink
fix a bunch of bugs and add bandwidth inout
Browse files Browse the repository at this point in the history
  • Loading branch information
SpazElectro committed Aug 28, 2024
1 parent d2eba6e commit 77d5925
Showing 1 changed file with 60 additions and 18 deletions.
78 changes: 60 additions & 18 deletions levels/motorracer/STVmotorracer.j2as
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ uint type = 3;
struct readypkt {};
*/

// god please give me power to finish this project
// -spaz electro

#pragma require "STVmotorracer_split.png"
#pragma require "STVutil.asc"

Expand All @@ -47,6 +50,15 @@ int max(int a, int b) {
return a > b ? a : b;
}

int bandwidthInThisSecond = 0;
int bandwidthOutThisSecond = 0;
uint64 lastPKTtime = 0;

bool sendPacket(jjSTREAM &in packet, int toClientID = 0, uint toScriptModuleID = jjScriptModuleID) {
bandwidthOutThisSecond += packet.getSize();
return jjSendPacket(packet, toClientID, toScriptModuleID);
}

class MotorPlayer
{
AnimatedSprite@ sprite;
Expand Down Expand Up @@ -204,7 +216,12 @@ class MotorPlayer
stream.push(this.velocity.x);
stream.push(this.velocity.y);

return jjSendPacket(stream);
// if we're the server
// send to everyone except the person who moved
// TODO check if this actually works because the bandwidth in/out says otherwise
// maybe that's because our bandwidth in/out implementation doesn't check
// if we're excluded?
return sendPacket(stream, jjIsServer ? -this.id : 0);
}
#pragma endregion

Expand All @@ -220,15 +237,15 @@ void sendPlayerJoinPacket(string name, int8 id, int target = 0) {
stream.push(type);
stream.push(id);
stream.push(name);
jjSendPacket(stream, target);
sendPacket(stream, target);
}

void sendPlayerLeavePacket(int8 id) {
uint8 type = 2;
jjSTREAM stream;
stream.push(type);
stream.push(id);
jjSendPacket(stream);
sendPacket(stream);
}

MotorPlayer@ addPlayer(string name, int8 id) {
Expand Down Expand Up @@ -271,15 +288,25 @@ jjPLAYER@ getPlayerByClientID(int clientId) {
}

void removePlayer(int8 id) {
// jjConsole("I am a " + (jjIsServer ? "server" : "client") + ", deleting " + id);

if(jjIsServer)
sendPlayerLeavePacket(id);
motorPlayers.delete(getMotorPlayerById(id).name);

auto motorPlayer = getMotorPlayerById(id);
if(@motorPlayer != null) {
// jjConsole("Deleted " + id + "!");
motorPlayers.delete(motorPlayer.name);
} else {
jjConsole("removePlayer()::motorPlayer is null! I am a " + (jjIsServer ? "server" : "client") + " ID: " + id);
}
}

array<bool> activePlayersArray(32, false);
array<int8> activePlayersClientIDsArray(32, -1);

void onLevelBegin() {
jjConsole("Started!");
// jjConsole("Started!");

@leftKey = getKeyById("A");
@rightKey = getKeyById("D");
Expand All @@ -301,15 +328,18 @@ void onLevelBegin() {
uint8 id = 3;
jjSTREAM readyPacket;
readyPacket.push(id);
jjSendPacket(readyPacket);
sendPacket(readyPacket);
}

}

void onReceive(jjSTREAM &in packet, int clientID) {
uint8 type;
packet.pop(type);

bandwidthInThisSecond += packet.getSize();
if(jjIsServer)
jjConsole("Recv: " + type + " from " + clientID);

if(!jjIsServer) {
switch(type) {
// move
Expand Down Expand Up @@ -352,11 +382,13 @@ void onReceive(jjSTREAM &in packet, int clientID) {
// ready
case 3: {
// send all the other players that have joined
for(uint i = 0; i < motorPlayers.getKeys().length(); i++)
for(uint i = 0; i < motorPlayers.getKeys().length(); i++) {
sendPlayerJoinPacket(motorPlayers.getKeys()[i], cast<MotorPlayer@>(motorPlayers[motorPlayers.getKeys()[i]]).id, clientID);

cast<MotorPlayer@>(motorPlayers[motorPlayers.getKeys()[i]]).sendMovePacket();
}

auto player = getPlayerByClientID(clientID);
jjConsole(player.nameUnformatted + " is ready!");
// jjConsole(player.nameUnformatted + " is ready!");
addPlayer(player.nameUnformatted, clientID);

break;
Expand All @@ -373,13 +405,23 @@ void onMain() {
for(uint i = 0; i < motorPlayers.getKeys().length(); i++)
cast<MotorPlayer@>(motorPlayers[motorPlayers.getKeys()[i]]).update();

for(uint i = 0; i < 32; i++) {
if(activePlayersArray[i] && !jjPlayers[i].isActive) {
jjConsole(jjPlayers[i].nameUnformatted + " has left the game!");
// removePlayer(jjPlayers[i].clientID);
if(jjIsServer) {
for(uint i = 0; i < 32; i++) {
if(activePlayersArray[i] && !jjPlayers[i].isActive) {
// jjConsole(jjPlayers[i].nameUnformatted + " has left the game!");
// jjPlayers[i].clientID here is going to be -1 so we use this array
removePlayer(activePlayersClientIDsArray[i]);
}

activePlayersArray[i] = jjPlayers[i].isActive;
activePlayersClientIDsArray[i] = jjPlayers[i].clientID;
}
}

activePlayersArray[i] = jjPlayers[i].isActive;
if(jjUnixTimeSec() >= lastPKTtime+1) {
lastPKTtime = jjUnixTimeSec();
bandwidthInThisSecond = 0;
bandwidthOutThisSecond = 0;
}
}

Expand All @@ -390,12 +432,12 @@ bool onDrawAmmo(jjPLAYER@ player, jjCANVAS@ canvas) {
cast<MotorPlayer@>(motorPlayers[motorPlayers.getKeys()[i]]).draw(canvas);

if(@localMotorPlayer != null)
canvas.drawString(0, 16, "Velocity: "+localMotorPlayer.velocity.x+", "+localMotorPlayer.velocity.y);
canvas.drawString(0, 16, "||||Velocity: "+localMotorPlayer.velocity.x+", "+localMotorPlayer.velocity.y+" -| Bandwidth IN: " + bandwidthInThisSecond);

canvas.drawString(0, 32, "Player count: "+motorPlayers.getKeys().length());
canvas.drawString(0, 32, "|Player count: "+motorPlayers.getKeys().length()+" -| Bandwidth OUT: " + bandwidthOutThisSecond);
canvas.drawString(0, 48, "-- MotorPlayer ids in order --");
for(uint i = 0; i < motorPlayers.getKeys().length(); i++)
canvas.drawString(0, 56+(i*8), "ID: " + (cast<MotorPlayer@>(motorPlayers[motorPlayers.getKeys()[i]]).id));
canvas.drawString(0, 60+(i*12), "|||ID: " + (cast<MotorPlayer@>(motorPlayers[motorPlayers.getKeys()[i]]).id));

return false;
}

0 comments on commit 77d5925

Please sign in to comment.