Client LuaCsForBarotrauma
FriendInfo.cs
1 #nullable enable
2 using System;
4 
5 namespace Barotrauma;
6 
7 sealed class FriendInfo : IDisposable
8 {
9  public readonly string Name;
10  public readonly AccountId Id;
11 
12  public readonly FriendStatus CurrentStatus;
13  public readonly string ServerName;
14  public readonly Option<ConnectCommand> ConnectCommand;
15 
16  public readonly FriendProvider Provider;
17  public Option<Sprite> Avatar { get; set; }
18 
19  public bool IsInServer
20  => CurrentStatus == FriendStatus.PlayingBarotrauma && ConnectCommand.IsSome();
21 
22  public bool IsOnline
23  => CurrentStatus != FriendStatus.Offline;
24 
25  public LocalizedString StatusText
26  => CurrentStatus switch
27  {
28  FriendStatus.Offline => "",
29  _ when ConnectCommand.IsSome()
30  => TextManager.GetWithVariable("FriendPlayingOnServer", "[servername]", ServerName),
31  _ => TextManager.Get($"Friend{CurrentStatus}")
32  };
33 
34  public FriendInfo(string name, AccountId id, FriendStatus status, string serverName, Option<ConnectCommand> connectCommand, FriendProvider provider)
35  {
36  Name = name;
37  Id = id;
38  CurrentStatus = status;
39  ServerName = serverName;
40  ConnectCommand = connectCommand;
41  Provider = provider;
42  Avatar = Option.None;
43  }
44 
45  public void RetrieveOrInheritAvatar(Option<Sprite> inheritableAvatar, int size)
46  {
47  if (Avatar.IsSome()) { return; }
48 
49  if (inheritableAvatar.IsSome())
50  {
51  Avatar = inheritableAvatar;
52  return;
53  }
54 
55  TaskPool.Add(
56  "RetrieveAvatar",
57  Provider.RetrieveAvatar(this, size),
58  t =>
59  {
60  if (!t.TryGetResult(out Option<Sprite> spr)) { return; }
61  Avatar = Avatar.Fallback(spr);
62  });
63  }
64 
65  public void Dispose()
66  {
67  if (Avatar.TryUnwrap(out var avatar))
68  {
69  avatar.Remove();
70  }
71  Avatar = Option.None;
72  }
73 }
FriendInfo(string name, AccountId id, FriendStatus status, string serverName, Option< ConnectCommand > connectCommand, FriendProvider provider)
Definition: FriendInfo.cs:34
readonly FriendProvider Provider
Definition: FriendInfo.cs:16
readonly FriendStatus CurrentStatus
Definition: FriendInfo.cs:12
readonly string Name
Definition: FriendInfo.cs:9
readonly Option< ConnectCommand > ConnectCommand
Definition: FriendInfo.cs:14
bool IsOnline
Definition: FriendInfo.cs:23
Option< Sprite > Avatar
Definition: FriendInfo.cs:17
readonly AccountId Id
Definition: FriendInfo.cs:10
readonly string ServerName
Definition: FriendInfo.cs:13
void Dispose()
Definition: FriendInfo.cs:65
LocalizedString StatusText
Definition: FriendInfo.cs:26
void RetrieveOrInheritAvatar(Option< Sprite > inheritableAvatar, int size)
Definition: FriendInfo.cs:45
bool IsInServer
Definition: FriendInfo.cs:20