fix a whole bunch of codefactor issues
This commit is contained in:
parent
fa27b476e7
commit
16c7b5ddcc
9 changed files with 61 additions and 60 deletions
|
@ -6,7 +6,7 @@ enum LogType {
|
|||
INFO,
|
||||
WARN,
|
||||
ERROR
|
||||
};
|
||||
}
|
||||
|
||||
const LogTags = {
|
||||
INFO: chalk.bgGreen(chalk.black(" INFO ")),
|
||||
|
|
2
index.ts
2
index.ts
|
@ -3,4 +3,4 @@ import { readFileSync } from "fs";
|
|||
import { MinecraftServer } from "./server/MinecraftServer";
|
||||
const config:Config = JSON.parse(readFileSync("./config.json").toString()) as Config;
|
||||
|
||||
const mcServer = new MinecraftServer(config);
|
||||
new MinecraftServer(config);
|
|
@ -1,5 +1,4 @@
|
|||
import { FunkyArray } from "../funkyArray";
|
||||
import { Block } from "./blocks/Block";
|
||||
import { Player } from "./entities/Player";
|
||||
import { World } from "./World";
|
||||
|
||||
|
|
|
@ -37,7 +37,7 @@ export class MPClient {
|
|||
}
|
||||
|
||||
handlePacketPlayer(packet:PacketPlayer) {
|
||||
// TODO
|
||||
this.entity.onGround = packet.onGround;
|
||||
}
|
||||
|
||||
handlePacketPlayerPosition(packet:PacketPlayerPosition) {
|
||||
|
|
|
@ -11,12 +11,8 @@ import { PacketKeepAlive } from "./packets/KeepAlive";
|
|||
import { PacketLoginRequest } from "./packets/LoginRequest";
|
||||
import { PacketDisconnectKick } from "./packets/DisconnectKick";
|
||||
import { Player } from "./entities/Player";
|
||||
import { PacketTimeUpdate } from "./packets/TimeUpdate";
|
||||
import { PacketSpawnPosition } from "./packets/SpawnPosition";
|
||||
import { Chunk } from "./Chunk";
|
||||
import { PacketMapChunk } from "./packets/MapChunk";
|
||||
import { PacketPlayerPositionLook } from "./packets/PlayerPositionLook";
|
||||
import { PacketPreChunk } from "./packets/PreChunk";
|
||||
import { PacketChat } from "./packets/Chat";
|
||||
|
||||
export class MinecraftServer {
|
||||
|
@ -25,10 +21,9 @@ export class MinecraftServer {
|
|||
private static readonly TICK_RATE_MS = 1000 / MinecraftServer.TICK_RATE;
|
||||
private readonly keepalivePacket = new PacketKeepAlive().writeData();
|
||||
|
||||
private totalClients:number = 0;
|
||||
private config:Config;
|
||||
private server:Server;
|
||||
private serverClock:NodeJS.Timer;
|
||||
private readonly serverClock:NodeJS.Timer;
|
||||
private tickCounter:number = 0;
|
||||
private clients:FunkyArray<string, MPClient>;
|
||||
private worlds:FunkyArray<number, World>;
|
||||
|
@ -85,7 +80,7 @@ export class MinecraftServer {
|
|||
}
|
||||
|
||||
this.worlds.forEach(world => {
|
||||
world.tick(this.tickCounter);
|
||||
world.tick();
|
||||
});
|
||||
this.tickCounter++;
|
||||
}, MinecraftServer.TICK_RATE_MS);
|
||||
|
@ -101,33 +96,7 @@ export class MinecraftServer {
|
|||
});
|
||||
}
|
||||
|
||||
onConnection(socket:Socket) {
|
||||
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:
|
||||
handleLoginRequest(reader:Reader, socket:Socket, setMPClient:(mpclient:MPClient) => void) {
|
||||
const loginPacket = new PacketLoginRequest().readData(reader);
|
||||
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);
|
||||
world.addEntity(clientEntity);
|
||||
|
||||
const client = mpClient = new MPClient(socket, clientEntity);
|
||||
const client = new MPClient(socket, clientEntity);
|
||||
setMPClient(client);
|
||||
clientEntity.mpClient = client;
|
||||
this.clients.set(loginPacket.username, client);
|
||||
|
||||
|
@ -156,12 +126,45 @@ export class MinecraftServer {
|
|||
} else {
|
||||
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);
|
||||
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;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
|
|
@ -1,9 +1,8 @@
|
|||
import { Console } from "../console";
|
||||
import { FunkyArray } from "../funkyArray";
|
||||
import { Chunk } from "./Chunk";
|
||||
import { IEntity } from "./entities/IEntity";
|
||||
import { Player } from "./entities/Player";
|
||||
import { FlatGenerator } from "./generators/Flat";
|
||||
//import { FlatGenerator } from "./generators/Flat";
|
||||
import { HillyGenerator } from "./generators/Hilly";
|
||||
import { IGenerator } from "./generators/IGenerator";
|
||||
|
||||
|
@ -26,7 +25,7 @@ export class World {
|
|||
// TODO: getChunkByCoordPair failed in here during removeEntity, figure out why.
|
||||
public removeEntity(entity:IEntity) {
|
||||
if (entity instanceof Player) {
|
||||
for (let coordPair of entity.loadedChunks) {
|
||||
for (const coordPair of entity.loadedChunks) {
|
||||
const chunk = this.getChunkByCoordPair(coordPair);
|
||||
chunk.playersInChunk.remove(entity.entityId);
|
||||
|
||||
|
@ -68,11 +67,11 @@ export class World {
|
|||
this.chunks.remove(coordPair);
|
||||
}
|
||||
|
||||
public tick(tickCount:number) {
|
||||
public tick() {
|
||||
this.entites.forEach(entity => {
|
||||
if (entity instanceof Player) {
|
||||
if (entity.justUnloaded.length > 0) {
|
||||
for (let coordPair of entity.justUnloaded) {
|
||||
for (const coordPair of entity.justUnloaded) {
|
||||
const chunkToUnload = this.getChunkByCoordPair(coordPair);
|
||||
chunkToUnload.playersInChunk.remove(entity.entityId);
|
||||
if (chunkToUnload.playersInChunk.length === 0) {
|
||||
|
|
|
@ -4,12 +4,14 @@ import { Entity } from "./Entity";
|
|||
export class EntityLiving extends Entity {
|
||||
public yaw:number;
|
||||
public pitch:number;
|
||||
public onGround:boolean;
|
||||
|
||||
public constructor(world:World) {
|
||||
super(world);
|
||||
|
||||
this.yaw = 0;
|
||||
this.pitch = 0;
|
||||
this.onGround = false;
|
||||
}
|
||||
|
||||
onTick() {
|
||||
|
|
|
@ -1,12 +1,9 @@
|
|||
import { FunkyArray } from "../../funkyArray";
|
||||
import { Chunk } from "../Chunk";
|
||||
import { MPClient } from "../MPClient";
|
||||
import { MinecraftServer } from "../MinecraftServer";
|
||||
import { World } from "../World";
|
||||
import { PacketMapChunk } from "../packets/MapChunk";
|
||||
import { EntityLiving } from "./EntityLiving";
|
||||
import { Entity } from "./Entity";
|
||||
import { Socket } from "net";
|
||||
import { PacketPreChunk } from "../packets/PreChunk";
|
||||
|
||||
export class Player extends EntityLiving {
|
||||
|
@ -64,7 +61,7 @@ export class Player extends EntityLiving {
|
|||
}
|
||||
|
||||
// Mark any unaccounted chunks for unload
|
||||
for (let coordPair of this.loadedChunks) {
|
||||
for (const coordPair of this.loadedChunks) {
|
||||
if (!currentLoads.includes(coordPair)) {
|
||||
this.justUnloaded.push(coordPair);
|
||||
const chunkToUnload = this.world.getChunkByCoordPair(coordPair);
|
||||
|
|
|
@ -35,7 +35,8 @@ export class HillyGenerator implements IGenerator {
|
|||
// This is good enough (and fast enough) for what is needed here.
|
||||
private mulberry32(a:number) {
|
||||
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 ^= t + Math.imul(t ^ t >>> 7, t | 61);
|
||||
return ((t ^ t >>> 14) >>> 0) / 4294967296;
|
||||
|
|
Loading…
Reference in a new issue