void _slaveClient_GotGameUpdate(object sender, GameInstance.GameUpdateEventArgs e) { GamePlayer player = (GamePlayer)sender; if (e.Message == GameMessage.EndGame) { // the slaves can't end it } else if (e.Message == GameMessage.SetReady) { player.Device.Ready = true; player.SendUpdate(GameMessage.SetReadyResponse, new byte[0]); SendGameInfo(); } else if (e.Message == GameMessage.PushState) { byte[] buffer = new byte[e.GameState.Length + 10]; Encoding.ASCII.GetBytes("mobilesrc").CopyTo(buffer, 0); buffer[9] = 1; e.GameState.CopyTo(buffer, 10); //_masterClient.SendUpdate(GameMessage.NOP, new byte[1500]); _masterClient.SendUpdate(GameMessage.PushStateResponse, e.GameState); } else { // Send everything to the master // _masterClient.SendUpdate(e.Message, e.GameState); } }
void _masterClient_GotGameUpdate(object sender, GameInstance.GameUpdateEventArgs e) { GamePlayer player = (GamePlayer)sender; if (e.Message == GameMessage.EndGame) { Trace.WriteLine("Ending Game: " + player.Device.User); lock (_gameDb) { Game.Completed = true; _gameDb.SubmitChanges(); } if (null != GameEnded) { GameEnded(this, new EventArgs()); } player.Close(); } else if (e.Message == GameMessage.AcceptUser) { Int16 queryStringLength = System.Net.IPAddress.NetworkToHostOrder(BitConverter.ToInt16(e.GameState, 0)); string queryString = System.Text.Encoding.ASCII.GetString(e.GameState, 2, queryStringLength); NameValueCollection queryBucket = System.Web.HttpUtility.ParseQueryString(queryString); string device = queryBucket["id"]; foreach (GamePlayer slave in _slaveClients) { if (string.Equals(slave.Device.Device, device, StringComparison.InvariantCultureIgnoreCase)) { slave.Device.Accepted = true; slave.SendUpdate(GameMessage.AcceptUserResponse, new byte[0]); break; } } SendGameInfo(); } else if (e.Message == GameMessage.RejectUser) { Int16 queryStringLength = System.Net.IPAddress.NetworkToHostOrder(BitConverter.ToInt16(e.GameState, 0)); string queryString = System.Text.Encoding.ASCII.GetString(e.GameState, 2, queryStringLength); NameValueCollection queryBucket = System.Web.HttpUtility.ParseQueryString(queryString); string device = queryBucket["id"]; foreach (GamePlayer slave in _slaveClients) { if (string.Equals(slave.Device.Device, device, StringComparison.InvariantCultureIgnoreCase)) { slave.SendUpdate(GameMessage.RejectUserResponse, new byte[0]); slave.Close(); _slaveClients.Remove(slave); break; } } SendGameInfo(); } else if (e.Message == GameMessage.Start) { this.Game.Ready = true; SendGameInfo(); Thread.Sleep(1500); player.SendUpdate(GameMessage.StartResponse, new byte[0]); } else if (e.Message == GameMessage.PushState) { //Int16 queryStringLength = System.Net.IPAddress.NetworkToHostOrder(BitConverter.ToInt16(e.GameState, 0)); //string queryString = System.Text.Encoding.ASCII.GetString(e.GameState, 2, queryStringLength); //NameValueCollection queryBucket = System.Web.HttpUtility.ParseQueryString(queryString); //byte[] buffer = new byte[e.GameState.Length + 10]; //Encoding.ASCII.GetBytes("mobilesrc").CopyTo(buffer, 0); //buffer[9] = 1; //e.GameState.CopyTo(buffer, 10); foreach (GamePlayer slave in _slaveClients) { Console.WriteLine("_masterClient_GotGameUpdate: " + slave.Device.User); //slave.SendUpdate(GameMessage.NOP, new byte[1500]); try { slave.SendUpdate(GameMessage.PushStateResponse, e.GameState); } catch (Exception ex) { Console.WriteLine("ERROR _masterClient_GotGameUpdate"); Console.WriteLine(ex.ToString()); } } } else { } }
public static GameInstance CreateInstance(UdpClient client, IPEndPoint endPoint, NameValueCollection queryString, byte[] mapData) { lock (_gameDb) { var games = (from delGame in _gameDb.Games where delGame.User == queryString["user"] select delGame); foreach (Database.Game delGame in games) { delGame.Completed = true; //_gameDb.Games.DeleteOnSubmit(delGame); } Database.Game game = new Database.Game(); game.PkGame = Guid.NewGuid(); game.MapData = System.Text.Encoding.ASCII.GetString(mapData); game.User = queryString["user"]; if (string.IsNullOrEmpty(game.User)) { game.User = queryString["id"]; } game.Completed = false; game.Ready = false; game.Date = DateTime.Now; game.Width = Convert.ToInt32(queryString["width"]); game.Height = Convert.ToInt32(queryString["height"]); float latitude = 0, longitude = 0; float.TryParse(queryString["latitude"], out latitude); game.Latitude = latitude; float.TryParse(queryString["longitude"], out longitude); game.Longitude = longitude; game.GameId = Convert.ToInt32(queryString["gameId"]); // TODO: lookup here float version = 0; float.TryParse(queryString["gameversion"], out version); Database.GameDevice device = new Database.GameDevice(); device.PkGameDevice = Guid.NewGuid(); device.Game = game; device.User = queryString["user"]; if (string.IsNullOrEmpty(device.User)) { device.User = queryString["id"]; } device.Latitude = game.Latitude; device.Longitude = game.Longitude; device.LocalAddress = queryString["address"]; device.LocalPort = int.Parse(queryString["port"]); device.Device = queryString["id"]; device.Date = DateTime.Now; device.Ready = true; device.Accepted = true; _gameDb.Games.InsertOnSubmit(game); _gameDb.GameDevices.InsertOnSubmit(device); _gameDb.SubmitChanges(); Trace.WriteLine("Starting Game: " + game.User); GameInstance instance = new GameInstance(game, device, client, endPoint); GamePlayer.SendUpdate(GameMessage.CreateResponse, client, endPoint, Encoding.ASCII.GetBytes(game.PkGame.ToString()), 1); ActiveGames.Add(game.PkGame, instance); Thread gameThread = new Thread(new ParameterizedThreadStart(LaunchGame)); gameThread.IsBackground = true; gameThread.Start(instance); return(instance); } }
public static GameInstance CreateInstance(UdpClient client, IPEndPoint endPoint, NameValueCollection queryString, byte[] mapData) { lock (_gameDb) { var games = (from delGame in _gameDb.Games where delGame.User == queryString["user"] select delGame); foreach (Database.Game delGame in games) { delGame.Completed = true; //_gameDb.Games.DeleteOnSubmit(delGame); } Database.Game game = new Database.Game(); game.PkGame = Guid.NewGuid(); game.MapData = System.Text.Encoding.ASCII.GetString(mapData); game.User = queryString["user"]; if (string.IsNullOrEmpty(game.User)) { game.User = queryString["id"]; } game.Completed = false; game.Ready = false; game.Date = DateTime.Now; game.Width = Convert.ToInt32(queryString["width"]); game.Height = Convert.ToInt32(queryString["height"]); float latitude = 0, longitude = 0; float.TryParse(queryString["latitude"], out latitude); game.Latitude = latitude; float.TryParse(queryString["longitude"], out longitude); game.Longitude = longitude; game.GameId = Convert.ToInt32(queryString["gameId"]); // TODO: lookup here float version = 0; float.TryParse(queryString["gameversion"], out version); Database.GameDevice device = new Database.GameDevice(); device.PkGameDevice = Guid.NewGuid(); device.Game = game; device.User = queryString["user"]; if (string.IsNullOrEmpty(device.User)) { device.User = queryString["id"]; } device.Latitude = game.Latitude; device.Longitude = game.Longitude; device.LocalAddress = queryString["address"]; device.LocalPort = int.Parse(queryString["port"]); device.Device = queryString["id"]; device.Date = DateTime.Now; device.Ready = true; device.Accepted = true; _gameDb.Games.InsertOnSubmit(game); _gameDb.GameDevices.InsertOnSubmit(device); _gameDb.SubmitChanges(); Trace.WriteLine("Starting Game: " + game.User); GameInstance instance = new GameInstance(game, device, client, endPoint); GamePlayer.SendUpdate(GameMessage.CreateResponse, client, endPoint, Encoding.ASCII.GetBytes(game.PkGame.ToString()), 1); ActiveGames.Add(game.PkGame, instance); Thread gameThread = new Thread(new ParameterizedThreadStart(LaunchGame)); gameThread.IsBackground = true; gameThread.Start(instance); return instance; } }
static void ProcessMessage(UdpClient client, byte[] bytes, IPEndPoint endPoint) { // Send this - TIME (4-bytes)|MessageType|MessageData int messageOffset = 0; Int32 timeStamp = System.Net.IPAddress.NetworkToHostOrder(BitConverter.ToInt32(bytes, messageOffset)); messageOffset += 4; GameMessage gameMessage = (GameMessage)bytes[messageOffset]; messageOffset += 1; byte[] data = new byte[bytes.Length - messageOffset]; Array.Copy(bytes, messageOffset, data, 0, data.Length); int hash = endPoint.ToString().GetHashCode(); if (_mappings.ContainsKey(hash) && !_mappings[hash].Game.Completed) { GameInstance instance = _mappings[hash]; instance.EnqueuePacket(endPoint, timeStamp, gameMessage, data); } else { // We aren't a game member yet int offset = 0; Int16 queryStringLength = 0; if (data.Length >= 2) { queryStringLength = System.Net.IPAddress.NetworkToHostOrder(BitConverter.ToInt16(data, offset)); offset += 2; } if (gameMessage == GameMessage.Replay) { Thread thread = new Thread(new ParameterizedThreadStart(ReplayGame)); thread.Start(new object[] { client, endPoint }); } else if (gameMessage == GameMessage.Create) { // Parse like a query string (for compatability with GAE version) string queryString = System.Text.Encoding.ASCII.GetString(data, offset, queryStringLength); offset += queryStringLength; Trace.WriteLine(string.Format("Received Create: {0}", queryString)); Int16 mapDataLength = System.Net.IPAddress.NetworkToHostOrder(BitConverter.ToInt16(data, offset)); offset += 2; // Create the game and farm it out if (offset + mapDataLength <= data.Length && mapDataLength > 0) { byte[] mapData = new byte[mapDataLength + 1]; Array.Copy(data, offset, mapData, 0, mapDataLength); GameInstance instance = GameInstance.CreateInstance(client, endPoint, System.Web.HttpUtility.ParseQueryString(queryString), mapData); instance.GameEnded += new EventHandler(instance_GameEnded); lock (_mappings) { _mappings.Add(hash, instance); } } // We had something bad } else if (gameMessage == GameMessage.Join) { // Parse like a query string (for compatability with GAE version) string queryString = System.Text.Encoding.ASCII.GetString(data, offset, queryStringLength); offset += queryStringLength; Trace.WriteLine(string.Format("Received Join: {0}", queryString)); // Create the game and farm it out GameInstance instance = GameInstance.JoinGame(client, endPoint, System.Web.HttpUtility.ParseQueryString(queryString)); lock (_mappings) { _mappings.Add(hash, instance); } } else if (gameMessage == GameMessage.List) { // Parse like a query string (for compatability with GAE version) string queryString = System.Text.Encoding.ASCII.GetString(data, offset, queryStringLength); offset += queryStringLength; Trace.WriteLine(string.Format("Received List: {0}", queryString)); GameInstance.ListGames(client, endPoint, System.Web.HttpUtility.ParseQueryString(queryString)); } else if (gameMessage == GameMessage.NOP) { Trace.WriteLine(string.Format("Received NOP")); // If we get a NOP, just ignore it //GameInstance.GamePlayer.SendUpdate(GameMessage.NOP, client, endPoint, new byte[0], 1); } } }