Skip to content

Commit

Permalink
Add scoring management
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-rabault authored and JeromeGalan committed Feb 15, 2023
1 parent 54e7b3f commit 8bbb2ab
Show file tree
Hide file tree
Showing 5 changed files with 185 additions and 11 deletions.
42 changes: 34 additions & 8 deletions examples/projects/native/ping_pong/lib/PingPong/graph.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@
#include <errno.h>
#include "luos_engine.h"
#include "game_anim.h"
#include "scoring.h"

static void score_display(void);

const char start[768]
= " ((((. \n"
Expand Down Expand Up @@ -91,7 +94,7 @@ void start_view(void)
need_update = false;
clear_screen();
printf("%s", start);
printf("THE LUOS PING PONG GAME\n\tPress SPACE BAR to start!\n");
printf("THE LUOS PING PONG World cup!\n\tPress SPACE BAR to start!\n");
}
}

Expand All @@ -111,21 +114,23 @@ void service_view(void)
{
if (need_update)
{
need_update = false;
clear_screen();
printf("%s\n", table[EMPTY][1]);
printf("Press SPACE BAR to serve\n");
score_display();
printf("\n\tPress SPACE BAR to serve\n");
need_update = false;
}
}

void gameOver_view(void)
{
if (need_update)
{
need_update = false;
clear_screen();
printf("GAME_OVER\n");
printf("Press SPACE BAR to restart\a\n");
score_display();
printf("\n\tPress SPACE BAR to restart\a\n");
need_update = false;
}
}

Expand All @@ -134,17 +139,17 @@ void game_view(void)
static int animation_frame = 0;
if (need_update)
{
need_update = false;
clear_screen();
animation_frame = 0;
printf("%s\n", table[*ball_state][animation_frame]);
printf("Game is running\n");
score_display();
need_update = false;
}
if (animation_frame < GAME_ANIMATION_FRAME_NB)
{
clear_screen();
printf("%s\n", table[*ball_state][animation_frame]);
printf("Game is running\n");
score_display();
animation_frame++;
msleep(10);
}
Expand All @@ -159,4 +164,25 @@ void wait_view(void)
printf("%s", start);
printf("Game is starting, please wait...\n");
}
}

void score_display(void)
{
score_table_t *score = get_score();
for (int i = 0; i < score->player_nb; i++)
{
printf("\t%d |%16s |\t %d\n", i + 1, score->scores[i].alias, score->scores[i].score);
}
}

void score_view(void)
{
if (need_update)
{
clear_screen();
printf("%s\n", table[EMPTY][1]);
score_display();
printf("\n\tSomeone just lost...\n");
need_update = false;
}
}
1 change: 1 addition & 0 deletions examples/projects/native/ping_pong/lib/PingPong/graph.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@ void gameOver_view(void);
void game_view(void);
void wait_view(void);
void alone_view(void);
void score_view(void);

#endif /* PINGPONG_H */
21 changes: 18 additions & 3 deletions examples/projects/native/ping_pong/lib/PingPong/ping_pong.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <time.h>
#include "graph.h"
#include <pthread.h>
#include "scoring.h"

/*******************************************************************************
* Definitions
Expand All @@ -29,7 +30,7 @@
#define get_character() getchar()
#endif

#define BALL_TIMEOUT 500
#define BALL_TIMEOUT 700

static void game_start(void);
static void game_loading(void);
Expand All @@ -51,16 +52,30 @@ ball_t ball = EMPTY;
typedef void (*GAME_STATE)(void);
GAME_STATE game_state = NULL;
bool initialized = false;
uint16_t last_id = 0;

/*******************************************************************************
* Function
******************************************************************************/

void Player_MsgHandler(service_t *service, msg_t *msg)
{
if ((msg->header.target_mode == TOPIC) & (msg->header.target = SCORE_TOPIC) & (msg->header.source != service->ll_service->id))
{
score_update(msg);
set_screen_to(score_view);
}
if (msg->header.cmd == BALL_POS_CMD)
{
ball = (ball_t)msg->data[0];
ball = (ball_t)msg->data[0];
last_id = msg->header.source;
}
if (msg->header.cmd == END_DETECTION)
{
search_result_t target_list;
RTFilter_Reset(&target_list);
RTFilter_Type(&target_list, PLAYER_TYPE);
score_init(player, &target_list);
}
}

