Esempio n. 1
0
        private async Task logout_impl(Player player)
        {
            player.online = false;
            if (!permanent_id_mapped_players.TryRemove(player.permanent_id, out player))
            {
                Log.Warning("When logging out player it is expected that permanent_id_mapped_players contains the player when the player is removed from session_mapped_players.");
                return;
            }

            await in_memory_worker.Schedule(async() =>
            {
                var friends = player.friends;
                if (friends == null || friends.Count == 0)
                {
                    return;
                }
                Event ev = null;

                foreach (var friend_info in friends)
                {
                    var permanent_id = friend_info.PermanentId;
                    Player friend;
                    if (permanent_id_mapped_players.TryGetValue(permanent_id, out friend) && friend != null)
                    {
                        if (ev == null)
                        {
                            ev = player.EventOnlineStateChanged(OnlineState.Offline);
                        }
                        friend.event_stream.Enqueue(ev);
                    }
                }

                var group = player.group;
                if (group != null)
                {
                    group.RemoveInvitedMember(player);
                }

                var arena = player.arena;
                if (arena != null)
                {
                    arena.RemovePlayerAsync(player);
                }

                var player_info = player.ToPlayerInfo();
                await Task.Run(async() => await db.SavePlayerInfo(player_info));
            });
        }
Esempio n. 2
0
        public override async Task Subscribe(SubscriptionAttempt request, IServerStreamWriter <Event> responseStream, ServerCallContext context)
        {
            var login_result_ev = new Event
            {
                LoginResult = new Event_LoginResult()
            };
            var login_result = login_result_ev.LoginResult;

            Player player;
            List <BasicPlayerInfo> incoming_friend_requests;

            try
            {
                if (auth.Count > config.MaxPlayersOnline)
                {
                    login_result.Success = false;
                    await responseStream.WriteAsync(login_result_ev);

                    return;
                }

                var login_result_data = await auth.Login(request.Username, request.Password);

                if (!login_result_data.HasValue)
                {
                    login_result.Success = false;
                    await responseStream.WriteAsync(login_result_ev);

                    return;
                }
                player = login_result_data.Value.player;
                incoming_friend_requests = login_result_data.Value.pending_friend_requests_incoming;
            } catch (InvalidOperationException)
            {
                //Expected to happen when response stream is closed
                return;
            } catch (RpcException)
            {
                //Expected to happen when user logs out or gets disconnected for any other reason
                return;
            } catch (Exception e)
            {
                Log.Exception(e);
                return;
            }

            try
            {
                if (!player.event_stream.SetStream(responseStream, context))
                {
                    return;
                }

                login_result.Success = true;
                login_result.Token   = ByteString.CopyFrom(player.session_id.ToByteArray());
                await in_memory_worker.Schedule(() =>
                {
                    login_result.Friends.AddRange(player.friends);
                    login_result.Ignored.AddRange(player.ignored);
                    login_result.PendingFriendRequestsOutgoing.AddRange(player.pending_friend_requests_outgoing);
                    login_result.PendingFriendRequestsIncoming.AddRange(incoming_friend_requests);
                });

                await responseStream.WriteAsync(login_result_ev);

                for (; ;)
                {
                    if (!player.online || !await player.event_stream.SendCurrentEvents())
                    {
                        return;
                    }
                    await Task.Delay(500);
                }
            } catch (InvalidOperationException)
            {
                //Expected to happen when response stream is closed
                return;
            }
            catch (RpcException)
            {
                //Expected to happen when user logs out or gets disconnected for any other reason

                return;
            }
            catch (Exception e)
            {
                Log.Exception(e);
                await auth.Logout(player.session_id);
            }
        }
Esempio n. 3
0
 internal void Run(SingleThreadScheduler scheduler)
 {
     scheduler.Schedule(run_impl);
 }