Skip to content

Instantly share code, notes, and snippets.

@monoclex
Created September 10, 2018 16:49
Show Gist options
  • Save monoclex/a6e274bddf92a3baede7f991b90efeb7 to your computer and use it in GitHub Desktop.
Save monoclex/a6e274bddf92a3baede7f991b90efeb7 to your computer and use it in GitHub Desktop.
using Decorator;
using Decorator.Attributes;
using PlayerIOClient;
using System;
namespace demo {
[Message("say")]
public class Chat {
[Position(0), Required]
public int PlayerId { get; set; }
[Position(1), Required]
public string Message { get; set; }
}
[Message("updatemeta")]
public class UpdateMeta {
[Position(0), Required]
public string OwnerName { get; set; }
[Position(1), Required]
public string Title { get; set; }
[Position(2), Required]
public int Plays { get; set; }
[Position(3), Required]
public int Favorites { get; set; }
[Position(4), Required]
public int Likes { get; set; }
}
public class Program {
private static void Main(string[] args)
=> new Program().Run(args);
public Connection Con { get; set; }
[DeserializedHandler]
public void OnSay(Chat msg) {
Console.WriteLine($"Recieved chat: {msg.Message}");
}
[DeserializedHandler]
public void OnUpdateMeta(UpdateMeta updateMeta) {
Console.WriteLine($"Meta updated: \n" +
$"Owner: {updateMeta.OwnerName}\n" +
$"Title: {updateMeta.Title}\n" +
$"Plays: {updateMeta.Plays}\n" +
$"Favorites: {updateMeta.Favorites}\n" +
$"Likes: {updateMeta.Likes}\n");
}
public void Run(string[] args) {
this.Con = PlayerIO
.QuickConnect
.SimpleConnect("everybody-edits-su9rn58o40itdbnw69plyw", "guest", "guest", null)
.Multiplayer
.CreateJoinRoom("PW", "Everybodyedits240", false, null, null);
this.Con.OnMessage += (s, m) => {
if (m.Type == "init")
this.Con.Send("init2");
Deserializer<Program>.DeserializeMessageToMethod(this, new BasicMessage(m.Type, m.GetObjects()));
};
this.Con.Send("init");
Console.ReadLine();
}
}
internal static class Helper {
public static object[] GetObjects(this Message m) {
var res = new object[m.Count];
for (var i = 0u; i < m.Count; i++)
res[(int)i] = m[i];
return res;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment