public void HangarRemove(string NameOrSteamID, string GridNameOrNumber) { if (!Plugin.Config.PluginEnabled) { return; } Chat chat = new Chat(Context, true); if (Utils.AdminTryGetPlayerSteamID(NameOrSteamID, chat, out ulong SteamID)) { GridMethods methods = new GridMethods(SteamID, Plugin.Config.FolderDirectory); if (!methods.LoadInfoFile(out PlayerInfo Data)) { return; } if (Int32.TryParse(GridNameOrNumber, out int result)) { if (result > Data.Grids.Count) { chat.Respond("This hangar slot is empty! Select a grid that is in the hangar!"); return; } if (result != 0) { GridStamp Grid = Data.Grids[result - 1]; Data.Grids.RemoveAt(result - 1); string path = Path.Combine(methods.FolderPath, Grid.GridName + ".sbc"); File.Delete(path); chat.Respond(string.Format("{0} was successfully deleted!", Grid.GridName)); FileSaver.Save(Path.Combine(methods.FolderPath, "PlayerInfo.json"), Data); return; } else if (result == 0) { int counter = 0; foreach (var grid in Data.Grids) { string path = Path.Combine(methods.FolderPath, grid.GridName + ".sbc"); File.Delete(path); counter++; } Data.Grids.Clear(); chat.Respond(string.Format("Successfully deleted {0} grids!", counter)); FileSaver.Save(Path.Combine(methods.FolderPath, "PlayerInfo.json"), Data); return; } } else { foreach (var grid in Data.Grids) { if (grid.GridName == GridNameOrNumber) { Data.Grids.Remove(grid); string path = Path.Combine(methods.FolderPath, grid.GridName + ".sbc"); File.Delete(path); chat.Respond(string.Format("{0} was successfully deleted!", grid.GridName)); FileSaver.Save(Path.Combine(methods.FolderPath, "PlayerInfo.json"), Data); return; } } } } }
//Force update public bool Update(CrossServerMessage Message) { if (_Market.IsHostServer) { string MarketServerData = JsonConvert.SerializeObject(Message); //Write to file and broadcast to all clients! Hangar.Debug("Sending new market data to clients! " + Message.Type.ToString()); if (Message.Type == MessageType.AddItem) { //Main.Debug("Point1"); Server.BroadcastLine(MarketServerData); //Main.Debug("Point2"); //Update server. GridMarket.GridList.Add(Message.List[0]); //Main.GridDefinition.Add(Message.GridDefinition[0]); //Send update to clients on this game server! Comms.AddSingleItem(Message.List[0]); //Main.Debug("Point3"); //Save data to file (This is server!) MarketData Data = new MarketData(); Data.List = GridMarket.GridList; FileSaver.Save(Path.Combine(Hangar.Dir, "Market.json"), Data); } else if (Message.Type == MessageType.RemoveItem) { Server.BroadcastLine(MarketServerData); //Update server. GridMarket.GridList.Remove(Message.List[0]); //Main.GridDefinition.RemoveAll(Message.GridDefinition[0]); //Main.GridDefinition.RemoveAll(x => x.name == Message.List[0].Name); //Send update to clients on this game server! Comms.RemoveSingleItem(Message.List[0]); //Save data to file (This is server!) MarketData Data = new MarketData(); Data.List = GridMarket.GridList; FileSaver.Save(Path.Combine(Hangar.Dir, "Market.json"), Data); } else if (Message.Type == MessageType.PlayerAccountUpdated) { //Do nothing. (Send data to server and wait for reply) Server.BroadcastLine(MarketServerData); foreach (PlayerAccount account in Message.BalanceUpdate) { Utils.TryUpdatePlayerBalance(account); if (!GridMarket.PlayerAccounts.ContainsKey(account.SteamID)) { if (!account.AccountAdjustment) { GridMarket.PlayerAccounts.Add(account.SteamID, account.AccountBalance); } } else { if (!account.AccountAdjustment) { GridMarket.PlayerAccounts[account.SteamID] = account.AccountBalance; } else { //Add this to the general list GridMarket.PlayerAccounts[account.SteamID] = account.AccountBalance + GridMarket.PlayerAccounts[account.SteamID]; } } } Accounts accounts = new Accounts(); accounts.PlayerAccounts = GridMarket.PlayerAccounts; FileSaver.Save(Path.Combine(Hangar.Dir, "PlayerAccounts.json"), accounts); } else if (Message.Type == MessageType.PlayerOnline) { //Server found the player online! (By Joining) //var first = Message.BalanceUpdate.First(); //ulong key = first.Key; // Main.PlayerAccountUpdate.Remove(key); //Broadcast to all clients! Server.BroadcastLine(MarketServerData); } else if (Message.Type == MessageType.PurchasedGrid) { var t = Task.Run(() => _Market.PurchaseGrid(Message.GridDefinition[0])); } //File.WriteAllText(Path.Combine(Main.Dir, "Market.json"), JsonConvert.SerializeObject(Data)); } else { try { string MarketClientData = JsonConvert.SerializeObject(Message); //Send to server to get reply Client.Write(MarketClientData); if (Message.Type == MessageType.AddItem) { //Send update to clients on this game server! Comms.AddSingleItem(Message.List[0]); } else if (Message.Type == MessageType.RemoveItem) { //Send update to clients on this game server! Comms.RemoveSingleItem(Message.List[0]); } } catch (System.IO.IOException e) { //Server no longer responding? Perhaps it shutdown? Or restarted? //This means THIS server needs to be the new server //Or this is an old client needing to be re-connected to server. This means we need to redo/Reconnect everything! Hangar.Debug("Server closed! Trying to Update!", e, Hangar.ErrorType.Warn); //Remove client DataRecieved Event! Client.DelimiterDataReceived -= Client_DelimiterDataReceived; //Re run create servers and client class! CreateServers(); //Re run the update Update(Message); } catch (Exception g) { Hangar.Debug("CrossServer Market Network Failed Fatally!", g, Hangar.ErrorType.Fatal); } } return(true); }
public void SaveInfoFile(PlayerInfo Data) { FileSaver.Save(Path.Combine(FolderPath, "PlayerInfo.json"), Data); }
private void Server_DataReceived(object sender, SimpleTCP.Message e) { //if we recieve data to server, this means we need to update clients, and save file to disk CrossServerMessage RecievedData = JsonConvert.DeserializeObject <CrossServerMessage>(e.MessageString); Hangar.Debug("Client Data Recieved! " + RecievedData.Type.ToString()); if (RecievedData.Type == MessageType.AddItem) { Server.BroadcastLine(e.MessageString); //Now that we added our items... we need to save file //Main.GridDefinition.Add(RecievedData.GridDefinition[0]); GridMarket.GridList.Add(RecievedData.List[0]); //Send update to clients on this game server! Comms.AddSingleItem(RecievedData.List[0]); //Save data to file (This is server!) MarketData Data = new MarketData(); Data.List = GridMarket.GridList; FileSaver.Save(Path.Combine(Hangar.Dir, "Market.json"), Data); } else if (RecievedData.Type == MessageType.RemoveItem) { Server.BroadcastLine(e.MessageString); //Just goahead and check to see if the list contains etc. if (GridMarket.GridList.Any(x => x.Name == RecievedData.List[0].Name)) { Hangar.Debug("Removing: " + RecievedData.List[0].Name + " from market!"); GridMarket.GridList.RemoveAll(x => x.Name == RecievedData.List[0].Name); //Main.GridDefinition.RemoveAll(x => x.name == RecievedData.List[0].Name); //Send update to clients on this game server! Comms.RemoveSingleItem(RecievedData.List[0]); } //Save data to file (This is server!) MarketData Data = new MarketData(); Data.List = GridMarket.GridList; FileSaver.Save(Path.Combine(Hangar.Dir, "Market.json"), Data); } else if (RecievedData.Type == MessageType.PlayerAccountUpdated) { Server.BroadcastLine(e.MessageString); //Store values foreach (PlayerAccount account in RecievedData.BalanceUpdate) { Utils.TryUpdatePlayerBalance(account); if (!GridMarket.PlayerAccounts.ContainsKey(account.SteamID)) { if (!account.AccountAdjustment) { GridMarket.PlayerAccounts.Add(account.SteamID, account.AccountBalance); } } else { if (!account.AccountAdjustment) { GridMarket.PlayerAccounts[account.SteamID] = account.AccountBalance; } else { //Add this to the general list GridMarket.PlayerAccounts[account.SteamID] = account.AccountBalance + GridMarket.PlayerAccounts[account.SteamID]; } } } Accounts accounts = new Accounts(); accounts.PlayerAccounts = GridMarket.PlayerAccounts; try { FileSaver.Save(Path.Combine(Hangar.Dir, "PlayerAccounts.json"), accounts); //File.WriteAllText(Path.Combine(Main.Dir, "PlayerAccounts.json"), JsonConvert.SerializeObject(accounts)); } catch (Exception a) { Hangar.Debug("IO Exception!", a, Hangar.ErrorType.Warn); } /* * //Need to check to see if the player is online! * //var first = RecievedData.BalanceUpdate.First(); * //ulong key = first.Key; * //long val = first.Value; * * * * List<IMyPlayer> Seller = new List<IMyPlayer>(); * //MyAPIGateway.Players.GetPlayers(Seller, x => x.SteamUserId == Item.Steamid); * MyAPIGateway.Players.GetPlayers(Seller, x => x.SteamUserId == key); * * if (Seller.Count == 1) * { * //Player is online! Attempt to change player balance! * Seller[0].RequestChangeBalance(val); * * //Send message to server that play is online! * CrossServerMessage PlayerOnlineMessage = new CrossServerMessage(); * PlayerOnlineMessage.Type = CrossServer.MessageType.PlayerOnline; * //PlayerOnlineMessage.BalanceUpdate.Add(key, val); * * Update(PlayerOnlineMessage); * } * else * { * //Player is offline. Check to see if this server already has the player! (Remove and add new!) * if (Main.PlayerAccountUpdate.ContainsKey(key)) * { * //Remove old Key * Main.PlayerAccountUpdate.Remove(key); * } * * Main.PlayerAccountUpdate.Add(key, val); * * Server.BroadcastLine(e.MessageString); * } */ } else if (RecievedData.Type == MessageType.RequestAll) { CrossServerMessage AllData = new CrossServerMessage(); //AllData.GridDefinition = Main.GridDefinition; AllData.List = GridMarket.GridList; //AllData.BalanceUpdate = Main.PlayerAccounts; AllData.Type = MessageType.RequestAll; string AllDataString = JsonConvert.SerializeObject(AllData); Server.BroadcastLine(AllDataString); } //File.WriteAllText(Path.Combine(Main.Dir, "Market.json"), FileData); //throw new NotImplementedException(); }