-
Notifications
You must be signed in to change notification settings - Fork 6
/
KerbalXUploadData.cs
205 lines (177 loc) · 9.15 KB
/
KerbalXUploadData.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
using System;
using System.IO;
using System.Collections.Generic;
using UnityEngine;
using KatLib;
using SimpleJSON;
namespace CraftManager
{
public class KerbalXUploadData
{
public CraftData craft = null;
public int update_to_id = 0; //KerbalX craft ID, which this craft would send an update to. (0 is considered null/false as 0 can never be a KerbalX ID)
internal string craft_name = "";
internal string hash_tags = "";
internal string craft_type = "Ship";
internal List<Image> images = new List<Image>();
internal Dictionary<string, string> action_groups = new Dictionary<string, string>() {
{ "1", "" }, { "2", "" }, { "3", "" }, { "4", "" }, { "5", "" }, { "6", "" }, { "7", "" }, { "8", "" }, { "9", "" }, { "0", "" },
{ "stage", "" }, { "gears", "" }, { "lights", "" }, { "RCS", "" }, { "SAS", "" }, { "brakes", "" }, { "abort", "" }
};
//keeps hold of created instances of KerbalXUploadData so they can be reloaded
internal static Dictionary<string, KerbalXUploadData> upload_data_store = new Dictionary<string, KerbalXUploadData>();
//returns a new instance of KerbalXUploadData for the given craft or returns an existing instance if one has already been created.
public static KerbalXUploadData prepare_for(CraftData for_craft){
KerbalXUploadData data;
if(upload_data_store.ContainsKey(for_craft.path)){
CraftManager.log("loading upload data for: " + for_craft.name);
data = upload_data_store[for_craft.path];
} else{
CraftManager.log("creating upload data for: " + for_craft.name);
data = new KerbalXUploadData(for_craft);
upload_data_store.Add(for_craft.path, data);
}
return data;
}
public KerbalXUploadData(CraftData for_craft){
craft = for_craft;
craft_name = craft.name;
List<string> craft_tags = new List<string>();
foreach(string tag in craft.tag_names()){
craft_tags.Add(System.Threading.Thread.CurrentThread.CurrentCulture.TextInfo.ToTitleCase(tag.Trim()).Replace(" ", ""));
}
hash_tags = String.Join(", ", craft_tags.ToArray());
}
public List<string> errors = new List<string>();
//returns true/false if images contains the given image, but unlike images.Contains(image), this checks by image path rather than instance of image object
public bool has_image(Image image){
return images.FindAll(c => c.path == image.path).Count > 0;
}
public void toggle_image(Image image){
if(images.FindAll(c => c.path == image.path).Count > 0){
images.RemoveAll(c => c.path == image.path);
}else{
if(images.Count > 2){
errors.Clear();
errors.Add("You can only add 3 images");
} else{
images.Add(image);
}
}
}
public bool is_valid{
get{
errors.Clear();
craft_name.Trim();
hash_tags.Trim();
if(!System.IO.File.Exists(craft.path)){
errors.Add("unable to find craft file");
return false;
}
if(String.IsNullOrEmpty(craft_name)){
errors.Add("The craft's name can't be blank");
}
if(craft_name.Length <= 2){
errors.Add("The craft's name must be at least 3 characters");
}
if(images.Count == 0){
errors.Add("You need to add at least 1 picture");
}
if(!KerbalX.craft_styles.Contains(craft_type)){
errors.Add("The craft's 'type' is not valid");
}
if(CraftManager.main_ui.show_transfer_indicator){
errors.Add("Another craft is still uploading, slow your roll!");
}
return errors.Count == 0;
}
}
public void post(){
if(is_valid){
CraftManager.main_ui.show_transfer_indicator = true;
CraftManager.main_ui.transfer_is_upload = true;
WWWForm craft_data = new WWWForm();
craft_data.AddField("craft_name", craft_name);
craft_data.AddField("craft_style", craft_type);
craft_data.AddField("craft_file", System.IO.File.ReadAllText(craft.path));
craft_data.AddField("part_data", JSONX.toJSON(part_info()));
craft_data.AddField("action_groups", JSONX.toJSON(action_groups));
craft_data.AddField("hash_tags", hash_tags);
int pic_count = 0;
foreach(Image image in images){
craft_data.AddField("images[image_" + pic_count++ + "]", Convert.ToBase64String(image.read_as_jpg()));
// craft_data.AddField("image_urls[url_" + pic_count++ + "]", "https://i.imgur.com/nSUkIe0.jpg");
}
KerbalX.api.upload_craft(craft_data, (resp, code) =>{
// var resp_data = JSON.Parse(resp);
if(code == 200){
CraftManager.log("craft uploaded OK");
KerbalX.api.fetch_existing_craft((empty_resp, status_code)=>{ //refresh remote craft info
craft.matching_remote_ids = null;
CraftManager.main_ui.close_upload_interface();
});
} else{
CraftManager.log("craft upload failed");
}
//return UI to craft list mode and show upload complete dialog.
CraftManager.main_ui.show_transfer_indicator = false;
// CraftManager.main_ui.unlock_ui();
CraftManager.main_ui.upload_complete_dialog(code, JSON.Parse(resp));
});
}
}
public void put(){
errors.Clear();
if(CraftManager.main_ui.show_transfer_indicator){
errors.Add("unable to update, another craft is still uploading");
}
if(errors.Count == 0){
CraftManager.main_ui.close_update_kerbalx_craft_dialog();
CraftManager.main_ui.show_transfer_indicator = true;
CraftManager.main_ui.transfer_is_upload = false;
WWWForm craft_data = new WWWForm();
craft_data.AddField("craft_name", craft_name);
craft_data.AddField("craft_file", System.IO.File.ReadAllText(craft.path));
craft_data.AddField("part_data", JSONX.toJSON(part_info()));
KerbalX.api.update_craft(update_to_id, craft_data, (resp, code) =>{
if(code == 200){
CraftManager.log("craft updated OK");
KerbalX.api.fetch_existing_craft((empty_resp, status_code) =>{
CraftManager.main_ui.close_upload_interface();
});
} else{
CraftManager.log("craft update failed");
}
CraftManager.main_ui.show_transfer_indicator = false;
});
}
}
private Dictionary<string, object> part_info(){
Dictionary<string, object> part_data = new Dictionary<string, object>();
foreach(string part_name in craft.part_name_list){
if(!part_data.ContainsKey(part_name) && CraftDataCache.part_data.ContainsKey(part_name)){
AvailablePart av_part = CraftDataCache.part_data[part_name];
if(av_part != null){
Part part = av_part.partPrefab;
Dictionary<string, object> part_detail = new Dictionary<string, object>();
part_detail.Add("mod", part.partInfo.partUrl.Split('/')[0]);
part_detail.Add("mass", part.mass);
part_detail.Add("cost", part.partInfo.cost);
part_detail.Add("category", part.partInfo.category.ToString());
if(part.CrewCapacity > 0){
part_detail.Add("CrewCapacity", part.CrewCapacity);
}
part_detail.Add("TechRequired", part.partInfo.TechRequired);
Dictionary<string, object> part_resources = new Dictionary<string, object>();
foreach(PartResource r in part.Resources){
part_resources.Add(r.resourceName, r.maxAmount);
}
part_detail.Add("resources", part_resources);
part_data.Add(part.name, part_detail);
}
}
}
return part_data;
}
}
}