private void OnReadyToPlayAll(Error error, ServerEventArgs <Player[], Dictionary <Guid, Dictionary <Guid, Player> >, VoxelAbilitiesArray[], SerializedTaskArray[], SerializedTaskTemplatesArray[], AssignmentGroupArray[], Room> args)
        {
            Room room = args.Arg7;

            if (room.Mode == GameMode.Replay)
            {
                Send(RemoteEvent.Evt.ReadyToPlayAll, error, room.CreatorClientId,
                     RemoteArg.Create(args.Arg),
                     RemoteArg.Create(new Guid[0]),
                     RemoteArg.Create(args.Arg3),
                     RemoteArg.Create(args.Arg4),
                     RemoteArg.Create(args.Arg5),
                     RemoteArg.Create(args.Arg6),
                     RemoteArg.Create(args.Arg7));
            }
            else
            {
                Dictionary <Guid, Dictionary <Guid, Player> > clientIdToPlayers = args.Arg2;
                foreach (KeyValuePair <Guid, Dictionary <Guid, Player> > kvp in clientIdToPlayers)
                {
                    Guid clientId = kvp.Key;
                    Dictionary <Guid, Player> players = kvp.Value;

                    Send(RemoteEvent.Evt.ReadyToPlayAll, error, clientId,
                         RemoteArg.Create(args.Arg),
                         RemoteArg.Create(players.Keys.ToArray()),
                         RemoteArg.Create(args.Arg3),
                         RemoteArg.Create(args.Arg4),
                         RemoteArg.Create(args.Arg5),
                         RemoteArg.Create(args.Arg6),
                         RemoteArg.Create(args.Arg7));
                }
            }
        }
        public void Logoff(Guid clientId, Guid[] playerIds, ServerEventHandler <Guid[]> callback)
        {
            RemoteCall rpc = new RemoteCall(
                RemoteCall.Proc.LogoffMultiple,
                clientId,
                RemoteArg.Create(playerIds));

            Call(rpc, (error, result) =>
            {
                playerIds = result.Get <Guid[]>(0);

                if (!HasError(error))
                {
                    if (playerIds != null)
                    {
                        for (int i = 0; i < playerIds.Length; ++i)
                        {
                            m_localPlayers.Remove(playerIds[i]);
                        }
                    }
                }


                callback(error, playerIds);
            });
        }
        public void DestroyBot(Guid clientId, Guid botId, ServerEventHandler <Guid, Room> callback)
        {
            RemoteCall rpc = new RemoteCall(
                RemoteCall.Proc.DestroyBot,
                clientId,
                RemoteArg.Create(botId));

            Call(rpc, (error, result) => callback(error, result.Get <Guid>(0), result.Get <Room>(1)));
        }
        public void SetReplay(Guid clientId, Guid id, ServerEventHandler callback)
        {
            RemoteCall rpc = new RemoteCall(
                RemoteCall.Proc.SetReplay,
                clientId,
                RemoteArg.Create(id));

            Call(rpc, (error, result) => callback(error));
        }
        public void SetReadyToLaunch(Guid clientId, bool isReady, ServerEventHandler <Room> callback)
        {
            RemoteCall rpc = new RemoteCall(
                RemoteCall.Proc.SetReadyToLaunch,
                clientId,
                RemoteArg.Create(isReady));

            Call(rpc, (error, result) => callback(error, result.Get <Room>(0)));
        }
        public void SaveReplay(Guid clientId, string name, ServerEventHandler callback)
        {
            RemoteCall rpc = new RemoteCall(
                RemoteCall.Proc.SaveReplay,
                clientId,
                RemoteArg.Create(name));

            Call(rpc, (error, result) => callback(error));
        }
        public void Pause(Guid clientId, bool pause, ServerEventHandler callback)
        {
            RemoteCall rpc = new RemoteCall(
                RemoteCall.Proc.Pause,
                clientId,
                RemoteArg.Create(pause));

            Call(rpc, (error, result) => callback(error));
        }
        public void GetTaskTemplates(Guid clientId, Guid playerId, ServerEventHandler <SerializedTask[], SerializedNamedTaskLaunchInfo[]> callback)
        {
            RemoteCall rpc = new RemoteCall(
                RemoteCall.Proc.GetTaskTemplates,
                clientId,
                RemoteArg.Create(playerId));

            Call(rpc, (error, result) => callback(error, result.Get <SerializedTask[]>(0), result.Get <SerializedNamedTaskLaunchInfo[]>(1)));
        }
        public void JoinRoom(Guid clientId, Guid roomId, ServerEventHandler <Room> callback)
        {
            RemoteCall rpc = new RemoteCall(
                RemoteCall.Proc.JoinRoom,
                clientId,
                RemoteArg.Create(roomId));

            Call(rpc, (error, result) => callback(error, result.Get <Room>(0)));
        }
        public void GetPlayers(Guid clientId, Guid roomId, ServerEventHandler <Player[]> callback)
        {
            RemoteCall rpc = new RemoteCall(
                RemoteCall.Proc.GetPlayersByRoomId,
                clientId,
                RemoteArg.Create(roomId));

            Call(rpc, (error, result) => callback(error, result.Get <Player[]>(0)));
        }
        public void GetPlayer(Guid clientId, Guid playerId, ServerEventHandler <Player> callback)
        {
            RemoteCall rpc = new RemoteCall(
                RemoteCall.Proc.GetPlayer,
                clientId,
                RemoteArg.Create(playerId));

            Call(rpc, (error, result) => callback(error, result.Get <Player>(0)));
        }
        public void GetRooms(Guid clientId, int page, int count, ServerEventHandler <Room[]> callback)
        {
            RemoteCall rpc = new RemoteCall(
                RemoteCall.Proc.GetRooms,
                clientId,
                RemoteArg.Create(page),
                RemoteArg.Create(count));

            Call(rpc, (error, result) => callback(error, result.Get <Room[]>(0)));
        }
        public void CreateBot(Guid clientId, string botName, BotType botType, ServerEventHandler <Guid, Room> callback)
        {
            RemoteCall rpc = new RemoteCall(
                RemoteCall.Proc.CreateBot,
                clientId,
                RemoteArg.Create(botName),
                RemoteArg.Create((int)botType));

            Call(rpc, (error, result) => callback(error, result.Get <Guid>(0), result.Get <Room>(1)));
        }
        public void CreateBots(Guid clientId, string[] botNames, BotType[] botTypes, ServerEventHandler <Guid[], Room> callback)
        {
            RemoteCall rpc = new RemoteCall(
                RemoteCall.Proc.CreateBots,
                clientId,
                RemoteArg.Create(botNames),
                RemoteArg.Create(botTypes.ToIntArray()));

            Call(rpc, (error, result) => callback(error, result.Get <Guid[]>(0), result.Get <Room>(1)));
        }
        public void UploadMap(Guid clientId, MapInfo mapInfo, MapData mapData, ServerEventHandler callback)
        {
            RemoteCall rpc = new RemoteCall(
                RemoteCall.Proc.UploadMapData,
                clientId,
                RemoteArg.Create(mapInfo),
                RemoteArg.Create(m_serializer.Serialize(mapData)));

            Call(rpc, (error, result) => callback(error));
        }
        public void CreateRoom(Guid clientId, Guid mapId, GameMode gameMode, ServerEventHandler <Room> callback)
        {
            RemoteCall rpc = new RemoteCall(
                RemoteCall.Proc.CreateRoom,
                clientId,
                RemoteArg.Create(mapId),
                RemoteArg.Create((int)gameMode));

            Call(rpc, (error, result) => callback(error, result.Get <Room>(0)));
        }
        public void SaveTaskTemplate(Guid clientId, Guid playerId, SerializedTask taskTemplate, SerializedNamedTaskLaunchInfo TaskTemplateData, ServerEventHandler callback)
        {
            RemoteCall rpc = new RemoteCall(
                RemoteCall.Proc.SaveTaskTemplate,
                clientId,
                RemoteArg.Create(playerId),
                RemoteArg.Create(taskTemplate),
                RemoteArg.Create(TaskTemplateData));

            Call(rpc, (error, result) => callback(error));
        }
        public void SendMessage(Guid clientId, ChatMessage message, ServerEventHandler <Guid> callback)
        {
            RemoteCall rpc = new RemoteCall(
                RemoteCall.Proc.SendChatMessage,
                clientId,
                RemoteArg.Create(message));

            Call(rpc, (error, result) =>
            {
                Guid messageId = result.Get <Guid>(0);
                callback(error, messageId);
            });
        }
        public T Get <T>(int index)
        {
            if (index >= Args.Length)
            {
                return(default(T));
            }

            RemoteArg arg    = Args[index];
            T         result = (T)arg.Value;

            if (result == null && Reflection.IsArray(typeof(T)))
            {
                return((T)Activator.CreateInstance(typeof(T), new object[] { 0 }));
            }
            return(result);
        }
        public void Logoff(Guid clientId, Guid playerId, ServerEventHandler <Guid> callback)
        {
            RemoteCall rpc = new RemoteCall(
                RemoteCall.Proc.Logoff,
                clientId,
                RemoteArg.Create(playerId));

            Call(rpc, (error, result) =>
            {
                playerId = result.Get <Guid>(0);

                if (!HasError(error))
                {
                    m_localPlayers.Remove(playerId);
                }
                callback(error, playerId);
            });
        }
        public void Login(string name, byte[] pwdHash, Guid clientId, ServerEventHandler <Guid> callback)
        {
            RemoteCall rpc = new RemoteCall(
                RemoteCall.Proc.LoginHash,
                clientId,
                RemoteArg.Create(name),
                RemoteArg.Create(pwdHash));

            Call(rpc, (error, result) =>
            {
                Guid playerId = result.Get <Guid>(0);
                if (!HasError(error))
                {
                    m_localPlayers.Add(playerId);
                }

                callback(error, playerId);
            });
        }
        public void DownloadMapData(Guid clientId, Guid mapId, ServerEventHandler <MapData> callback)
        {
            RemoteCall rpc = new RemoteCall(
                RemoteCall.Proc.DownloadMapData,
                clientId,
                RemoteArg.Create(mapId));

            Call(rpc, (error, result) =>
            {
                byte[] mapDataBin = result.Get <byte[]>(0);
                MapData mapData   = null;

                if (mapDataBin != null && !HasError(error))
                {
                    mapData = m_serializer.Deserialize <MapData>(mapDataBin);
                }

                callback(error, mapData);
            });
        }
        public void SignUp(string name, string password, Guid clientId, ServerEventHandler <Guid, byte[]> callback)
        {
            RemoteCall rpc = new RemoteCall(
                RemoteCall.Proc.SignUp,
                clientId,
                RemoteArg.Create(name),
                RemoteArg.Create(password));

            Call(rpc, (error, result) =>
            {
                Guid playerId  = result.Get <Guid>(0);
                byte[] pwdHash = result.Get <byte[]>(1);
                if (!HasError(error))
                {
                    m_localPlayers.Add(playerId);
                }

                callback(error, playerId, pwdHash);
            });
        }
 private void OnLoggedOff(Error error, ServerEventArgs <Guid[]> args)
 {
     Broadcast(RemoteEvent.Evt.LoggedOff, error, args, RemoteArg.Create(args.Arg));
 }
 private void OnChatMessage(Error error, ServerEventArgs <ChatMessage> args)
 {
     Broadcast(RemoteEvent.Evt.ChatMessage, error, args, RemoteArg.Create(args.Arg));
 }
        protected override void OnRequest(ILowProtocol sender, LowRequestArgs request)
        {
            RemoteCall rpc;

            try
            {
                rpc = m_serializer.Deserialize <RemoteCall>(request.Data);
            }
            catch (Exception e)
            {
                Log.Error("Invalid RemoteCall format ", e);

                //#warning Should force disconnect client
                #warning This code is not tested
                sender.Disable();

                throw;
            }


            switch (rpc.Procedure)
            {
            case RemoteCall.Proc.RegisterClient:
            {
                RegisterClient(sender, rpc.ClientId);
                Return(sender, request, new Error(StatusCode.OK));
            }
            break;

            case RemoteCall.Proc.GetPlayers:
                m_gameServer.GetPlayers(rpc.ClientId, (error, players) =>
                {
                    Return(sender, request, error, RemoteArg.Create(players));
                });
                break;

            case RemoteCall.Proc.GetPlayersByRoomId:
                m_gameServer.GetPlayers(rpc.ClientId, rpc.Get <Guid>(0), (error, players) =>
                {
                    Return(sender, request, error, RemoteArg.Create(players));
                });
                break;

            case RemoteCall.Proc.GetPlayer:
                m_gameServer.GetPlayer(rpc.ClientId, rpc.Get <Guid>(0), (error, players) =>
                {
                    Return(sender, request, error, RemoteArg.Create(players));
                });
                break;

            case RemoteCall.Proc.Login:
                m_gameServer.Login(rpc.Get <string>(0), rpc.Get <string>(1), rpc.ClientId, (error, playerId, pwdHash) =>
                {
                    Return(sender, request, error, RemoteArg.Create(playerId), RemoteArg.Create(pwdHash));
                });
                break;

            case RemoteCall.Proc.LoginHash:
                m_gameServer.Login(rpc.Get <string>(0), rpc.Get <byte[]>(1), rpc.ClientId, (error, playerId) =>
                {
                    Return(sender, request, error, RemoteArg.Create(playerId));
                });
                break;

            case RemoteCall.Proc.SignUp:
                m_gameServer.SignUp(rpc.Get <string>(0), rpc.Get <string>(1), rpc.ClientId, (error, playerId, pwdHash) =>
                {
                    Return(sender, request, error, RemoteArg.Create(playerId), RemoteArg.Create(pwdHash));
                });
                break;

            case RemoteCall.Proc.Logoff:
                m_gameServer.Logoff(rpc.ClientId, rpc.Get <Guid>(0), (error, guid) =>
                {
                    Return(sender, request, error, RemoteArg.Create(guid));
                });
                break;

            case RemoteCall.Proc.LogoffMultiple:
                m_gameServer.Logoff(rpc.ClientId, rpc.Get <Guid[]>(0), (error, guids) =>
                {
                    Return(sender, request, error, RemoteArg.Create(guids));
                });
                break;

            case RemoteCall.Proc.JoinRoom:
                m_gameServer.JoinRoom(rpc.ClientId, rpc.Get <Guid>(0), (error, room) =>
                {
                    //Boradcast to room players
                    Return(sender, request, error, RemoteArg.Create(room));
                });
                break;

            case RemoteCall.Proc.LeaveRoom:
                m_gameServer.LeaveRoom(rpc.ClientId, (error) =>
                {
                    //Brodcast to room players
                    Return(sender, request, error);
                });
                break;

            case RemoteCall.Proc.GetRooms:
                m_gameServer.GetRooms(rpc.ClientId, rpc.Get <int>(0), rpc.Get <int>(1), (error, rooms) =>
                {
                    Return(sender, request, error, RemoteArg.Create(rooms));
                });
                break;

            case RemoteCall.Proc.GetRoom:
                m_gameServer.GetRoom(rpc.ClientId, (error, room) =>
                {
                    Return(sender, request, error, RemoteArg.Create(room));
                });
                break;

            case RemoteCall.Proc.GetRoomById:
                m_gameServer.GetRoom(rpc.ClientId, rpc.Get <Guid>(0), (error, room) =>
                {
                    Return(sender, request, error, RemoteArg.Create(room));
                });
                break;

            case RemoteCall.Proc.CreateRoom:
                m_gameServer.CreateRoom(rpc.ClientId, rpc.Get <Guid>(0), rpc.Get <GameMode>(1), (error, room) =>
                {
                    //Do not broadcast
                    //client will get rooms list using polling each 10 seconds
                    //or using refres button
                    Return(sender, request, error, RemoteArg.Create(room));
                });
                break;

            case RemoteCall.Proc.DestroyRoom:
                m_gameServer.DestroyRoom(rpc.ClientId, rpc.Get <Guid>(0), (error, guid) =>
                {
                    Return(sender, request, error, RemoteArg.Create(guid));
                });
                break;

            case RemoteCall.Proc.CreateBot:
                m_gameServer.CreateBot(rpc.ClientId, rpc.Get <string>(0), rpc.Get <BotType>(1), (error, playerId, room) =>
                {
                    Return(sender, request, error, RemoteArg.Create(playerId), RemoteArg.Create(room));
                });
                break;

            case RemoteCall.Proc.CreateBots:
                m_gameServer.CreateBots(rpc.ClientId, rpc.Get <string[]>(0), rpc.Get <int[]>(1).ToEnum <BotType>(), (error, playerIds, room) =>
                {
                    Return(sender, request, error, RemoteArg.Create(playerIds), RemoteArg.Create(room));
                });
                break;

            case RemoteCall.Proc.DestroyBot:
                m_gameServer.DestroyBot(rpc.ClientId, rpc.Get <Guid>(0), (error, playerId, room) =>
                {
                    Return(sender, request, error, RemoteArg.Create(playerId), RemoteArg.Create(room));
                });
                break;

            case RemoteCall.Proc.UploadMapData:
                m_gameServer.UploadMap(rpc.ClientId, rpc.Get <MapInfo>(0), rpc.Get <byte[]>(1), (error) =>
                {
                    Return(sender, request, error);
                });
                break;

            case RemoteCall.Proc.GetMaps:
                m_gameServer.GetMaps(rpc.ClientId, (Error error, ByteArray[] mapsInfo) =>
                {
                    Return(sender, request, error, RemoteArg.Create(mapsInfo));
                });
                break;

            case RemoteCall.Proc.DownloadMapData:
                m_gameServer.DownloadMapData(rpc.ClientId, rpc.Get <Guid>(0), (Error error, byte[] mapData) =>
                {
                    Return(sender, request, error, RemoteArg.Create(mapData));
                });
                break;

            case RemoteCall.Proc.GetReplays:
                m_gameServer.GetReplays(rpc.ClientId, (Error error, ByteArray[] replaysInfo) =>
                {
                    Return(sender, request, error, RemoteArg.Create(replaysInfo));
                });
                break;

            case RemoteCall.Proc.SetReplay:
                m_gameServer.SetReplay(rpc.ClientId, rpc.Get <Guid>(0), (error) =>
                {
                    Return(sender, request, error);
                });
                break;

            case RemoteCall.Proc.SaveReplay:
                m_gameServer.SaveReplay(rpc.ClientId, rpc.Get <string>(0), (error) =>
                {
                    Return(sender, request, error);
                });
                break;

            case RemoteCall.Proc.GetStats:
                m_gameServer.GetStats(rpc.ClientId, (error, serverStats) =>
                {
                    Return(sender, request, error, RemoteArg.Create(serverStats));
                });
                break;

            case RemoteCall.Proc.SetReadyToLaunch:
                m_gameServer.SetReadyToLaunch(rpc.ClientId, rpc.Get <bool>(0), (error, room) =>
                {
                    Return(sender, request, error, RemoteArg.Create(room));
                });
                break;

            case RemoteCall.Proc.Launch:
                m_gameServer.Launch(rpc.ClientId, (error, serverUrl) =>
                {
                    Return(sender, request, error, RemoteArg.Create(serverUrl));
                });
                break;

            case RemoteCall.Proc.SendChatMessage:
                m_gameServer.SendMessage(rpc.ClientId, rpc.Get <ChatMessage>(0), (error, messageId) =>
                {
                    Return(sender, request, error, RemoteArg.Create(messageId));
                });
                break;
            }
        }
 private void OnLaunched(Error error, ServerEventArgs <string> args)
 {
     Broadcast(RemoteEvent.Evt.Launched, error, args, RemoteArg.Create(args.Arg));
 }
 private void OnReadyToLaunch(Error error, ServerEventArgs <Room> args)
 {
     Broadcast(RemoteEvent.Evt.ReadyToLaunch, error, args, RemoteArg.Create(args.Arg));
 }
 private void OnLeftRoom(Error error, ServerEventArgs <Guid[], Room> args)
 {
     Broadcast(RemoteEvent.Evt.LeftRoom, error, args, RemoteArg.Create(args.Arg), RemoteArg.Create(args.Arg2));
 }
 private void OnPaused(Error error, ServerEventArgs <bool> args)
 {
     Broadcast(RemoteEvent.Evt.Pause, error, args, RemoteArg.Create(args.Arg));
 }