using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
namespace homework
{
public class WeaponInfo
{
private int id;
public int Id
{
get { return this.id; }
set { this.id = value; }
}
private int enforce;
public int Enforce
{
get { return this.enforce; }
set { this.enforce = value; }
}
public WeaponInfo(int id, int enforce = 0)
{
this.id = id;
this.enforce = enforce;
}
}
}
weaponinfo 클래스
안에 일단은 id와 강화수치밖에 없지만 id로 다른내용을 찾는 dictionary를 추가할 수 있음
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
namespace homework
{
public class Item
{
private WeaponInfo weaponInfo;
public int Id
{
get;set;
}
public int Enforce
{
get;set;
}
public Item(int id , int enforce = 0)
{
this.weaponInfo = new WeaponInfo(id,enforce);
this.Id = this.weaponInfo.Id;
this.Enforce = this.weaponInfo.Enforce;
}
}
}
item클래스
weaponinfo에게 id와 enforce를 넘겨주는 역할을 함
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using System.IO;
namespace homework
{
public class Inventory
{
List<Item> items;
public List<Item> Items
{
get { return this.items; }
}
private string ItemsInfoJson
{
get;set;
}
public Inventory()
{
this.items = new List<Item>();
}
public void Add(Item item)
{
this.items.Add(item);
}
public void Add(int id,int enforce = 0)
{
this.items.Add(new Item(id,enforce));
}
public string Print()
{
/*foreach(Item item in this.items)
{
Console.WriteLine("id : {0} , enforce : {1}", item.Id, item.Enforce);
}*/
this.ItemsInfoJson = JsonConvert.SerializeObject(items);
//Console.WriteLine(this.ItemsInfoJson);
File.WriteAllText("./inventory_info.json",this.ItemsInfoJson);
return this.ItemsInfoJson;
}
}
}
inventory클래스
LIst<item>을 갖고있음
1번 과제로 아이템을 얻고 아이템 목록 json만들기가 가능함

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using System.IO;
namespace homework
{
public class Hero
{
public int PlayerId
{
get; set;
}
private Inventory Inven
{
get;set;
}
public List<Item> items;
public Hero(int id)
{
this.PlayerId = id;
this.Inven = new Inventory();
items = this.Inven.Items;
}
public void CreatePlayerInfo()
{
string PlayerInfoJson = JsonConvert.SerializeObject(this);
Console.WriteLine(PlayerInfoJson);
File.WriteAllText("./player" + this.PlayerId + "_info.json", PlayerInfoJson);
}
public void ObtainItem(int id, int enforce = 0)
{
Inven.Add(id, enforce);
}
}
}
Hero클래스
playerId와 inventory를 갖고있어 영웅마다 따로 아이템을 얻는게 가능함
json을 생성할때 playerId에 따라 다른파일로 생성함

???
왜 playerId가 뒤로 가는진 진짜 모르겠음
+
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using System.IO;
namespace homework
{
public class Hero
{
public int PlayerId
{
get; set;
}
private Inventory Inven
{
get;set;
}
public List<Item> items;
public Hero(int id)
{
this.PlayerId = id;
this.Inven = new Inventory();
this.PlayerCheck();
}
public Hero()
{
}
public void CreatePlayerInfo()
{
string PlayerInfoJson = JsonConvert.SerializeObject(this);
Console.WriteLine(PlayerInfoJson);
File.WriteAllText("./player" + this.PlayerId + "_info.json", PlayerInfoJson);
}
public void PlayerCheck()
{
if(File.Exists("./player" + this.PlayerId + "_info.json"))
{
Console.WriteLine("기존유저");
string PlayerInfoJson = File.ReadAllText("./player" + this.PlayerId + "_info.json");
Hero hero;
hero = JsonConvert.DeserializeObject<Hero>(PlayerInfoJson);
this.PlayerId = hero.PlayerId;
this.items = hero.items;
}
else
{
Console.WriteLine("신규유저");
items = this.Inven.Items;
return;
}
}
public void ObtainItem(int id, int enforce = 0)
{
Inven.Add(id, enforce);
}
}
}
기존유저와 신규유저 구분하는 기능추가
해당 playerId의 "./player" + this.PlayerId + "_info.json" 가 있는지 체크해서 있다면 기존유저로 기존 인벤토리를 로드
없다면 신규유저로 새로 인벤토리 지급
'C# > 수업과제' 카테고리의 다른 글
| 0705 (0) | 2022.07.05 |
|---|---|
| 0701 c# 프로그래밍 과제 / 알고리즘 과제 (0) | 2022.07.01 |
| 반복문 과제 (0) | 2022.06.10 |
| 디아블로 아이템 출력 하기 (0) | 2022.06.09 |