public static List <World.Block> FromWorldData(this DatabaseArray input) { var blocks = new List <World.Block>(); if (input == null || input.Count == 0) { return(blocks); } for (int i = 0; i < input.Count; i++) { if (input.Contains(i) && input.GetObject(i).Count != 0) { var obj = input.GetObject(i); dynamic temp = new World.Block(); foreach (var kvp in obj) { temp[kvp.Key] = kvp.Value; } byte[] x = obj.TryGetBytes("x", new byte[0]), y = obj.TryGetBytes("y", new byte[0]); byte[] x1 = obj.TryGetBytes("x1", new byte[0]), y1 = obj.TryGetBytes("y1", new byte[0]); for (int j = 0; j < x1.Length; j++) { temp.Locations.Add(new World.Block.Location() { X = x1[j], Y = y1[j] }); } for (int k = 0; k < x.Length; k += 2) { temp.Locations.Add(new World.Block.Location() { X = (x[k] << 8) + x[k + 1], Y = (y[k] << 8) + y[k + 1] }); } blocks.Add(temp); } } return(blocks); }
public void addLastPlayedMap(String mapKey) { if (isGuest) { return; } DatabaseArray maps = PlayerObject.GetArray("LastPlayedMaps"); DatabaseObject mapEntry = null; DatabaseObject oldestEntry = null; int oldestIndex = 0; for (int i = 0; i < maps.Count; ++i) { DatabaseObject entry = maps.GetObject(i.ToString()); if (entry.Contains("Map")) { if (entry.GetString("Map") == mapKey) { mapEntry = entry; } else { if (oldestEntry == null || DateTime.Compare(entry.GetDateTime("Date"), oldestEntry.GetDateTime("Date")) < 0) { oldestEntry = entry; oldestIndex = i; } } } } // remove oldest entry if list is full //if (maps.Count > 8) { // maps.Remove(oldestIndex.ToString()); //} // if current map is already in last played list, just update date if (mapEntry != null) { mapEntry.Set("Date", DateTime.Now); } else { DatabaseObject entry = new DatabaseObject(); entry.Set("Map", mapKey); entry.Set("Date", DateTime.Now); maps.Add(entry); } PlayerObject.Remove("LastPlayedMaps"); //sort the array from the latest to oldest DatabaseArray newMaps = new DatabaseArray(); int newestIndex = 0; bool found = false; do { found = false; newestIndex = 0; DatabaseObject newestEntry = null; //foreach(DatabaseObject entry in maps) { for (int i = 0; i < maps.Count; ++i) { if (!maps.Contains(i.ToString())) { continue; } DatabaseObject entry = maps.GetObject(i.ToString()); if (!entry.Contains("Date")) { continue; } if (newestEntry == null || DateTime.Compare(entry.GetDateTime("Date"), newestEntry.GetDateTime("Date")) > 0) { newestEntry = entry; newestIndex = i; found = true; } } if (found) { DatabaseObject tmp = maps.GetObject(newestIndex.ToString()); maps.Remove(newestIndex.ToString()); bool duplicate = false; foreach (DatabaseObject dbo in newMaps) { if (dbo.GetString("Map") == tmp.GetString("Map")) { duplicate = true; break; } } if (duplicate) { continue; } DatabaseObject tmp2 = new DatabaseObject(); tmp2.Set("Map", tmp.GetString("Map")); tmp2.Set("Date", tmp.GetDateTime("Date")); //newMaps.Add(tmp2); newMaps.Insert(0, tmp2); } } while (found && newMaps.Count < 8); //newMaps = (DatabaseArray)newMaps.Reverse(); PlayerObject.Set("LastPlayedMaps", newMaps); /*maps = PlayerObject.GetArray("LastPlayedMaps"); * for (int i = 0; i < maps.Count; i++) { * Console.WriteLine(maps.GetObject(i).GetString("Map")); * }*/ savePlayerData(); }