Browse Source

Merge pull request #232 from OctaAcid/dev

chore: Bind upload button
tags/0.1.0
Changli Tang GitHub 2 years ago
parent
commit
3bd1d877e2
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 232 additions and 84 deletions
  1. +23
    -6
      installer/Installer/MainWindow.xaml
  2. +79
    -61
      installer/Installer/Model.cs
  3. +130
    -17
      installer/Installer/ViewModel.cs

+ 23
- 6
installer/Installer/MainWindow.xaml View File

@@ -61,12 +61,29 @@
<TextBlock Grid.Row="6" Grid.Column="1" Text="用户名或密码错误!" Visibility="{Binding LoginFailVis}"/>

<TextBlock Grid.Row="2" Grid.Column="1" Grid.ColumnSpan="2" Text="你有已完成的比赛!" Visibility="{Binding MatchFinishedVis}"/>
<Button Grid.Row="3" Grid.Column="1" Name ="Upload" Content="上传代码" Command="{Binding ClickUploadCommand}" Visibility="{Binding WebVis}"/>
<Button Grid.Row="5" Grid.Column="1" Name="Download" Content="下载回放" Command="{Binding ClickDownloadCommand}" Visibility="{Binding WebVis}" />
<Button Grid.Row="7" Grid.Column="1" Name="Replay" Content="打开回放" Command="{Binding ClickReplayCommand}" Visibility="{Binding WebVis}" />
<Button Grid.Row="3" Grid.Column="3" Grid.ColumnSpan="2" Content="申请对战" Command="{Binding ClickRequestCommand}" Visibility="{Binding WebVis}" />
<TextBox Grid.Row="4" Grid.Column="3" Grid.ColumnSpan="2" Text="{Binding Enemy}" Visibility="{Binding WebVis}" />
<Button Grid.Row="3" Grid.Column="1" Name ="Upload" Content="{Binding UploadBtnCont}" Command="{Binding ClickUploadCommand}" Visibility="{Binding WebVis}"/>
<TextBlock Grid.Row="4" Grid.Column="1" Grid.ColumnSpan="2" Text="{Binding CodeName}" Visibility="{Binding UploadReadyVis}" />
<Button Grid.Row="3" Grid.Column="2" Name="ReUpload" Content="重新选择代码" Command="{Binding ReselectCommand}" Visibility="{Binding UploadReadyVis}" />
<Button Grid.Row="6" Grid.Column="3" Grid.ColumnSpan="2" Content="退出登录" Command="{Binding ClickBackCommand}" Visibility="{Binding WebVis}" />

<!--objects below are not enabled-->
<Button Grid.Row="5" Grid.Column="1" Name="Download" Content="下载回放" IsEnabled="False" Command="{Binding ClickDownloadCommand}" Visibility="{Binding CoverVis}" />
<Button Grid.Row="7" Grid.Column="1" Name="Replay" Content="打开回放" IsEnabled="False" Command="{Binding ClickReplayCommand}" Visibility="{Binding CoverVis}" />
<Button Grid.Row="3" Grid.Column="3" Grid.ColumnSpan="2" Content="申请对战" IsEnabled="False" Command="{Binding ClickRequestCommand}" Visibility="{Binding WebVis}" />
<TextBox Grid.Row="4" Grid.Column="3" Grid.ColumnSpan="2" Text="暂不支持" IsEnabled="False" Visibility="{Binding WebVis}" />
<StackPanel Grid.Row="5" Grid.Column="1" Grid.RowSpan="3" Grid.ColumnSpan="2">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="15"/>
<RowDefinition Height="15"/>
<RowDefinition Height="15"/>
<RowDefinition Height="15"/>
</Grid.RowDefinitions>
<RadioButton GroupName="playerNum" Grid.Row="0" Visibility="{Binding UploadReadyVis}">玩家1</RadioButton>
<RadioButton GroupName="playerNum" Grid.Row="1" Visibility="{Binding UploadReadyVis}">玩家2</RadioButton>
<RadioButton GroupName="playerNum" Grid.Row="2" Visibility="{Binding UploadReadyVis}">玩家3</RadioButton>
<RadioButton GroupName="playerNum" Grid.Row="3" Visibility="{Binding UploadReadyVis}">玩家4</RadioButton>
</Grid>
</StackPanel>
</Grid>
</Window>

+ 79
- 61
installer/Installer/Model.cs View File

@@ -48,6 +48,9 @@ namespace starter.viewmodel.settings
Username = "";
Password = "";
updates = "";
CodeRoute = "";
UploadReady = false;
LoginFailed = false;
}

