public void unserializeFromComplexObject(DatabaseArray worlddata) { for (var a = 0; a < worlddata.Count; a++) { var ct = worlddata.GetObject(a); var type = (uint)ct.GetValue("type"); var layerNum = ct.GetInt("layer", 0); var xs = ct.GetBytes("x", new byte[0]); var ys = ct.GetBytes("y", new byte[0]); var b = 0; while (b < xs.Length) { var nx = (uint)(((int)xs[b] << 8) + (int)xs[b + 1]); var ny = (uint)(((int)ys[b] << 8) + (int)ys[b + 1]); var num = type; if (num <= 77U) { if (num != 43U) { if (num != 77U) { goto IL_168; } this.setBrickSound(ItemTypes.Piano, nx, ny, ct.GetUInt("id", 0U)); } else { this.setBrickCoindoor(nx, ny, (uint)ct.GetValue("goal"), false); } } else if (num != 83U) { if (num != 242U) { if (num != 1000U) { goto IL_168; } this.setBrickLabel(nx, ny, ct.GetString("text", "no text found")); } else { this.setBrickPortal(nx, ny, ct.GetUInt("rotation", 0U), ct.GetUInt("id", 0U), ct.GetUInt("target", 0U), false); } } else { this.setBrickSound(ItemTypes.Drums, nx, ny, ct.GetUInt("id", 0U)); } IL_179: b += 2; continue; IL_168: this.setBrick(layerNum, nx, ny, type, true); goto IL_179; } } }
public static List <World.Block> FromWorldData(this DatabaseArray input) { var blocks = new List <World.Block>(); if (input == null || input.Count == 0) { return(blocks); } for (int i = 0; i < input.Count; i++) { if (input.Contains(i) && input.GetObject(i).Count != 0) { var obj = input.GetObject(i); dynamic temp = new World.Block(); foreach (var kvp in obj) { temp[kvp.Key] = kvp.Value; } byte[] x = obj.TryGetBytes("x", new byte[0]), y = obj.TryGetBytes("y", new byte[0]); byte[] x1 = obj.TryGetBytes("x1", new byte[0]), y1 = obj.TryGetBytes("y1", new byte[0]); for (int j = 0; j < x1.Length; j++) { temp.Locations.Add(new World.Block.Location() { X = x1[j], Y = y1[j] }); } for (int k = 0; k < x.Length; k += 2) { temp.Locations.Add(new World.Block.Location() { X = (x[k] << 8) + x[k + 1], Y = (y[k] << 8) + y[k + 1] }); } blocks.Add(temp); } } return(blocks); }
// This method is called when a player sends a message into the server code public override void GotMessage(Player player, Message message) { switch (message.Type) { // player has moved up, down, left, or right case "move": { //Console.WriteLine("Player position before move: " + player.positionX + ", " + player.positionY); //debug int messageX = message.GetInt(0); int messageY = message.GetInt(1); player.positionX = player.positionX + messageX; player.positionY = player.positionY + messageY; //Console.WriteLine("Player " + player.Id + " is moving to (" + player.positionX + ", " + player.positionY + ")"); //debug Broadcast("PlayerMove", player.Id, messageX, messageY); break; } case "LoadPlayers": { // this is how you broadcast a messa"LoadPlayers": //Update them on who is already in the game foreach (Player x in players) { if (x != null && x != player) { //Console.WriteLine("Sending Player " + player.Id + " Player " + x.Id + " Position (" + x.positionX + ", " + x.positionY + ")"); //debug player.Send("UserJoined", x.Id, x.positionX, x.positionY, x.ConnectUserId); } } break; } //Load the coins, randomize them if they dont exist case "LoadCoins": { Random random = new Random(); if (Coins == null) { DatabaseArray coins = new DatabaseArray(); for (int i = 0; i < 5; i++) { DatabaseObject coin = new DatabaseObject(); int randomX = random.Next(0, 9); int randomY = random.Next(0, 9); coin.Set("xTile", randomX); coin.Set("yTile", randomY); coin.Set("Uses", 0); player.Send("coinSetup", randomX, randomY, 0); coins.Set(i, coin); } Coins = coins; quest.Set("Coins", Coins); quest.Save(); } else { for (int i = 0; i < 5; i++) { DatabaseObject coin = Coins.GetObject(i); player.Send("coinSetup", coin.GetInt("xTile"), coin.GetInt("yTile"), coin.GetInt("Uses")); } } break; } // client is asking for data about player to draw on the screen case "playerInfo": { if (players[player.Id - 1] == null) { player.Send("noSuchPlayer"); } else { player.Send("playerInfo", players[player.Id - 1].positionX, players[player.Id - 1].positionY, playerConnectUserId, player.characterClass); } break; } case "MapTileChanged": { int xTile = message.GetInt(0); int yTile = message.GetInt(1); int newTileType = message.GetInt(2); //Console.WriteLine("Map Tile Change From Player " + player.Id + " (" + xTile + "," + yTile + ") to type: " + newTileType); Broadcast("MapTileChanged", player.Id, xTile, yTile, newTileType); break; } // update server's variables for this player stat case "updateStat": { String statType = message.GetString(0); switch (statType) { case "AP": player.AP = message.GetInt(1); //Console.WriteLine("server: player's AP changed! " + player.AP); break; case "lumber": amountLumber = message.GetInt(1); //Console.WriteLine("server: player's lumber changed! " + amountLumber); break; case "cherry": amountCherry = message.GetInt(1); //Console.WriteLine("server: player's cherry changed! " + amountCherry); break; } break; } case "win": { PlayerIO.BigDB.Load(mapType, levelKey, delegate(DatabaseObject result) { // todo: change these based on what you got in the level int gainedxp = 0; // = result.GetInt("XP"); int gainedcoin = 0; // = result.GetInt("Coin"); if (result != null) { gainedxp = result.GetInt("XP", 0); //How much XP the Level was worth gainedcoin = result.GetInt("Coin", 0); //How mucg coin the level was worth } String nextLevel = ""; //Check to see if player completed Tutorial level, in which case update their tutorial value if (player.PlayerObject.GetInt("tutorial") == 1) { DatabaseArray abilities = new DatabaseArray(); abilities.Add("Crafter_Bridge"); player.PlayerObject.Set("abilities", abilities); player.PlayerObject.Set("tutorial", 2); nextLevel = "Tutorial_2"; } else if (player.PlayerObject.GetInt("tutorial") == 2) { DatabaseArray abilities = player.PlayerObject.GetArray("abilities"); abilities.Add("Planter_RedFlower"); player.PlayerObject.Set("tutorial", 3); nextLevel = "Tutorial_3"; } else if (player.PlayerObject.GetInt("tutorial") == 3) { player.PlayerObject.Set("tutorial", 4); nextLevel = "Tutorial_4"; } else if (player.PlayerObject.GetInt("tutorial") == 4) { DatabaseArray abilities = player.PlayerObject.GetArray("abilities"); abilities.Add("Cook_MonsterBacon"); player.PlayerObject.Set("tutorial", 5); nextLevel = "Tutorial_5"; } else if (player.PlayerObject.GetInt("tutorial") == 5) { nextLevel = "Class_Choose"; player.PlayerObject.Set("tutorial", 6); } int experience = player.PlayerObject.GetInt("xp"); /*int level = player.PlayerObject.GetInt("level"); * * * //Check to see if the player has enough XP to level up * if (experience + gainedxp >= levelXP(level + 1)) * { * player.PlayerObject.Set("level", level + 1); * }*/ player.PlayerObject.Set("xp", experience + gainedxp); player.PlayerObject.Set("coin", player.PlayerObject.GetInt("coin", 0) + gainedcoin); player.PlayerObject.Save(delegate() { // quest is finished; remove this quest from the table // todo: what happens if another player is playing this quest? PlayerIO.BigDB.DeleteKeys("NewQuests", questID); //Console.WriteLine("deleted newquest key"); PlayerIO.BigDB.Load("PlayerObjects", player.ConnectUserId, delegate(DatabaseObject thisPlayer) { thisPlayer.Set("questID", "noQuest"); thisPlayer.Save(delegate() { Broadcast("win", gainedxp, gainedcoin, nextLevel); }); } ); }); }, delegate(PlayerIOError error) { //Console.WriteLine(error.ToString()); }); break; } // recieves one string that is the newly updated map; save to associated quest object case "QuestMapUpdate": { questMap = message.GetString(0); player.GetPlayerObject( delegate(DatabaseObject updatedPlayerObject){ PlayerIO.BigDB.Load("NewQuests", questID, delegate(DatabaseObject dbo) { dbo.Set("tileValues", message.GetString(0)); dbo.Save(); }); }); break; } case "MonsterAPChange": { int newAp = message.GetInt(0); int monsterIndex = message.GetInt(1); PlayerIO.BigDB.Load("newQuests", questID, delegate(DatabaseObject dbo) { DatabaseArray monsters = dbo.GetArray("Monsters"); monsters.GetObject(monsterIndex).Set("AP", newAp); dbo.Save(); }); Broadcast("MonsterAPChange", player.Id, newAp, monsterIndex); break; } case "SpriteMove": { String type = message.GetString(0); int newXTile = message.GetInt(1); int newYTile = message.GetInt(2); int monsterIndex = message.GetInt(3); PlayerIO.BigDB.Load("newQuests", questID, delegate(DatabaseObject dbo) { DatabaseArray sprites = dbo.GetArray(type); DatabaseObject sprite = sprites.GetObject(monsterIndex); sprite.Set("xTile", newXTile); sprite.Set("yTile", newYTile); dbo.Save(); }); break; } case "AddSprite": { String type = message.GetString(0); int xTile = message.GetInt(1); int yTile = message.GetInt(2); String name = message.GetString(3); int range = message.GetInt(4); DatabaseObject newSprite = new DatabaseObject(); newSprite.Set("xTile", xTile); newSprite.Set("yTile", yTile); newSprite.Set("Type", name); newSprite.Set("Uses", 0); newSprite.Set("Range", range); Broadcast("AddSprite", xTile, yTile, name, range); PlayerIO.BigDB.Load("newQuests", questID, delegate(DatabaseObject dbo) { if (dbo.Contains(type)) { DatabaseArray sprites = dbo.GetArray(type); sprites.Add(newSprite); } else { DatabaseArray sprites = new DatabaseArray(); sprites.Add(newSprite); dbo.Set(type, sprites); } dbo.Save(); }); break; } case "SpriteUse": { int index = message.GetInt(0); int uses = message.GetInt(1); //Console.WriteLine("Sprite " + index + " Is being used for the " + uses + " time"); Broadcast("SpriteUse", player.Id, index); PlayerIO.BigDB.Load("newQuests", questID, delegate(DatabaseObject dbo) { DatabaseArray sprites = dbo.GetArray("Effects"); DatabaseObject sprite = sprites.GetObject(index); sprite.Set("Uses", uses); dbo.Save(); }); break; } case "CoinUse": { int index = message.GetInt(0); int uses = message.GetInt(1); Broadcast("CoinUse", player.Id, index); PlayerIO.BigDB.Load("newQuests", questID, delegate(DatabaseObject dbo) { DatabaseArray sprites = dbo.GetArray("Coins"); DatabaseObject sprite = sprites.GetObject(index); Coins.GetObject(index).Set("Uses", uses); sprite.Set("Uses", uses); dbo.Save(); }); break; } } }
// This method is called whenever a player joins the game public override void UserJoined(Player player) { // this is how you send a player a message //Send the player their player Number. playerConnectUserId = player.ConnectUserId; if (numPlayers < players.Length) { players[numPlayers] = player; //Console.WriteLine("New Player " + player.Id); numPlayers++; // if player is not attached to a quest, give them a new quest ID PlayerIO.BigDB.Load("PlayerObjects", player.ConnectUserId, delegate(DatabaseObject result) { player.characterClass = result.GetString("role", "Novice"); player.costume = result.GetString("costume", "novice"); player.tutorialLevel = result.GetInt("tutorial", 1); //Console.WriteLine("player class: " + player.characterClass); if (questID != null && (!result.Contains("questID") || result.GetString("questID") == "noQuest")) { result.Set("questID", questID); result.Save(); PlayerIO.BigDB.Load("NewQuests", questID, delegate(DatabaseObject quest) { this.quest = quest; DatabaseObject questPlayerData = new DatabaseObject(); player.positionX = startX; player.positionY = startY; questPlayerData.Set("positionX", startX); questPlayerData.Set("positionY", startY); questPlayerData.Set("AP", 20); quest.GetObject("players").Set(player.ConnectUserId, questPlayerData); quest.GetObject("players").Set("numPlayers", quest.GetObject("players").Count - 1); quest.Save(delegate() { player.Send("init", player.Id, player.ConnectUserId, startX, startY, questID, 20, levelKey, "", player.characterClass); }); Broadcast("UserJoined", player.Id, player.positionX, player.positionY); }); } // if player does not have a questID associated with it // create new object in Quests db else if (!result.Contains("questID") || result.GetString("questID") == "noQuest") { // create new quest object DatabaseObject newQuest = new DatabaseObject(); // create array for players playing this new quest object DatabaseObject questPlayers = new DatabaseObject(); // create new object for this player and their quest data DatabaseObject questPlayerData = new DatabaseObject(); //Console.WriteLine("questPlayers contents: " + questPlayers.ToString()); //Console.WriteLine("Level key: " + levelKey); //Add Static Map to Quest, to be updated later PlayerIO.BigDB.Load(mapType, levelKey, delegate(DatabaseObject staticMap) { player.positionX = startX = staticMap.GetInt("startX", 0); player.positionY = startY = staticMap.GetInt("startY", 0); questPlayerData.Set("positionX", startX); questPlayerData.Set("positionY", startY); questPlayerData.Set("AP", 20); // add this player to players playing this quest questPlayers.Set("numPlayers", 1); questPlayers.Set(player.ConnectUserId, questPlayerData); newQuest.Set("players", questPlayers); newQuest.Set("StaticMapKey", staticMap.Key); newQuest.Set("tileValues", staticMap.GetString("tileValues")); newQuest.Set("MonsterCount", staticMap.GetInt("MonsterCount")); if (staticMap.Contains("Monsters")) { DatabaseArray monsters = staticMap.GetArray("Monsters"); DatabaseArray newMonsters = new DatabaseArray(); for (int i = 1; i <= monsters.Count; i++) { DatabaseObject monster = new DatabaseObject(); monster.Set("Type", monsters.GetObject(i - 1).GetString("Type")); monster.Set("xTile", monsters.GetObject(i - 1).GetInt("xTile")); monster.Set("yTile", monsters.GetObject(i - 1).GetInt("yTile")); monster.Set("AP", monsters.GetObject(i - 1).GetInt("AP")); newMonsters.Add(monster); } newQuest.Set("Monsters", newMonsters); } if (staticMap.Contains("Buttons")) { DatabaseArray buttons = staticMap.GetArray("Buttons"); DatabaseArray newButtons = new DatabaseArray(); for (int i = 1; i <= buttons.Count; i++) { DatabaseObject button = new DatabaseObject(); button.Set("xTile", buttons.GetObject(i - 1).GetInt("xTile")); button.Set("yTile", buttons.GetObject(i - 1).GetInt("yTile")); button.Set("xOpen", buttons.GetObject(i - 1).GetInt("xOpen")); button.Set("yOpen", buttons.GetObject(i - 1).GetInt("yOpen")); newButtons.Add(button); } newQuest.Set("Buttons", newButtons); } Console.WriteLine("Setting up new quest " + newQuest.ToString()); // add this quest object to Quests db PlayerIO.BigDB.CreateObject("NewQuests", null, newQuest, delegate(DatabaseObject addedQuest) { questID = addedQuest.Key; addedQuest.Set("RoomID", this.RoomId); Console.WriteLine("made new questID! new questID is: " + questID); result.Set("questID", addedQuest.Key); result.Save(); //levelKey = addedQuest.Key; // tell client to initialize (board, monsters, player object & player sprite) with max AP amount addedQuest.Save(delegate() { quest = addedQuest; player.Send("init", player.Id, player.ConnectUserId, startX, startY, questID, 20, levelKey, "", player.costume); }); Broadcast("UserJoined", player.Id, player.positionX, player.positionY); }); }); // save positions in the serverside player.AP = 20; } // else, this player has a questID saved else { questID = result.GetString("questID"); //levelKey = questID; // obtain player's last position and save to serverside PlayerIO.BigDB.Load("NewQuests", questID, delegate(DatabaseObject questObject) { quest = questObject; questObject.Set("RoomID", this.RoomId); questObject.Save(); Coins = questObject.GetArray("Coins"); String resources = ""; // player's resources, to pass to client if (questObject != null) { levelKey = questObject.GetString("StaticMapKey"); // extract players playing this quest DatabaseObject playersInQuest = questObject.GetObject("players"); DatabaseObject thisPlayer = playersInQuest.GetObject(player.ConnectUserId); player.positionX = thisPlayer.GetInt("positionX"); player.positionY = thisPlayer.GetInt("positionY"); int startAP = thisPlayer.GetInt("AP"); if (thisPlayer.Contains("lastSessionEndTime")) { // figure out how much AP player should have based on how long they've been away lastSessionEndTime = thisPlayer.GetDateTime("lastSessionEndTime"); //Console.WriteLine("last session end time : " + lastSessionEndTime.ToString(DateTimeFormat)); int minutesPassedSinceLastPlay = (startSessionTime - lastSessionEndTime).Minutes; startAP += minutesPassedSinceLastPlay / 3; //Console.WriteLine("minutes passed: " + minutesPassedSinceLastPlay + ", amount of AP to add: " + (minutesPassedSinceLastPlay / 3) + ", starting AP: " + startAP); if (startAP > 20) { startAP = 20; } player.AP = startAP; } else { player.AP = 20; } // get information about player resources from db if (thisPlayer.Contains("resources")) { DatabaseObject resourcesObject = thisPlayer.GetObject("resources"); Console.WriteLine("resources object: " + resourcesObject.ToString()); if (resourcesObject.Contains("lumber")) { amountLumber = resourcesObject.GetInt("lumber"); resources += "Lumber:" + amountLumber; } if (resourcesObject.Contains("cherry")) { amountCherry = resourcesObject.GetInt("cherry"); resources += "/Cherry:" + amountCherry; } Console.WriteLine("resources string: " + resources); } } // tell client to initialize (board, monsters, player object & player sprite) player.Send("init", player.Id, player.ConnectUserId, player.positionX, player.positionY, questID, player.AP, levelKey, resources, player.costume); Broadcast("UserJoined", player.Id, player.positionX, player.positionY); } ); } } ); } else { player.Send("full"); } Console.WriteLine("userJoined is done"); }
public void addLastPlayedMap(String mapKey) { if (isGuest) { return; } DatabaseArray maps = PlayerObject.GetArray("LastPlayedMaps"); DatabaseObject mapEntry = null; DatabaseObject oldestEntry = null; int oldestIndex = 0; for (int i = 0; i < maps.Count; ++i) { DatabaseObject entry = maps.GetObject(i.ToString()); if (entry.Contains("Map")) { if (entry.GetString("Map") == mapKey) { mapEntry = entry; } else { if (oldestEntry == null || DateTime.Compare(entry.GetDateTime("Date"), oldestEntry.GetDateTime("Date")) < 0) { oldestEntry = entry; oldestIndex = i; } } } } // remove oldest entry if list is full //if (maps.Count > 8) { // maps.Remove(oldestIndex.ToString()); //} // if current map is already in last played list, just update date if (mapEntry != null) { mapEntry.Set("Date", DateTime.Now); } else { DatabaseObject entry = new DatabaseObject(); entry.Set("Map", mapKey); entry.Set("Date", DateTime.Now); maps.Add(entry); } PlayerObject.Remove("LastPlayedMaps"); //sort the array from the latest to oldest DatabaseArray newMaps = new DatabaseArray(); int newestIndex = 0; bool found = false; do { found = false; newestIndex = 0; DatabaseObject newestEntry = null; //foreach(DatabaseObject entry in maps) { for (int i = 0; i < maps.Count; ++i) { if (!maps.Contains(i.ToString())) { continue; } DatabaseObject entry = maps.GetObject(i.ToString()); if (!entry.Contains("Date")) { continue; } if (newestEntry == null || DateTime.Compare(entry.GetDateTime("Date"), newestEntry.GetDateTime("Date")) > 0) { newestEntry = entry; newestIndex = i; found = true; } } if (found) { DatabaseObject tmp = maps.GetObject(newestIndex.ToString()); maps.Remove(newestIndex.ToString()); bool duplicate = false; foreach (DatabaseObject dbo in newMaps) { if (dbo.GetString("Map") == tmp.GetString("Map")) { duplicate = true; break; } } if (duplicate) { continue; } DatabaseObject tmp2 = new DatabaseObject(); tmp2.Set("Map", tmp.GetString("Map")); tmp2.Set("Date", tmp.GetDateTime("Date")); //newMaps.Add(tmp2); newMaps.Insert(0, tmp2); } } while (found && newMaps.Count < 8); //newMaps = (DatabaseArray)newMaps.Reverse(); PlayerObject.Set("LastPlayedMaps", newMaps); /*maps = PlayerObject.GetArray("LastPlayedMaps"); * for (int i = 0; i < maps.Count; i++) { * Console.WriteLine(maps.GetObject(i).GetString("Map")); * }*/ savePlayerData(); }