Skip to content

Commit

Permalink
Merge pull request #143 from daud-io/firebase
Browse files Browse the repository at this point in the history
Firebase
  • Loading branch information
andylippitt authored Mar 3, 2019
2 parents d65f0d3 + 08725b8 commit 20f4d11
Show file tree
Hide file tree
Showing 13 changed files with 140 additions and 17 deletions.
9 changes: 9 additions & 0 deletions Game.Engine/Auditing/AuditEventBase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Game.Engine.Auditing
{
public class AuditEventBase
{
public string Type { get => this.GetType().Name; }
public string PublicURL { get; set; }
public string WorldKey { get; set; }
}
}
21 changes: 21 additions & 0 deletions Game.Engine/Auditing/AuditEventDeath.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
namespace Game.Engine.Auditing
{
using Game.Engine.Core;

public class AuditEventDeath : AuditEventBase
{
public long GameTime { get; set; }
public AuditModelPlayer Killer { get; set; }
public AuditModelPlayer Victim { get; set; }

public AuditEventDeath(Player killer, Player victim, Fleet fleet)
{
GameTime = fleet?.World.Time ?? 0;
PublicURL = fleet?.World?.GameConfiguration?.PublicURL;
WorldKey = fleet?.World?.WorldKey;

Killer = new AuditModelPlayer(killer);
Victim = new AuditModelPlayer(victim);
}
}
}
18 changes: 18 additions & 0 deletions Game.Engine/Auditing/AuditEventSpawn.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
namespace Game.Engine.Auditing
{
using Game.Engine.Core;

public class AuditEventSpawn : AuditEventBase
{
public long GameTime { get; set; }
public AuditModelPlayer Player { get; set; }

public AuditEventSpawn(Player player)
{
GameTime = player?.World?.Time ?? 0;
PublicURL = player?.World?.GameConfiguration?.PublicURL;
WorldKey = player?.World?.WorldKey;
Player = new AuditModelPlayer(player);
}
}
}
33 changes: 33 additions & 0 deletions Game.Engine/Auditing/AuditModelPlayer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
namespace Game.Engine.Auditing
{
using Game.Engine.Core;

public class AuditModelPlayer
{
public ulong LoginID { get; }
public string LoginName { get; set; }
public uint FleetID { get; set; }
public string FleetName { get; set; }
public int FleetSize { get; set; }
public int Score { get; set; }
public long AliveSince { get; set; }
public int ComboCounter { get; set; }
public uint Latency { get; set; }

public AuditModelPlayer(Player player)
{
if (player == null)
return;

this.LoginID = player.LoginID;
this.LoginName = player.LoginName;
this.FleetID = player.Fleet?.ID ?? 0;
this.FleetName = player.Name;
this.FleetSize = player.Fleet?.Ships?.Count ?? 0;
this.Score = player.Score;
this.AliveSince = player.AliveSince;
this.ComboCounter = player.ComboCounter;
this.Latency = player.Connection?.Latency ?? 0;
}
}
}
11 changes: 11 additions & 0 deletions Game.Engine/Auditing/OnDeath.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace Game.Engine.Auditing
{
public class OnDeath
{
#pragma warning disable IDE1006 // Naming Styles
public string token { get; set; }
public string name { get; set; }
public string killedBy { get; set; }
#pragma warning restore IDE1006 // Naming Styles
}
}
39 changes: 28 additions & 11 deletions Game.Engine/Auditing/RemoteEventLog.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
namespace Game.Engine.Core.Auditing
namespace Game.Engine.Auditing
{
using Firebase.Database;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Net.Http;
Expand All @@ -8,22 +10,38 @@

public static class RemoteEventLog
{
private static readonly Timer Heartbeat = null;
private static Timer Heartbeat = null;
private static readonly HttpClient HttpClient = new HttpClient();
private static readonly Queue<object> queue = new Queue<object>();
private static Queue<object> queue = new Queue<object>();
private static readonly int POST_TIMER_MS = 1000;
private static FirebaseClient Firebase;
private static GameConfiguration GameConfiguration;
private static bool Initialized = false;

static RemoteEventLog()
public static void Initialize(GameConfiguration gameConfiguration)
{
GameConfiguration = gameConfiguration;

Heartbeat = new Timer((state) =>
{
PostData().Wait();
}, null, 0, POST_TIMER_MS);


if (gameConfiguration.FirebaseAuthKey != null)
Firebase = new FirebaseClient(
gameConfiguration.FirebaseUrl,
new FirebaseOptions
{
AuthTokenAsyncFactory = () => Task.FromResult(gameConfiguration.FirebaseAuthKey)
});
Initialized = true;
}

public static void SendEvent(object message)
{
queue.Enqueue(message);
if (Initialized)
queue.Enqueue(message);
}

private async static Task PostData()
Expand All @@ -33,16 +51,15 @@ private async static Task PostData()
while (queue.Count > 0)
{
var message = queue.Dequeue();

var response = await HttpClient.PostAsJsonAsync("https://daud-discord.glitch.me/", message);

if (response.IsSuccessStatusCode)
if (message is OnDeath)
{
// cool
if (GameConfiguration.DuelBotURL != null)
await HttpClient.PostAsJsonAsync(GameConfiguration.DuelBotURL, message);
}
else
{
//
if (Firebase != null)
await Firebase.Child("events").PostAsync(JsonConvert.SerializeObject(message));
}
}
}
Expand Down
9 changes: 5 additions & 4 deletions Game.Engine/Core/Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{
using Game.API.Common;
using Game.API.Common.Models;
using Game.Engine.Core.Auditing;
using Game.Engine.Auditing;
using Game.Engine.Core.Weapons;
using Game.Engine.Networking;
using Newtonsoft.Json;
Expand All @@ -14,8 +14,6 @@

public class Player : IActor
{
private static readonly HttpClient HttpClient = new HttpClient();

public World World = null;
public Fleet Fleet = null;

Expand Down Expand Up @@ -96,6 +94,8 @@ public virtual void CreateDestroy()
Fleet.SpawnLocation = SpawnLocation;
Fleet.Init(World);

RemoteEventLog.SendEvent(new AuditEventSpawn(this));

if (World.Hook.GearheadName != null && this.Name == World.Hook.GearheadName)
{
Fleet.BaseWeapon = new FleetWeaponRobot();
Expand Down Expand Up @@ -149,6 +149,7 @@ public void Init(World world)
}

public string Name { get; set; }
public ulong LoginID { get; set; }

public static List<Player> GetTeam(World world, string color)
{
Expand Down Expand Up @@ -301,7 +302,7 @@ protected virtual void OnDeath(Player player = null)
Connection.SpectatingFleet = player.Fleet;

if (!string.IsNullOrEmpty(this.Token) && !string.IsNullOrEmpty(player?.Token))
RemoteEventLog.SendEvent(new
RemoteEventLog.SendEvent(new OnDeath
{
token = this.Token,
name = this.Name,
Expand Down
2 changes: 2 additions & 0 deletions Game.Engine/Core/Scoring/DefaultScoring.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@ public override void FleetDied(Player killer, Player victim, Fleet fleet)
victim.MaxCombo = 0;
}
}

base.FleetDied(killer, victim, fleet);
}
}
}
6 changes: 4 additions & 2 deletions Game.Engine/Core/Scoring/ScoringBase.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
namespace Game.Engine.Core.Scoring
using Game.Engine.Auditing;

namespace Game.Engine.Core.Scoring
{
public abstract class ScoringBase
{
Expand All @@ -9,7 +11,7 @@ public virtual void ShipDied(Player killer, Player victim, Ship ship)

public virtual void FleetDied(Player killer, Player victim, Fleet fleet)
{

RemoteEventLog.SendEvent(new AuditEventDeath(killer, victim, fleet));
}
}
}
1 change: 1 addition & 0 deletions Game.Engine/Core/SystemActors/Authenticator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ protected override void CycleThink()
}

player.LoginName = user.Nickname;
player.LoginID = user.Id;
player.Roles = playerRoles;
player.OnAuthenticated();
}
Expand Down
1 change: 1 addition & 0 deletions Game.Engine/Game.Engine.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<ItemGroup>
<PackageReference Include="ACMESharpCore" Version="2.0.0.49-beta1" />
<PackageReference Include="Docker.DotNet" Version="3.125.2" />
<PackageReference Include="FirebaseDatabase.net" Version="4.0.1" />
<PackageReference Include="KdSoft.FlatBuffers" Version="1.9.3" />
<PackageReference Include="Nito.AsyncEx" Version="5.0.0-pre-05" />
<PackageReference Include="RBush" Version="2.0.31" />
Expand Down
5 changes: 5 additions & 0 deletions Game.Engine/GameConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,10 @@ public class GameConfiguration
public bool LetsEncryptEnabled { get; set; }

public string PublicURL { get; set; }

public string FirebaseAuthKey { get; set; }
public string FirebaseUrl { get; set; }

public string DuelBotURL { get; set; } = "https://daud-discord.glitch.me/";
}
}
2 changes: 2 additions & 0 deletions Game.Engine/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Game.API.Authentication;
using Game.API.Client;
using Game.API.Common.Security;
using Game.Engine.Auditing;
using Game.Engine.Authentication;
using Game.Engine.ChatBot;
using Game.Engine.Core;
Expand Down Expand Up @@ -121,6 +122,7 @@ GameConfiguration config
});
app.UseGameWebsocketHandler();

RemoteEventLog.Initialize(config);
Worlds.Initialize(config);

if (config.RegistryEnabled)
Expand Down

0 comments on commit 20f4d11

Please sign in to comment.