/// <summary>
@@ -58,11 +61,11 @@ namespace starter.viewmodel.settings
if (Tencent_cos_download.CheckAlreadyDownload())
{
MessageBoxResult repeatOption = MessageBox.Show($"文件已存在于{Downloader.Program.Data.FilePath},是否移动到新位置?", "重复安装", MessageBoxButton.YesNo, MessageBoxImage.Warning, MessageBoxResult.No);
// ask if abort install, with warning sign, defalut no;
// ask if abort install, with warning sign, defalut move instead of abort;
if (repeatOption == MessageBoxResult.No)
{
Route = Data.FilePath;
return false; // 回到选择地址界面
return false;
}
else
{
@@ -120,9 +123,9 @@ namespace starter.viewmodel.settings
}
}

public void Login()
public async Task<bool> Login()
{
_ = web.LoginToEEsast(client, Username, Password);
return await web.LoginToEEsast(client, Username, Password);
}
public bool Update()
{
@@ -146,6 +149,25 @@ namespace starter.viewmodel.settings
return false;
}
}

public async Task<int> Upload()
{
switch (CodeRoute.Substring(CodeRoute.LastIndexOf('.') + 1))
{
case "cpp":
Language = "cpp";
break;
case "h":
Language = "cpp";
break;
case "py":
Language = "python";
break;
default:
return -8;
}
return await web.UploadFiles(client, CodeRoute, Language, "player_1");
}
/// <summary>
/// Route of files
/// </summary>
@@ -162,6 +184,18 @@ namespace starter.viewmodel.settings
{
get; set;
}
public string CodeRoute
{
get; set;
}
public string Language
{
get; set;
}
public string PlayerNum
{
get; set;
}
/// <summary>
/// 关于更新的屏幕显示信息
/// </summary>
@@ -212,6 +246,11 @@ namespace starter.viewmodel.settings
{
get; set;
}

public bool UploadReady
{
get; set;
}
}
}
namespace Downloader
@@ -355,8 +394,8 @@ namespace Downloader
.Build(); // 创建 CosXmlConfig 对象

// 永久密钥访问凭证
string secretId = "***"; //"云 API 密钥 SecretId";
string secretKey = "***"; //"云 API 密钥 SecretKey";
string secretId = "***"; //"云 API 密钥 SecretId";
string secretKey = "***"; //"云 API 密钥 SecretKey";

