├── .gitignore ├── Models ├── Result │ ├── LoginResult.cs │ ├── ResultBase.cs │ ├── UserResult.cs │ ├── RankResult.cs │ └── UpgradeResult.cs ├── Request │ ├── UpgradeRequest.cs │ ├── RankRequest.cs │ └── StageRequest.cs ├── Data │ ├── UpgradeData.cs │ ├── RankUser.cs │ └── User.cs ├── DB.cs └── Dao │ ├── UpgradeDao.cs │ ├── RankDao.cs │ └── UserDao.cs ├── appsettings.json ├── Program.cs ├── Properties └── launchSettings.json ├── Controllers ├── UserController.cs ├── UpdateResultController.cs ├── ValuesController.cs ├── RankController.cs ├── LoginController.cs └── UpgradeController.cs ├── DotnetCoreServer.csproj └── Startup.cs /.gitignore: -------------------------------------------------------------------------------- 1 | obj 2 | bin 3 | Debug 4 | -------------------------------------------------------------------------------- /Models/Result/LoginResult.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace DotnetCoreServer.Models 4 | { 5 | public class LoginResult : ResultBase{ 6 | public User Data; 7 | 8 | } 9 | } -------------------------------------------------------------------------------- /Models/Request/UpgradeRequest.cs: -------------------------------------------------------------------------------- 1 | 2 | namespace DotnetCoreServer.Models{ 3 | public class UpgradeRequest 4 | { 5 | public long UserID; 6 | public string UpgradeType; 7 | } 8 | } -------------------------------------------------------------------------------- /Models/Request/RankRequest.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace DotnetCoreServer.Models{ 4 | public class RankRequest 5 | { 6 | public List FriendList; 7 | } 8 | } -------------------------------------------------------------------------------- /Models/Result/ResultBase.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace DotnetCoreServer.Models 4 | { 5 | public class ResultBase{ 6 | public string Message; 7 | public int ResultCode; 8 | 9 | } 10 | } -------------------------------------------------------------------------------- /Models/Request/StageRequest.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | namespace DotnetCoreServer.Models 3 | { 4 | public class StageRequest 5 | { 6 | public Int64 UserID { get; set; } 7 | public int Point { get; set; } 8 | } 9 | } -------------------------------------------------------------------------------- /Models/Result/UserResult.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace DotnetCoreServer.Models 5 | { 6 | public class UserResult : ResultBase{ 7 | public User Data; 8 | 9 | } 10 | } -------------------------------------------------------------------------------- /Models/Result/RankResult.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace DotnetCoreServer.Models 5 | { 6 | public class RankResult : ResultBase{ 7 | public List Data; 8 | 9 | } 10 | } -------------------------------------------------------------------------------- /Models/Result/UpgradeResult.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | 4 | namespace DotnetCoreServer.Models 5 | { 6 | public class UpgradeResult : ResultBase{ 7 | public List Data; 8 | 9 | } 10 | } -------------------------------------------------------------------------------- /Models/Data/UpgradeData.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace DotnetCoreServer.Models 4 | { 5 | public class UpgradeData 6 | { 7 | public string UpgradeType; 8 | public int UpgradeLevel; 9 | public int UpgradeAmount; 10 | public int UpgradeCost; 11 | } 12 | } -------------------------------------------------------------------------------- /appsettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "ConnectionStrings":{ 3 | "DefaultConnection": "Server=unity-mydb.mysql.database.azure.com;User Id=sjhshy@unity-mydb;Password=unitycamp!1;Database=unity;Pooling=False" 4 | }, 5 | "Logging": { 6 | "IncludeScopes": false, 7 | "LogLevel": { 8 | "Default": "Warning" 9 | } 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /Models/Data/RankUser.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace DotnetCoreServer.Models 4 | { 5 | public class RankUser 6 | { 7 | public long UserID; 8 | public string FacebookID; 9 | public string FacebookName; 10 | public string FacebookPhotoURL; 11 | public int Point; 12 | public DateTime CreatedAt; 13 | public int Rank; 14 | 15 | } 16 | } -------------------------------------------------------------------------------- /Models/DB.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using MySql.Data.MySqlClient; 3 | using Microsoft.Extensions.Configuration; 4 | public interface IDB 5 | { 6 | MySqlConnection GetConnection(); 7 | } 8 | 9 | public class DB : IDB 10 | { 11 | public string ConnectionString; 12 | 13 | public DB(IConfiguration configuration){ 14 | this.ConnectionString = configuration.GetConnectionString("DefaultConnection"); 15 | } 16 | 17 | public MySqlConnection GetConnection() 18 | { 19 | MySqlConnection connection = new MySqlConnection 20 | { 21 | ConnectionString = this.ConnectionString 22 | }; 23 | connection.Open(); 24 | return connection; 25 | } 26 | 27 | } -------------------------------------------------------------------------------- /Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.IO; 4 | using System.Linq; 5 | using System.Threading.Tasks; 6 | using Microsoft.AspNetCore.Builder; 7 | using Microsoft.AspNetCore.Hosting; 8 | 9 | namespace DotnetCoreServer 10 | { 11 | public class Program 12 | { 13 | public static void Main(string[] args) 14 | { 15 | var host = new WebHostBuilder() 16 | .UseKestrel() 17 | .UseContentRoot(Directory.GetCurrentDirectory()) 18 | .UseIISIntegration() 19 | .UseStartup() 20 | .UseApplicationInsights() 21 | .Build(); 22 | 23 | host.Run(); 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /Properties/launchSettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "iisSettings": { 3 | "windowsAuthentication": false, 4 | "anonymousAuthentication": true, 5 | "iisExpress": { 6 | "applicationUrl": "http://localhost:54399/", 7 | "sslPort": 0 8 | } 9 | }, 10 | "profiles": { 11 | "IIS Express": { 12 | "commandName": "IISExpress", 13 | "launchBrowser": true, 14 | "launchUrl": "api/values", 15 | "environmentVariables": { 16 | "ASPNETCORE_ENVIRONMENT": "Development" 17 | } 18 | }, 19 | "DotnetCoreServer": { 20 | "commandName": "Project", 21 | "launchBrowser": true, 22 | "launchUrl": "api/values", 23 | "environmentVariables": { 24 | "ASPNETCORE_ENVIRONMENT": "Development" 25 | }, 26 | "applicationUrl": "http://localhost:54400" 27 | } 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /Models/Data/User.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace DotnetCoreServer.Models 4 | { 5 | public class User 6 | { 7 | 8 | public long UserID; 9 | public string FacebookID; 10 | public string FacebookName; 11 | public string FacebookPhotoURL; 12 | public string FacebookAccessToken; 13 | public int Point; 14 | public string AccessToken; 15 | public DateTime CreatedAt; 16 | public int Diamond; 17 | public int Health; 18 | public int Defense; 19 | public int Damage; 20 | public int Speed; 21 | public int HealthLevel; 22 | public int DefenseLevel; 23 | public int DamageLevel; 24 | public int SpeedLevel; 25 | public int Level; 26 | public int Experience; 27 | public int ExpForNextLevel; 28 | public int ExpAfterLastLevel; 29 | 30 | 31 | } 32 | } -------------------------------------------------------------------------------- /Controllers/UserController.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Threading.Tasks; 5 | using Microsoft.AspNetCore.Mvc; 6 | using DotnetCoreServer.Models; 7 | 8 | namespace DotnetCoreServer.Controllers 9 | { 10 | [Route("[controller]/[action]")] 11 | public class UserController : Controller 12 | { 13 | IUserDao userDao; 14 | public UserController(IUserDao userDao){ 15 | this.userDao = userDao; 16 | } 17 | 18 | // // GET api/user 19 | // [HttpGet] 20 | // public IEnumerable Get() 21 | // { 22 | // return new string[] { "value1", "value2" }; 23 | // } 24 | 25 | // GET User/Inf 26 | [HttpGet] 27 | public UserResult Info(int UserID) 28 | { 29 | UserResult result = new UserResult(); 30 | result.Data = userDao.GetUser(UserID); 31 | result.ResultCode = 1; 32 | result.Message = "OK"; 33 | return result; 34 | } 35 | 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /Controllers/UpdateResultController.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Threading.Tasks; 5 | using Microsoft.AspNetCore.Mvc; 6 | using DotnetCoreServer.Models; 7 | 8 | namespace DotnetCoreServer.Controllers 9 | { 10 | [Route("[controller]/[action]")] 11 | public class UpdateResultController : Controller 12 | { 13 | IUserDao userDao; 14 | 15 | public UpdateResultController(IUserDao userDao){ 16 | this.userDao = userDao; 17 | } 18 | 19 | // POST UpdateResult/Post 20 | [HttpPost] 21 | public ResultBase Post([FromBody] StageRequest request) 22 | { 23 | 24 | ResultBase result = new ResultBase(); 25 | 26 | User user = this.userDao.GetUser(request.UserID); 27 | 28 | user.Point = user.Point + request.Point; 29 | 30 | this.userDao.UpdateUser(user); 31 | 32 | result.ResultCode = 1; 33 | result.Message = "Success"; 34 | 35 | return result; 36 | 37 | } 38 | 39 | } 40 | } 41 | -------------------------------------------------------------------------------- /DotnetCoreServer.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | Exe 5 | netcoreapp2.0 6 | win10-x64;osx.10.11-x64 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /Controllers/ValuesController.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Threading.Tasks; 5 | using Microsoft.AspNetCore.Mvc; 6 | using DotnetCoreServer.Models; 7 | 8 | namespace DotnetCoreServer.Controllers 9 | { 10 | [Route("api/[controller]")] 11 | public class ValuesController : Controller 12 | { 13 | // GET api/values 14 | [HttpGet] 15 | public IEnumerable Get() 16 | { 17 | return new string[] { "value1", "value2" }; 18 | } 19 | 20 | // GET api/values/5 21 | [HttpGet("{id}")] 22 | public User Get(int id) 23 | { 24 | User user = new User(); 25 | return user; 26 | } 27 | 28 | // POST api/values 29 | [HttpPost] 30 | public void Post([FromBody]string value) 31 | { 32 | } 33 | 34 | // PUT api/values/5 35 | [HttpPut("{id}")] 36 | public void Put(int id, [FromBody]string value) 37 | { 38 | } 39 | 40 | // DELETE api/values/5 41 | [HttpDelete("{id}")] 42 | public void Delete(int id) 43 | { 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /Controllers/RankController.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Threading.Tasks; 5 | using Microsoft.AspNetCore.Mvc; 6 | using DotnetCoreServer.Models; 7 | 8 | namespace DotnetCoreServer.Controllers 9 | { 10 | [Route("[controller]/[action]")] 11 | public class RankController : Controller 12 | { 13 | 14 | IRankDao rankDao; 15 | public RankController(IRankDao rankDao){ 16 | this.rankDao = rankDao; 17 | } 18 | 19 | // POST Rank/Total 20 | [HttpGet] 21 | public RankResult Total(int Start, int Count) 22 | { 23 | 24 | RankResult result = new RankResult(); 25 | 26 | List list = rankDao.TotalRank(Start, Count); 27 | 28 | result.Data = list; 29 | 30 | return result; 31 | 32 | } 33 | 34 | // POST Rank/Friend 35 | [HttpPost] 36 | public RankResult Friend([FromBody] RankRequest rankRequest) 37 | { 38 | 39 | RankResult result = new RankResult(); 40 | 41 | List list = rankDao.FriendRank(rankRequest.FriendList); 42 | 43 | result.Data = list; 44 | 45 | return result; 46 | 47 | } 48 | 49 | } 50 | } 51 | -------------------------------------------------------------------------------- /Controllers/LoginController.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Threading.Tasks; 5 | using Microsoft.AspNetCore.Mvc; 6 | using DotnetCoreServer.Models; 7 | 8 | namespace DotnetCoreServer.Controllers 9 | { 10 | [Route("[controller]/[action]")] 11 | public class LoginController : Controller 12 | { 13 | 14 | IUserDao userDao; 15 | public LoginController(IUserDao userDao){ 16 | this.userDao = userDao; 17 | } 18 | 19 | // GET api/user/5 20 | [HttpGet("{id}")] 21 | public User Get(int id) 22 | { 23 | User user = userDao.GetUser(id); 24 | return user; 25 | } 26 | 27 | // POST Login/Facebook 28 | [HttpPost] 29 | public LoginResult Facebook([FromBody] User requestUser) 30 | { 31 | 32 | LoginResult result = new LoginResult(); 33 | 34 | User user = userDao.FindUserByFUID(requestUser.FacebookID); 35 | 36 | if(user != null && user.UserID > 0){ // 이미 가입되어 있음 37 | 38 | result.Data = user; 39 | result.Message = "OK"; 40 | result.ResultCode = 1; 41 | 42 | return result; 43 | 44 | } else { // 회원가입 해야함 45 | 46 | string AccessToken = Guid.NewGuid().ToString(); 47 | 48 | requestUser.AccessToken = AccessToken; 49 | userDao.InsertUser(requestUser); 50 | user = userDao.FindUserByFUID(requestUser.FacebookID); 51 | result.Data = user; 52 | result.Message = "New User"; 53 | result.ResultCode = 2; 54 | 55 | return result; 56 | 57 | } 58 | 59 | } 60 | 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /Startup.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Threading.Tasks; 5 | using Microsoft.AspNetCore.Builder; 6 | using Microsoft.AspNetCore.Hosting; 7 | using Microsoft.Extensions.Configuration; 8 | using Microsoft.Extensions.DependencyInjection; 9 | using Microsoft.Extensions.Logging; 10 | using DotnetCoreServer.Models; 11 | 12 | namespace DotnetCoreServer 13 | { 14 | public class Startup 15 | { 16 | public Startup(IHostingEnvironment env) 17 | { 18 | var builder = new ConfigurationBuilder() 19 | .SetBasePath(env.ContentRootPath) 20 | .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true) 21 | .AddEnvironmentVariables(); 22 | Configuration = builder.Build(); 23 | } 24 | 25 | public IConfigurationRoot Configuration { get; } 26 | 27 | // This method gets called by the runtime. Use this method to add services to the container. 28 | public void ConfigureServices(IServiceCollection services) 29 | { 30 | // Add framework services. 31 | services.AddMvc() 32 | .AddJsonOptions(options => 33 | { 34 | options.SerializerSettings.ContractResolver = new Newtonsoft.Json.Serialization.DefaultContractResolver(); 35 | });; 36 | 37 | services.AddSingleton(Configuration); 38 | services.AddSingleton(); 39 | services.AddSingleton(); 40 | services.AddSingleton(); 41 | services.AddSingleton(); 42 | 43 | } 44 | 45 | // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. 46 | public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) 47 | { 48 | loggerFactory.AddConsole(Configuration.GetSection("Logging")); 49 | loggerFactory.AddDebug(); 50 | 51 | app.UseMvc(); 52 | } 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /Models/Dao/UpgradeDao.cs: -------------------------------------------------------------------------------- 1 | using MySql.Data.MySqlClient; 2 | using System; 3 | using System.Collections.Generic; 4 | 5 | namespace DotnetCoreServer.Models 6 | { 7 | public interface IUpgradeDao{ 8 | List GetUpgradeInfo(); 9 | UpgradeData GetUpgradeInfo(string UpgradeType, int UpgradeLevel); 10 | 11 | } 12 | 13 | public class UpgradeDao : IUpgradeDao 14 | { 15 | public IDB db {get;} 16 | 17 | public UpgradeDao(IDB db){ 18 | this.db = db; 19 | } 20 | 21 | public List GetUpgradeInfo(){ 22 | 23 | List list = new List(); 24 | using(MySqlConnection conn = db.GetConnection()) 25 | { 26 | 27 | string query = String.Format( 28 | "SELECT upgrade_type, upgrade_level, upgrade_amount, upgrade_cost FROM tb_upgrade_info"); 29 | 30 | Console.WriteLine(query); 31 | using(MySqlCommand cmd = (MySqlCommand)conn.CreateCommand()) 32 | { 33 | cmd.CommandText = query; 34 | using (MySqlDataReader reader = (MySqlDataReader)cmd.ExecuteReader()) 35 | { 36 | while (reader.Read()) 37 | { 38 | UpgradeData data = new UpgradeData(); 39 | data.UpgradeType = reader.GetString(0); 40 | data.UpgradeLevel = reader.GetInt32(1); 41 | data.UpgradeAmount = reader.GetInt32(2); 42 | data.UpgradeCost = reader.GetInt32(3); 43 | list.Add(data); 44 | } 45 | } 46 | } 47 | 48 | conn.Close(); 49 | 50 | } 51 | 52 | return list; 53 | } 54 | 55 | public UpgradeData GetUpgradeInfo(string UpgradeType, int UpgradeLevel){ 56 | 57 | UpgradeData data = new UpgradeData(); 58 | using(MySqlConnection conn = db.GetConnection()) 59 | { 60 | string query = String.Format( 61 | @" 62 | SELECT 63 | upgrade_type, upgrade_level, 64 | upgrade_amount, upgrade_cost 65 | FROM tb_upgrade_info 66 | WHERE upgrade_type = '{0}' AND upgrade_level = {1} 67 | ", UpgradeType, UpgradeLevel); 68 | 69 | Console.WriteLine(query); 70 | using(MySqlCommand cmd = (MySqlCommand)conn.CreateCommand()) 71 | { 72 | cmd.CommandText = query; 73 | using (MySqlDataReader reader = (MySqlDataReader)cmd.ExecuteReader()) 74 | { 75 | if (reader.Read()) 76 | { 77 | data.UpgradeType = reader.GetString(0); 78 | data.UpgradeLevel = reader.GetInt32(1); 79 | data.UpgradeAmount = reader.GetInt32(2); 80 | data.UpgradeCost = reader.GetInt32(3); 81 | return data; 82 | } 83 | } 84 | } 85 | 86 | conn.Close(); 87 | 88 | } 89 | 90 | return null; 91 | } 92 | 93 | } 94 | } -------------------------------------------------------------------------------- /Controllers/UpgradeController.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Threading.Tasks; 5 | using Microsoft.AspNetCore.Mvc; 6 | using DotnetCoreServer.Models; 7 | 8 | namespace DotnetCoreServer.Controllers 9 | { 10 | [Route("[controller]/[action]")] 11 | public class UpgradeController : Controller 12 | { 13 | 14 | IUpgradeDao upgradeDao; 15 | IUserDao userDao; 16 | 17 | public UpgradeController(IUpgradeDao upgradeDao, IUserDao userDao){ 18 | this.upgradeDao = upgradeDao; 19 | this.userDao = userDao; 20 | } 21 | 22 | // GET Upgrade/Info 23 | [HttpGet] 24 | public UpgradeResult Info() 25 | { 26 | UpgradeResult result = new UpgradeResult(); 27 | 28 | result.Data = this.upgradeDao.GetUpgradeInfo(); 29 | 30 | result.ResultCode = 1; 31 | result.Message = "OK"; 32 | 33 | return result; 34 | } 35 | 36 | // POST Upgrade/Execute 37 | [HttpPost] 38 | public ResultBase Execute([FromBody] UpgradeRequest request) 39 | { 40 | 41 | ResultBase result = new ResultBase(); 42 | 43 | User user = this.userDao.GetUser(request.UserID); 44 | UpgradeData upgradeInfo = null; 45 | if("Health".Equals(request.UpgradeType)){ 46 | upgradeInfo = this.upgradeDao.GetUpgradeInfo(request.UpgradeType, user.HealthLevel + 1); 47 | }else if("Damage".Equals(request.UpgradeType)){ 48 | upgradeInfo = this.upgradeDao.GetUpgradeInfo(request.UpgradeType, user.HealthLevel + 1); 49 | }else if("Defense".Equals(request.UpgradeType)){ 50 | upgradeInfo = this.upgradeDao.GetUpgradeInfo(request.UpgradeType, user.HealthLevel + 1); 51 | }else if("Speed".Equals(request.UpgradeType)){ 52 | upgradeInfo = this.upgradeDao.GetUpgradeInfo(request.UpgradeType, user.HealthLevel + 1); 53 | }else{ 54 | // 유효하지 않은 업그레이드 타입입니다. 55 | } 56 | 57 | // 다이아몬드가 있는지? 58 | if(user.Diamond < upgradeInfo.UpgradeCost){ 59 | // TODO: 돈이 없어서 업그레이드 못해줌 60 | result.ResultCode = 5; 61 | result.Message = "Not Enough Diamond"; 62 | return result; 63 | 64 | } 65 | 66 | if(upgradeInfo == null){ 67 | // 최대 레벨에 도달했습니다. 68 | result.ResultCode = 4; 69 | result.Message = "Upgrade Fail : Max Level"; 70 | return result; 71 | } 72 | 73 | if("Health".Equals(request.UpgradeType)){ 74 | 75 | user.HealthLevel = user.HealthLevel + 1; 76 | user.Health = user.Health + upgradeInfo.UpgradeAmount; 77 | user.Diamond = user.Diamond - upgradeInfo.UpgradeCost; 78 | 79 | }else if("Damage".Equals(request.UpgradeType)){ 80 | 81 | user.DamageLevel = user.DamageLevel + 1; 82 | user.Damage = user.Damage + upgradeInfo.UpgradeAmount; 83 | user.Diamond = user.Diamond - upgradeInfo.UpgradeCost; 84 | 85 | }else if("Defense".Equals(request.UpgradeType)){ 86 | 87 | user.DefenseLevel = user.DefenseLevel + 1; 88 | user.Defense = user.Defense + upgradeInfo.UpgradeAmount; 89 | user.Diamond = user.Diamond - upgradeInfo.UpgradeCost; 90 | 91 | }else if("Speed".Equals(request.UpgradeType)){ 92 | 93 | user.SpeedLevel = user.SpeedLevel + 1; 94 | user.Speed = user.Speed + upgradeInfo.UpgradeAmount; 95 | user.Diamond = user.Diamond - upgradeInfo.UpgradeCost; 96 | 97 | } 98 | 99 | this.userDao.UpdateUser(user); 100 | 101 | result.ResultCode = 1; 102 | result.Message = "Success"; 103 | 104 | return result; 105 | 106 | } 107 | 108 | } 109 | } 110 | -------------------------------------------------------------------------------- /Models/Dao/RankDao.cs: -------------------------------------------------------------------------------- 1 | using MySql.Data.MySqlClient; 2 | using System; 3 | using System.Collections.Generic; 4 | 5 | namespace DotnetCoreServer.Models 6 | { 7 | public interface IRankDao{ 8 | List TotalRank(int Start, int Count); 9 | List FriendRank(List FacebookIDList); 10 | } 11 | 12 | public class RankDao : IRankDao 13 | { 14 | public IDB db {get;} 15 | 16 | public RankDao(IDB db){ 17 | this.db = db; 18 | } 19 | 20 | public List TotalRank(int Start, int Count){ 21 | 22 | List list = new List(); 23 | using(MySqlConnection conn = db.GetConnection()) 24 | { 25 | 26 | string query = String.Format( 27 | @" 28 | SELECT 29 | user_id, facebook_id, facebook_name, 30 | facebook_photo_url, point, created_at 31 | FROM tb_user 32 | ORDER BY point desc 33 | LIMIT {0}, {1}", Start, Count ); 34 | 35 | Console.WriteLine(query); 36 | int rank = 0; 37 | using(MySqlCommand cmd = (MySqlCommand)conn.CreateCommand()) 38 | { 39 | cmd.CommandText = query; 40 | using (MySqlDataReader reader = (MySqlDataReader)cmd.ExecuteReader()) 41 | { 42 | while (reader.Read()) 43 | { 44 | rank++; 45 | RankUser user = new RankUser(); 46 | user.UserID = reader.GetInt64(0); 47 | user.FacebookID = reader.GetString(1); 48 | user.FacebookName = reader.GetString(2); 49 | user.FacebookPhotoURL = reader.GetString(3); 50 | user.Point = reader.GetInt32(4); 51 | user.CreatedAt = reader.GetDateTime(5); 52 | user.Rank = rank; 53 | list.Add(user); 54 | } 55 | } 56 | } 57 | 58 | conn.Close(); 59 | 60 | } 61 | 62 | return list; 63 | 64 | } 65 | 66 | public List FriendRank(List FacebookIDList){ 67 | 68 | for(int i = 0; i < FacebookIDList.Count; i++){ 69 | FacebookIDList[i] = string.Format("'{0}'", FacebookIDList[i]); 70 | } 71 | 72 | string StrFacebookIDList = string.Join(",", FacebookIDList); 73 | 74 | List list = new List(); 75 | using(MySqlConnection conn = db.GetConnection()) 76 | { 77 | string query = String.Format( 78 | @" 79 | SELECT 80 | user_id, facebook_id, facebook_name, 81 | facebook_photo_url, point, created_at 82 | FROM tb_user 83 | WHERE facebook_id IN ( {0} )", 84 | StrFacebookIDList); 85 | 86 | Console.WriteLine(query); 87 | int rank = 0; 88 | 89 | using(MySqlCommand cmd = (MySqlCommand)conn.CreateCommand()) 90 | { 91 | cmd.CommandText = query; 92 | using (MySqlDataReader reader = (MySqlDataReader)cmd.ExecuteReader()) 93 | { 94 | while (reader.Read()) 95 | { 96 | rank++; 97 | RankUser user = new RankUser(); 98 | user.UserID = reader.GetInt64(0); 99 | user.FacebookID = reader.GetString(1); 100 | user.FacebookName = reader.GetString(2); 101 | user.FacebookPhotoURL = reader.GetString(3); 102 | user.Point = reader.GetInt32(4); 103 | user.CreatedAt = reader.GetDateTime(5); 104 | user.Rank = rank; 105 | list.Add(user); 106 | } 107 | } 108 | } 109 | 110 | conn.Close(); 111 | } 112 | return list; 113 | } 114 | 115 | 116 | 117 | } 118 | } -------------------------------------------------------------------------------- /Models/Dao/UserDao.cs: -------------------------------------------------------------------------------- 1 | using MySql.Data.MySqlClient; 2 | 3 | using System; 4 | using System.Collections.Generic; 5 | 6 | namespace DotnetCoreServer.Models 7 | { 8 | public interface IUserDao{ 9 | User FindUserByFUID(string FacebookID); 10 | User GetUser(Int64 UserID); 11 | User InsertUser(User user); 12 | bool UpdateUser(User user); 13 | } 14 | 15 | public class UserDao : IUserDao 16 | { 17 | public IDB db {get;} 18 | 19 | public UserDao(IDB db){ 20 | this.db = db; 21 | } 22 | 23 | public User FindUserByFUID(string FacebookID){ 24 | User user = new User(); 25 | using(MySqlConnection conn = db.GetConnection()) 26 | { 27 | string query = String.Format( 28 | "SELECT user_id, facebook_id, facebook_name, facebook_photo_url, point, created_at, access_token FROM unity.tb_user WHERE facebook_id = '{0}'", 29 | FacebookID); 30 | 31 | Console.WriteLine(query); 32 | 33 | using(MySqlCommand cmd = (MySqlCommand)conn.CreateCommand()) 34 | { 35 | cmd.CommandText = query; 36 | using (MySqlDataReader reader = (MySqlDataReader)cmd.ExecuteReader()) 37 | { 38 | if (reader.Read()) 39 | { 40 | user.UserID = reader.GetInt64(0); 41 | user.FacebookID = reader.GetString(1); 42 | user.FacebookName = reader.GetString(2); 43 | user.FacebookPhotoURL = reader.GetString(3); 44 | user.Point = reader.GetInt32(4); 45 | user.CreatedAt = reader.GetDateTime(5); 46 | user.AccessToken = reader.GetString(6); 47 | return user; 48 | } 49 | } 50 | } 51 | conn.Close(); 52 | } 53 | return null; 54 | } 55 | 56 | public User GetUser(Int64 UserID){ 57 | User user = new User(); 58 | using(MySqlConnection conn = db.GetConnection()) 59 | { 60 | string query = String.Format( 61 | @" 62 | SELECT 63 | user_id, facebook_id, facebook_name, 64 | facebook_photo_url, point, created_at, 65 | access_token, diamond, health, defense, damage, 66 | speed, health_level, defense_level, 67 | damage_level, speed_level, 68 | level, experience 69 | FROM tb_user 70 | WHERE user_id = {0}", 71 | UserID); 72 | 73 | Console.WriteLine(query); 74 | 75 | using(MySqlCommand cmd = (MySqlCommand)conn.CreateCommand()) 76 | { 77 | cmd.CommandText = query; 78 | using (MySqlDataReader reader = (MySqlDataReader)cmd.ExecuteReader()) 79 | { 80 | if (reader.Read()) 81 | { 82 | user.UserID = reader.GetInt64(0); 83 | user.FacebookID = reader.GetString(1); 84 | user.FacebookName = reader.GetString(2); 85 | user.FacebookPhotoURL = reader.GetString(3); 86 | user.Point = reader.GetInt32(4); 87 | user.CreatedAt = reader.GetDateTime(5); 88 | user.AccessToken = reader.GetString(6); 89 | 90 | user.Diamond = reader.GetInt32(7); 91 | user.Health = reader.GetInt32(8); 92 | user.Defense = reader.GetInt32(9); 93 | user.Damage = reader.GetInt32(10); 94 | user.Speed = reader.GetInt32(11); 95 | user.HealthLevel = reader.GetInt32(12); 96 | user.DefenseLevel = reader.GetInt32(13); 97 | user.DamageLevel = reader.GetInt32(14); 98 | user.SpeedLevel = reader.GetInt32(15); 99 | user.Level = reader.GetInt32(16); 100 | user.Experience = reader.GetInt32(17); 101 | 102 | } 103 | } 104 | } 105 | 106 | conn.Close(); 107 | } 108 | return user; 109 | } 110 | 111 | public User InsertUser(User user){ 112 | 113 | string query = String.Format( 114 | "INSERT INTO unity.tb_user (facebook_id, facebook_name, facebook_photo_url, point, access_token, created_at) VALUES ('{0}','{1}','{2}',{3}, '{4}', now())", 115 | user.FacebookID, user.FacebookName, user.FacebookPhotoURL, 0, user.AccessToken); 116 | 117 | Console.WriteLine(query); 118 | 119 | using(MySqlConnection conn = db.GetConnection()) 120 | using(MySqlCommand cmd = (MySqlCommand)conn.CreateCommand()) 121 | { 122 | 123 | cmd.CommandText = query; 124 | cmd.ExecuteNonQuery(); 125 | 126 | conn.Close(); 127 | } 128 | 129 | 130 | return user; 131 | } 132 | 133 | public bool UpdateUser(User user){ 134 | using(MySqlConnection conn = db.GetConnection()) 135 | { 136 | string query = String.Format( 137 | @" 138 | UPDATE tb_user SET 139 | health = {0}, defense = {1}, damage = {2}, speed = {3}, 140 | health_level = {4}, defense_level = {5}, damage_level = {6}, speed_level = {7}, 141 | diamond = {8}, point = {9} 142 | WHERE user_id = {10} 143 | ", 144 | user.Health, user.Defense, user.Damage, user.Speed, 145 | user.HealthLevel, user.DefenseLevel, user.DamageLevel, user.SpeedLevel, 146 | user.Diamond, user.Point, user.UserID 147 | ); 148 | 149 | Console.WriteLine(query); 150 | 151 | using(MySqlCommand cmd = (MySqlCommand)conn.CreateCommand()) 152 | { 153 | cmd.CommandText = query; 154 | cmd.ExecuteNonQuery(); 155 | 156 | } 157 | 158 | conn.Close(); 159 | } 160 | return true; 161 | } 162 | 163 | 164 | 165 | } 166 | } --------------------------------------------------------------------------------