Jump to content
Sign in to follow this  
Winter

A Weewar API notifier

Recommended Posts

Winter

I've been playing at Weewar.com for a while now and it's been lots of fun, but the constant deluge of turn emails was getting annoying and the other 3rd party notifier applications used too much ram (like 60+ megs) since they are all based on frameworks (Python, Java, AIR). So I built one in Auto It and it uses a whopping 500-900KB of ram. Awesome!

The main page is here: http://wwdb.sourceforge.net

The Weewar API provides responses in XML, so I parsed that as well as possible, but I'm sure there's a better way.

The settings window is a little weird in that you can't click on which input you want to use, you have to tab to it. I'm really not sure why but it still works and that's fine with me.

Anyways, here's the current version of the script:

CODE
#cs ----------------------------------------------------------------------------

AutoIt Version: 3.2.4.9

Author: Craig Woodward AKA winter on weewar.com

Script Function:

Use the HQ API on weewar.com to check and see if any games need attention.

#ce ----------------------------------------------------------------------------

#include <GUIConstants.au3>

;Some global variables

Global Const $READMODE = 0; Open a file to be read

Global Const $WRITEMODE = 2; Open a file to be written to.

Global $strCredentialsFile = @AppDataDir&"\WWD\WWCredentials.txt";

Global $strResultsFile = @AppDataDir&"\WWD\WWHQ.xml";

Global $strUserName = "";

Global $strAPIKey = "";

Global $numMinutes = 5; Minutes between checks user can set this later

Opt("TrayOnEventMode",1) ; Simplify checking for tray menu clicks

Opt("TrayMenuMode",1) ; Default tray menu items (Script Paused/Exit) will not be shown.

AutoItSetOption ( "GUICloseOnESC", 1); Allow the user to hit ESC to close the GUIs.

TraySetClick(20) ; Double Left click or single right click will show the tray menu.

TraySetToolTip("Starting, please wait"); Let the impatient user know we're still starting.

;Set up the Notification Tray icon Menu

$CheckNowItem = TrayCreateItem("Check Now");

TrayItemSetOnEvent($CheckNowItem,"CheckNow");

$OpenHQItem = TrayCreateItem("Open HQ Web Page");

TrayItemSetOnEvent($OpenHQItem,"OpenHQ");

TrayCreateItem("")

$settingsItem = TrayCreateItem("Settings")

TrayItemSetOnEvent($settingsItem,"Settings");

$aboutItem = TrayCreateItem("About")

TrayItemSetOnEvent($aboutItem, "ShowAbout");

TrayCreateItem("")

$exitItem = TrayCreateItem("Exit");

TrayItemSetOnEvent($exitItem,"ExitScript");

TraySetState()

TraySetClick(16)

;MAIN LINE (all 11 lines of it...)

While (FileExists($strCredentialsFile)<>1)

Settings();

WEnd

$hSettingsFile = FileOpen($strCredentialsFile,$READMODE);

$strUserName = FileReadLine($hSettingsFile);

$strAPIKey = FileReadLine($hSettingsFile);

$numMinutes = FileReadLine($hSettingsFile);

FileClose($hSettingsFile);

While (1==1)

CheckNow();

Sleep($numMinutes*60000)

WEnd

;FUNCTIONS

Func ShowGames() ; To be implemented

EndFunc

Func OpenHQ() ; Open the user's HQ web page.

ShellExecute("http://weewar.com/headquarters/games");

Send("{ESC}");

EndFunc

; Create a window and get the user ID, password and time between checks. Write the results out to a file.

Func Settings()

$hSettingsWindow = GUICreate ( "Weewar Account Settings" , 300 , 130);

GUISetBkColor(0xFFFFFF,$hSettingsWindow);

GuiSetIcon(@ScriptFullPath, 0)

$ctrlID = GUICtrlCreateLabel("User Name:",5,7,150);

SetNormalTahomaFont(12,$ctrlID);

$ctrlID = GUICtrlCreateLabel("API Token:",5,35,150);

SetNormalTahomaFont(12,$ctrlID);

$ctrlID = GUICtrlCreateLabel("Minutes between Checks:",5,65,200);

SetNormalTahomaFont(12,$ctrlID);

$ctrlUserName = GuiCtrlCreateInput($strUserName, 95, 7,200,20);

$ctrlTokenInput = GuiCtrlCreateInput($strAPIKey, 95, 36,200,20);

$ctrlMinutesInput = GUICtrlCreateInput($numMinutes,185,66,20,20);

$buttonTest = GUICtrlCreateButton("Test Settings", 185,95);

$buttonOK = GUICtrlCreateButton("OK",255,95,40);

GUICtrlSetState($buttonOK,$GUI_DISABLE);

GUISetState(@SW_SHOW,$hSettingsWindow);

; GUI MESSAGE LOOP

GuiSetState()

$guiMsg = GUIGetMsg(1);

While $guiMsg[0] <> $GUI_EVENT_CLOSE

;If ($guiMsg[0] <>0) Then

