/// <summary> /// Implements the execution of login command. /// </summary> /// <param name="current">Current service instance.</param> /// <param name="sessions">Collection of all sessions.</param> /// <param name="message">Command arguments.</param> public void Execute(GameService current, GameSessions sessions, string message) { if (!string.IsNullOrEmpty(current.Context.LogOnName)) { sessions.SendMessage(current, "LoginResponse:Failure;Loggedin"); return; } if (string.IsNullOrEmpty(message)) { sessions.SendMessage(current, "LoginResponse:Failure;LoginNameEmpty"); return; } var service = sessions.FindSession(message); if (null == service && loggedInUsers < MaxUsers) { Interlocked.Increment(ref loggedInUsers); current.Context.LogOnName = message; // Need to send successful response to the current service and LogOn response to all other sessions. // so that other sessions can also see that this user is logged in. sessions.BroadcastMessage(BroadcastMessageType.LogOnResponse, current, null, "LoginResponse:Successful", null); } else if (MaxUsers == loggedInUsers) { sessions.SendMessage(current, "LoginResponse:Failure;LoginLimitReached"); } else { sessions.SendMessage(current, "LoginResponse:Failure;LoginNameTaken"); } }
/// <summary> /// Implements the execution of login command. /// </summary> /// <param name="current">Current service instance.</param> /// <param name="sessions">Collection of all sessions.</param> /// <param name="message">Command arguments.</param> public void Execute(JigsawGameService current, GameSessions sessions, string message) { // Check if the context already have Logged in context. if (!string.IsNullOrEmpty(current.Context.LogOnName)) { sessions.SendMessage(current, "LoginResponse:Failure;Loggedin"); return; } if (string.IsNullOrEmpty(message)) { sessions.SendMessage(current, "LoginResponse:Failure;LoginNameEmpty"); return; } var service = sessions.FindSession(message); if (null == service && loggedInUsers < MaxUsers) { Interlocked.Increment(ref loggedInUsers); current.Context.LogOnName = message; // Need to send successful response to the current service and LogOn response to all other sessions. // so that other sessions can also see that this user is logged in. sessions.BroadcastMessage(BroadcastMessageType.LogOnResponse, current, null, "LoginResponse:Successful", null); } else if (MaxUsers == loggedInUsers) { sessions.SendMessage(current, "LoginResponse:Failure;LoginLimitReached"); } else { sessions.SendMessage(current, "LoginResponse:Failure;LoginNameTaken"); } }
/// <summary> /// Implements the execution of selectPlayer command. /// </summary> /// <param name="current">Current service instance.</param> /// <param name="sessions">Collection of all sessions.</param> /// <param name="message">Command arguments.</param> public void Execute(JigsawGameService current, GameSessions sessions, string message) { if (null != current.Context.PlayerInstance) { sessions.SendMessage(current, "SelectPlayerResponse:Failure;PlayerExist."); return; } else { if (string.IsNullOrEmpty(message)) { sessions.SendMessage(current, "SelectPlayerResponse:Failure;WrongRequest."); return; } var playerInstance = sessions.FindSession(message); if (null == playerInstance) { sessions.SendMessage(current, "SelectPlayerResponse:Failure;PlayerDoesNotExist" + message); return; } else { // Associate both players and then send all responses in the same atomic operation. lock (selectPlayerLock) { if (null == playerInstance.Context.PlayerInstance && null == current.Context.PlayerInstance) { current.Context.PlayerInstance = playerInstance; playerInstance.Context.PlayerInstance = current; // Need to send selectPlayer response to the current service, fixedPlayerresponse to PlayerInstance // and also we need to braodcast selectPlayerResposne to other sessions. sessions.BroadcastMessage(BroadcastMessageType.SelectPlayerResponse, current, playerInstance, "SelectPlayerResponse:Successful", "FixedPlayerResponse:" + current.Context.LogOnName); return; } } // Selected player is already a Player. sessions.SendMessage(current, "SelectPlayerResponse:Failure;BusyPlayer;" + message); } } }