static void Main(string[] args) { GameRegistry.Initialize(); Server room = new Server(44325, 0.02f); room.Start(); while (true) { room.Update(); if (Console.KeyAvailable) { ConsoleKeyInfo key = Console.ReadKey(true); switch (key.Key) { case ConsoleKey.F1: room.Stop(); return; default: break; } } } }
/// <summary> /// Parse the login sting /// </summary> public void Login(string line) { if (line.ToLower().StartsWith("login ")) { if (line.Length >= 6) { username = line.Substring(6, line.Length - 6); Server.Update(200, Server.gameState.GetMapSizeX() + ", " + Server.gameState.GetMapSizeY()); Console.WriteLine("Logged in as: " + username); } } // Send the map Server.SendMap(username); // Spawn the player to a random part of the map Random randomizer = new Random(); bool placed = false; while (!placed) { int xPos = randomizer.Next(0, Server.gameState.GetMapSizeX()); int yPos = randomizer.Next(0, Server.gameState.GetMapSizeY()); if (CheckCollision(xPos, yPos)) { // Initialize the player cookieCount = 20; Server.Update(104, username + ", " + xPos + ", " + yPos + ", " + cookieCount); x = xPos; y = yPos; placed = true; } } }
/// <summary> /// Run the incoming stream /// </summary> public void Run() { Thread thread = new Thread(() => CheckConnection()); thread.Start(); try { //NetworkStream stream = new NetworkStream(_incomingSocket); NetworkStream ns = new NetworkStream(_incomingSocket); StreamReader stream = new StreamReader(ns); bool loggedIn = false; string line = ""; while (stream != null && !stream.EndOfStream && _incomingSocket.Connected) { // Parse the incoming data line = stream.ReadLine(); Console.WriteLine("Readin: " + line); if (!loggedIn) { loggedIn = true; Login(line); } Move(line); Throw(line); Message(line); } } catch (Exception) { // Tell the player there's a network issue. } Console.WriteLine("Player disconnected"); Server.Update(104, username + ", -1, -1, -1"); }
/// <summary> /// Send this cookies updated info /// </summary> public void SendUpdate() { // Check time to live if (ttl > 0) { ttl--; string dirStr = ""; switch (direction) { case directions.up: dirStr = "u"; break; case directions.down: dirStr = "d"; break; case directions.left: dirStr = "l"; break; case directions.right: dirStr = "r"; break; } Server.Update(103, id + ", " + x + ", " + y + ", " + dirStr); } }
/// <summary> /// Parse an incoming move command /// </summary> public void Move(string line) { bool moved = false; if (line.ToLower().StartsWith("move ") || line.ToLower().StartsWith("m ")) { line = line.Substring(line.IndexOf(" ") + 1); if (line.StartsWith("u") || line.StartsWith("up")) { // Change this player's location if (CheckCollision(x, y + 1)) { y++; y %= Server.gameState.GetMapSizeY(); moved = true; } } else if (line.StartsWith("d") || line.StartsWith("down")) { if (CheckCollision(x, y - 1)) { y--; if (y < 0) { y = Server.gameState.GetMapSizeY() - 1; } moved = true; } } else if (line.StartsWith("l") || line.StartsWith("left")) { if (CheckCollision(x - 1, y)) { x--; if (x < 0) { x = Server.gameState.GetMapSizeX() - 1; } moved = true; } } else if (line.StartsWith("r") || line.StartsWith("right")) { if (CheckCollision(x + 1, y)) { x++; x %= Server.gameState.GetMapSizeX(); moved = true; } } } // Update the other clients if (moved) { Server.Update(104, username + ", " + x + ", " + y + ", " + cookieCount); } }
static void Main(string[] args) { Server.EventSystem.LoadAssembly(typeof(Server).Assembly); Server.EventSystem.LoadAssembly(typeof(Sample.Sample).Assembly); Server.Awake(); Server.Start(); while (true) { try { Thread.Sleep(1); Server.Update(); Server.LateUpdate(); } catch (Exception e) { Logger.WriteException(e); } } }