//! Command to interface which dispatches start of game event to all controller clients. public void StartGame() { foreach (Client client in this.clients) { if (!client.IsReady()) { client.SetStatus(5); client.SetReadiness(false); // LoadSave.savedData.Add (Time.time.ToString () + " : OUT :" + API_KEYWORD_GAME_END + "\n"); GameInterface.dispatchEvent(client.Username(), API_KEYWORD_GAME_END); } else { // LoadSave.savedData.Add (Time.time.ToString () + " : OUT :" + API_KEYWORD_GAME_START + "\n"); GameInterface.dispatchEvent(client.Username(), API_KEYWORD_GAME_START); } } }
/** * Dispatches a list of parameters to the docent remote control to initialize it * * @param gameId the unique game name identifier * @param mode1 the first mode toggle integer * @param mode2 the second mode toggle integer * @param mode3 the third mode toggle integer * @param param1 the first custom string paramter * @param param2 the second custom string paramter * @param param3 the third custom string paramter * * !DEPRECATED! */ public void DocentApplyAdvancedSettings(string gameId, int mode1, int mode2, int mode3, string param1, string param2, string param3) { Dictionary <string, string> dict = new Dictionary <string, string>(); dict.Add("game", gameId); if (-1 != mode1) { dict.Add("mode1", mode1.ToString()); } if (-1 != mode2) { dict.Add("mode2", mode2.ToString()); } if (-1 != mode3) { dict.Add("mode3", mode3.ToString()); } if ("" != param1) { dict.Add("param1", param1.ToString()); } if ("" != param1) { dict.Add("param2", param1.ToString()); } if ("" != param1) { dict.Add("param3", param1.ToString()); } EscEvent evt = new EscEvent("start", dict); GameInterface.dispatchEvent("game-engine", evt.ToString()); if (null != OnDocentApplyAdvancedSettings) { OnDocentApplyAdvancedSettings(gameId); } }
/** * Dispatches an EscEvent to the game engine to start the game * * @param gameId the unique game name identifier * @param round the round setting to dispatch * @param difficulty the difficulty setting to dispatch */ public void DocentStartGame(string gameId, int round, int difficulty) { Dictionary <string, string> dict = new Dictionary <string, string>(); dict.Add("game", gameId); if (-1 != round) { dict.Add("round", round.ToString()); } if (-1 != difficulty) { dict.Add("difficulty", difficulty.ToString()); } EscEvent evt = new EscEvent("start", dict); GameInterface.dispatchEvent("game-engine", evt.ToString()); if (null != OnDocentGameStart) { OnDocentGameStart(gameId); } }
/** * Sends 1-3 parameter strings to the docent remote to serve as acceptable defaults. This provides an opportunity * for a game administrator to custom tailor the game experience to the current audience. Each game should document * what parameters they are able to accept and what effect those parameters will have so an administrator will know * what to expect of their inputs. * * @param parameter1 the first string option * @param parameter2 the optional second string option * @param parameter3 the optional third string option * @return TRUE if successfully sent, FALSE otherwise */ public bool SubmitParameterStrings(string parameter1, string parameter2 = "", string parameter3 = "") { bool eventSent = false; if (GameInterface.isConnected()) { // LoadSave.savedData.Add (Time.time.ToString () + " : SubmitParameterStrings \n"); string options = API_KEYWORD_APPLY_PARAMS + "param1=" + parameter1 + ",param2=" + parameter2 + ",param3=" + parameter3; if (0 != GameInterface.dispatchEvent(KEYWORD_DOCENT, options)) { Debug.LogWarning("parameter strings message delivery failed."); } else { eventSent = true; } } else { Debug.LogWarning("game not yet connected"); } return(eventSent); }
//! Loads the game represented by the input game id public void DocentLoadGame(string gameId) { GameInterface.writeLog("DocentLoadGame: for id = " + gameId); StartCoroutine(LoadGame(gameId)); }
/** * Loads the ESC Platform Launcher's .plist via the plugin and * parses it as XML. The resulting Dictionary is then passed to * the Docent application. */ public void ReadGamesPlist() { Dictionary <string, string> newGamesList = new Dictionary <string, string>(); string appPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal); appPath = appPath.Replace("/Documents", ""); string plistPath = appPath + "/esclauncher/Info.plist"; StringBuilder contents = new StringBuilder(16384); if (0 == GameInterface.getLauncherInfoPlist(plistPath, contents, (uint)contents.Capacity)) { // load external content string contentsString = contents.ToString(); XmlDocument xmlDoc = new XmlDocument(); try { xmlDoc.LoadXml(contentsString); } catch (XmlException e) { Debug.LogWarning("Failed to load Info.plist: " + e.Message); return; } // parse loaded XML XmlNodeList dictList = xmlDoc.GetElementsByTagName("dict"); XmlNode xmlNode = dictList.Item(1); for (int i = 0; i < xmlNode.ChildNodes.Count; i += 2) { XmlNode keyNode = xmlNode.ChildNodes.Item(i); XmlNode valNode = xmlNode.ChildNodes.Item(i + 1); newGamesList.Add(keyNode.InnerText, valNode.InnerText); } // Compare new plist to existing plist for equality and send a new message to docent if they are not equal bool gameListsAreEqual = (gamesList.Count == newGamesList.Count); if (gameListsAreEqual) { foreach (var pair in gamesList) // If count is same, compare the key / values { string value; if (newGamesList.TryGetValue(pair.Key, out value)) // Requires value to be equal { if (value != pair.Value) { gameListsAreEqual = false; break; } } else // Requires key to be present { gameListsAreEqual = false; break; } } } if (!gameListsAreEqual) { gamesList = newGamesList; UpdateDocentGamesList(); } } else { Debug.LogWarning("Could not load Info.plist"); } }
/** * Handles docent connection messages that occur during each frame update. * * @see Esc.ServerConnection */ public new void Update() { // Read games plist every once second if (GameInterface.isConnected()) { if (cumulativeTime > CHECK_GAMES_LIST_INTERVAL) { cumulativeTime = 0.0f; ReadGamesPlist(); } else { cumulativeTime += Time.deltaTime; } } // Dispatch heartbeat ping to Medialon system if necessary if (medialonTime > MEDIALON_PING_INTERVAL) { medialonTime = 0.0f; DispatchMedialonPing(); } else { medialonTime += Time.deltaTime; } // Reconnect if necessary if (this.willReconnect) { this.willReconnect = false; GameInterface.startLauncherInterface(); } while (GameInterface.hasMoreStatusChanges()) { int stat; if (0 == GameInterface.getNextStatusChange(out stat)) { switch (stat) { case 0: // connected //GameInterface.writeLog("socket connection established"); break; case 1: // TLS connected break; case 2: // disconnected if (null != OnDisconnected) { OnDisconnected(); } if (this.autoReconnect) { willReconnect = true; } break; case 3: // roster loaded; if (null != OnConnected) { OnConnected(); } break; case 4: // error break; default: break; } } } while (GameInterface.hasMoreEvents()) { StringBuilder user = new StringBuilder(64); StringBuilder message = new StringBuilder(256); if (user == null || user.Capacity == null || message == null || message.Capacity == null) { return; } if (0 == GameInterface.getNextEvent(user, (uint)user.Capacity, message, (uint)message.Capacity)) { string username = user.ToString(); string msgString = message.ToString(); if (username.Equals("docent")) { if (msgString.StartsWith(API_KEYWORD_GAME)) { EscEvent evt = EscEvent.FromString(msgString); //Debug.Log("message: " + msgString); string gameIdValue = ""; string option = ""; int round = -1; int difficulty = -1; //! advance settings strings int mode1 = -1; int mode2 = -1; int mode3 = -1; string param1 = ""; string param2 = ""; string param3 = ""; foreach (KeyValuePair <string, string> pair in evt.attributes) { switch (pair.Key) { case "game": gameIdValue = pair.Value; break; case "option": option = pair.Value; break; case "round": round = Convert.ToInt32(pair.Value); break; case "difficulty": difficulty = Convert.ToInt32(pair.Value); break; case "mode1": mode1 = Convert.ToInt32(pair.Value); break; case "mode2": mode2 = Convert.ToInt32(pair.Value); break; case "mode3": mode3 = Convert.ToInt32(pair.Value); break; case "param1": param1 = pair.Value; break; case "param2": param2 = pair.Value; break; case "param3": param3 = pair.Value; break; } } switch (option) { case "load": if (!this.currentLoadedGame.Equals("")) { DocentQuitGame(this.currentLoadedGame); } this.currentLoadedGame = gameIdValue; DocentLoadGame(gameIdValue); break; case "start": DocentStartGame(gameIdValue, round, difficulty); break; case "pause": DocentPauseGame(gameIdValue); break; case "stop": DocentStopGame(gameIdValue); break; case "apply": DocentApplyAdvancedSettings(gameIdValue, mode1, mode2, mode3, param1, param2, param3); break; case "quit": DocentQuitGame(gameIdValue); break; } } else if (msgString.StartsWith(API_KEYWORD_INIT)) { UpdateDocentGamesList(); } } } } }
/** * Overwrites the Start method in ServerConnection. */ public new void Start() { GameInterface.goFullScreen(); }
//! Exit full screen mode. public void ExitFullScreen() { GameInterface.leaveFullScreen(); }
/** * Dispatches an EscEvent to a controller client without any special attributes. * * @param client the client to be initialized */ public void InitializeController(Client client) { GameInterface.dispatchEvent(client.Username(), API_KEYWORD_INIT); // LoadSave.savedData.Add (Time.time.ToString () + " : OUT :" + client.Username () + " " + API_KEYWORD_INIT + "\n"); }
/** * Handles any client connection changes that occur during each frame update. * If the state listener is connected, it will call its update function. * While the Game Server Interface has status changes, it will attempt to reconnect until connection is successful. * When TLS is connected, this class's server object will instantiate with its current IP address. * While the Game Server Interface has queued presence events, each event will be marshaled based upon the client's identity. * If a client is not yet recognized, it is added to the client collection and a registration message containing the * game server IP is dispatched to the controller client. If the client already exists, the presence state received is * used to determine connectivity status. * * While the Game Server Interface has queued events, each event will be marshaled to the client object associated * with the sender and its message string parsed for either registration, initialization, or generic events, which * are then dispatched directly to the game class. * * Finally, if any client state changes have been made they are propogated to the clients. * * @see Esc.GameInterface * @see Esc.Server * @see Esc.StateListener */ public void Update() { // Go Full Screen after delay if (cumulativeTime > launchDelay && !gameFullScreen) { if (ServerConnection.developerMode) { GameInterface.goFullScreenDevMode(); WriteToLog("Set Game Full Screen Dev Mode"); } else { GameInterface.goFullScreen(); WriteToLog("Set Game Full Screen Production Mode"); } gameFullScreen = true; } else { cumulativeTime += Time.deltaTime; } if (this.stateListener != null) { if (this.stateListener.IsConnected()) { this.stateListener.Update(); } } if (this.willReconnect) { this.willReconnect = false; GameInterface.startServerInterface(); } // process status changes while (GameInterface.hasMoreStatusChanges()) { int stat; if (0 == GameInterface.getNextStatusChange(out stat)) { switch (stat) { case 0: // connected break; case 1: // TLS connected break; case 2: // disconnected if (null != OnDisconnected) { OnDisconnected(); } if (this.autoReconnect) { willReconnect = true; } // if (this.gameStarted){ // analytics.TrackGameDisconnected(); // } break; case 3: // roster loaded; this.server = new Server(KEYWORD_GAME_ENGINE); this.server.SetRemoteEndpoint("127.0.0.1"); if (null != OnConnected) { OnConnected(); } SendGameLoadedMessageToDocent(); //analytics.TrackGameLoad(GetClientRegisteredCount()); break; case 4: // error break; default: break; } } } // process presence events while (GameInterface.hasMorePresenceEvents()) { StringBuilder user = new StringBuilder(64); int presence; if (0 == GameInterface.getNextPresenceEvent(user, user.Capacity, out presence)) { uint index; string username = user.ToString(); if (KEYWORD_DOCENT != username && KEYWORD_GAME_LAUNCHER != username) { if (this.clientUsernameMap.ContainsKey(username)) { this.clientUsernameMap.TryGetValue(username, out index); if (index >= 0 && clients.Count > 0 && index < clients.Count) { this.clients [(int)index].SetStatus(presence); if (presence == 5) { DisconnectClient(this.clients [(int)index]); // if (this.gameStarted) // analytics.TrackControllerDisconnected(); } else if (presence == 0 && !this.gameStarted) { string registrationMessage = API_KEYWORD_REGISTERED + this.serverIpAddress + "," + index.ToString() + "," + this.gameId; GameInterface.dispatchEvent(username, registrationMessage); // LoadSave.savedData.Add (Time.time.ToString () + " : OUT :" + username + " " + registrationMessage + "\n"); WriteToLog(" 1 registering user: "******"," + index.ToString() + "," + this.gameId; GameInterface.dispatchEvent(username, registrationMessage); // LoadSave.savedData.Add (Time.time.ToString () + " : OUT :" + username + " " + registrationMessage + "\n"); WriteToLog(" 2 registering user: "******" : IN :" + username + " " + msgString + "\n"); WriteToLog(" " + username + " sent a message: " + msgString); if (username.Equals(KEYWORD_GAME_LAUNCHER)) { EscEvent evt = EscEvent.FromString(msgString); string requestedGameID = evt.attributes ["game"]; if (msgString.StartsWith(API_KEYWORD_START)) { if (requestedGameID.Equals(this.gameId)) { this.gameStarted = true; this.gameStartRound = Convert.ToInt32(evt.attributes ["round"]); this.gameDifficulty = Convert.ToInt32(evt.attributes ["difficulty"]); StartGame(); OnGameStart(requestedGameID); // track how many total controllers are registered //analytics.TrackGameLoad(GetClientRegisteredCount()); // track the round started //analytics.TrackRoundStart(this.gameStartRound.ToString()); // track how many total controllers are ready to play the game //analytics.TrackGameStart(GetClientReadyCount ()); } } else if (msgString.StartsWith(API_KEYWORD_PAUSE)) { if (requestedGameID.Equals(this.gameId)) { this.gamePaused = !this.gamePaused; PauseControllers(); OnGamePause(requestedGameID); // track game pause //analytics.TrackGamePause(GetClientReadyCount ()); } } else if (msgString.StartsWith(API_KEYWORD_STOP)) { if (requestedGameID.Equals(this.gameId)) { this.gameStarted = false; OnGameStop(requestedGameID); WriteToLog(msgString); // track the round ended //analytics.TrackRoundEnd(this.gameStartRound.ToString()); } } else if (msgString.StartsWith(API_KEYWORD_QUIT)) { if (requestedGameID.Equals(this.gameId)) { this.gameStarted = false; OnGameEnd(requestedGameID); Application.Quit(); WriteToLog(msgString); // track game end //analytics.TrackGameEnd(GetClientReadyCount ()); } } } else if (!username.Equals(KEYWORD_DOCENT)) { if (this.clientUsernameMap.ContainsKey(username)) { this.clientUsernameMap.TryGetValue(username, out index); if (index >= clients.Count) { WriteToLog(" " + username + " has an invalid index: " + index.ToString()); continue; } if (msgString.StartsWith(API_KEYWORD_REGISTERED)) { string ipAddress = msgString.Substring(API_KEYWORD_REGISTERED.Length); this.clients [(int)index].Register(ipAddress); if (null != OnClientRegistered) { OnClientRegistered(this.clients [(int)index]); } } else if (msgString.StartsWith(API_KEYWORD_INIT)) { this.clients [(int)index].SetReadiness(true); if (null != OnClientInitialized) { OnClientInitialized(this.clients [(int)index], EscEvent.FromString(msgString)); } } else { EscEvent evt = EscEvent.FromString(msgString); this.clients [(int)index].AppendEvent(evt); } } } else if (username.Equals(KEYWORD_DOCENT)) { if (msgString.StartsWith(API_KEYWORD_APPLY_PARAMS)) { // determine which command it is and parse it... EscEvent evt = EscEvent.FromString(msgString); int modeParam1 = 0; int modeParam2 = 0; int modeParam3 = 0; try { // WARNING: This won't work if mode3 was sent but mode 2 was not modeParam1 = Int32.Parse(evt.attributes ["mode1"]); modeParam2 = Int32.Parse(evt.attributes ["mode2"]); modeParam3 = Int32.Parse(evt.attributes ["mode3"]); } catch { WriteToLog(" error parsing docent parameters "); } // store parameters locally... this.gameStringParameter1 = evt.attributes ["param1"]; this.gameStringParameter2 = evt.attributes ["param2"]; this.gameStringParameter3 = evt.attributes ["param3"]; this.gameModeParameter1 = modeParam1; this.gameModeParameter2 = modeParam2; this.gameModeParameter3 = modeParam3; // notify listeners that a change was applied if (null != OnParametersUpdated) { OnParametersUpdated(); } } else if (msgString.StartsWith(KEYWORD_NEXT)) { // notify listeners that the next button was pressed if (null != OnNextScreen) { OnNextScreen(); } } else if (msgString.StartsWith(KEYWORD_CHECK_IF_LOADED)) { string options = API_KEYWORD_GAME_PREVIOUSLY_LOADED + "gameId=" + this.gameId + ",gameStarted=" + this.gameStarted; GameInterface.dispatchEvent(username, options); // LoadSave.savedData.Add (Time.time.ToString () + " : OUT :" + username + " " + options + "\n"); } } } } if (null != this.server && this.server.StateVarsAreStale()) { this.server.PropagateStateVars(); } }
//! Command to interface which dispatches start of game event to all controller clients. public int EndRound() { return(GameInterface.endRound()); }
//! Command to interface which dispatches start of game event to all controller clients. public int CancelRound() { return(GameInterface.cancelRound()); }
//! Command to interface which dispatches start of game event to all controller clients. public int StartRound() { return(GameInterface.startRound()); }
//! Command to interface which dispatches canceling game event to all controller clients. public int CancelGame() { return(GameInterface.cancelGame()); }
//! Returns indication of the Esc-Unity-Plugin connection to the XMPP server. public bool IsConnected() { return(GameInterface.isConnected()); }