Exemplo n.º 1
0
 public bool RegisterPlayerEvent(RegisterEventModel model)
 {
     return RegisterEvent(model);
 }
Exemplo n.º 2
0
        public bool RegisterEvent(RegisterEventModel model)
        {
            var server = Context.Servers.FirstOrDefault(x => x.Guid == model.ServerGuid && x.AuthKey == model.ServerKey);
            if(server == null)
                throw new InvalidOperationException("No server matching guid/key pair");
            Task.Run(() =>
            {
                Player player = null;
                if (!String.IsNullOrWhiteSpace(model.PlayerGuid))
                {
                    player = Context.Players.FirstOrDefault(x => x.Guid == model.PlayerGuid);
                    if (player == null)
                    {
                        // Create new player
                        player = new Player
                        {
                            Guid = model.PlayerGuid,
                            FirstSeen = DateTime.UtcNow
                        };
                        Context.Players.Add(player);
                        Context.SaveChanges();
                    }
                    player.LastSeen = DateTime.UtcNow;
                    player.LastServer = server;
                    if (player.Username != model.PlayerName && !String.IsNullOrWhiteSpace(player.Username) &&
                        !String.IsNullOrWhiteSpace(model.PlayerName))
                    {
                        // new event for namechange
                        Context.Events.Add(Event.NewChangeEvent(player, "namechanged", player.Username, model.PlayerName));

                    }
                    player.Username = model.PlayerName;

                    if (player.Ip != model.PlayerIp && !String.IsNullOrWhiteSpace(player.Ip) &&
                        !String.IsNullOrWhiteSpace(model.PlayerIp))
                    {
                        // new event for ipchange
                        Context.Events.Add(Event.NewChangeEvent(player, "ipchanged", player.Ip, model.PlayerIp));

                    }
                    player.Ip = model.PlayerIp;
                    if (!String.IsNullOrWhiteSpace(player.Ip))
                        player.LastCountry = Startup.IpDatabase.City(player.Ip).Country.IsoCode;
                }
                var evt = new Event
                {
                    Player = player,
                    ServerId = server.Id,
                    Type = model.EventType,
                    Submitted = DateTime.UtcNow
                };
                Context.Events.Add(evt);
                Context.SaveChanges();

                if (model.EventData != null)
                {
                    foreach (var evd in model.EventData.Select(kv => new EventData
                    {
                        EventId = evt.Id,
                        Key = kv.Key,
                        Value = kv.Value
                    }))
                    {
                        Context.EventDatas.Add(evd);
                    }
                }
                Context.SaveChanges();

                RealtimeDispatcher.Get().Broadcast("event.new", evt);
            });
            return true;
        }