RPS play again signal from remote user.

This commit is contained in:
Derek Jamison 2023-04-09 09:24:27 -04:00
parent a5ca0c1976
commit ffdd292550
2 changed files with 88 additions and 2 deletions

View File

@ -268,12 +268,41 @@ static void rps_receive_data(GameContext* game_context, uint32_t tick) {
break; break;
} }
case GameRfPurposePlayAgain: {
// IMPORTANT: The code processing the event needs to furi_string_free the senderName!
FuriString* name = furi_string_alloc();
furi_string_set(name, sender_name);
GameEvent event = {
.type = GameEventRemotePlayAgain, .sender_name = name, .game_number = game_number};
furi_message_queue_put(game_context->queue, &event, FuriWaitForever);
break;
}
case GameRfPurposeQuit: {
// IMPORTANT: The code processing the event needs to furi_string_free the senderName!
FuriString* name = furi_string_alloc();
furi_string_set(name, sender_name);
GameEvent event = {
.type = GameEventRemoteQuit, .sender_name = name, .game_number = game_number};
furi_message_queue_put(game_context->queue, &event, FuriWaitForever);
break;
}
case GameRfPurposeJoin: case GameRfPurposeJoin:
if(index >= len) { if(index >= len) {
FURI_LOG_W(TAG, "Failed to parse join message. >%s<", message); FURI_LOG_W(TAG, "Failed to parse join message. >%s<", message);
return; return;
} else { } else {
sender_contact = (char*)message + index; sender_contact = (char*)message + index;
while(index < len) {
if(message[index] == '\n' || message[index] == '\r') {
message[index] = 0;
break;
}
index++;
}
FURI_LOG_T(TAG, "Join had contact of >%s<", sender_contact); FURI_LOG_T(TAG, "Join had contact of >%s<", sender_contact);
// IMPORTANT: The code processing the event needs to furi_string_free the senderName! // IMPORTANT: The code processing the event needs to furi_string_free the senderName!
@ -297,6 +326,13 @@ static void rps_receive_data(GameContext* game_context, uint32_t tick) {
return; return;
} else { } else {
sender_contact = (char*)message + index; sender_contact = (char*)message + index;
while(index < len) {
if(message[index] == '\n' || message[index] == '\r') {
message[index] = 0;
break;
}
index++;
}
FURI_LOG_T(TAG, "Join acknowledge for game %d.", game_number); FURI_LOG_T(TAG, "Join acknowledge for game %d.", game_number);
FURI_LOG_T(TAG, "Join ack had contact of >%s<", sender_contact); FURI_LOG_T(TAG, "Join ack had contact of >%s<", sender_contact);
@ -1015,6 +1051,38 @@ static void rps_broadcast_not_beacon(GameContext* game_context) {
rps_broadcast(game_context, data->buffer); rps_broadcast(game_context, data->buffer);
} }
static void rps_broadcast_play_again(GameContext* game_context) {
GameData* data = game_context->data;
FURI_LOG_I(TAG, "Sending play again");
// The message for game 42 should look like... "YourFlip: RPS:PA042\r\n"
furi_string_printf(
data->buffer,
"%s: %s:%c%c%03u\r\n",
furi_hal_version_get_name_ptr(),
RPS_GAME_NAME,
GameRfPurposePlayAgain,
MAJOR_VERSION,
data->game_number);
rps_broadcast(game_context, data->buffer);
}
static void rps_broadcast_quit(GameContext* game_context) {
GameData* data = game_context->data;
FURI_LOG_I(TAG, "Sending quit");
// The message for game 42 should look like... "YourFlip: RPS:QA042\r\n"
furi_string_printf(
data->buffer,
"%s: %s:%c%c%03u\r\n",
furi_hal_version_get_name_ptr(),
RPS_GAME_NAME,
GameRfPurposeQuit,
MAJOR_VERSION,
data->game_number);
rps_broadcast(game_context, data->buffer);
}
// Send message that indicates Flipper is joining a specific game. // Send message that indicates Flipper is joining a specific game.
// We broadcast - "YourFlip: " + "RPS:" + join"J" + version"A" + game"###" + "NYourNameHere" + "\r\n" // We broadcast - "YourFlip: " + "RPS:" + join"J" + version"A" + game"###" + "NYourNameHere" + "\r\n"
// @param game_context pointer to a GameContext. // @param game_context pointer to a GameContext.
@ -2240,17 +2308,19 @@ int32_t rock_paper_scissors_app(void* p) {
event.input.type == InputTypeShort) { event.input.type == InputTypeShort) {
switch(event.input.key) { switch(event.input.key) {
case InputKeyLeft: case InputKeyLeft:
game_context->data->keyboard_col = 0; // YES game_context->data->keyboard_col = 0; // YES - Play again
break; break;
case InputKeyRight: case InputKeyRight:
game_context->data->keyboard_col = 1; // NO game_context->data->keyboard_col = 1; // NO - Back to main menu
break; break;
case InputKeyOk: case InputKeyOk:
if(game_context->data->keyboard_col == 1) { if(game_context->data->keyboard_col == 1) {
rps_broadcast_quit(game_context);
game_context->data->local_player = StateMainMenuHost; game_context->data->local_player = StateMainMenuHost;
game_context->data->remote_player = StateUnknown; game_context->data->remote_player = StateUnknown;
game_context->data->screen_state = ScreenMainMenu; game_context->data->screen_state = ScreenMainMenu;
} else { } else {
rps_broadcast_play_again(game_context);
game_context->data->remote_player = StateReady; game_context->data->remote_player = StateReady;
game_context->data->remote_move_tick = furi_get_tick(); game_context->data->remote_move_tick = furi_get_tick();
game_context->data->local_player = StateReady; game_context->data->local_player = StateReady;
@ -2335,6 +2405,18 @@ int32_t rock_paper_scissors_app(void* p) {
FURI_LOG_I(TAG, "Remote not beacon detected. game number %03u", event.game_number); FURI_LOG_I(TAG, "Remote not beacon detected. game number %03u", event.game_number);
remote_games_remove(game_context, &event); remote_games_remove(game_context, &event);
break; break;
case GameEventRemotePlayAgain:
game_context->data->remote_player = StateReady;
game_context->data->remote_move_tick = furi_get_tick();
game_context->data->local_player = StateReady;
game_context->data->local_move_tick = furi_get_tick();
game_context->data->screen_state = ScreenPlayingGame;
break;
case GameEventRemoteQuit:
game_context->data->local_player = StateMainMenuHost;
game_context->data->remote_player = StateUnknown;
game_context->data->screen_state = ScreenMainMenu;
break;
case GameEventRemoteJoined: case GameEventRemoteJoined:
if(furi_mutex_acquire(game_context->mutex, FuriWaitForever) == FuriStatusOk) { if(furi_mutex_acquire(game_context->mutex, FuriWaitForever) == FuriStatusOk) {
if(event.game_number == game_context->data->game_number) { if(event.game_number == game_context->data->game_number) {

View File

@ -170,6 +170,8 @@ typedef enum {
GameRfPurposeJoin = 'J', // Join a game. GameRfPurposeJoin = 'J', // Join a game.
GameRfPurposeJoinAcknowledge = 'A', // Acknowledge a join request. GameRfPurposeJoinAcknowledge = 'A', // Acknowledge a join request.
GameRfPurposeMove = 'M', // Player move. GameRfPurposeMove = 'M', // Player move.
GameRfPurposePlayAgain = 'P', // Play another game.
GameRfPurposeQuit = 'Q', // Quit, don't play another game.
} GameRfPurpose; } GameRfPurpose;
// Messages in our event queue are one of the following types. // Messages in our event queue are one of the following types.
@ -182,6 +184,8 @@ typedef enum {
GameEventRemoteJoined, GameEventRemoteJoined,
GameEventRemoteJoinAcknowledged, GameEventRemoteJoinAcknowledged,
GameEventLocalMove, GameEventLocalMove,
GameEventRemotePlayAgain,
GameEventRemoteQuit,
GameEventRemoteMove, GameEventRemoteMove,
GameEventSendMove, GameEventSendMove,
GameEventPlaySong, GameEventPlaySong,