Expand Down Expand Up @@ -214,7 +229,7 @@ void game_service()
void game_over()
{
char c;

score_increase(player, last_id);
set_screen_to(gameOver_view);
while (Luos_IsNodeDetected())
{
Expand Down
99 changes: 99 additions & 0 deletions examples/projects/native/ping_pong/lib/PingPong/scoring.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
#include "scoring.h"
#include <string.h>

#define SCORE_TABLE_SIZE 100

typedef struct __attribute__((__packed__))
{
uint16_t score;
uint16_t player_id;
} msg_score_t;

score_t scores[SCORE_TABLE_SIZE];
score_table_t score_table;

void score_init(service_t *player, search_result_t *player_list)
{
Luos_TopicSubscribe(player, SCORE_TOPIC);
memset(scores, 0, sizeof(scores));
score_table.player_nb = player_list->result_nbr;
score_table.scores = scores;
for (int i = 0; i < score_table.player_nb; i++)
{
scores[i].alias = player_list->result_table[i]->alias;
scores[i].id = player_list->result_table[i]->id;
scores[i].score = 0;
}
}

void score_increase(service_t *player, uint16_t winning_player_id)
{
// Update the scores
for (int i = 0; i < score_table.player_nb; i++)
{
if (scores[i].alias == NULL)
{
break;
}
if (scores[i].id == winning_player_id)
{
scores[i].score++;
// Sort the score table
while (i > 0 && scores[i].score > scores[i - 1].score)
{
score_t tmp = scores[i];
scores[i] = scores[i - 1];
scores[i - 1] = tmp;
i--;
}
break;
}
}
// Generate the score message value
msg_score_t msg_score[score_table.player_nb];
for (int i = 0; i < score_table.player_nb; i++)
{
msg_score[i].score = scores[i].score;
msg_score[i].player_id = scores[i].id;
}

msg_t msg;
msg.header.target = SCORE_TOPIC;
msg.header.target_mode = TOPIC;
msg.header.size = sizeof(msg_score);
msg.header.cmd = SCORE_CMD;
memcpy(msg.data, &msg_score, sizeof(msg_score));
Luos_SendMsg(player, &msg);
}

void score_update(msg_t *msg)
{
score_t tmp_score[SCORE_TABLE_SIZE];
msg_score_t *msg_score = (msg_score_t *)msg->data;
// Transform the message value into a score table
for (int i = 0; i < score_table.player_nb; i++)
{
tmp_score[i].score = msg_score[i].score;
tmp_score[i].id = msg_score[i].player_id;
for (int j = 0; j < score_table.player_nb; j++)
{
if (tmp_score[i].id == scores[j].id)
{
tmp_score[i].alias = scores[j].alias;
break;
}
}
}
// Update the score table
for (int i = 0; i < score_table.player_nb; i++)
{
scores[i].score = tmp_score[i].score;
scores[i].id = tmp_score[i].id;
scores[i].alias = tmp_score[i].alias;
}
}

score_table_t *get_score(void)
{
return &score_table;
}
33 changes: 33 additions & 0 deletions examples/projects/native/ping_pong/lib/PingPong/scoring.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/******************************************************************************
* @file scoring
* @brief the score management of the game
* @author Luos
* @version 0.0.0
******************************************************************************/
#ifndef SCORING_H
#define SCORING_H

#include "luos_engine.h"

#define SCORE_TOPIC 1
#define SCORE_CMD LUOS_LAST_STD_CMD + 1

typedef struct
{
uint8_t score;
char *alias;
uint16_t id;
} score_t;

typedef struct
{
uint8_t player_nb;
score_t *scores;
} score_table_t;

void score_init(service_t *player, search_result_t *player_list);
void score_increase(service_t *player, uint16_t winning_player_id);
void score_update(msg_t *msg);
score_table_t *get_score(void);

#endif /* SCORING_H */

0 comments on commit 8bbb2ab

Please sign in to comment.