Coordinator v2
Dependencies: NerfUSXbee PinDetect EthernetInterface JSON MFRC522 WebSocketClient mbed-rtos mbed
src/GameCoordinator.cpp@2:019d8848cf7e, 2017-04-11 (annotated)
- Committer:
- Ismael Balafrej
- Date:
- Tue Apr 11 12:40:05 2017 -0400
- Revision:
- 2:019d8848cf7e
- Parent:
- 1:e1c5259b7d9a
- Child:
- 3:501120a68c11
Final version without allies/enemies
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Ismael Balafrej |
1:e1c5259b7d9a | 1 | #include "GameCoordinator.hpp" |
Ismael Balafrej |
1:e1c5259b7d9a | 2 | |
Ismael Balafrej |
1:e1c5259b7d9a | 3 | GameCoordinator gameCoordinator; |
Ismael Balafrej |
1:e1c5259b7d9a | 4 | |
Ismael Balafrej |
1:e1c5259b7d9a | 5 | //TODO: move that to .cpp |
Ismael Balafrej |
1:e1c5259b7d9a | 6 | GameCoordinator::GameCoordinator() |
Ismael Balafrej |
1:e1c5259b7d9a | 7 | { |
Ismael Balafrej |
1:e1c5259b7d9a | 8 | //Adding all the game modes to the game_modes vector |
Ismael Balafrej |
1:e1c5259b7d9a | 9 | //TODO: can we avoid new operator ? |
Ismael Balafrej |
1:e1c5259b7d9a | 10 | ReflexMode *reflex_mode = new ReflexMode(&targets); |
Ismael Balafrej |
1:e1c5259b7d9a | 11 | SpeedMode *speed_mode = new SpeedMode(&targets); |
Ismael Balafrej |
1:e1c5259b7d9a | 12 | game_modes.push_back(reflex_mode); |
Ismael Balafrej |
1:e1c5259b7d9a | 13 | game_modes.push_back(speed_mode); |
Ismael Balafrej |
1:e1c5259b7d9a | 14 | |
Ismael Balafrej |
1:e1c5259b7d9a | 15 | //Starting internal thread |
Ismael Balafrej |
2:019d8848cf7e | 16 | Tget_next_round = Thread(osPriorityNormal, 1020); |
Ismael Balafrej |
1:e1c5259b7d9a | 17 | Tget_next_round.start(this, &GameCoordinator::get_next_round); |
Ismael Balafrej |
1:e1c5259b7d9a | 18 | } |
Ismael Balafrej |
1:e1c5259b7d9a | 19 | |
Ismael Balafrej |
1:e1c5259b7d9a | 20 | void GameCoordinator::start_game(ServerData *configs) |
Ismael Balafrej |
1:e1c5259b7d9a | 21 | { |
Ismael Balafrej |
1:e1c5259b7d9a | 22 | current_game = game_modes[configs->game_id]; |
Ismael Balafrej |
1:e1c5259b7d9a | 23 | target_timeout = configs->max_reflex_time; |
Ismael Balafrej |
1:e1c5259b7d9a | 24 | number_of_targets = configs->number_of_target; |
Ismael Balafrej |
2:019d8848cf7e | 25 | current_game->reset(); |
Ismael Balafrej |
2:019d8848cf7e | 26 | timer.reset(); |
Ismael Balafrej |
2:019d8848cf7e | 27 | timer.start(); |
Ismael Balafrej |
1:e1c5259b7d9a | 28 | ticker.attach(callback(this, &GameCoordinator::ticker_callback), current_game->getTimeBetweenTargets() / 1000.0); |
Ismael Balafrej |
1:e1c5259b7d9a | 29 | } |
Ismael Balafrej |
1:e1c5259b7d9a | 30 | |
Ismael Balafrej |
1:e1c5259b7d9a | 31 | void GameCoordinator::stop_game() |
Ismael Balafrej |
1:e1c5259b7d9a | 32 | { |
Ismael Balafrej |
1:e1c5259b7d9a | 33 | ticker.detach(); |
Ismael Balafrej |
1:e1c5259b7d9a | 34 | LPC_TIM3->TC = 0; |
Ismael Balafrej |
2:019d8848cf7e | 35 | timer.stop(); |
Ismael Balafrej |
2:019d8848cf7e | 36 | ServerEvent stats; |
Ismael Balafrej |
2:019d8848cf7e | 37 | stats.data = current_game->GetStats(); |
Ismael Balafrej |
2:019d8848cf7e | 38 | stats.data.gameLength = timer.read_ms(); |
Ismael Balafrej |
2:019d8848cf7e | 39 | if (stats.data.targets != 0) |
Ismael Balafrej |
2:019d8848cf7e | 40 | { |
Ismael Balafrej |
2:019d8848cf7e | 41 | stats.data.averageReflexTime = stats.data.gameLength / stats.data.targets; |
Ismael Balafrej |
2:019d8848cf7e | 42 | } |
Ismael Balafrej |
2:019d8848cf7e | 43 | |
Ismael Balafrej |
2:019d8848cf7e | 44 | strcpy(stats.event, "report"); |
Ismael Balafrej |
2:019d8848cf7e | 45 | if (on_game_finish != NULL) |
Ismael Balafrej |
2:019d8848cf7e | 46 | { |
Ismael Balafrej |
2:019d8848cf7e | 47 | on_game_finish(stats); |
Ismael Balafrej |
2:019d8848cf7e | 48 | } |
Ismael Balafrej |
1:e1c5259b7d9a | 49 | } |
Ismael Balafrej |
1:e1c5259b7d9a | 50 | |
Ismael Balafrej |
1:e1c5259b7d9a | 51 | void GameCoordinator::ticker_callback() |
Ismael Balafrej |
1:e1c5259b7d9a | 52 | { |
Ismael Balafrej |
1:e1c5259b7d9a | 53 | Tget_next_round.signal_set(0x1); |
Ismael Balafrej |
1:e1c5259b7d9a | 54 | } |
Ismael Balafrej |
1:e1c5259b7d9a | 55 | |
Ismael Balafrej |
1:e1c5259b7d9a | 56 | //Thread |
Ismael Balafrej |
1:e1c5259b7d9a | 57 | void GameCoordinator::get_next_round() |
Ismael Balafrej |
1:e1c5259b7d9a | 58 | { |
Ismael Balafrej |
1:e1c5259b7d9a | 59 | while (1) |
Ismael Balafrej |
1:e1c5259b7d9a | 60 | { |
Ismael Balafrej |
1:e1c5259b7d9a | 61 | Thread::signal_wait(0x1); |
Ismael Balafrej |
1:e1c5259b7d9a | 62 | Target *nextTarget = current_game->GetNextTarget(); |
Ismael Balafrej |
1:e1c5259b7d9a | 63 | if (nextTarget == NULL || number_of_targets-- <= 0) |
Ismael Balafrej |
1:e1c5259b7d9a | 64 | { |
Ismael Balafrej |
1:e1c5259b7d9a | 65 | stop_game(); |
Ismael Balafrej |
1:e1c5259b7d9a | 66 | } |
Ismael Balafrej |
1:e1c5259b7d9a | 67 | else |
Ismael Balafrej |
1:e1c5259b7d9a | 68 | { |
Ismael Balafrej |
2:019d8848cf7e | 69 | //TODO: how to select enemy or ally ? rand() % 2 |
Ismael Balafrej |
2:019d8848cf7e | 70 | nextTarget->rise(0, target_timeout); |
Ismael Balafrej |
1:e1c5259b7d9a | 71 | } |
Ismael Balafrej |
1:e1c5259b7d9a | 72 | Thread::yield(); |
Ismael Balafrej |
1:e1c5259b7d9a | 73 | } |
Ismael Balafrej |
2:019d8848cf7e | 74 | } |
Ismael Balafrej |
2:019d8848cf7e | 75 | |
Ismael Balafrej |
2:019d8848cf7e | 76 | void GameCoordinator::target_hit(int time_taken) |
Ismael Balafrej |
2:019d8848cf7e | 77 | { |
Ismael Balafrej |
2:019d8848cf7e | 78 | current_game->OnTargetHit(time_taken); |
Ismael Balafrej |
2:019d8848cf7e | 79 | } |
Ismael Balafrej |
2:019d8848cf7e | 80 | |
Ismael Balafrej |
2:019d8848cf7e | 81 | void GameCoordinator::target_miss() |
Ismael Balafrej |
2:019d8848cf7e | 82 | { |
Ismael Balafrej |
2:019d8848cf7e | 83 | current_game->OnTargetMiss(); |
Ismael Balafrej |
1:e1c5259b7d9a | 84 | } |