long durationSecond = 1000; // 每次请求签名有效时长,单位为秒
QCloudCredentialProvider cosCredentialProvider = new DefaultQCloudCredentialProvider(
@@ -997,7 +1036,7 @@ namespace Downloader
}
else if (choose == "9")
{
await web.UploadFiles(client);
await web.UploadFiles(client, "", "", "");
}
else if (choose == "exit")
{
@@ -1013,8 +1052,9 @@ namespace WebConnect
{
class Web
{
public enum language { cpp, py };
public static string logintoken = "";
async public Task LoginToEEsast(HttpClient client, string useremail, string password)
async public Task<bool> LoginToEEsast(HttpClient client, string useremail, string password)
{
string token = "";
using (var response = await client.PostAsync("https://api.eesast.com/users/login", JsonContent.Create(new
@@ -1045,71 +1085,42 @@ namespace WebConnect
Console.WriteLine(code);
if (code == 401)
{
Console.WriteLine("邮箱或密码错误!");
//Console.WriteLine("邮箱或密码错误!");
return false;
}
return;
break;
}
return true;
}
}
async public Task UploadFiles(HttpClient client) //用来上传文件
/// <summary>
///
/// </summary>
/// <param name="client">http client</param>
/// <param name="tarfile">代码源位置</param>
/// <param name="type">编程语言,格式为"cpp"或"python"</param>
/// <param name="plr">第x位玩家,格式为"player_x"</param>
/// <returns>-1:tokenFail;-2:FileNotExist;-3:CosFail;-4:loginTimeout;-5:Fail;-6:ReadFileFail;-7:networkError</returns>
async public Task<int> UploadFiles(HttpClient client, string tarfile, string type, string plr) //用来上传文件
{
if (!ReadToken()) //读取token失败
{
return;
return -1;
}
try
{
string tarfile; //要上传的文件路径
string content;
string filedest; //文件目的地
client.DefaultRequestHeaders.Authorization = new("Bearer", logintoken);
Console.WriteLine("请输入要上传的文件完整路径:");
tarfile = Console.ReadLine();
if (!File.Exists(tarfile))
{
Console.WriteLine("文件不存在!");
return;
}
Console.WriteLine("请选择语言类型:1.C++ 2.python");
string type = Console.ReadLine();
if (type == "1")
{
type = "cpp";
}
else if (type == "2")
{
type = "python";
}
else
{
return;
}
Console.WriteLine("请确认这是哪个玩家的代码:1.player_1 2.player_2 3.player_3 4.player_4");
string plr = Console.ReadLine();
if (plr == "1")
{
plr = "player_1";
}
else if (plr == "2")
{
plr = "player_2";
}
else if (plr == "3")
{
plr = "player_3";
}
else if (plr == "4")
{
plr = "player_4";
}
else
{
return;
//Console.WriteLine("文件不存在!");
return -2;
}
using FileStream fs = new FileStream(tarfile, FileMode.Open, FileAccess.Read);
using StreamReader sr = new StreamReader(fs);
content = sr.ReadToEnd();
using (var response = await client.GetAsync($"https://api.eesast.com/static/player?team_id={GetTeamId()}"))
using (var response = await client.GetAsync($"https://api.eesast.com/static/player?team_id={await GetTeamId()}"))
{
switch (response.StatusCode)
{
@@ -1162,27 +1173,30 @@ namespace WebConnect
}
catch (Exception e)
{
Console.WriteLine("CosException: " + e);
return -3;
}

break;
case System.Net.HttpStatusCode.Unauthorized:
Console.WriteLine("您未登录或登录过期,请先登录");
break;
//Console.WriteLine("您未登录或登录过期,请先登录");
return -4;
default:
Console.WriteLine("上传失败!");
break;
//Console.WriteLine("上传失败!");
return -5;
}
}
}
catch (IOException)
{
Console.WriteLine("文件读取错误!请检查文件是否被其它应用占用!");
//Console.WriteLine("文件读取错误!请检查文件是否被其它应用占用!");
return -6;
}
catch
{
Console.WriteLine("请求错误!请检查网络连接!");
//Console.WriteLine("请求错误!请检查网络连接!");
return -7;
}
return 0;
}

async public Task UserDetails(HttpClient client) // 用来测试访问网站
@@ -1238,6 +1252,10 @@ namespace WebConnect
json += @"{""THUAI6""" + ":" + @"""2023""}";
}
dict = JsonConvert.DeserializeObject<Dictionary<string, string>>(json);
if (dict.ContainsKey("token"))
{
dict.Remove("token");
}
dict?.Add("token", logintoken);
}
using FileStream fs2 = new FileStream(savepath, FileMode.OpenOrCreate, FileAccess.ReadWrite);


+ 130
- 17
installer/Installer/ViewModel.cs View File

@@ -20,6 +20,7 @@ namespace starter.viewmodel.settings
/// </summary>
public SettingsViewModel()
{
//Program.Tencent_cos_download.UpdateHash();
if (Downloader.Program.Tencent_cos_download.CheckAlreadyDownload())
{
obj.checkUpdate();
@@ -135,10 +136,7 @@ namespace starter.viewmodel.settings

public string Route
{
get
{
return obj.Route;
}
get => obj.Route;
set
{
obj.Route = value;
@@ -147,10 +145,7 @@ namespace starter.viewmodel.settings
}
public string Username
{
get
{
return obj.Username;
}
get => obj.Username;
set
{
obj.Username = value;
@@ -159,13 +154,20 @@ namespace starter.viewmodel.settings
}
public string Password
{
get { return obj.Password; }
get => obj.Password;
set
{
obj.Password = value;
this.RaisePropertyChanged("Password");
}
}
public string CodeName
{
get
{
return obj.CodeRoute.Substring(obj.CodeRoute.LastIndexOf('/') == -1 ? obj.CodeRoute.LastIndexOf('\\') + 1 : obj.CodeRoute.LastIndexOf('/') + 1);
}
}
public Visibility MenuVis
{
get
@@ -208,6 +210,13 @@ namespace starter.viewmodel.settings
return Status == SettingsModel.Status.web ? Visibility.Visible : Visibility.Collapsed;
}
}
public Visibility CoverVis
{
get
{
return Status == SettingsModel.Status.web && !obj.UploadReady ? Visibility.Visible : Visibility.Collapsed;
}
}
public Visibility LoginFailVis
{
get
@@ -222,6 +231,10 @@ namespace starter.viewmodel.settings
return obj.CombatCompleted ? Visibility.Visible : Visibility.Collapsed;
}
}
public Visibility UploadReadyVis
{
get { return obj.UploadReady ? Visibility.Visible : Visibility.Collapsed; }
}

public string UpdateBtnCont
{
@@ -247,6 +260,39 @@ namespace starter.viewmodel.settings
return obj.UpdatePlanned ? "更新" : "启动";
}
}
public string UploadBtnCont
{
get
{
return obj.UploadReady ? "上传代码" : "选择代码上传";
}
}

public string RouteSelectWindow(string type)
{
if (type == "Folder")
{
using (FolderBrowserDialog dialog = new FolderBrowserDialog())
{
_ = dialog.ShowDialog();
if (dialog.SelectedPath != String.Empty)
return dialog.SelectedPath;
}
}
else if (type == "File")
{
var openFileDialog = new Microsoft.Win32.OpenFileDialog()
{
Filter = "c++ Source Files (.cpp)|*.cpp|c++ Header File (.h)|*.h|python Source File (.py)|*.py"
};
var result = openFileDialog.ShowDialog();
if (result == true)
{
return openFileDialog.FileName;
}
}
return "";
}

private BaseCommand clickBrowseCommand;
public BaseCommand ClickBrowseCommand
@@ -257,12 +303,7 @@ namespace starter.viewmodel.settings
{
clickBrowseCommand = new BaseCommand(new Action<object>(o =>
{
using (FolderBrowserDialog dialog = new FolderBrowserDialog())
{
_ = dialog.ShowDialog();
if (dialog.SelectedPath != String.Empty)
Route = dialog.SelectedPath;
}
Route = RouteSelectWindow("Folder");
}));
}
return clickBrowseCommand;
@@ -397,9 +438,16 @@ namespace starter.viewmodel.settings
{
if (clickLoginCommand == null)
{
clickLoginCommand = new BaseCommand(new Action<object>(o =>
clickLoginCommand = new BaseCommand(new Action<object>(async o =>
{
obj.Login();
if (!(await obj.Login()))
{
obj.LoginFailed = true;
}
else
{
Status = SettingsModel.Status.web;
}
this.RaisePropertyChanged("LoginFailVis");
}));
}
@@ -469,5 +517,70 @@ namespace starter.viewmodel.settings
return clickBackCommand;
}
}
private BaseCommand clickUploadCommand;
public BaseCommand ClickUploadCommand
{
get
{
if (clickUploadCommand == null)
{
clickUploadCommand = new BaseCommand(new Action<object>(async o =>
{
if (obj.UploadReady)
{
switch (await obj.Upload())
{
case -1:
MessageBox.Show("Token失效!", "", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK);
break;
case -2:
MessageBox.Show("目标路径不存在!", "", MessageBoxButton.OK, MessageBoxImage.Warning, MessageBoxResult.OK);
break;
case -3:
MessageBox.Show("服务器错误", "", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK);
break;
case -4:
MessageBox.Show("您未登录或登录失效", "", MessageBoxButton.OK, MessageBoxImage.Warning, MessageBoxResult.OK);
Status = SettingsModel.Status.login;
break;
case -5:
MessageBox.Show("您未报名THUAI!", "", MessageBoxButton.OK, MessageBoxImage.Error, MessageBoxResult.OK);
break;
case -6:
MessageBox.Show("读取文件失败,请确认文件是否被占用", "", MessageBoxButton.OK, MessageBoxImage.Warning, MessageBoxResult.OK);
break;
case -7:
MessageBox.Show("网络错误,请检查你的网络", "", MessageBoxButton.OK, MessageBoxImage.Warning, MessageBoxResult.OK);
break;
case -8:
MessageBox.Show("不是c++或python源文件", "", MessageBoxButton.OK, MessageBoxImage.Warning, MessageBoxResult.OK);
break;
}
obj.CodeRoute = "";
obj.UploadReady = false;
this.RaisePropertyChanged("UploadBtnCont");
this.RaisePropertyChanged("UploadReadyVis");
}
else
{
obj.CodeRoute = RouteSelectWindow("File");
if (obj.CodeRoute != "")
{
obj.UploadReady = true;
this.RaisePropertyChanged("UploadBtnCont");
this.RaisePropertyChanged("UploadReadyVis");
this.RaisePropertyChanged("CodeName");
}
else
{
MessageBox.Show("未选择代码,请重新选择!", "", MessageBoxButton.OK, MessageBoxImage.Warning, MessageBoxResult.OK);
}
}
}));
}
return clickUploadCommand;
}
}
}

}

Loading…
Cancel
Save