using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Threading;
using PlayerIOClient;
using WorldData;
namespace ConsolEE
{
class Program
{
static Dictionary<int, Player> Players = new Dictionary<int, Player>();
static World world = new World();
static Connection con;
static Client cli;
static bool connected = false;
static void Main(string[] args)
{
string email, pass, worldid;
#if !DEBUG// release
Console.Write("Email: ");
email = Console.ReadLine();
Console.Write("Password: ");
pass = Console.ReadLine();
Console.Write("WorldID: ");
worldid = Console.ReadLine();
#else // debug
email = "snip";
pass = "snip";
worldid = "snip";
#endif
Console.Clear();
try
{
const string Gameid = "everybody-edits-su9rn58o40itdbnw69plyw";
const string Roomtype = "Everybodyedits";
string version;
cli = PlayerIO.QuickConnect.SimpleConnect(Gameid, email, pass, null);
version = cli.BigDB.Load("config", "config")["version"].ToString();
con = cli.Multiplayer.CreateJoinRoom(worldid, Roomtype + version, true, null, null);
}
finally { email = pass = new String('\0', email.Length + pass.Length); email = pass = null; }
con.OnDisconnect += delegate (object s, string str)
{
lock (LockObj)
{
//var prevloc = new Point(Console.CursorLeft, Console.CursorTop);
//var prevcolor = Console.ForegroundColor;
Console.SetCursorPosition(0, 0);
Console.ForegroundColor = ConsoleColor.Red;
Console.BackgroundColor = ConsoleColor.Black;
Console.Write($"Disconnected: {str} ");
//Console.SetCursorPosition(prevloc.X, prevloc.Y);
//Console.ForegroundColor = prevcolor;
}
};
con.OnMessage += OnMessage;
con.Send("init");
while (!connected) { Thread.Sleep(1); }
//Console.ReadKey(true);
Plr.x = 1; Plr.y = 1;
UpdateWorld(true);
Console.CursorVisible = false;
con.Send("god", true);
int block = Blocks[1];
for (;;)
{
lock (LockObj)
{
//Console.BackgroundColor = world.Data[Plr.x, Plr.y, 0].Id != 0 ? GetColorFromId((int)world.Data[Plr.x, Plr.y, 0].Id) : ConsoleColor.Black;
Console.BackgroundColor = GetColorFromId((int)world.Data[Plr.x, Plr.y, 0].Id);
Console.SetCursorPosition(Plr.x * 2, Plr.y);
Console.Write("[]");
}
con.Send("m", Plr.x * 16, Plr.y * 16, 0, 0, 0, 0, 0, 0, 0, false, false, 0);
var key = Console.ReadKey(true).Key;
lock (LockObj)
{
//Console.BackgroundColor = world.Data[Plr.x, Plr.y, 0].Id != 0 ? GetColorFromId((int)world.Data[Plr.x, Plr.y, 0].Id) : ConsoleColor.Black;
Console.BackgroundColor = GetColorFromId((int)world.Data[Plr.x, Plr.y, 0].Id);
Console.SetCursorPosition(Plr.x * 2, Plr.y);
Console.Write(" ");
}
switch (key)
{//movements/block placement
case ConsoleKey.W:
case ConsoleKey.UpArrow:
if (Plr.y <= 0) { break; }
Plr.y--;
break;
case ConsoleKey.D:
case ConsoleKey.RightArrow:
if (Plr.x >= world.Size.X - 1) { break; }
Plr.x++;
break;
case ConsoleKey.S:
case ConsoleKey.DownArrow:
if (Plr.y >= world.Size.Y - 1) { break; }
Plr.y++;
break;
case ConsoleKey.A:
case ConsoleKey.LeftArrow:
if (Plr.x <= 0) { break; }
Plr.x--;
break;
case ConsoleKey.Spacebar:
con.Send("b", 0, Plr.x, Plr.y, world.Data[Plr.x, Plr.y, 0].Id != 0 ? 0 : block);
break;
default:
if (key >= ConsoleKey.D0 && key <= ConsoleKey.D9)
{
if (key == ConsoleKey.D0) key = ConsoleKey.D9 + 1; // hack
block = Blocks[(int)key - (int)ConsoleKey.D0 - 1];
}
break;
}
}
//for (;;) { Console.ReadLine(); }
}
struct Plr
{
public static int x;
public static int y;
}
//const ConsoleColor BlockColor = ConsoleColor.Gray;
static readonly int[] Blocks = { 1088, 9, 182, 12, 1018, 13, 14, 15, 10, 11 };
static readonly ConsoleColor[] Colors = { ConsoleColor.White, ConsoleColor.Gray, ConsoleColor.DarkGray, ConsoleColor.Red, ConsoleColor.DarkRed, ConsoleColor.Yellow, ConsoleColor.Green, ConsoleColor.Cyan, ConsoleColor.Blue, ConsoleColor.Magenta };
const ConsoleColor PlayerColor = ConsoleColor.Green;
private static void OnMessage(object sender, Message m)
{
world.Parse(m);
switch (m.Type)
{
case "init":
////if (m.GetInt(18) + m.GetInt(19) != 100) // ok no
//if (!((m.GetInt(18) == 50 && m.GetInt(19) == 50) || (m.GetInt(18) == 25 || m.GetInt(19) == 25)))
//{// ^ if not width and height is 50 or width and height is 25
// Console.SetCursorPosition(0, 20);
// Console.ForegroundColor = ConsoleColor.Red;
// Console.BackgroundColor = ConsoleColor.Black;
// Console.WriteLine("Error: Only worlds 25x25 and 50x50 are allowed!");
// con.Disconnect();
// return;
//} // actually, why not allow joining all worlds now that coordinates aren't hardcoded?
if (m.GetString(0) != m.GetString(13))
{
Console.SetCursorPosition(0, 2);
Console.ForegroundColor = ConsoleColor.Red;
Console.BackgroundColor = ConsoleColor.Black;
Console.WriteLine("Error: Not a world owner!");
con.Disconnect();
}
{//resize console window to the size of the world
//const int x = 100, y = 51;
int x = m.GetInt(18) * 2, y = m.GetInt(19) + 1;
try
{
Console.SetWindowSize(x, y);
Console.SetBufferSize(x, y);
}
catch (ArgumentOutOfRangeException)
{
try
{
Console.SetBufferSize(x, y);
Console.SetWindowSize(x, y);
}
catch (ArgumentOutOfRangeException)
{
Console.SetBufferSize(x, y);
}
}
Console.SetCursorPosition(1, y - 1);
int i = 1;
foreach (var val in Colors)
{
Console.ForegroundColor = val;
if (i == 10) i = 0;
Console.Write(i++);
}
Console.ForegroundColor = ConsoleColor.DarkGray;
Console.Write(' ');
Console.Write(new String('-', x - 14));
Console.ForegroundColor = PlayerColor;
}
Players.Add(m.GetInt(5), new Player(m.GetInt(5), m.GetString(13)));
con.Send("init2");
break;
case "init2":
connected = true;
break;
case "add":
Players.Add(m.GetInt(0), new Player(m.GetInt(0), m.GetString(1)));
break;
case "left":
Players.Remove(m.GetInt(0));
break;
case "b":
if (m.GetInt(0) != 0) { break; }
ToggleBlock(m.GetInt(1), m.GetInt(2), GetColorFromId(m.GetInt(3)));
break;
case "reset":
case "clear":
UpdateWorld();
break;
}
}
static void UpdateWorld(bool ignoreempty = false)
{
Point prevpos = new Point(Console.CursorLeft, Console.CursorTop);
for (int y = 0; y < world.Size.X; y++)
{
for (int x = 0; x < world.Size.Y; x++)
{
if (!ignoreempty || world.Data[x, y, 0].Id != 0)
{
ToggleBlock(x, y, GetColorFromId((int)world.Data[x, y, 0].Id), false);
}
}
}
Console.SetCursorPosition(prevpos.X, prevpos.Y);
}
static void ToggleBlock(int x, int y, ConsoleColor block, bool restorepos = true)
{
lock (LockObj)
{
Point prevpos = new Point(0xdead, 0xbeef);
if (restorepos) { prevpos = new Point(Console.CursorLeft, Console.CursorTop); }
Console.SetCursorPosition(x * 2, y);
Console.BackgroundColor = block;
if (x == Plr.x && y == Plr.y) { Console.Write("[]"); } else { Console.Write(" "); }
if (restorepos) { Console.SetCursorPosition(prevpos.X, prevpos.Y); }
}
}
static ConsoleColor GetColorFromId(int id)
{
switch (id)
{
case 1088: return ConsoleColor.White;
case 9: return ConsoleColor.Gray;
case 182: return ConsoleColor.DarkGray;
case 12: return ConsoleColor.Red;
case 1018: return ConsoleColor.DarkRed;
case 13: return ConsoleColor.Yellow;
case 14: return ConsoleColor.Green;
case 15: return ConsoleColor.Cyan;
case 10: return ConsoleColor.Blue;
case 11: return ConsoleColor.Magenta;
default: return ConsoleColor.DarkYellow; // undefined
case 0: return ConsoleColor.Black;
}
}
private static readonly object LockObj = new object();
//class Helper
//{
// private static object _obj = new object();// S2 o thearsd cando nost omeng thiwicoth nsoat le ths e amte ime
// public static void SafeInvoke(Action act) // So 2 threads cannot do something with console at the same time
// {
// lock (_obj)
// {
// act();
// }
// }
//}
struct Point
{
public Point(int x, int y) //: this()
{
this.X = x;
this.Y = y;
}
public int X;
public int Y;
}
struct Player // idk why
{
public Player(int id = -1, string username = null)
{
this.id = id;
this.username = username;
}
public int id;
public string username;
}
}
}