Skip to content

Instantly share code, notes, and snippets.

@miou-gh
Created February 23, 2017 18:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save miou-gh/36036dca2cca655c03f620fd2029fa10 to your computer and use it in GitHub Desktop.
Save miou-gh/36036dca2cca655c03f620fd2029fa10 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using PlayerIOClient;
using System.Text.RegularExpressions;
using System.Diagnostics;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using System.Net.Security;
namespace EEFriendsGuess
{
class Program
{
static void Main(string[] args)
{
do
{
Console.Write("Username: ");
var username = Console.ReadLine();
var user = new Friend(username, null, Friend.Options.FullDepth);
var ids = user.Likes.Select(x => x.Owner).Concat(user.Favorites.Select(x => x.Owner)).Distinct();
var friends = ids.Select(x => new Friend(null, x, Friend.Options.NoDepth)).ToList();
foreach (var friend in friends)
{
friend.Value += user.Likes.Count(x => friend.Worlds.Any(w => w.Id == x.Id));
friend.Value += user.Favorites.Count(x => friend.Worlds.Any(w => w.Id == x.Id));
friend.Value += friend.Likes.Count(x => user.Worlds.Any(w => w.Id == x.Id));
friend.Value += friend.Favorites.Count(x => user.Worlds.Any(w => w.Id == x.Id));
}
Console.WriteLine(friends.OrderByDescending(x => x.Value).Where(x => !string.IsNullOrEmpty(x.Username)).ToStringTable(new[] { "Username", "Value" }, u => u.Username, u => u.Value));
} while (true);
}
private class Friend
{
public string UserId { get; set; }
public string Username { get; set; }
public int Value { get; set; } = 0;
public List<World> Likes { get; set; } = new List<World>();
public List<World> Favorites { get; set; } = new List<World>();
public List<World> Worlds { get; set; } = new List<World>();
public enum Options { FullDepth, NoDepth }
public Friend(string Username = null, string UserId = null, Options options = Options.NoDepth)
{
this.Username = Username;
this.UserId = UserId;
if (string.IsNullOrEmpty(Username) && !string.IsNullOrEmpty(UserId))
this.Username = Global.Client.BigDB.LoadSingle("usernames", "owner", new[] { UserId })?.Key;
if (string.IsNullOrEmpty(UserId) && !string.IsNullOrEmpty(Username))
this.UserId = Global.Client.BigDB.Load("usernames", Username)?["owner"] as string;
if (this.UserId == null)
return;
var playerobject = Global.Client.BigDB.Load("PlayerObjects", this.UserId);
if (playerobject == null)
return;
if (playerobject.Contains("myworldnames"))
foreach (var world in (DatabaseObject)playerobject["myworldnames"])
this.Worlds.Add(new World(world.Key, 0));
if (playerobject.Contains("likes"))
if (playerobject.GetObject("likes").Count > 0)
this.Likes.AddRange(playerobject.GetObject("likes").Select(x => new World(x.Key, (options == Options.FullDepth) ? 1 : 0)));
if (playerobject.Contains("favorites"))
if (playerobject.GetObject("favorites").Count > 0)
this.Favorites.AddRange(playerobject.GetObject("favorites").Select(x => new World(x.Key, (options == Options.FullDepth) ? 1 : 0)));
}
}
private class World
{
public string Id { get; set; } = "";
public string Owner { get; set; } = "";
public World(string Id, int Depth = 0)
{
this.Id = Id;
if (Depth > 0)
{
var world = Global.Client.BigDB.Load("worlds", Id);
if (world == null)
return;
this.Id = Id;
this.Owner = world["owner"] as string;
Debug.WriteLine("Found world: " + this.Id);
}
}
}
private static class Global
{
public static Client Client = PlayerIO.QuickConnect.SimpleConnect("everybody-edits-su9rn58o40itdbnw69plyw", "guest", "guest", null);
}
}
public static class TableParser
{
public static string ToStringTable<T>(
this IEnumerable<T> values,
string[] columnHeaders,
params Func<T, object>[] valueSelectors)
{
return ToStringTable(values.ToArray(), columnHeaders, valueSelectors);
}
public static string ToStringTable<T>(
this T[] values,
string[] columnHeaders,
params Func<T, object>[] valueSelectors)
{
Debug.Assert(columnHeaders.Length == valueSelectors.Length);
var arrValues = new string[values.Length + 1, valueSelectors.Length];
// Fill headers
for (int colIndex = 0; colIndex < arrValues.GetLength(1); colIndex++)
{
arrValues[0, colIndex] = columnHeaders[colIndex];
}
// Fill table rows
for (int rowIndex = 1; rowIndex < arrValues.GetLength(0); rowIndex++)
{
for (int colIndex = 0; colIndex < arrValues.GetLength(1); colIndex++)
{
arrValues[rowIndex, colIndex] = valueSelectors[colIndex]
.Invoke(values[rowIndex - 1]).ToString();
}
}
return ToStringTable(arrValues);
}
public static string ToStringTable(this string[,] arrValues)
{
int[] maxColumnsWidth = GetMaxColumnsWidth(arrValues);
var headerSpliter = new string('-', maxColumnsWidth.Sum(i => i + 3) - 1);
var sb = new StringBuilder();
for (int rowIndex = 0; rowIndex < arrValues.GetLength(0); rowIndex++)
{
for (int colIndex = 0; colIndex < arrValues.GetLength(1); colIndex++)
{
// Print cell
string cell = arrValues[rowIndex, colIndex];
cell = cell.PadRight(maxColumnsWidth[colIndex]);
sb.Append(" | ");
sb.Append(cell);
}
// Print end of line
sb.Append(" | ");
sb.AppendLine();
// Print splitter
if (rowIndex == 0)
{
sb.AppendFormat(" |{0}| ", headerSpliter);
sb.AppendLine();
}
}
return sb.ToString();
}
private static int[] GetMaxColumnsWidth(string[,] arrValues)
{
var maxColumnsWidth = new int[arrValues.GetLength(1)];
for (int colIndex = 0; colIndex < arrValues.GetLength(1); colIndex++)
{
for (int rowIndex = 0; rowIndex < arrValues.GetLength(0); rowIndex++)
{
int newLength = arrValues[rowIndex, colIndex].Length;
int oldLength = maxColumnsWidth[colIndex];
if (newLength > oldLength)
{
maxColumnsWidth[colIndex] = newLength;
}
}
}
return maxColumnsWidth;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment