diff --git a/BuGS/game.s b/BuGS/game.s index 7c21daa..ff2ecb6 100644 --- a/BuGS/game.s +++ b/BuGS/game.s @@ -92,6 +92,11 @@ gameLoop anop lda #1 sta shouldPreloadSound gameLoop_skipPreload anop + lda gameState + bne gameLoop_skipNetwork + jsl pollNetwork +gameLoop_skipNetwork anop + jsl checkKeyboard jsl waitForVbl diff --git a/BuGS/globalScores.c b/BuGS/globalScores.c index 173bac8..82adc67 100644 --- a/BuGS/globalScores.c +++ b/BuGS/globalScores.c @@ -6,13 +6,18 @@ * Copyright © 2021 Jeremy Rand. All rights reserved. */ +#include +#include #include #include +#include #include #include "globalScores.h" +// Defines + #define REQUEST_TYPE_GET_HIGH_SCORES 0 #define REQUEST_TYPE_SET_SCORE 1 @@ -21,6 +26,8 @@ #define RESPONSE_TYPE_STATUS 2 +// Types + typedef struct tSessionSecrets { uint32_t secret; uint32_t nonce; @@ -70,6 +77,22 @@ typedef struct tStatusResponse { } tStatusResponse; +typedef enum tGameNetworkState { + GAME_NETWORK_CONNECT_FAILED = 0, + GAME_NETWORK_UNCONNECTED, + GAME_NETWORK_CONNECTED, +} tGameNetworkState; + + +// Globals + +Boolean networkToolsStarted = FALSE; +Boolean networkStartedConnected = FALSE; +tGameNetworkState gameNetworkState = GAME_NETWORK_UNCONNECTED; + + +// Implementation + #if 0 Word blah(void) { @@ -79,3 +102,86 @@ Word blah(void) return ReadBParam(hrtz50or60); } #endif + + +void initNetwork(void) +{ + networkToolsStarted = FALSE; + LoadOneTool(54, 0x200); // Load Marinetti + if (toolerror()) + return; + + LoadOneTool(128, 0x103); // Load the Hash toolset + if (toolerror()) { + UnloadOneTool(54); + return; + } + + TCPIPStartUp(); + if (toolerror()) { + UnloadOneTool(128); + UnloadOneTool(54); + return; + } + + hashStartUp(); + if (toolerror()) { + TCPIPShutDown(); + UnloadOneTool(128); + UnloadOneTool(54); + return; + } + + networkToolsStarted = TRUE; + + networkStartedConnected = TCPIPGetConnectStatus(); + if (networkStartedConnected) { + gameNetworkState = GAME_NETWORK_CONNECTED; + } else { + gameNetworkState = GAME_NETWORK_UNCONNECTED; + } +} + + +void shutdownNetwork(void) +{ + if (!networkToolsStarted) + return; + + if ((!networkStartedConnected) && + (gameNetworkState > GAME_NETWORK_UNCONNECTED)) { + TCPIPDisconnect(TRUE, NULL); + } + + hashShutDown(); + TCPIPShutDown(); + UnloadOneTool(128); // Unload the Hash toolset + UnloadOneTool(54); // Unload Marinetti +} + + +void pollNetwork(void) +{ + if (!networkToolsStarted) + return; + + switch (gameNetworkState) { + case GAME_NETWORK_CONNECT_FAILED: + // If Marinetti cannot connect to the network, then nothing more to do... + break; + + case GAME_NETWORK_UNCONNECTED: + TCPIPConnect(NULL); // TODO - Perhaps no feedback here is not a good user experience and I should provide some kind of display function. + if ((!toolerror()) && + (TCPIPGetConnectStatus())) { + gameNetworkState = GAME_NETWORK_CONNECTED; + } else { + gameNetworkState = GAME_NETWORK_CONNECT_FAILED; + } + break; + + case GAME_NETWORK_CONNECTED: + // TODO - Write more code to make network requests. + break; + } +} diff --git a/BuGS/globalScores.h b/BuGS/globalScores.h index 2134a91..34f1440 100644 --- a/BuGS/globalScores.h +++ b/BuGS/globalScores.h @@ -18,4 +18,9 @@ typedef struct tHighScore } tHighScore; +extern void initNetwork(void); +extern void pollNetwork(void); +extern void shutdownNetwork(void); + + #endif /* define _GUARD_PROJECTBuGS_FILEglobalScores_ */ diff --git a/BuGS/main.c b/BuGS/main.c index b43cabc..ee2f972 100644 --- a/BuGS/main.c +++ b/BuGS/main.c @@ -347,11 +347,15 @@ int main(void) InitMouse(0); SetMouse(transparent); + initNetwork(); + if (!loadSettings()) saveSettings(); game(); + shutdownNetwork(); + ShutDownTools(refIsHandle, toolStartupRef); TOOLFAIL("Unable to shutdown tools");