public static string ToStr(this SerializableGameState info) { StringBuilder builder = new StringBuilder(); builder.AppendLine(string.Format("SerializableGameState.ActorCounter={0}", info.ActorCounter)); builder.AppendLine(string.Format("SerializableGameState.CheckUserOnJoin={0}", info.CheckUserOnJoin)); builder.AppendLine(string.Format("SerializableGameState.DeleteCacheOnLeave={0}", info.DeleteCacheOnLeave)); builder.AppendLine(string.Format("SerializableGameState.EmptyRoomTTL={0}", info.EmptyRoomTTL)); builder.AppendLine(string.Format("SerializableGameState.IsOpen={0}", info.IsOpen)); builder.AppendLine(string.Format("SerializableGameState.IsVisible={0}", info.IsVisible)); builder.AppendLine(string.Format("SerializableGameState.LobbyId={0}", info.LobbyId)); builder.AppendLine(string.Format("SerializableGameState.LobbyType={0}", info.LobbyType)); builder.AppendLine(string.Format("SerializableGameState.MaxPlayers={0}", info.MaxPlayers)); builder.AppendLine(string.Format("SerializableGameState.PlayerTTL={0}", info.PlayerTTL)); builder.AppendLine(string.Format("SerializableGameState.PublishUserId={0}", info.PublishUserId)); builder.AppendLine(string.Format("SerializableGameState.Slice={0}", info.Slice)); builder.AppendLine(string.Format("SerializableGameState.SuppressRoomEvents={0}", info.SuppressRoomEvents)); builder.AppendLine(info.ActorList.ToStr <SerializableActor>("SerializableGameState.ActorList", ToStr)); builder.AppendLine(info.Binary.ToStr <string, object>("SerializableGameState.Binary")); builder.AppendLine(info.CustomProperties.ToStr <string, object>("SerializableGameState.CustomProperties")); builder.AppendLine(info.DebugInfo.ToStr <string, object>("SerializableGameState.DebugInfo")); builder.AppendLine(info.ExcludedActors.ToStr <ExcludedActorInfo>("SerializableGameState.ExcludedActors", ToStr)); builder.AppendLine(info.ExpectedUsers.ToStr("SerializableGameState.ExpectedUsers")); builder.AppendLine(info.LobbyProperties.ToStr("SerializableGameState.LobbyProperties")); return(builder.ToString()); }
public bool SetState(SerializableGameState state) { this.ActorsManager.ActorNumberCounter = state.ActorCounter; if (state.ActorList != null) { this.ActorsManager.DeserializeActors(state.ActorList); } if (!this.SetGameStateUencodedBinaryPart(state.Binary)) { return(false); } // - we now inlcude all properties in the binary state // - and decided it was confusing to loose type information // - so this filed is only for "Info" purposes // - only ignoring for now. if (state.EmptyRoomTTL != 0) { this.EmptyRoomLiveTime = state.EmptyRoomTTL; } this.IsOpen = state.IsOpen; this.IsVisible = state.IsVisible; this.LobbyId = state.LobbyId; this.LobbyType = (AppLobbyType)state.LobbyType; if (state.LobbyProperties != null) { this.LobbyProperties = new HashSet <object>(state.LobbyProperties.ToArray()); } if (state.MaxPlayers != 0) { this.MaxPlayers = state.MaxPlayers; } if (state.PlayerTTL != 0) { this.PlayerTTL = state.PlayerTTL; } this.EventCache.Slice = state.Slice; this.RoomFlags = state.RoomFlags; this.Properties.DeleteNullProps = (this.RoomFlags & RoomOptionFlags.DeleteNullProps) != 0; this.Properties.Set((byte)GameParameter.MasterClientId, 0); this.ActorsManager.ExcludedActors = state.ExcludedActors ?? new List <ExcludedActorInfo>(); this.ActorsManager.ExpectedUsers = state.ExpectedUsers ?? new List <string>(); if (state.ExpectedUsers != null && state.ExpectedUsers.Count > 0) { this.Properties.Set((byte)GameParameter.ExpectedUsers, state.ExpectedUsers.ToArray()); } return(true); }
public SerializableGameState GetSerializableGameState() { const bool withDebugInfo = true; Dictionary <string, object> customProperties; ArrayList lobbyProperties; var properties = PrepareProperties(out customProperties, out lobbyProperties); Dictionary <byte, ArrayList> actorGroups = null; int evCount; Dictionary <int, ArrayList> events; var binary = GetBinaryPartOfGameState(properties, ref actorGroups, out evCount, out events); var state = new SerializableGameState { ActorCounter = this.ActorsManager.ActorNumberCounter, ActorList = this.ActorsManager.SerializeActors(withDebugInfo), CheckUserOnJoin = this.CheckUserOnJoin, CustomProperties = customProperties, DeleteCacheOnLeave = this.DeleteCacheOnLeave, EmptyRoomTTL = this.EmptyRoomLiveTime, IsOpen = this.IsOpen, IsVisible = this.IsVisible, LobbyId = this.LobbyId, LobbyType = (int)this.LobbyType, LobbyProperties = lobbyProperties, MaxPlayers = this.MaxPlayers, PlayerTTL = this.PlayerTTL, SuppressRoomEvents = this.SuppressRoomEvents, Slice = this.EventCache.Slice, Binary = binary, ExcludedActors = this.actorsManager.ExcludedActors, ExpectedUsers = this.ActorsManager.ExpectedUsers, }; if (withDebugInfo) { state.DebugInfo = new Dictionary <string, object>(); if (properties.Count > 0) { state.DebugInfo.Add("DEBUG_PROPERTIES_18", properties); } if (evCount > 0) { state.DebugInfo.Add("DEBUG_EVENTS_19", events); } if (actorGroups != null && actorGroups.Count > 0) { state.DebugInfo.Add("DEBUG_GROUPS_20", actorGroups); } } return(state); }
private void WriteGameStateToFile(string name, SerializableGameState state) { var tempPath = System.IO.Path.GetTempPath(); var fileName = System.IO.Path.Combine(tempPath, name + ".txt"); var strState = JsonConvert.SerializeObject(state); // Append text to an existing file named "WriteLines.txt". using (var outputFile = new StreamWriter(fileName, false)) { outputFile.WriteLine(strState); outputFile.Flush(); } }
public bool SetState(Dictionary <string, object> state) { if (state.Keys.Contains("0")) { Log.ErrorFormat("Old style of serializaed data are used"); return(false); } var serializedState = new SerializableGameState(); foreach (var entry in state) { // TBD - improve performance using a Dictionary<String,YourEnum>, // see http://stackoverflow.com/questions/16100/how-do-i-convert-a-string-to-an-enum-in-c HiveHostGameState key; try { key = (HiveHostGameState)Enum.Parse(typeof(HiveHostGameState), entry.Key, false); } catch (ArgumentException) { continue; } switch (key) { case HiveHostGameState.ActorCounter: // this.ActorsManager.ActorNumberCounter = (int)entry.Value; serializedState.ActorCounter = (int)entry.Value; break; case HiveHostGameState.ActorList: { //var list = entry.Value as IList; //this.ActorsManager.DeserializeActors(list); var list = entry.Value as IList; serializedState.ActorList = list.Cast <Dictionary <string, object> >().Select(d => d.ToSerializableActor()).ToList(); } break; case HiveHostGameState.CustomProperties: // TBD - we now inlcude all properties in the binary state // - and decided it was confusing to loose type information // - so this filed is only for "Info" purposes // - only ignoring for now. // this.Properties.SetProperties((IDictionary)entry.Value); break; case HiveHostGameState.CheckUserOnJoin: //this.CheckUserOnJoin = (bool)entry.Value; serializedState.CheckUserOnJoin = (bool)entry.Value; break; case HiveHostGameState.DeleteCacheOnLeave: //this.DeleteCacheOnLeave = (bool)entry.Value; serializedState.DeleteCacheOnLeave = (bool)entry.Value; break; case HiveHostGameState.EmptyRoomTTL: // this.EmptyRoomLiveTime = (int)entry.Value; serializedState.EmptyRoomTTL = (int)entry.Value; break; case HiveHostGameState.IsOpen: // this.IsOpen = (bool)entry.Value; serializedState.IsOpen = (bool)entry.Value; break; case HiveHostGameState.IsVisible: //this.IsVisible = (bool)entry.Value; serializedState.IsVisible = (bool)entry.Value; break; case HiveHostGameState.LobbyId: serializedState.LobbyId = (string)entry.Value; // this.LobbyId = (string)entry.Value; break; case HiveHostGameState.LobbyType: serializedState.LobbyType = (int)(AppLobbyType)entry.Value; // this.LobbyType = (AppLobbyType)entry.Value; break; case HiveHostGameState.LobbyProperties: //var lobbyProperties = entry.Value as ArrayList; //if (lobbyProperties != null) //{ // this.LobbyProperties = new HashSet<object>((lobbyProperties).ToArray()); //} serializedState.LobbyProperties = entry.Value as ArrayList; break; case HiveHostGameState.MaxPlayers: // this.MaxPlayers = Convert.ToByte(entry.Value); serializedState.MaxPlayers = Convert.ToByte(entry.Value); break; case HiveHostGameState.PlayerTTL: // this.PlayerTTL = (int)entry.Value; serializedState.PlayerTTL = (int)entry.Value; break; case HiveHostGameState.SuppressRoomEvents: // this.SuppressRoomEvents = (bool)entry.Value; serializedState.SuppressRoomEvents = (bool)entry.Value; break; case HiveHostGameState.Slice: serializedState.Slice = (int)entry.Value; // this.EventCache.Slice = (int)entry.Value; break; case HiveHostGameState.Binary: //var uencodedBinaryState = (Dictionary<string, object>)entry.Value; //this.SetGameStateUencodedBinaryPart(uencodedBinaryState); serializedState.Binary = (Dictionary <string, object>)entry.Value; break; } } return(SetState(serializedState)); }
public bool SetGameState(SerializableGameState state) { return(this.host.SetGameState(state)); }