; MsgBox(0,"Debug",""&$buttonTest&"=" &$guiMsg[0]);

;EndIf

Select

Case $buttonTest == $guiMsg[0]

$strUserName = GUICtrlRead($ctrlUserName);

$strAPIKey = GUICtrlRead($ctrlTokenInput);

$testResults = InetGet ("http://"&$strUserName&":"&$strAPIKey&"@weewar.com/api1/headquarters",$strResultsFile, 1);

If ($testResults == 1) Then

MsgBox(64,"Success", "Everything looks good!");

GUICtrlSetState($buttonOK,$GUI_ENABLE);

Else

MsgBox(16,"Uh Oh", "Something went wrong."&@CRLF&"Check your internet connection, proxy settings and user information");

GUICtrlSetState($buttonOK,$GUI_DISABLE);

EndIf

Case $buttonOK == $guiMsg[0]

DirCreate(@AppDataDir&"\WWD");

$hSettingsFile = FileOpen($strCredentialsFile,$WRITEMODE);

$strUserName = GUICtrlRead($ctrlUserName);

FileWriteLine($hSettingsFile,$strUserName);

$strAPIKey = GUICtrlRead($ctrlTokenInput);

FileWriteLine($hSettingsFile,$strAPIKey);

$numMinutes = GUICtrlRead($ctrlMinutesInput);

If (StringIsInt($numMinutes) == 1) Then ;Check to makesure use entere a number

FileWriteLine($hSettingsFile,$numMinutes);

Else

FileWriteLine($hSettingsFile,5);If the user entered bad data use 5 minutes

$numMinutes = 5;

EndIf

FileClose($hSettingsFile);

Send("{ESC}");

EndSelect

$guiMsg = GUIGetMsg(1);

WEnd

GUISetState(@SW_HIDE,$hSettingsWindow);

CheckNow();

EndFunc

Func ShowAbout()

$hAboutWindow = GUICreate("About WeeWar Dashboard",400,101,-1,-1,$WS_CAPTION+$WS_POPUP+$WS_SYSMENU);

GUISetBkColor(0xFFFFFF,$hAboutWindow);

$ctrlID = GUICtrlCreateLabel("WeeWar Dashboard",5,5,300);

SetNormalTahomaFont(16,$ctrlID);

$ctrlID = GUICtrlCreateLabel("An application in AutoIt by Craig Woodward",5,35,350);

SetNormalTahomaFont(12,$ctrlID);

$ctrlID = GUICtrlCreateLabel("Version: "&FileGetVersion(@ScriptFullPath),5,55,250);

SetNormalTahomaFont(12,$ctrlID);

$ctrlID = GUICtrlCreateLabel("Website: ",5,75,60);

SetNormalTahomaFont(12,$ctrlID);

$linkCtrlID = GUICtrlCreateLabel("http://wwdb.sourceforge.net",75,75,210);

GUICtrlSetColor($linkCtrlID, 0x0000ff);Blue

GUICtrlSetFont($linkCtrlID,12, 400, 4, "Tahoma"); underlined tahoma

GUICtrlSetCursor($linkCtrlID,0);pointer hand

$picCtrlID = GuiCtrlCreatePic(@AppDataDir&"\WWD\weewar_corner_1.gif",325,0,75,101);

GUICtrlSetCursor($picCtrlID,0);pointer hand

GUISetState();

$msg = GUIGetMsg(1);

While $msg[0] <> $GUI_EVENT_CLOSE

If ($msg[0] > 0) Then

If ($msg[0] == $linkCtrlID) Then ShellExecute(GUICtrlRead($msg[0]));

If ($msg[0] == $picCtrlID) Then ShellExecute("http://weewar.com/user/winter?referrer=winter");

EndIf

$msg = GUIGetMsg(1);

WEnd

GUISetState(@SW_HIDE,$hAboutWindow);

EndFunc

Func SetNormalTahomaFont($size,$ctrlID)

GUICtrlSetBkColor ($ctrlID,$GUI_BKCOLOR_TRANSPARENT );0xFFFFFF);

GUICtrlSetFont ($ctrlID,$size, 400, 0, "Tahoma") ;regular Tohoma

EndFunc

Func CheckNow() ; Check if any games are 'inNeedOfAttention'

$results = InetGet ("http://"&$strUserName&":"&$strAPIKey&"@weewar.com/api1/headquarters",$strResultsFile, 1);

If ($results == 1) Then

Select

Case @MIN < 10-$numMinutes

TraySetToolTip("Last check was at "&@HOUR&":"&@MIN&@CR&"Next check will be at "&@HOUR&":0"&@MIN+$numMinutes );

Case @MIN == 60-$numMinutes

TraySetToolTip("Last check was at "&@HOUR&":"&@MIN&@CR&"Next check will be at "&@HOUR+1&":00" );

Case @MIN > 60-$numMinutes

TraySetToolTip("Last check was at "&@HOUR&":"&@MIN&@CR&"Next check will be at "&@HOUR+1&":0"&@MIN-(60-$numMinutes) );

