Skip to content

Instantly share code, notes, and snippets.

@mohemohe
Created March 6, 2015 05:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mohemohe/a8fc02b3e59b7cc7c65d to your computer and use it in GitHub Desktop.
Save mohemohe/a8fc02b3e59b7cc7c65d to your computer and use it in GitHub Desktop.
TynCounter
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Threading;
using System.Threading.Tasks;
using Livet;
using Livet.Commands;
using Livet.Messaging;
using Livet.Messaging.IO;
using Livet.EventListeners;
using Livet.Messaging.Windows;
using LoXIVsharp;
using TynCounter.Models;
namespace TynCounter.ViewModels
{
public class MainWindowViewModel : ViewModel
{
/* コマンド、プロパティの定義にはそれぞれ
*
* lvcom : ViewModelCommand
* lvcomn : ViewModelCommand(CanExecute無)
* llcom : ListenerCommand(パラメータ有のコマンド)
* llcomn : ListenerCommand(パラメータ有のコマンド・CanExecute無)
* lprop : 変更通知プロパティ(.NET4.5ではlpropn)
*
* を使用してください。
*
* Modelが十分にリッチであるならコマンドにこだわる必要はありません。
* View側のコードビハインドを使用しないMVVMパターンの実装を行う場合でも、ViewModelにメソッドを定義し、
* LivetCallMethodActionなどから直接メソッドを呼び出してください。
*
* ViewModelのコマンドを呼び出せるLivetのすべてのビヘイビア・トリガー・アクションは
* 同様に直接ViewModelのメソッドを呼び出し可能です。
*/
/* ViewModelからViewを操作したい場合は、View側のコードビハインド無で処理を行いたい場合は
* Messengerプロパティからメッセージ(各種InteractionMessage)を発信する事を検討してください。
*/
/* Modelからの変更通知などの各種イベントを受け取る場合は、PropertyChangedEventListenerや
* CollectionChangedEventListenerを使うと便利です。各種ListenerはViewModelに定義されている
* CompositeDisposableプロパティ(LivetCompositeDisposable型)に格納しておく事でイベント解放を容易に行えます。
*
* ReactiveExtensionsなどを併用する場合は、ReactiveExtensionsのCompositeDisposableを
* ViewModelのCompositeDisposableプロパティに格納しておくのを推奨します。
*
* LivetのWindowテンプレートではViewのウィンドウが閉じる際にDataContextDisposeActionが動作するようになっており、
* ViewModelのDisposeが呼ばれCompositeDisposableプロパティに格納されたすべてのIDisposable型のインスタンスが解放されます。
*
* ViewModelを使いまわしたい時などは、ViewからDataContextDisposeActionを取り除くか、発動のタイミングをずらす事で対応可能です。
*/
/* UIDispatcherを操作する場合は、DispatcherHelperのメソッドを操作してください。
* UIDispatcher自体はApp.xaml.csでインスタンスを確保してあります。
*
* LivetのViewModelではプロパティ変更通知(RaisePropertyChanged)やDispatcherCollectionを使ったコレクション変更通知は
* 自動的にUIDispatcher上での通知に変換されます。変更通知に際してUIDispatcherを操作する必要はありません。
*/
private LoXIV _loxiv = new LoXIV();
private bool Initialized;
public async void Initialize()
{
if (!_loxiv.SearchProcess())
{
Messenger.Raise(new WindowActionMessage(WindowAction.Close, "Close"));
}
if (!_loxiv.SearchAddress())
{
Messenger.Raise(new WindowActionMessage(WindowAction.Close, "Close"));
}
ReadTyn();
SetLabel();
Initialized = true;
await Task.Run(() => ReadLogLoop());
}
private void ReadTyn()
{
if (File.Exists("current.tyn"))
using (var sr = new StreamReader("current.tyn"))
{
Tyn = Convert.ToInt32(sr.ReadToEnd());
}
}
private void WriteTyn()
{
using (var sr = new StreamWriter("current.tyn"))
{
sr.Write(Tyn);
}
}
private void ReadLogLoop()
{
while (true)
{
var list = new List<string>();
if (_loxiv.ReadPreformattedLog(out list) > 0)
{
foreach (var l in list)
{
TynPlus(l);
}
}
Thread.Sleep(1000);
}
}
private void TynPlus(string str)
{
if (str.Contains("が、微かに輝いた"))
{
Delta += 2;
}
else if (str.Contains("が、淡く輝きを放った。"))
{
Delta += 4;
}
else if (str.Contains("が、輝きを放った。"))
{
Delta += 8;
}
else if (str.Contains("が、強く輝きを放った!"))
{
Delta += 12;
}
else if (str.Contains("が、とても強く輝きを放った!!"))
{
Delta += 24;
}
else if (str.Contains("が、眩い輝きを放った!!!"))
{
Delta += 32;
}
}
private void SetLabel()
{
Label = Tyn + " / 2000 " + "(+" + Delta + ")";
if (Initialized)
{
WriteTyn();
}
}
#region Label変更通知プロパティ
private string _Label;
public string Label
{
get
{ return _Label; }
set
{
if (_Label == value)
return;
_Label = value;
RaisePropertyChanged();
}
}
#endregion
#region Tyn変更通知プロパティ
private int _Tyn;
public int Tyn
{
get
{ return _Tyn; }
set
{
if (_Tyn == value)
return;
_Tyn = value;
RaisePropertyChanged();
SetLabel();
}
}
#endregion
#region Delta変更通知プロパティ
private int _Delta = 0;
public int Delta
{
get
{ return _Delta; }
set
{
if (_Delta == value)
return;
var delta = _Delta;
_Delta = value;
Tyn += value - delta;
RaisePropertyChanged();
}
}
#endregion
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment