Skip to content

Instantly share code, notes, and snippets.

@StuWookie
Last active August 9, 2018 01:01
Show Gist options
  • Save StuWookie/fac144a815f6ec7eec8672c51db051d9 to your computer and use it in GitHub Desktop.
Save StuWookie/fac144a815f6ec7eec8672c51db051d9 to your computer and use it in GitHub Desktop.
CSV to Dictionary - All credit to Sloane Kelly
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Card
{
public int CardID { get; set; }
public int CardLvl { get; set; }
public bool CardPlayed { get; set; }
public string CardText { get; set; }
public Card(int cardID, int cardLvl, bool cardPlayed, string cardText)
{
CardID = cardID;
CardLvl = cardLvl;
CardPlayed = cardPlayed;
CardText = cardText;
}
public Card()
{
CardID = 0;
CardLvl = 0;
CardPlayed = false;
CardText = "";
}
}
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using System.Linq;
using LINQtoCSV;
public class DeckBuilder : MonoBehaviour {
private Dictionary<int, Card> CompleteDeck = new Dictionary<int, Card>();
[Tooltip("The file that contains the list to be converted to cards")]
public TextAsset CardList;
//I thought that I might be able to call this whenever I wanted - the same with the custom deck builder.
//but when I put this into anything that isn't a start function..
//I get the following error:
//NullReferenceException: Object reference not set to an instance of an object
//DeckBuilder.CreateDeck () (at Assets/Scripts/DeckBuilder.cs:32)
//DeckBuilder.ReturnLevelDeck (Int32 level) (at Assets/Scripts/DeckBuilder.cs:60)
//GameController.TestStart () (at Assets/Scripts/GameController.cs:27)
//
//I'm also continually being nagged about using a "new monobehaviour" instead of finding a gameobject
//private void CreateDeck()
void Start()
{
CompleteDeck.Clear();
CsvFileDescription inputFileDescription = new CsvFileDescription
{
SeparatorChar = ',',
FirstLineHasColumnNames = true
};
using (MemoryStream ms = new MemoryStream())
{
using (StreamWriter txtWriter = new StreamWriter(ms))
{
using (StreamReader txtReader = new StreamReader(ms))
{
txtWriter.Write(CardList.text);
txtWriter.Flush();
ms.Seek(0, SeekOrigin.Begin);
CsvContext cc = new CsvContext();
cc.Read<Card>(txtReader, inputFileDescription)
.ToList()
.ForEach(card =>
{
CompleteDeck[card.CardID] = new Card
{
CardLvl = card.CardLvl,
CardPlayed = card.CardPlayed,
CardText = card.CardText,
};
});
}
}
}
//(Text, P1, P1Gender, P2, P2Gender, P3, P3Gender, P4, P4Gender);
string output = string.Format((CompleteDeck[2].CardText), "a", "b");
Debug.Log(output);
}
public Dictionary<int, Card> ReturnLevelDeck(int level)
{
//CreateDeck();
Debug.Log(CompleteDeck.Count);
Dictionary<int, Card> returnDictionary = new Dictionary<int, Card>();
for(int i = 0; i< CompleteDeck.Count; i++)
if (CompleteDeck[i].CardLvl == level)
{
Debug.Log("found");
returnDictionary.Add(CompleteDeck[i].CardID, CompleteDeck[i]);
}
return returnDictionary;
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Events;
using System.Linq;
using System;
public class GameController : MonoBehaviour
{
private DeckBuilder Deck = new DeckBuilder();
public void QuitGame()
{
Application.Quit();
}
public void TestStart()
{
//string output = string.Format((Deck.CompleteDeck[2].CardText), "a", "b");
Dictionary<int, Card> Level0Cards = Deck.ReturnLevelDeck(0);
//I appreciate that this doesn't make any proper use of the IEnumerable nature of the dictionary, but I wanted to see what was going on..
for(int i = 0; i < Level0Cards.Count; i++)
{
string retstring = string.Format((Level0Cards[i].CardText), "James", "Bob");
Debug.Log(retstring);
}
}
//TEST CODE ONLY DOWN HERE
}
CardID CardLvl CardPlayed CardText
0 0 FALSE {0} Card 0 {1} Card 0
1 0 FALSE {0} Card 1 {1} Card 1
2 1 FALSE {0} Card 2 {1} Card 2
3 1 FALSE {0} Card 3 {1} Card 3
4 2 FALSE {0} Card 4 {1} Card 4
5 2 FALSE {0} Card 5 {1} Card 5
6 3 FALSE {0} Card 6 {1} Card 6
7 3 FALSE {0} Card 7 {1} Card 7
8 4 FALSE {0} Card 8 {1} Card 8
9 4 FALSE {0} Card 9 {1} Card 9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment