fix a whole bunch of codefactor issues

This commit is contained in:
Holly Stubbs 2023-04-09 04:47:23 +01:00
parent fa27b476e7
commit 16c7b5ddcc
Signed by: tgpholly
GPG key ID: B8583C4B7D18119E
9 changed files with 61 additions and 60 deletions

View file

@ -6,7 +6,7 @@ enum LogType {
INFO, INFO,
WARN, WARN,
ERROR ERROR
}; }
const LogTags = { const LogTags = {
INFO: chalk.bgGreen(chalk.black(" INFO ")), INFO: chalk.bgGreen(chalk.black(" INFO ")),

View file

@ -3,4 +3,4 @@ import { readFileSync } from "fs";
import { MinecraftServer } from "./server/MinecraftServer"; import { MinecraftServer } from "./server/MinecraftServer";
const config:Config = JSON.parse(readFileSync("./config.json").toString()) as Config; const config:Config = JSON.parse(readFileSync("./config.json").toString()) as Config;
const mcServer = new MinecraftServer(config); new MinecraftServer(config);

View file

@ -1,5 +1,4 @@
import { FunkyArray } from "../funkyArray"; import { FunkyArray } from "../funkyArray";
import { Block } from "./blocks/Block";
import { Player } from "./entities/Player"; import { Player } from "./entities/Player";
import { World } from "./World"; import { World } from "./World";

View file

@ -37,7 +37,7 @@ export class MPClient {
} }
handlePacketPlayer(packet:PacketPlayer) { handlePacketPlayer(packet:PacketPlayer) {
// TODO this.entity.onGround = packet.onGround;
} }
handlePacketPlayerPosition(packet:PacketPlayerPosition) { handlePacketPlayerPosition(packet:PacketPlayerPosition) {

View file

@ -11,12 +11,8 @@ import { PacketKeepAlive } from "./packets/KeepAlive";
import { PacketLoginRequest } from "./packets/LoginRequest"; import { PacketLoginRequest } from "./packets/LoginRequest";
import { PacketDisconnectKick } from "./packets/DisconnectKick"; import { PacketDisconnectKick } from "./packets/DisconnectKick";
import { Player } from "./entities/Player"; import { Player } from "./entities/Player";
import { PacketTimeUpdate } from "./packets/TimeUpdate";
import { PacketSpawnPosition } from "./packets/SpawnPosition"; import { PacketSpawnPosition } from "./packets/SpawnPosition";
import { Chunk } from "./Chunk";
import { PacketMapChunk } from "./packets/MapChunk";
import { PacketPlayerPositionLook } from "./packets/PlayerPositionLook"; import { PacketPlayerPositionLook } from "./packets/PlayerPositionLook";
import { PacketPreChunk } from "./packets/PreChunk";
import { PacketChat } from "./packets/Chat"; import { PacketChat } from "./packets/Chat";
export class MinecraftServer { export class MinecraftServer {
@ -25,10 +21,9 @@ export class MinecraftServer {
private static readonly TICK_RATE_MS = 1000 / MinecraftServer.TICK_RATE; private static readonly TICK_RATE_MS = 1000 / MinecraftServer.TICK_RATE;
private readonly keepalivePacket = new PacketKeepAlive().writeData(); private readonly keepalivePacket = new PacketKeepAlive().writeData();
private totalClients:number = 0;
private config:Config; private config:Config;
private server:Server; private server:Server;
private serverClock:NodeJS.Timer; private readonly serverClock:NodeJS.Timer;
private tickCounter:number = 0; private tickCounter:number = 0;
private clients:FunkyArray<string, MPClient>; private clients:FunkyArray<string, MPClient>;
private worlds:FunkyArray<number, World>; private worlds:FunkyArray<number, World>;
@ -85,7 +80,7 @@ export class MinecraftServer {
} }
this.worlds.forEach(world => { this.worlds.forEach(world => {
world.tick(this.tickCounter); world.tick();
}); });
this.tickCounter++; this.tickCounter++;
}, MinecraftServer.TICK_RATE_MS); }, MinecraftServer.TICK_RATE_MS);
@ -101,33 +96,7 @@ export class MinecraftServer {
}); });
} }
onConnection(socket:Socket) { handleLoginRequest(reader:Reader, socket:Socket, setMPClient:(mpclient:MPClient) => void) {
let mpClient:MPClient;
const playerDisconnect = (err:Error) => {
mpClient.entity.world.removeEntity(mpClient.entity);
this.clients.remove(mpClient.entity.username);
this.sendToAllClients(new PacketChat(`\u00a7e${mpClient.entity.username} left the game`).writeData());
}
socket.on("close", playerDisconnect.bind(this));
socket.on("error", playerDisconnect.bind(this));
socket.on("data", chunk => {
const reader = new Reader(chunk);
// Let mpClient take over if it exists
if (mpClient instanceof MPClient) {
mpClient.handlePacket(reader);
return;
}
const packetId = reader.readUByte();
switch (packetId) {
// Handle timeouts at some point, idk.
case Packets.KeepAlive:
break;
case Packets.LoginRequest:
const loginPacket = new PacketLoginRequest().readData(reader); const loginPacket = new PacketLoginRequest().readData(reader);
if (loginPacket.protocolVersion !== MinecraftServer.PROTOCOL_VERSION) { if (loginPacket.protocolVersion !== MinecraftServer.PROTOCOL_VERSION) {
if (loginPacket.protocolVersion > MinecraftServer.PROTOCOL_VERSION) { if (loginPacket.protocolVersion > MinecraftServer.PROTOCOL_VERSION) {
@ -143,7 +112,8 @@ export class MinecraftServer {
const clientEntity = new Player(this, world, loginPacket.username); const clientEntity = new Player(this, world, loginPacket.username);
world.addEntity(clientEntity); world.addEntity(clientEntity);
const client = mpClient = new MPClient(socket, clientEntity); const client = new MPClient(socket, clientEntity);
setMPClient(client);
clientEntity.mpClient = client; clientEntity.mpClient = client;
this.clients.set(loginPacket.username, client); this.clients.set(loginPacket.username, client);
@ -156,12 +126,45 @@ export class MinecraftServer {
} else { } else {
socket.write(new PacketDisconnectKick("Failed to find world to put player in.").writeData()); socket.write(new PacketDisconnectKick("Failed to find world to put player in.").writeData());
} }
break; }
case Packets.Handshake: handleHandshake(reader:Reader, socket:Socket) {
const handshakePacket = new PacketHandshake().readData(reader); const handshakePacket = new PacketHandshake().readData(reader);
socket.write(handshakePacket.writeData()); socket.write(handshakePacket.writeData());
break; }
onConnection(socket:Socket) {
let mpClient:MPClient;
const setMPClient = (mpclient:MPClient) => {
mpClient = mpclient;
}
const playerDisconnect = (err:Error) => {
mpClient.entity.world.removeEntity(mpClient.entity);
this.clients.remove(mpClient.entity.username);
this.sendToAllClients(new PacketChat(`\u00a7e${mpClient.entity.username} left the game`).writeData());
if (typeof(err) !== "boolean") {
Console.printError(`Client disconnected with error: ${err.message}`);
}
}
socket.on("close", playerDisconnect.bind(this));
socket.on("error", playerDisconnect.bind(this));
socket.on("data", chunk => {
const reader = new Reader(chunk);
// Let mpClient take over if it exists
if (mpClient instanceof MPClient) {
mpClient.handlePacket(reader);
return;
}
const packetId = reader.readUByte();
switch (packetId) {
// TODO: Handle timeouts at some point, idk.
case Packets.KeepAlive: break;
case Packets.LoginRequest: this.handleLoginRequest(reader, socket, setMPClient.bind(this)); break;
case Packets.Handshake: this.handleHandshake(reader, socket); break;
} }
}); });
} }

View file

@ -1,9 +1,8 @@
import { Console } from "../console";
import { FunkyArray } from "../funkyArray"; import { FunkyArray } from "../funkyArray";
import { Chunk } from "./Chunk"; import { Chunk } from "./Chunk";
import { IEntity } from "./entities/IEntity"; import { IEntity } from "./entities/IEntity";
import { Player } from "./entities/Player"; import { Player } from "./entities/Player";
import { FlatGenerator } from "./generators/Flat"; //import { FlatGenerator } from "./generators/Flat";
import { HillyGenerator } from "./generators/Hilly"; import { HillyGenerator } from "./generators/Hilly";
import { IGenerator } from "./generators/IGenerator"; import { IGenerator } from "./generators/IGenerator";
@ -26,7 +25,7 @@ export class World {
// TODO: getChunkByCoordPair failed in here during removeEntity, figure out why. // TODO: getChunkByCoordPair failed in here during removeEntity, figure out why.
public removeEntity(entity:IEntity) { public removeEntity(entity:IEntity) {
if (entity instanceof Player) { if (entity instanceof Player) {
for (let coordPair of entity.loadedChunks) { for (const coordPair of entity.loadedChunks) {
const chunk = this.getChunkByCoordPair(coordPair); const chunk = this.getChunkByCoordPair(coordPair);
chunk.playersInChunk.remove(entity.entityId); chunk.playersInChunk.remove(entity.entityId);
@ -68,11 +67,11 @@ export class World {
this.chunks.remove(coordPair); this.chunks.remove(coordPair);
} }
public tick(tickCount:number) { public tick() {
this.entites.forEach(entity => { this.entites.forEach(entity => {
if (entity instanceof Player) { if (entity instanceof Player) {
if (entity.justUnloaded.length > 0) { if (entity.justUnloaded.length > 0) {
for (let coordPair of entity.justUnloaded) { for (const coordPair of entity.justUnloaded) {
const chunkToUnload = this.getChunkByCoordPair(coordPair); const chunkToUnload = this.getChunkByCoordPair(coordPair);
chunkToUnload.playersInChunk.remove(entity.entityId); chunkToUnload.playersInChunk.remove(entity.entityId);
if (chunkToUnload.playersInChunk.length === 0) { if (chunkToUnload.playersInChunk.length === 0) {

View file

@ -4,12 +4,14 @@ import { Entity } from "./Entity";
export class EntityLiving extends Entity { export class EntityLiving extends Entity {
public yaw:number; public yaw:number;
public pitch:number; public pitch:number;
public onGround:boolean;
public constructor(world:World) { public constructor(world:World) {
super(world); super(world);
this.yaw = 0; this.yaw = 0;
this.pitch = 0; this.pitch = 0;
this.onGround = false;
} }
onTick() { onTick() {

View file

@ -1,12 +1,9 @@
import { FunkyArray } from "../../funkyArray";
import { Chunk } from "../Chunk"; import { Chunk } from "../Chunk";
import { MPClient } from "../MPClient"; import { MPClient } from "../MPClient";
import { MinecraftServer } from "../MinecraftServer"; import { MinecraftServer } from "../MinecraftServer";
import { World } from "../World"; import { World } from "../World";
import { PacketMapChunk } from "../packets/MapChunk"; import { PacketMapChunk } from "../packets/MapChunk";
import { EntityLiving } from "./EntityLiving"; import { EntityLiving } from "./EntityLiving";
import { Entity } from "./Entity";
import { Socket } from "net";
import { PacketPreChunk } from "../packets/PreChunk"; import { PacketPreChunk } from "../packets/PreChunk";
export class Player extends EntityLiving { export class Player extends EntityLiving {
@ -64,7 +61,7 @@ export class Player extends EntityLiving {
} }
// Mark any unaccounted chunks for unload // Mark any unaccounted chunks for unload
for (let coordPair of this.loadedChunks) { for (const coordPair of this.loadedChunks) {
if (!currentLoads.includes(coordPair)) { if (!currentLoads.includes(coordPair)) {
this.justUnloaded.push(coordPair); this.justUnloaded.push(coordPair);
const chunkToUnload = this.world.getChunkByCoordPair(coordPair); const chunkToUnload = this.world.getChunkByCoordPair(coordPair);

View file

@ -35,7 +35,8 @@ export class HillyGenerator implements IGenerator {
// This is good enough (and fast enough) for what is needed here. // This is good enough (and fast enough) for what is needed here.
private mulberry32(a:number) { private mulberry32(a:number) {
return function() { return function() {
var t = a += 0x6D2B79F5; // TODO: Determine if "a" is needed
let t = a += 0x6D2B79F5;
t = Math.imul(t ^ t >>> 15, t | 1); t = Math.imul(t ^ t >>> 15, t | 1);
t ^= t + Math.imul(t ^ t >>> 7, t | 61); t ^= t + Math.imul(t ^ t >>> 7, t | 61);
return ((t ^ t >>> 14) >>> 0) / 4294967296; return ((t ^ t >>> 14) >>> 0) / 4294967296;