private MongoGameStateData.GameStateData initializeGameState() { MongoGameStateData.GameStateData stateData = new MongoGameStateData.GameStateData(); stateData.Board = new MongoGameStateData.HexBoard(); string boardStr = ""; stateData.Board.Width = 84; stateData.Board.Height = 84; for (int y = 0; y < stateData.Board.Height; y++) { for (int x = 0; x < stateData.Board.Width; x++) { if (random.Next(0, 100) < 10) { boardStr += "0"; } else { if (random.Next(0, 100) < 15) boardStr += 2; else if (random.Next(0, 100) < 6) boardStr += 3; else boardStr += 1; } boardStr += (y / (stateData.Board.Height / 3)) + 1; } boardStr += "|"; } stateData.Board.BoardStr = boardStr; stateData.LastGeneration = DateTime.UtcNow.AddMinutes(1); stateData.Factions = new List<MongoGameStateData.GameFaction>(); for (int f = 0; f < 3; f++) { var gameFaction = new MongoGameStateData.GameFaction(); gameFaction.Units = new List<MongoGameStateData.GameUnit>(); gameFaction.Id = ObjectId.GenerateNewId().ToString(); switch (f) { case 0: gameFaction.Color = "#FF87B7"; break; case 1: gameFaction.Color = "#C4E283"; break; case 2: gameFaction.Color = "#9985E5"; break; } var numOfUnits = 300; for (int i = 0; i < numOfUnits; i++) { var unitType = random.Next(0, 100); int x; int y; while (true) { x = random.Next(0, stateData.Board.Width); y = random.Next(stateData.Board.Height / 3*f, stateData.Board.Height / 3*(f+1)); if (!gameFaction.Units.Any(a => a.X == x && a.Y == y)) { break; } } MongoGameStateData.GameUnit gameUnit; if (unitType < 60) { gameUnit = new MongoGameStateData.GameUnit() { Id = ObjectId.GenerateNewId().ToString(), Health = 2, UnitType = GameUnitType.Infantry, X = x, Y = y }; } else if (unitType < 90) { gameUnit = new MongoGameStateData.GameUnit() { Id = ObjectId.GenerateNewId().ToString(), Health = 6, UnitType = GameUnitType.Tank, X = x, Y = y }; } else { gameUnit = new MongoGameStateData.GameUnit() { Id = ObjectId.GenerateNewId().ToString(), Health = 16, UnitType = GameUnitType.Base, X = x, Y = y }; } gameFaction.Units.Add(gameUnit); } stateData.Factions.Add(gameFaction); } stateData.Insert(); return stateData; }
private void loadGameState() { var states = MongoGameStateData.Collection.GetAll(); MongoGameStateData.GameStateData stateData; if (!states.Any()) { stateData = initializeGameState(); } else { stateData = states[0]; } StateData = stateData; }