Skip to content

Instantly share code, notes, and snippets.

@arturaz
Last active February 5, 2019 18:31
Show Gist options
  • Save arturaz/baa2b7404b9175cf4845b7174379aa50 to your computer and use it in GitHub Desktop.
Save arturaz/baa2b7404b9175cf4845b7174379aa50 to your computer and use it in GitHub Desktop.
// photon does not serialize ulong by default
public static readonly IPhotonProperty<QGuid> PROP_USER_ID =
PhotonProperty.a(new TypedKey<long>("UserId_p0"))
.add(
PhotonProperty.a(new TypedKey<long>("UserId_p1")),
(l1, l2) => new QGuid((ulong) l1, (ulong) l2),
id => (long) id.long1,
id => (long) id.long2
);
public static readonly IPhotonProperty<float>
PROP_MAP_LOADING_PROGRESS = PhotonProperty.a(new TypedKey<float>("MapLoadingProgress"));
PhotonLogic.PROP_USER_ID.set(localPlayers[0].userId);
foreach (var player in PhotonNetwork.playerList) {
if (
PhotonLogic.PROP_USER_ID.get(player).rightValueOut(out var playerId)
&& playerId == id
) {
return Mathf.Clamp01(PhotonLogic.PROP_MAP_LOADING_PROGRESS.get(player).getOrElse(0));
}
}
[Record] public partial struct TypedKey<Type> {
public readonly string key;
}
public interface IPhotonProperty<A> {
Either<string, A> get(Hashtable ht);
void addToHashTable(Hashtable ht, A a);
}
public static class PhotonProperty {
public static IPhotonProperty<A> a<A>(TypedKey<A> key) =>
new PhotonProperty<A>(key);
public static Either<string, A> get<A>(
this IPhotonProperty<A> property, PhotonPlayer player
) => property.get(player.CustomProperties);
public static void set<A>(this IPhotonProperty<A> property, A a) =>
property.set(a, new Hashtable());
public static void set<A>(this IPhotonProperty<A> property, A a, Hashtable ht) {
property.addToHashTable(ht, a);
PhotonNetwork.SetPlayerCustomProperties(ht);
}
public static IPhotonProperty<B> add<A1, A2, B>(
this IPhotonProperty<A1> p1,
IPhotonProperty<A2> p2,
Fn<A1, A2, B> getB, Fn<B, A1> getA1, Fn<B, A2> getA2
) => new JoinedPhotonProperty<A1,A2,B>(p1, p2, getB, getA1, getA2);
}
public class PhotonProperty<A> : IPhotonProperty<A> {
public readonly TypedKey<A> key;
public PhotonProperty(TypedKey<A> key) {
this.key = key;
}
public Either<string, A> get(Hashtable ht) =>
ht.getE(key.key).flatMapRight(o => o.cast().toE<A>());
public void addToHashTable(Hashtable ht, A a) => ht.Add(key, a);
}
public class JoinedPhotonProperty<A1, A2, B> : IPhotonProperty<B> {
readonly IPhotonProperty<A1> p1;
readonly IPhotonProperty<A2> p2;
readonly Fn<A1, A2, B> getB;
readonly Fn<B, A1> getA1;
readonly Fn<B, A2> getA2;
public JoinedPhotonProperty(
IPhotonProperty<A1> p1, IPhotonProperty<A2> p2,
Fn<A1, A2, B> b, Fn<B, A1> a1, Fn<B, A2> a2
) {
this.p1 = p1;
this.p2 = p2;
getB = b;
getA1 = a1;
getA2 = a2;
}
public Either<string, B> get(Hashtable ht) {
var a1E = p1.get(ht);
if (a1E.isLeft) return a1E.__unsafeGetLeft;
var a2E = p2.get(ht);
if (a2E.isLeft) return a2E.__unsafeGetLeft;
return getB(a1E.__unsafeGetRight, a2E.__unsafeGetRight);
}
public void addToHashTable(Hashtable ht, B b) {
p1.addToHashTable(ht, getA1(b));
p2.addToHashTable(ht, getA2(b));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment