public static Game ToGame(GameTransport game) { Game result = new Game(); result.Building = new Building(); result.Building.Floors = game.floors.Select(x => FloorData.ToFloor(x.d)).ToArray(); result.Building.TotalValue = game.game.d.buildingScore; result.Floors = game.game.d.floors; result.RoomsPerFloor = game.game.d.roomsPerFloor; result.PlayerVersions = new Dictionary<string, int>(); result.Players = game.game.d.players.Select(x => PlayerMetaData.ToPlayer(x)).ToList(); result.PlayerIds = game.game.d.players.Select(x => x.id).ToList(); result.Id = game.id; if (game.me != null && game.me.d != null) { result.PlayerScore.Add(game.me.d.id, game.me.d.score); } // set versions result.FloorVersions = game.floors.Select(x => x.v).ToArray(); result.GameMetaVersion = game.game.v; result.PlayerVersions.Add(game.me.d.id, game.me.v); result.StartUTC = game.game.d.start; result.RunningState = game.game.d.state; return result; }
public static WebGameTransport CheckGameVersion(Game sourceData, WebGameTransport game, int? floor, string playerId, int gameVersion, int floorVersion, int playerVersion) { game.id = sourceData.Id; if (sourceData.GameMetaVersion > gameVersion) { game.game = new VersionedGameData<GameMetaData>() { v = sourceData.GameMetaVersion, d = new GameMetaData() { floors = sourceData.Floors, roomsPerFloor = sourceData.RoomsPerFloor, players = GetPlayerModels(sourceData.Players), buildingScore = sourceData.Building.TotalValue, start= sourceData.StartUTC, state = sourceData.RunningState } }; if (sourceData.Winner != null) { game.game.d.winner = PlayerMetaData.FromPlayer(sourceData.Winner); } } if (floor.HasValue) { if (sourceData.FloorVersions[floor.Value] > floorVersion) { // only get floor with mismatched version game.floors = new List<VersionedGameData<FloorData>>() { new VersionedGameData<FloorData>() { v = sourceData.FloorVersions[floor.Value], d = new FloorData() { i = floor.Value, occ = sourceData.Building.Floors[floor.Value].OccupiedRooms, r = sourceData.Building.Floors[floor.Value].Rooms, t = sourceData.Building.Floors[floor.Value].Traps } } }; } } else { // get all floors for game (for initial requests) game.floors = new List<VersionedGameData<FloorData>>(); for (int fl = 0; fl < sourceData.Floors; fl++) { game.floors.Add(new VersionedGameData<FloorData>() { v = sourceData.FloorVersions[fl], d = new FloorData() { i = fl, occ = sourceData.Building.Floors[fl].OccupiedRooms, r = sourceData.Building.Floors[fl].Rooms, t = sourceData.Building.Floors[fl].Traps } }); } } if (sourceData.PlayerVersions[playerId] > playerVersion) { var player = sourceData.GetLocalPlayer(playerId); int playerScore = 0; sourceData.PlayerScore.TryGetValue(playerId, out playerScore); game.me = new VersionedGameData<PlayerMetaData>() { v = sourceData.PlayerVersions[playerId], d = new PlayerMetaData() { id = player.Id, username = player.Username, score = playerScore, floor = player.Floor, room = player.Room } }; } return game; }
public static Game Create(Player owner, string title, int floors, int roomsPerFloor, List<Player> players) { if (floors > MAX_GAME_FLOORS) floors = MAX_GAME_FLOORS; if (roomsPerFloor > MAX_GAME_ROOMS_PER_FLOOR) roomsPerFloor = MAX_GAME_ROOMS_PER_FLOOR; players.Add(owner); var playerIds = players.Select(x => x.Id).Distinct().ToList(); Game result = new Game() { Title = title, Creator = owner, PlayerIds = playerIds, Players = players.Select(x => x.Clone()).ToList(), Id = Guid.NewGuid().ToString("N"), Floors = floors, RoomsPerFloor = roomsPerFloor, Building = Building.Generate(floors, roomsPerFloor, MAX_ITEMS_PER_ROOM, 10), FloorVersions = new int[floors], PlayerVersions = new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase), GameMetaVersion = 1, RunningState = GameState.WaitingForAllToJoin }; for (int i = 0; i < floors; i++) { result.FloorVersions[i] = 1; } foreach (var id in playerIds) { result.PlayerVersions.Add(id, 1); result.PlayerScore.Add(id, 0); } return result; }