public static void InitializeServer() { var startTime = GetTickCount(); Cnsl.Info("Initializing Server", true); //Intializing all game data arrays Cnsl.Debug("Initializing Game Arrays", true); for (var i = 0; i < Constants.MAX_PLAYERS; i++) { ServerTcp.Client[i] = new Clients(); } Cnsl.Finalize("Initializing Game Arrays"); //Start the Networking Cnsl.Debug("Initializing Network", true); Cnsl.Debug("Initializing Status Listener", true); ServerHandleData.InitializePackets(); ServerTcp.InitializeNetwork(); ServerTcp.InitializeStatusListener(); //Load database items LoadData(); var endTime = GetTickCount(); Cnsl.Info(@"Initialization complete. Server loaded in " + (endTime - startTime) + " ms."); Cnsl.Banner(" Server has started successfully \n @ " + DateTime.UtcNow + " UTC"); Globals.Initialized = true; Cnsl.Finalize("Initializing Server"); }
public static void AddToInventory(User player, string itemId, int qty) { var item = Globals.Items.FirstOrDefault(it => it.Id == itemId); if (item == null) { return; } // Check if user already has it var slot = player.Inventory.FirstOrDefault(s => s.ItemId == itemId); if (slot != null) { if (item.Stack) { AddExisting(slot, player, itemId, qty); } else { AddNew(player, itemId, qty); } } else { AddNew(player, itemId, qty); } ServerTcp.SendInventory(Globals.PlayerIDs.First(p => p.Value == player.Id).Key); }
public static void PurchaseShip(int connectionId, string shopId, string invId, string color) { var player = Program._userService.ActiveUsers.FirstOrDefault(p => p.Id == Globals.PlayerIDs[connectionId]); var cost = 0; var newShip = "null"; if (invId != "null") { var shop = Globals.Structures.First(i => i.ID == shopId); var inv = shop.Inventory.First(i => i.Id == invId); var item = Globals.Items.First(i => i.Id == inv.ItemId); var oldShipInv = player.Inventory.First(i => i.Slot == 24); var oldShip = Globals.Items.First(i => i.Id == oldShipInv.ItemId); newShip = inv.ItemId; cost += item.Cost; cost -= oldShip.Cost; } if (color != "0") { cost += 1000; } if (cost <= player.Credits) { if (newShip != "null") { player.Inventory.First(i => i.Slot == 24).ItemId = newShip; } player.Color = color; player.Credits -= cost; } ServerTcp.SendIngame(connectionId, 1); }
public static void SellItem(int connectionId, int slot, int qty) { var player = Program._userService.ActiveUsers.FirstOrDefault(p => p.Id == Globals.PlayerIDs[connectionId]); var inv = player?.Inventory.FirstOrDefault(i => i.Slot == slot); if (inv == null) { return; } var itm = Globals.Items.FirstOrDefault(i => i.Id == inv.ItemId); if (itm == null) { return; } if (inv.Quantity > qty) { player.Credits += itm.Cost * qty; inv.Quantity -= qty; } else if (inv.Quantity <= qty) { player.Credits += itm.Cost * inv.Quantity; player.Inventory.Remove(inv); } ServerTcp.SendInventory(Globals.PlayerIDs.First(p => p.Value == player.Id).Key); }
private void ServerForm_Load(object sender, EventArgs e) { Controls.Add(new DataGridView() { ColumnCount = typeof(TransmissionData).GetProperties().Length + 1, Dock = DockStyle.Fill, Name = "dGrid", AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells }); serverTcp = new ServerTcp(8080, 1000, IPAddress.Parse("127.0.0.1")); serverTcp.DataReceivedEvent += Tcp_DataReceivedEvent; try { Task.Factory.StartNew(() => serverTcp.ListenClients(), serverTcp.CToken); } catch (Exception ex) { MessageBox.Show(ex.Message); } }
public void CloseSocket(int index) { Cnsl.Log("Connection from " + ip + " has been terminated"); var player = Program._userService.ActiveUsers.Find(p => p.Id == Globals.PlayerIDs[index]); if (player != null) { ServerTcp.SendMessage(-1, player.Name + @" has disconnected.", (int)ChatPackets.Notification); player.inGame = false; player.receiving = false; } Program._gameService.SaveGame(new List <User> { player }); socket.Close(); socket = null; Globals.PlayerIDs[index] = null; Program._userService.ActiveUsers.Remove(player); }
private static void PACKET_LOGIN(int connectionId, byte[] data) { var buffer = new ByteBuffer(); buffer.WriteBytes(data); buffer.ReadLong(); var user = buffer.ReadString(); var pass = buffer.ReadString(); var validate = Program._userService.PasswordOK(user, pass); if (!validate) { ServerTcp.SendSystemByte(connectionId, SystemBytes.SysInvPass); return; } buffer.Dispose(); var player = Program._userService.LoadPlayer(user); player.inGame = true; player.connectionID = connectionId; Globals.PlayerIDs.Add(connectionId, player.Id); Program._userService.ActiveUsers.Add(player); ServerTcp.SendGlobals(connectionId); System.Threading.Thread.Sleep(250); ServerTcp.SendItems(connectionId); System.Threading.Thread.Sleep(250); ServerTcp.SendToGalaxy(connectionId); System.Threading.Thread.Sleep(250); ServerTcp.SendIngame(connectionId); System.Threading.Thread.Sleep(250); ServerTcp.SendInventory(connectionId); //ServerTCP.SendNebulae(connectionID); ServerTcp.SendMessage(-1, player.Name + " has connected.", (int)ChatPackets.Notification); System.Threading.Thread.Sleep(250); ServerTcp.SendGalaxy(connectionId); System.Threading.Thread.Sleep(250); ServerTcp.SendRecipes(connectionId); Globals.FullData = true; Cnsl.Log(user + @" logged in successfully."); player.receiving = true; }
private static void PACKET_CLIENTMESSAGE(int connectionId, byte[] data) { var buffer = new ByteBuffer(); buffer.WriteBytes(data); buffer.ReadLong(); var msg = buffer.ReadString(); if (msg.ToLower().StartsWith("/c")) { var player = Program._userService.ActiveUsers.Find(p => p.Id == Globals.PlayerIDs[connectionId]); var newString = player.Name + ": " + msg.Substring(3); ServerTcp.SendMessage(-1, newString, (int)ChatPackets.Chat); } // Admin commands if (msg.StartsWith("*")) { Admin.HandleCommand(connectionId, msg); } buffer.Dispose(); }
public static void HandleCommand(int connectionID, string msg) { if (msg.ToLower().StartsWith("*goto")) { var player = Program._userService.ActiveUsers.Find(p => p.Id == Globals.PlayerIDs[connectionID]); var dest = msg.Split(new[] { ' ' }, 2); var coords = dest[1].Split(','); var testx = int.TryParse(coords[0].Trim(), out var x); var testy = int.TryParse(coords[1].Trim(), out var y); if (!testx || !testy || x > 100000 || x < 0 || y > 100000 || y < 0) { ServerTcp.SendMessage(connectionID, "Invalid coordinates", (int)ChatPackets.Error); } else { player.X = x; player.Z = y; ServerTcp.SendIngame(connectionID, 1); } } if (msg.ToLower().StartsWith("*savegame")) { Program.SaveGame(); } if (msg.ToLower().StartsWith("*repop")) { Program._mobService.RepopGalaxy(); } if (msg.ToLower().StartsWith("*give")) { if (msg.ToLower().Contains(" xp ")) { var part = msg.Split(new[] { ' ' }, 4); var player = Program._userService.ActiveUsers.Find(p => string.Equals(p.Name, part[1], StringComparison.CurrentCultureIgnoreCase)); if (player == null) { ServerTcp.SendMessage(connectionID, "User does not exist", (int)ChatPackets.Error); return; } player.Exp += int.Parse(part[3]); ServerTcp.SendIngame(connectionID, 1); } if (msg.ToLower().Contains(" item ")) { var part = msg.Split(new[] { ' ' }, 4); var player = Program._userService.ActiveUsers.Find(p => string.Equals(p.Name, part[1], StringComparison.CurrentCultureIgnoreCase)); if (player == null) { ServerTcp.SendMessage(connectionID, "User does not exist", (int)ChatPackets.Error); return; } ItemManager.AddToInventory(player, part[3], int.Parse(part[2])); } if (msg.ToLower().Contains(" $")) { var part = msg.Split(new[] { ' ' }, 3); var player = Program._userService.ActiveUsers.Find(p => string.Equals(p.Name, part[1], StringComparison.CurrentCultureIgnoreCase)); if (player == null) { ServerTcp.SendMessage(connectionID, "User does not exist", (int)ChatPackets.Error); return; } player.Credits += int.Parse(part[2].Substring(1)); } if (msg.ToLower().Contains(" lvl ")) { var part = msg.Split(new[] { ' ' }, 4); var player = Program._userService.ActiveUsers.Find(p => string.Equals(p.Name, part[1], StringComparison.CurrentCultureIgnoreCase)); if (player == null) { ServerTcp.SendMessage(connectionID, "User does not exist", (int)ChatPackets.Error); return; } player.Level = int.Parse(part[3]); } } if (msg.ToLower().StartsWith("*heal")) { var part = msg.Split(new[] { ' ' }, 2); if (part[1] == "all") { foreach (var player in Program._userService.ActiveUsers) { player.Health = player.MaxHealth; player.Shield = player.MaxShield; player.Weap1Charge = 100; player.Weap2Charge = 100; player.Weap3Charge = 100; player.Weap4Charge = 100; player.Weap5Charge = 100; } } else { var player = Program._userService.ActiveUsers.Find(p => string.Equals(p.Name, part[1], StringComparison.CurrentCultureIgnoreCase)); if (player == null) { ServerTcp.SendMessage(connectionID, "User does not exist", (int)ChatPackets.Error); return; } player.Health = player.MaxHealth; player.Shield = player.MaxShield; player.Weap1Charge = 100; player.Weap2Charge = 100; player.Weap3Charge = 100; player.Weap4Charge = 100; player.Weap5Charge = 100; } } if (msg.ToLower().StartsWith("*destroy")) { var player = Program._userService.ActiveUsers.Find(p => p.Id == Globals.PlayerIDs[connectionID]); var part = msg.Split(new[] { ' ' }, 2); var mob = Program._mobService.GetMob(part[1]); var weap = new Item { Id = new Guid().ToString(), Slot = 22, Damage = 100000, Recharge = 1, Type = "launcher", Name = "Act of God" }; var ammo = new Item { Id = new Guid().ToString(), Slot = 23, Damage = 0, Type = "missile" }; Program._mobService.DoAttack(part[1], player.Id, weap, ammo); ServerTcp.SendMessage(-1, player.Name + " has dealt " + mob.Name + " " + weap.Damage + " damage with an " + weap.Name, (int)ChatPackets.Notification); } }
private static void ConsoleThread() { var command = ""; var pulseTimer = new Timer(e => { if (Globals.Initialized) { ServerTcp.PreparePulseBroadcast(); } }, null, TimeSpan.FromSeconds(1), TimeSpan.FromMilliseconds(25)); // Recharge systems var rechargeTimer = new Timer(e => { if (Globals.Initialized) { _userService.RechargeSystems(); } }, null, TimeSpan.FromSeconds(1), TimeSpan.FromSeconds(1)); // Try a repop every 30 seconds // Remove old loots var repopTimer = new Timer(e => { if (Globals.Initialized) { _mobService.RepopGalaxy(); } if (Globals.Initialized) { _itemService.RemoveLoot(); } }, null, TimeSpan.FromSeconds(1), TimeSpan.FromMilliseconds(30000)); // Wander mobs everys 15 seconds var wanderTimer = new Timer(e => { if (Globals.Initialized) { _mobService.WanderMobs(); } }, null, TimeSpan.FromSeconds(1), TimeSpan.FromMilliseconds(15000)); // Save the game every 5 minutes var saveTimer = new Timer(e => { if (Globals.Initialized) { SaveGame(); } }, null, TimeSpan.FromSeconds(1), TimeSpan.FromMilliseconds(300000)); while (command != "end" && command != "e" && command != "exit" && command != "q" && command != "quit") { command = Console.ReadLine(); Console.CursorTop--; switch (command) { case "save": SaveGame(); break; case "": Cnsl.Debug("#"); break; default: if (command != "end" && command != "e" && command != "exit" && command != "q" && command != "quit") { Cnsl.Debug(@"Unknown Command"); } break; } } // Try one last save on exit SaveGame(); pulseTimer.Dispose(); repopTimer.Dispose(); wanderTimer.Dispose(); saveTimer.Dispose(); }
public static void Equip(int connectionId, int from, int to) { var player = Program._userService.ActiveUsers.FirstOrDefault(p => p.Id == Globals.PlayerIDs[connectionId]); if (player == null) { return; } var inv = player.Inventory.First(i => i.Slot == from); var newInv = player.Inventory.FirstOrDefault(i => i.Slot == to); if (to >= 100) { if (newInv == null) { inv.Slot = to; } else { if (inv.ItemId == newInv.ItemId) { AddExisting(newInv, player, inv.ItemId, inv.Quantity); } } } else { var item = Globals.Items.First(i => i.Id == inv.ItemId); if (item.Slot == to) { inv.Slot = to; } // Check special cases // Check for weapon if (to >= 4 && to <= 8 && item.Slot == 22) { inv.Slot = to; switch (to) { case 4: player.Weap1ChargeRate = item.Recharge; break; case 5: player.Weap2ChargeRate = item.Recharge; break; case 6: player.Weap3ChargeRate = item.Recharge; break; case 7: player.Weap4ChargeRate = item.Recharge; break; case 8: player.Weap5ChargeRate = item.Recharge; break; } } // Check for muns if (to >= 9 && to <= 11 && item.Slot == 23) { inv.Slot = to; } // Check for armor if (to >= 13 && to <= 20 && item.Slot == 21) { inv.Slot = to; } } ServerTcp.SendInventory(connectionId); }