Case Else

TraySetToolTip("Last check was at "&@HOUR&":"&@MIN&@CR&"Next check will be at "&@HOUR&":"&@MIN+$numMinutes );

EndSelect

$hResultsFile = FileOpen($strResultsFile, $READMODE);

$strTrayTipText = "";

If ($hResultsFile <> -1) Then

SetError(0);

$line = FileReadLine($hResultsFile);

While (@error <> -1);build the pop up window text

If (StringInStr($line,'inNeedOfAttention="true"')<>0) Then

While (StringInStr($line,'<name>') == 0)

$line = FileReadLine($hResultsFile);

WEnd

$strGameName = StringTrimLeft(StringTrimRight($line,7),6);

While (StringInStr($line,'<link>') == 0)

$line = FileReadLine($hResultsFile);

WEnd

$strGameLink = StringTrimLeft(StringTrimRight($line,7),6);

$strTrayTipText = $strTrayTipText&$strGameName & @CR & $strGameLink&@CR;

EndIf

$line = FileReadLine($hResultsFile);

WEnd

;Next two lines are for debugging, can simulate games needing attention.

;TrayTip("It's your turn",$strTrayTipText,$numMinutes*30);

;$strTrayTipText = "8th Floor special"&@CR&"http://weewar.com/game/20250"&@CR&"Battle Royale"&@CR&"http://weewar.com/game/21660"&@CR;

If ($strTrayTipText <> "") Then

TraySetIcon(@AppDataDir&"\WWD\DFA-Green.ICO");

ShowGamesPopup($strTrayTipText);

Else

TraySetIcon(@ScriptFullPath,0);

EndIf

EndIf

Else

TraySetIcon(@AppDataDir&"\WWD\DFA-Red.ICO");

TraySetToolTip("Error connecting on last attempt at "&@HOUR&":"&@MIN)

EndIf

EndFunc

Func ShowGamesPopup($strText)

$blankLines = 0;

$arrStrings = StringSplit($strText,@CR,1);

$TaskbarHeight = 0;

;The taskbar height is stored in a binary reg entry in big endian format so it needs to be stripped and reordered.

$TaskbarHeight1 = StringTrimRight(StringTrimLeft(RegRead("HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Desktop\Components","Position"),34), 52);

$TaskbarHeight2 = StringTrimRight(StringTrimLeft(RegRead("HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Desktop\Components","Position"),32), 54);

$TaskbarHeight = $TaskbarHeight1&$TaskbarHeight2; now it's in the right order

$TaskbarHeight = @DesktopHeight-Dec($TaskbarHeight);converted to decimal

$hPopupWindow = GUICreate("It's your turn...",210,($arrStrings[0]-1)*20+5,@DesktopWidth-215,@DesktopHeight-($TaskbarHeight+29)-(($arrStrings[0]-1)*20),$WS_CAPTION+$WS_POPUP+$WS_SYSMENU,$WS_EX_TOOLWINDOW+$WS_EX_TOPMOST);;,$WS_POPUP);

GUISetBkColor ( 0xFFFFFF,$hPopupWindow);white background

For $i = 1 To $arrStrings[0]

If ($arrStrings[$i] <> "") Then

$ctrlID = GUICtrlCreateLabel($arrStrings[$i],5,(5+(20*($i-1-$blankLines))),205)

GUICtrlSetBkColor ($ctrlID,0xFFFFFF);

GUICtrlSetFont ($ctrlID,9, 400, 0, "Tahoma") ;regular Tohoma

If (StringInStr(GUICtrlRead($ctrlID),"http://weewar.com/game/") <>0) Then

GUICtrlSetColor( $ctrlID, 0x0000ff);Blue

GUICtrlSetFont ($ctrlID,9, 400, 4, "Tahoma"); underlined tahoma

GUICtrlSetCursor($ctrlID,0);pointer hand

EndIf

Else

$blankLines = $blankLines+1;

EndIf

Next

GUISetState(@SW_SHOW,$hPopupWindow);

$guiMsg = 0;

$guiMsg = GUIGetMsg(1);

$milliSeconds = 0;

While $guiMsg[0] <> $GUI_EVENT_CLOSE

If ($guiMsg[0] > 0) Then

If (StringInStr(GUICtrlRead($guiMsg[0]),"http://weewar.com/game/") <>0) Then ShellExecute(GUICtrlRead($guiMsg[0]));

EndIf

$guiMsg = GUIGetMsg(1);

Sleep(10);

$milliSeconds = $milliSeconds+10;

If $milliSeconds >= (45000*$numMinutes) Then ExitLoop ; Close popup after approximately n minutes

WEnd

;Send("{ESC}");

GUISetState(@SW_HIDE,$hPopupWindow);

For $i = 1 To 40

GUICtrlDelete($i);

Next

TraySetIcon(@ScriptFullPath,0)

;CheckNow();

EndFunc

Func ExitScript() ; Exit called by the menu

Exit

EndFunc

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.