{"__v":39,"_id":"548a78adb77bb70b00ac8c10","category":{"__v":8,"_id":"543b9ef065bf840e00b473e0","project":"543b9b0065bf840e00b473d5","version":"543b9b0065bf840e00b473d8","pages":["543b9f11b1479b1400c42f58","548a78adb77bb70b00ac8c10","55082e75c79a211900a8de1b","55083e4e31eeba2d00d66a2d","550bfa9022ccb01700a79466","550c312a5fdefb19003d1201","554f015d1bc7d20d00458588","554f03de1bc7d20d0045858c"],"reference":false,"createdAt":"2014-10-13T09:44:16.284Z","from_sync":false,"order":4,"slug":"pvp-battles","title":"PvP Battles"},"project":"543b9b0065bf840e00b473d5","user":"543b9aa865bf840e00b473d1","version":{"__v":11,"_id":"543b9b0065bf840e00b473d8","project":"543b9b0065bf840e00b473d5","createdAt":"2014-10-13T09:27:28.467Z","releaseDate":"2014-10-13T09:27:28.467Z","categories":["543b9b0065bf840e00b473d9","543b9ef065bf840e00b473e0","54890012f291f61400c02d36","54890902f291f61400c02d3e","54890c43f291f61400c02d44","54890d71c178b40b00aa3086","5508125c0c4d8c19008a5f83","55094050961f17170070abbd","550945111c38c50d006118ad","550a4c2e42fff40d00ae6049","55221c074801a40d00a77610"],"is_hidden":false,"is_beta":false,"is_stable":false,"codename":"","version_clean":"1.0.0","version":"1.0"},"updates":[],"createdAt":"2014-12-12T05:10:05.904Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"basic_auth":false,"results":{"codes":[]},"settings":"","try":true,"auth":"never","params":[],"url":""},"order":1,"body":"[block:callout]\n{\n  \"type\": \"success\",\n  \"title\": \"Scripts included with the Complete Game Kit\",\n  \"body\": \"Purchasing the Complete Kit includes demo Online Game Sync™ PHP server software. [Get it now](https://www.citybuildingkit.com/#pricing)\"\n}\n[/block]\nThe City Building Strategy Kit comes with the first-of-its-kind feature called Online Game Sync™ that operates on any standard Linux web server with PHP. No subscription or cloud required. \n\nPlus, hosting hundreds of thousands of users is possible. Data for 10,000 players uses approximately 15MBs of space. A top-grossing game with a million players is just 1.5GB of data.\n\nFor advanced developers launching a game worldwide, we recommend deploying the Online Game Sync™ demo software to the cloud. For example AWS EC2 with CloudFront CDN for delivery. More details at the bottom of this page.\n[block:image]\n{\n  \"images\": [\n    {\n      \"image\": [\n        \"https://www.filepicker.io/api/file/Ovgg8eQvTZKJ1Hpxhg3N\",\n        \"game-sync.jpg\",\n        \"800\",\n        \"600\",\n        \"#5e85c1\",\n        \"\"\n      ],\n      \"caption\": \"Diagram summary of data transfer\"\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"About Online Game Sync™\"\n}\n[/block]\nTechnically, your player's village configuration and map data is saved locally on each device. This allows for offline gameplay but with the Online Game Sync™ a copy of each map is sent online to your central server when the player opts to battle other players and enter the online gameplay component.\n\nBattles use the industry standard: **asynchronous gameplay**, meaning when Player #1 searches for a village to attack, only the village of a player who has not entered a battle for a minimum of 60 minutes will be returned. This prevents attacks on live players.\n\n**There are two components involved in getting a match: **\n\n- Sending the Player's Map Online\n- Retrieval of Player #2 map data for battle\n\n**And two components involved in finishing a match: **\n- Storing results of Player #2 back online \n- Updating resources for Player #1 \n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Complete Kit - Testing the CityBuildingKit.com PvP\"\n}\n[/block]\nAdd your License code to both the Game and Map01 scenes for both Save Load WWW and Save Load Battle. The following two screenshots show that you open the scene, then select SaveLoadWWW or SaveLoadBattle in the Hierarchy GameManager.\n[block:image]\n{\n  \"images\": [\n    {\n      \"image\": [\n        \"https://www.filepicker.io/api/file/hLYjSGC8QMCzrurWOxMI\",\n        \"license1.jpg\",\n        \"981\",\n        \"666\",\n        \"#76ae3a\",\n        \"\"\n      ],\n      \"caption\": \"Game > GameManager > SaveLoadWWW\"\n    }\n  ]\n}\n[/block]\n\n[block:image]\n{\n  \"images\": [\n    {\n      \"image\": [\n        \"https://www.filepicker.io/api/file/swrIl8LsTlWnpMJdxbfh\",\n        \"license2.jpg\",\n        \"979\",\n        \"665\",\n        \"#75af3a\",\n        \"\"\n      ],\n      \"caption\": \"Map01 > Game Manager > SaveLoadBattle\"\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Pro Kit - Testing With CityBuildingKit.com PvP\"\n}\n[/block]\nFor the Pro Kit, you'll find the URLs hardcoded in the SaveLoadWWW.cs and SaveLoadBattle.cs files. To test PvP with our server software demo, edit both SaveLoadWWW.cs and SaveLoadBattle.cs and insert your license code into every URL request, such as:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"?license=INSERT-YOUR-LICENSE-CODE\",\n      \"language\": \"text\"\n    }\n  ]\n}\n[/block]\n\n[block:image]\n{\n  \"images\": [\n    {\n      \"image\": [\n        \"https://www.filepicker.io/api/file/ojf6Zg6rQWd6x3uyFT1c\",\n        \"license.jpg\",\n        \"800\",\n        \"600\",\n        \"#67ac38\",\n        \"\"\n      ],\n      \"caption\": \"If you haven't set the license, you'll get this error.\"\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Setup\"\n}\n[/block]\n\n[block:callout]\n{\n  \"type\": \"success\",\n  \"title\": \"Server Requirements\",\n  \"body\": \"PHP Version 5+\\n\\nStandard Web Host (any shared hosting or VPS is fine for development testing)\"\n}\n[/block]\n\n[block:callout]\n{\n  \"type\": \"warning\",\n  \"title\": \"For Security, Change The Default Variable Names\",\n  \"body\": \"Use the demo scripts at your own risk. We recommend randomizing the default variable names in each of the demo scripts and restricting uploads to secure your server.\"\n}\n[/block]\nOpen each of the PHP scripts included with the server software demo and customize the variables such as your@email.com for notifications. Upload all files including the maps directory with the sample maps to your server. Then update the URLs to your hosting in both SaveLoadWWW.cs and SaveLoadBattle.cs in Unity.\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Part 1 - Sending the Player's Map Online\"\n}\n[/block]\nThe first component involves both Unity and the server side scripting. When a player initiates a battle search, their map data is first saved locally on the device with their ID #, then a copy is sent to the server online.\n\n**Post Variables:** \n[block:parameters]\n{\n  \"data\": {\n    \"h-0\": \"Variables\",\n    \"h-1\": \"Description\",\n    \"0-1\": \"**(File)** This is a copy of map data (textfile) saved locally\",\n    \"1-1\": \"**(Alphanumeric Text)** This is the player's ID number. If one is not yet created, the scripting in Unity creates one for the player and saves it.\",\n    \"1-0\": \"**mapid**\",\n    \"0-0\": \"**savefile**\"\n  },\n  \"cols\": 2,\n  \"rows\": 2\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Part 2 - Retrieval of Player #2 Map Data for Battle\"\n}\n[/block]\n\n[block:image]\n{\n  \"images\": [\n    {\n      \"image\": [\n        \"https://www.filepicker.io/api/file/gl7xtZyNQ7CiKNIUtWz0\",\n        \"receive-server-results.jpg\",\n        \"800\",\n        \"600\",\n        \"#bf420f\",\n        \"\"\n      ],\n      \"caption\": \"To receive the attack results, you must load your map from the server (for development purposes, we've added the Server Load to the Settings Menu)\"\n    }\n  ]\n}\n[/block]\nAfter connecting online and posting the player's map data and ID, the game will download the results. The output expected is another player's map data, and nothing else. No formatting or response codes. The map will be loaded in the battle script and displayed to the user as a battle they play.\n\nHowever, if a failure occurs, the game expects the response code \"0\" from the server. You can customize the expected behavior in the battle script in Unity. \n[block:parameters]\n{\n  \"data\": {\n    \"h-0\": \"Response\",\n    \"0-0\": \"**Player #2 map data**\",\n    \"0-1\": \"The complete source of Player #2's map data (no formatting, nothing else)\",\n    \"1-0\": \"**0**\",\n    \"1-1\": \"An error occurred\",\n    \"h-1\": \"Description\"\n  },\n  \"cols\": 2,\n  \"rows\": 2\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Part 3 -  Storing results of Player #2 back online\"\n}\n[/block]\nAfter the battle is finished, the Player #2 map data is sent online and if successfully store the script expects a response code of \"1\", nothing else. (No formatting, no quotes)\n\nHowever, if a failure occurs, the game expects the response code \"0\" from the server. Like before, you can customize the expected behavior in the battle script in Unity. \n[block:parameters]\n{\n  \"data\": {\n    \"h-1\": \"Description\",\n    \"h-0\": \"Variable\",\n    \"0-0\": \"**savefile**\",\n    \"1-0\": \"**mapid**\",\n    \"0-1\": \"The complete source of Player #2's map data with resources changed\",\n    \"1-1\": \"**(Alphanumeric Text)** This is Player #2's ID number.\"\n  },\n  \"cols\": 2,\n  \"rows\": 2\n}\n[/block]\n\n[block:parameters]\n{\n  \"data\": {\n    \"0-0\": \"1\",\n    \"h-0\": \"Response Code\",\n    \"0-1\": \"Successful storage of Player #2 map data (attacked city)\",\n    \"1-0\": \"0\",\n    \"1-1\": \"An error occurred on the server\",\n    \"h-1\": \"Description\"\n  },\n  \"cols\": 2,\n  \"rows\": 2\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Part 4 - Updating resources for Player #1\"\n}\n[/block]\nAfter the battle is finished, Player #1 has their resources updated with what they gained from the battle in the local copy of their map data.\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Player Data Encryption / Decryption\"\n}\n[/block]\nTo secure your player data for actual deployment of your game, rather than the text format used for development - you can encrypt and decrypt your player maps before transferring them to the server. For example, the following demonstration encrypts and decrypts the player data. \n\nAll you need to do is import these functions into the SaveLoadMap.cs script (inside the Scripts/Save folder) and before the player data is saved and loaded to the file, encrypt the string.\n\nEncryption example:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"public static string Encrypt (string toEncrypt)\\n{\\n byte[] keyArray = UTF8Encoding.UTF8.GetBytes (\\\"12345678901234567890123456789012\\\");\\n // 256-AES key\\n byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes (toEncrypt);\\n RijndaelManaged rDel = new RijndaelManaged ();\\n rDel.Key = keyArray;\\n rDel.Mode = CipherMode.ECB;\\n // http://msdn.microsoft.com/en-us/library/system.security.cryptography.ciphermode.aspx\\n rDel.Padding = PaddingMode.PKCS7;\\n // better lang support\\n ICryptoTransform cTransform = rDel.CreateEncryptor ();\\n byte[] resultArray = cTransform.TransformFinalBlock (toEncryptArray, 0, toEncryptArray.Length);\\n return Convert.ToBase64String (resultArray, 0, resultArray.Length);\\n}\",\n      \"language\": \"csharp\"\n    }\n  ]\n}\n[/block]\n\nDecryption example:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"public static string Decrypt (string toDecrypt)\\n{\\n byte[] keyArray = UTF8Encoding.UTF8.GetBytes (\\\"12345678901234567890123456789012\\\");\\n // AES-256 key\\n byte[] toEncryptArray = Convert.FromBase64String (toDecrypt);\\n RijndaelManaged rDel = new RijndaelManaged ();\\n rDel.Key = keyArray;\\n rDel.Mode = CipherMode.ECB;\\n // http://msdn.microsoft.com/en-us/library/system.security.cryptography.ciphermode.aspx\\n rDel.Padding = PaddingMode.PKCS7;\\n // better lang support\\n ICryptoTransform cTransform = rDel.CreateDecryptor ();\\n byte[] resultArray = cTransform.TransformFinalBlock (toEncryptArray, 0, toEncryptArray.Length);\\n return UTF8Encoding.UTF8.GetString (resultArray);\\n}\",\n      \"language\": \"csharp\"\n    }\n  ]\n}\n[/block]\n\nMany thanks to [Rodrigo Barros](http://unitynoobs.blogspot.jp/2012/01/xml-encrypting.html) for the demo code.\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Installation Instructions for the Sample Scripts\"\n}\n[/block]\nOur Complete Game Kit comes with demo server software designed in PHP ready-to-test on your website and fully compatible with the City Builder Kit. [Get the kit now](https://www.citybuildingkit.com/#pricing)\n\n**Required Server Configuration:** Any standard web host supports the PHP sample scripts and file writing. (For example, [Bluehost](http://www.bluehost.com/track/hf/ffi)) No special server or database is necessary for the online player-versus-player asyncrnous gameplay.\n\n**Installation Instructions:**\n\n1. Unzip the demo PHP software scripts included with your Complete Game Kit purchase. You will also find a maps directory with 5 prefilled demo maps. The readme.txt file included will repeat these instructions. If you don't have the kit, you can [upgrade or purchase the Complete Kit](https://www.citybuildingkit.com/#pricing)\n\n2. First, connect through FTP to your server. Any regular Linux server with PHP support should work.\n \n3. Create a new folder. Optionally, if you experience trouble uploading sample maps, make sure the folder you create has 777 READ/WRITE/EXEC permission. ([Watch how here](https://www.youtube.com/watch?v=oq0oM2w9lcQ), not recommended for every installation, only if necessary)\n[block:callout]\n{\n  \"type\": \"info\",\n  \"body\": \"A demo administration file is included. Access this file to see uploaded maps, upload sample player maps, and delete maps. Each of the maps listed use a random non-identifiable user map ID.\\n\\nWe highly recommend you use this file to test your configuration before playing the game. Make sure upload, viewing, and deleting works before using your Unity game.\",\n  \"title\": \"Test Your Setup\"\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"How to Generate Maps\"\n}\n[/block]\nYou can create your own maps by playing the game in Unity with the Pro or Complete Kits, designing a layout and then opening the settings and pressing Local Save. The Internal Operations console will report the exact filename that was saved based on your user's identification **mapid** created when the game started for the first time.\n[block:image]\n{\n  \"images\": [\n    {\n      \"image\": [\n        \"https://www.filepicker.io/api/file/2wP6dTnRjyCnZDvCWNSQ\",\n        \"local-load-mobile-game.jpg\",\n        \"800\",\n        \"600\",\n        \"#425c75\",\n        \"\"\n      ]\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Sample Map Data\"\n}\n[/block]\nWith the server demo scripts included with the Complete Kit, we've provided 5 demo maps. You can generate your own. Download the original copy you purchased and look for the server scripts. You'll see a folder called \"maps\" with 5 demo player maps inside like **demoplayer1.txt**\n\n**Here is an example of one:** \n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"###StartofMapid###\\ndemoplayer\\n###EndofMapid###\\n\\n###StartofFile###\\n###Buildings###\\nBarrel,1,-384,270\\nChessboard,2,-1280,-360\\nClassroom,3,-1280,180\\nForge,4,-256,-360\\nGenerator,5,-768,-720\\nGlobe,6,0,540\\nSummon,8,384,270\\nVault,7,0,0\\n###Grass###\\n4,0,-768,-90\\n3,1,-384,270\\n3,2,-1280,-360\\n3,3,-1280,180\\n3,4,-256,-360\\n3,5,-768,-720\\n3,6,0,540\\n3,7,0,0\\n3,8,384,270\\n3,9,384,-270\\n2,10,640,0\\n###Construction###\\nAcademy,0,15,11,0,-768,-90\\nWorkshop,9,1,0,0,384,-270\\nToolhouse,10,1,0,0,640,0\\n###BuildingIndex###\\n10\\n0.00,0\\n0,0,0,0,0,0,0,0,0,0,0,0\\n0,0,0,0,0,0,0,0,0,0,0,0\\n0,0,0,0,0,0,0,0,0,0,0,0\\n###Stats###\\n2410,7,3,28670.52,8169.019,591,64750,18250,605,0.2,0.2,True,False,True,True\\n03/02/2015 23:03:36\\n###EndofFile###\",\n      \"language\": \"text\"\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Deploying to the Cloud\"\n}\n[/block]\nFor professional developers who want reliable, fast, worldwide player map hosting, we recommend deploying a simple Apache with PHP AWS EC2 instance combined with S3 for map storage and CloudFront for worldwide instantaneous delivery. The setup won't be as easy as a standard Apache VPS, but the gameplay loading will be near zero and minimal costs since each player's data averages 1KB in size. \n\nYou can host data for 10,000 players with approximately 15MBs of space.\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Build Alternative Player-to-Player Games\"\n}\n[/block]\nPlus with a few customizations to the Unity scripts, you can replace the battle component with player-to-player social visiting and sharing villages.\n\nRecreate farming simulators like Farmville, Hay day, or other popular restaurant and farming game simulators seen in the iOS or Android app stores. The options are endless.\n[block:image]\n{\n  \"images\": [\n    {\n      \"image\": [\n        \"https://www.filepicker.io/api/file/0CplNI9YQzKMbTVTlh4o\",\n        \"farming-game2.jpg\",\n        \"720\",\n        \"480\",\n        \"\",\n        \"\"\n      ],\n      \"caption\": \"Farming game art from the Complete Kit\"\n    }\n  ]\n}\n[/block]\n\n[block:image]\n{\n  \"images\": [\n    {\n      \"image\": [\n        \"https://www.filepicker.io/api/file/AY8slK7zTCmlmKSXRpKc\",\n        \"restaurant-game-art.jpg\",\n        \"720\",\n        \"480\",\n        \"#a98162\",\n        \"\"\n      ],\n      \"caption\": \"Restaurant game art from the Complete Kit\"\n    }\n  ]\n}\n[/block]","excerpt":"Game players can battle other players just like popular mobile strategy games such as Castle Clash","slug":"server-scripts","type":"basic","title":"Online Game Sync™"}

Online Game Sync™

Game players can battle other players just like popular mobile strategy games such as Castle Clash

[block:callout] { "type": "success", "title": "Scripts included with the Complete Game Kit", "body": "Purchasing the Complete Kit includes demo Online Game Sync™ PHP server software. [Get it now](https://www.citybuildingkit.com/#pricing)" } [/block] The City Building Strategy Kit comes with the first-of-its-kind feature called Online Game Sync™ that operates on any standard Linux web server with PHP. No subscription or cloud required. Plus, hosting hundreds of thousands of users is possible. Data for 10,000 players uses approximately 15MBs of space. A top-grossing game with a million players is just 1.5GB of data. For advanced developers launching a game worldwide, we recommend deploying the Online Game Sync™ demo software to the cloud. For example AWS EC2 with CloudFront CDN for delivery. More details at the bottom of this page. [block:image] { "images": [ { "image": [ "https://www.filepicker.io/api/file/Ovgg8eQvTZKJ1Hpxhg3N", "game-sync.jpg", "800", "600", "#5e85c1", "" ], "caption": "Diagram summary of data transfer" } ] } [/block] [block:api-header] { "type": "basic", "title": "About Online Game Sync™" } [/block] Technically, your player's village configuration and map data is saved locally on each device. This allows for offline gameplay but with the Online Game Sync™ a copy of each map is sent online to your central server when the player opts to battle other players and enter the online gameplay component. Battles use the industry standard: **asynchronous gameplay**, meaning when Player #1 searches for a village to attack, only the village of a player who has not entered a battle for a minimum of 60 minutes will be returned. This prevents attacks on live players. **There are two components involved in getting a match: ** - Sending the Player's Map Online - Retrieval of Player #2 map data for battle **And two components involved in finishing a match: ** - Storing results of Player #2 back online - Updating resources for Player #1 [block:api-header] { "type": "basic", "title": "Complete Kit - Testing the CityBuildingKit.com PvP" } [/block] Add your License code to both the Game and Map01 scenes for both Save Load WWW and Save Load Battle. The following two screenshots show that you open the scene, then select SaveLoadWWW or SaveLoadBattle in the Hierarchy GameManager. [block:image] { "images": [ { "image": [ "https://www.filepicker.io/api/file/hLYjSGC8QMCzrurWOxMI", "license1.jpg", "981", "666", "#76ae3a", "" ], "caption": "Game > GameManager > SaveLoadWWW" } ] } [/block] [block:image] { "images": [ { "image": [ "https://www.filepicker.io/api/file/swrIl8LsTlWnpMJdxbfh", "license2.jpg", "979", "665", "#75af3a", "" ], "caption": "Map01 > Game Manager > SaveLoadBattle" } ] } [/block] [block:api-header] { "type": "basic", "title": "Pro Kit - Testing With CityBuildingKit.com PvP" } [/block] For the Pro Kit, you'll find the URLs hardcoded in the SaveLoadWWW.cs and SaveLoadBattle.cs files. To test PvP with our server software demo, edit both SaveLoadWWW.cs and SaveLoadBattle.cs and insert your license code into every URL request, such as: [block:code] { "codes": [ { "code": "?license=INSERT-YOUR-LICENSE-CODE", "language": "text" } ] } [/block] [block:image] { "images": [ { "image": [ "https://www.filepicker.io/api/file/ojf6Zg6rQWd6x3uyFT1c", "license.jpg", "800", "600", "#67ac38", "" ], "caption": "If you haven't set the license, you'll get this error." } ] } [/block] [block:api-header] { "type": "basic", "title": "Setup" } [/block] [block:callout] { "type": "success", "title": "Server Requirements", "body": "PHP Version 5+\n\nStandard Web Host (any shared hosting or VPS is fine for development testing)" } [/block] [block:callout] { "type": "warning", "title": "For Security, Change The Default Variable Names", "body": "Use the demo scripts at your own risk. We recommend randomizing the default variable names in each of the demo scripts and restricting uploads to secure your server." } [/block] Open each of the PHP scripts included with the server software demo and customize the variables such as your@email.com for notifications. Upload all files including the maps directory with the sample maps to your server. Then update the URLs to your hosting in both SaveLoadWWW.cs and SaveLoadBattle.cs in Unity. [block:api-header] { "type": "basic", "title": "Part 1 - Sending the Player's Map Online" } [/block] The first component involves both Unity and the server side scripting. When a player initiates a battle search, their map data is first saved locally on the device with their ID #, then a copy is sent to the server online. **Post Variables:** [block:parameters] { "data": { "h-0": "Variables", "h-1": "Description", "0-1": "**(File)** This is a copy of map data (textfile) saved locally", "1-1": "**(Alphanumeric Text)** This is the player's ID number. If one is not yet created, the scripting in Unity creates one for the player and saves it.", "1-0": "**mapid**", "0-0": "**savefile**" }, "cols": 2, "rows": 2 } [/block] [block:api-header] { "type": "basic", "title": "Part 2 - Retrieval of Player #2 Map Data for Battle" } [/block] [block:image] { "images": [ { "image": [ "https://www.filepicker.io/api/file/gl7xtZyNQ7CiKNIUtWz0", "receive-server-results.jpg", "800", "600", "#bf420f", "" ], "caption": "To receive the attack results, you must load your map from the server (for development purposes, we've added the Server Load to the Settings Menu)" } ] } [/block] After connecting online and posting the player's map data and ID, the game will download the results. The output expected is another player's map data, and nothing else. No formatting or response codes. The map will be loaded in the battle script and displayed to the user as a battle they play. However, if a failure occurs, the game expects the response code "0" from the server. You can customize the expected behavior in the battle script in Unity. [block:parameters] { "data": { "h-0": "Response", "0-0": "**Player #2 map data**", "0-1": "The complete source of Player #2's map data (no formatting, nothing else)", "1-0": "**0**", "1-1": "An error occurred", "h-1": "Description" }, "cols": 2, "rows": 2 } [/block] [block:api-header] { "type": "basic", "title": "Part 3 - Storing results of Player #2 back online" } [/block] After the battle is finished, the Player #2 map data is sent online and if successfully store the script expects a response code of "1", nothing else. (No formatting, no quotes) However, if a failure occurs, the game expects the response code "0" from the server. Like before, you can customize the expected behavior in the battle script in Unity. [block:parameters] { "data": { "h-1": "Description", "h-0": "Variable", "0-0": "**savefile**", "1-0": "**mapid**", "0-1": "The complete source of Player #2's map data with resources changed", "1-1": "**(Alphanumeric Text)** This is Player #2's ID number." }, "cols": 2, "rows": 2 } [/block] [block:parameters] { "data": { "0-0": "1", "h-0": "Response Code", "0-1": "Successful storage of Player #2 map data (attacked city)", "1-0": "0", "1-1": "An error occurred on the server", "h-1": "Description" }, "cols": 2, "rows": 2 } [/block] [block:api-header] { "type": "basic", "title": "Part 4 - Updating resources for Player #1" } [/block] After the battle is finished, Player #1 has their resources updated with what they gained from the battle in the local copy of their map data. [block:api-header] { "type": "basic", "title": "Player Data Encryption / Decryption" } [/block] To secure your player data for actual deployment of your game, rather than the text format used for development - you can encrypt and decrypt your player maps before transferring them to the server. For example, the following demonstration encrypts and decrypts the player data. All you need to do is import these functions into the SaveLoadMap.cs script (inside the Scripts/Save folder) and before the player data is saved and loaded to the file, encrypt the string. Encryption example: [block:code] { "codes": [ { "code": "public static string Encrypt (string toEncrypt)\n{\n byte[] keyArray = UTF8Encoding.UTF8.GetBytes (\"12345678901234567890123456789012\");\n // 256-AES key\n byte[] toEncryptArray = UTF8Encoding.UTF8.GetBytes (toEncrypt);\n RijndaelManaged rDel = new RijndaelManaged ();\n rDel.Key = keyArray;\n rDel.Mode = CipherMode.ECB;\n // http://msdn.microsoft.com/en-us/library/system.security.cryptography.ciphermode.aspx\n rDel.Padding = PaddingMode.PKCS7;\n // better lang support\n ICryptoTransform cTransform = rDel.CreateEncryptor ();\n byte[] resultArray = cTransform.TransformFinalBlock (toEncryptArray, 0, toEncryptArray.Length);\n return Convert.ToBase64String (resultArray, 0, resultArray.Length);\n}", "language": "csharp" } ] } [/block] Decryption example: [block:code] { "codes": [ { "code": "public static string Decrypt (string toDecrypt)\n{\n byte[] keyArray = UTF8Encoding.UTF8.GetBytes (\"12345678901234567890123456789012\");\n // AES-256 key\n byte[] toEncryptArray = Convert.FromBase64String (toDecrypt);\n RijndaelManaged rDel = new RijndaelManaged ();\n rDel.Key = keyArray;\n rDel.Mode = CipherMode.ECB;\n // http://msdn.microsoft.com/en-us/library/system.security.cryptography.ciphermode.aspx\n rDel.Padding = PaddingMode.PKCS7;\n // better lang support\n ICryptoTransform cTransform = rDel.CreateDecryptor ();\n byte[] resultArray = cTransform.TransformFinalBlock (toEncryptArray, 0, toEncryptArray.Length);\n return UTF8Encoding.UTF8.GetString (resultArray);\n}", "language": "csharp" } ] } [/block] Many thanks to [Rodrigo Barros](http://unitynoobs.blogspot.jp/2012/01/xml-encrypting.html) for the demo code. [block:api-header] { "type": "basic", "title": "Installation Instructions for the Sample Scripts" } [/block] Our Complete Game Kit comes with demo server software designed in PHP ready-to-test on your website and fully compatible with the City Builder Kit. [Get the kit now](https://www.citybuildingkit.com/#pricing) **Required Server Configuration:** Any standard web host supports the PHP sample scripts and file writing. (For example, [Bluehost](http://www.bluehost.com/track/hf/ffi)) No special server or database is necessary for the online player-versus-player asyncrnous gameplay. **Installation Instructions:** 1. Unzip the demo PHP software scripts included with your Complete Game Kit purchase. You will also find a maps directory with 5 prefilled demo maps. The readme.txt file included will repeat these instructions. If you don't have the kit, you can [upgrade or purchase the Complete Kit](https://www.citybuildingkit.com/#pricing) 2. First, connect through FTP to your server. Any regular Linux server with PHP support should work. 3. Create a new folder. Optionally, if you experience trouble uploading sample maps, make sure the folder you create has 777 READ/WRITE/EXEC permission. ([Watch how here](https://www.youtube.com/watch?v=oq0oM2w9lcQ), not recommended for every installation, only if necessary) [block:callout] { "type": "info", "body": "A demo administration file is included. Access this file to see uploaded maps, upload sample player maps, and delete maps. Each of the maps listed use a random non-identifiable user map ID.\n\nWe highly recommend you use this file to test your configuration before playing the game. Make sure upload, viewing, and deleting works before using your Unity game.", "title": "Test Your Setup" } [/block] [block:api-header] { "type": "basic", "title": "How to Generate Maps" } [/block] You can create your own maps by playing the game in Unity with the Pro or Complete Kits, designing a layout and then opening the settings and pressing Local Save. The Internal Operations console will report the exact filename that was saved based on your user's identification **mapid** created when the game started for the first time. [block:image] { "images": [ { "image": [ "https://www.filepicker.io/api/file/2wP6dTnRjyCnZDvCWNSQ", "local-load-mobile-game.jpg", "800", "600", "#425c75", "" ] } ] } [/block] [block:api-header] { "type": "basic", "title": "Sample Map Data" } [/block] With the server demo scripts included with the Complete Kit, we've provided 5 demo maps. You can generate your own. Download the original copy you purchased and look for the server scripts. You'll see a folder called "maps" with 5 demo player maps inside like **demoplayer1.txt** **Here is an example of one:** [block:code] { "codes": [ { "code": "###StartofMapid###\ndemoplayer\n###EndofMapid###\n\n###StartofFile###\n###Buildings###\nBarrel,1,-384,270\nChessboard,2,-1280,-360\nClassroom,3,-1280,180\nForge,4,-256,-360\nGenerator,5,-768,-720\nGlobe,6,0,540\nSummon,8,384,270\nVault,7,0,0\n###Grass###\n4,0,-768,-90\n3,1,-384,270\n3,2,-1280,-360\n3,3,-1280,180\n3,4,-256,-360\n3,5,-768,-720\n3,6,0,540\n3,7,0,0\n3,8,384,270\n3,9,384,-270\n2,10,640,0\n###Construction###\nAcademy,0,15,11,0,-768,-90\nWorkshop,9,1,0,0,384,-270\nToolhouse,10,1,0,0,640,0\n###BuildingIndex###\n10\n0.00,0\n0,0,0,0,0,0,0,0,0,0,0,0\n0,0,0,0,0,0,0,0,0,0,0,0\n0,0,0,0,0,0,0,0,0,0,0,0\n###Stats###\n2410,7,3,28670.52,8169.019,591,64750,18250,605,0.2,0.2,True,False,True,True\n03/02/2015 23:03:36\n###EndofFile###", "language": "text" } ] } [/block] [block:api-header] { "type": "basic", "title": "Deploying to the Cloud" } [/block] For professional developers who want reliable, fast, worldwide player map hosting, we recommend deploying a simple Apache with PHP AWS EC2 instance combined with S3 for map storage and CloudFront for worldwide instantaneous delivery. The setup won't be as easy as a standard Apache VPS, but the gameplay loading will be near zero and minimal costs since each player's data averages 1KB in size. You can host data for 10,000 players with approximately 15MBs of space. [block:api-header] { "type": "basic", "title": "Build Alternative Player-to-Player Games" } [/block] Plus with a few customizations to the Unity scripts, you can replace the battle component with player-to-player social visiting and sharing villages. Recreate farming simulators like Farmville, Hay day, or other popular restaurant and farming game simulators seen in the iOS or Android app stores. The options are endless. [block:image] { "images": [ { "image": [ "https://www.filepicker.io/api/file/0CplNI9YQzKMbTVTlh4o", "farming-game2.jpg", "720", "480", "", "" ], "caption": "Farming game art from the Complete Kit" } ] } [/block] [block:image] { "images": [ { "image": [ "https://www.filepicker.io/api/file/AY8slK7zTCmlmKSXRpKc", "restaurant-game-art.jpg", "720", "480", "#a98162", "" ], "caption": "Restaurant game art from the Complete Kit" } ] } [/block]