private void OnEnable() { if (PlayerLocations.PL == null) { PlayerLocations.PL = this; } }
/** * Send the location of all players to all players, so that they can be drawn on the map * This should only be called when a player moves or a new player joins */ public static void SendLocations() { //created a string with the player names and their locations String rStr = "&"; foreach (Player p in clientList) { if (p.RoomIndex != -1) { rStr += p.PlayerName + " " + p.RoomIndex + "&"; } } PlayerLocations m = new PlayerLocations(); m.LocationString = rStr; //send that message to all the players foreach (Player p in clientList) { try { MemoryStream outStream = m.WriteData(p.Salt); p.Socket.Send(outStream.ToArray()); } catch (System.Exception) { RemoveClientByPlayer(p); } } }
/** * Thread responsible for receiving all incoming data from the server * @param o form object reference */ private void ClientReceive(Object o) { MudClient form = (MudClient)o; while (form.bConnected == true) { try { byte[] buffer = new byte[4096]; int result; //listen for buffer result = form.clientSocket.Receive(buffer); if (result > 0) { //decode stream Msg m = Msg.DecodeStream(buffer, Salt, ShouldDecrypt); //Get message type and do approriate actiobn if (m != null) { Console.Write("Got a message: "); switch (m.mID) { case DungeonResponse.ID: { DungeonResponse dr = (DungeonResponse)m; form.AddDungeonText(dr.response, true); } break; case MapLayout.ID: { MapLayout ml = (MapLayout)m; DrawQueue.Enqueue(() => ParseMap(ml.mapInfo)); break; } case LoginResponse.ID: { LoginResponse lm = (LoginResponse)m; if (lm.loggedIn == "1") { //Login was successful start decrypting and start main game loginScreen.LoginResponse(lm.message, true); ShouldDecrypt = true; } else { //login failed, show failed message loginScreen.LoginResponse(lm.message, false); ShouldDecrypt = false; } } break; case PlayerLocations.ID: { PlayerLocations pl = (PlayerLocations)m; DrawQueue.Enqueue(() => UpdatePlayerLocations(pl.LocationString)); } break; case UpdateChat.ID: { UpdateChat uc = (UpdateChat)m; form.AddDungeonText(uc.message, false); } break; case SaltSend.ID: { SaltSend ss = (SaltSend)m; Salt = ss.salt; SendLoginMessage(); break; } case SaltRequest.ID: { SaltRequest sr = (SaltRequest)m; SendCreateUserMessage(); break; } default: break; } } } } catch (Exception ex) { //If a discconect happens restart the service form.bConnected = false; Console.WriteLine(U.NL("Lost server!") + ex); Application.Restart(); Environment.Exit(0); } } }