using System; using System.Collections.Generic; using System.Linq; using System.Text; using GenericNetplayImplementation; using WOMGeneral; using System.IO; namespace WorldOfMinimalism { public class Client : GNIClient { Main main = null; public int playerID = -1; public Client(Main main, string host = "127.0.0.1", int port = 51535) { this.main = main; StartClient(host, port); this.autoPollInterval = 100; AutoPoll(); } //--Signal receiving-- public override void OnDataReceived(GNIData data, uint source = 0) { switch (data.keyType) { case GNIDataType.Short: switch (data.keyInt) { case 1001: //Your cell { Address address = new Address(data.valueBytes); SetLocation(address); break; } case 1002: //Serialized cell { MemoryStream ms = new MemoryStream(data.valueBytes); byte[] addressBuffer = new byte[4]; ms.Read(addressBuffer, 0, 4); Address address = new Address(addressBuffer); byte[] cellBuffer = new byte[ms.Length - 4]; ms.Read(cellBuffer, 0, cellBuffer.Length); SetCell(address, cellBuffer); break; } case 1003: //Serialized tile { MemoryStream ms = new MemoryStream(data.valueBytes); byte[] addressBuffer = new byte[6]; ms.Read(addressBuffer, 0, 6); Address address = new Address(addressBuffer); byte[] tileBuffer = new byte[ms.Length - 6]; ms.Read(tileBuffer, 0, tileBuffer.Length); SetTile(address, tileBuffer); break; } case 1004: //Add player { if (playerID != -1 && playerID != data.valueInt) { main.otherPlayers.Add((uint)data.valueInt, new Player(main)); } break; } case 1005: //Remove player { if (playerID != -1 && playerID != data.valueInt) { if (main.otherPlayers.ContainsKey((uint)data.valueInt)) { main.otherPlayers.Remove((uint)data.valueInt); } } break; } case 1006: //Your ID { playerID = data.valueInt; break; } case 1007: //Ping { GNIData toSend = new GNIData(true); toSend.keyType = GNIDataType.Short; toSend.keyInt = 5; SendSignal(this.tcpClient, toSend); break; } default: { if (playerID != -1 && playerID != data.keyInt) { uint otherID = (uint)data.keyInt; if (main.otherPlayers.ContainsKey(otherID)) { Address address = new Address(data.valueBytes); main.otherPlayers[otherID].cellX = address.cellX; main.otherPlayers[otherID].cellY = address.cellY; main.otherPlayers[otherID].x = address.x; main.otherPlayers[otherID].y = address.y; main.otherPlayers[otherID].UpdateSpritePosition(); } } break; } } break; } base.OnDataReceived(data, source); } //--Server-initiated actions-- //Sets the user's current location public void SetLocation(Address address) { main.ChangePlayerLocation(address); } //Deserializes a cell public void SetCell(Address address, byte[] serializedCell) { main.delayedActions.Add(new DASetCell(serializedCell, address)); } //Changes a tile, if the cell is present locally public void SetTile(Address address, byte[] serializedTile) { main.delayedActions.Add(new DASetTile(serializedTile, address)); } //--Client-initiated actions-- public void RequestSpawnLocation() { SendSignal(this.tcpClient, new GNIData() { keyType = GNIDataType.Short, keyInt = 1 }); } //Ask server to serialize and send a cell public void RequestCell(Address address) { main.SetLoading(true); SendSignal(2, address.Serialize(false)); } public void ChangeTile(Address address, byte[] serializedTile) { SendSignal(3, Util.MergeArrays(new byte[][] { address.Serialize(true), serializedTile })); } public void SendPosition() { SendSignal(4, new Address(main.player.cellX, main.player.cellY, main.player.x, main.player.y).Serialize(true)); } //--Misc-- public void SendSignal(int key, byte[] value) { GNIData toSend = new GNIData(true); toSend.keyType = GNIDataType.Short; toSend.valueType = GNIDataType.ByteArray; toSend.keyInt = key; toSend.valueBytes = value; SendSignal(this.tcpClient, toSend); } } }