-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathExtensions.cs
104 lines (83 loc) · 2.32 KB
/
Extensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
using System;
using System.Collections.Generic;
using System.Linq;
using Newtonsoft.Json;
namespace Hunt.Common
{
public static class Extensions
{
public static string TrimTo(this string s, int maxChars)
{
if(s.Length <= maxChars)
return s;
return s.Substring(0, maxChars - 3) + "...";
}
public static T Get<T>(this IList<T> list, string id) where T : BaseModel
{
return list.SingleOrDefault(t => t.Id == id);
}
public static string ToJson(this object o)
{
if(o == null)
return null;
return JsonConvert.SerializeObject(o);
}
public static T ToObject<T>(this string json)
{
if(string.IsNullOrWhiteSpace(json))
return default(T);
return JsonConvert.DeserializeObject<T>(json);
}
/// <summary>
/// Checks to see if the current player's team has acquired all treasures
/// </summary>
public static bool EvaluateGameForWinner(this Game game, string teamId)
{
var team = game.Teams.SingleOrDefault(t => t.Id == teamId);
if(team == null)
return false;
foreach(var t in game.Treasures)
{
var exists = team.AcquiredTreasure.Any(at => at.TreasureId == t.Id);
if(!exists)
return false;
}
return true;
}
public static Player[] GetAllPlayers(this Game game)
{
var list = new List<Player>();
list.Add(game.Coordinator);
var players = (from t in game.Teams
from p in t.Players
select p).ToArray();
list.AddRange(players);
return list.ToArray();
}
public static Player GetPlayer(this Game game, string id)
{
if(game.Coordinator?.Id.Equals(id, StringComparison.OrdinalIgnoreCase) == true)
return game.Coordinator;
foreach(var team in game.Teams)
foreach(var player in team.Players)
if(player.Id.Equals(id, StringComparison.OrdinalIgnoreCase) == true)
return player;
return null;
}
public static Team GetTeam(this Game game, Player player)
{
if(game == null || player == null)
return null;
return game.Teams.SingleOrDefault(t => t.Players.Exists(p => p.Id == player.Id));
}
public static bool IsCoordinator(this Game game, Player player)
{
if(game == null || player == null || player.Id == null)
return false;
return game.Coordinator.Id.Equals(player.Id, StringComparison.CurrentCultureIgnoreCase);
}
}
public class KVP : Dictionary<string, string>
{
}
}