def on_sio_game_over(self, data): # Callback for when the game ends print 'socketio: game_over' # Parse parameters winner = data['winner'] # Disable swings self.enable_swings = False # Chose which color and which end-state color = SocketRacket.COLOR_WIN if (self.player_num == winner): self.state = GameState.END_GAME_WIN else: color = SocketRacket.COLOR_LOSE self.state = GameState.END_GAME_LOST # Parameterize the transition with animations self.state_data = { 'events': [(event.Event(SocketRacket.COLOR_TRANS_TIME, self.generic_color_trans(None, color)), None), (event.Event(SocketRacket.OVER_TIME, self.generic_flash(freq=5)), color), (clear_event.ClearEvent(clear_color=True), color), (event.Event( SocketRacket.COLOR_TRANS_TIME, self.generic_color_trans(color, SocketRacket.COLOR_CLEAR)), None)] }
def on_sio_game_won_rally(self): # Callback for when a player wins the rally print 'socketio: game_won_rally' # Keep swings enabled self.enable_swings = True # Parameterize the transition with animations self.state = GameState.WON_RALLY self.state_data = { 'events': [ (event.Event( SocketRacket.COLOR_TRANS_TIME, self.generic_color_trans(None, SocketRacket.COLOR_WIN)), None), (event.Event(SocketRacket.WON_RALLY_TIME, self.generic_flash(freq=2)), SocketRacket.COLOR_WIN), (clear_event.ClearEvent(clear_color=True), SocketRacket.COLOR_WIN), (event.Event( SocketRacket.COLOR_TRANS_TIME, self.generic_color_trans(SocketRacket.COLOR_WIN, None)), None), ] }
def on_sio_game_is_server(self): # Callback for when the player becomes the person serving the ball print 'socketio: game_is_server' # Keep swings enabled self.enable_swings = True # Parameterize the transition with animations self.state = GameState.SERVER self.state_data = { 'events': [(event.Event(SocketRacket.SERVER_TIME, self.generic_flash(freq=2)), None), (clear_event.ClearEvent(), None)] }
def on_button(self, controller, pressed, held, released): # Method to parse button presses # Forceful reset if ((self.state != GameState.RESET_WAIT) and (psmoveapi.Button.SELECT in held) and (psmoveapi.Button.START in held) and (psmoveapi.Button.PS in held)): self.sio_game_reset() self.state = GameState.RESET_WAIT # Color choosing logic if (self.state == GameState.COLOR_SELECTION): choices = (psmoveapi.Button.SQUARE, psmoveapi.Button.TRIANGLE, psmoveapi.Button.CROSS, psmoveapi.Button.CIRCLE) # Cycle through button options for button in choices: if (button in pressed): self.color_choice = SocketRacket.COLORS[button] controller.color = psmoveapi.RGB(*self.color_choice) return # Generic animation for a confirmation confirm_data = { 'events': [(event.Event( SocketRacket.COLOR_WAIT_TIME, self.generic_flash(rumble_scale=0.75, color_scale=0.75)), None), (clear_event.ClearEvent(), None)] } # Handedness confirmation if (psmoveapi.Button.SELECT in pressed): self.hand = 0 self.state_data = confirm_data elif (psmoveapi.Button.START in pressed): self.hand = 1 self.state_data = confirm_data # Color confirmation logic elif ((self.color_choice is not None) and (psmoveapi.Button.MOVE in pressed)): self.sio_init_color_choice(self.color_choice, self.hand) # Signal a transition to the next state self.state = GameState.COLOR_WAIT self.state_data = confirm_data
def on_sio_init_color_confirm(self): # Callback for a color confirmation event print 'socketio: init_color_confirm' # Ready for swings! self.enable_swings = True # Parameterize the transition with animations self.state = GameState.START_WAIT self.state_data = { 'events': [(event.Event(SocketRacket.COLOR_CONFIRM_TIME, self.generic_flash(freq=3, color_scale=0.75)), None), (clear_event.ClearEvent(), None)] }
def on_sio_game_hit_ball(self, data): # Callback for a hit ball event print 'socketio: game_hit_ball' # Parse parameters strength = data['strength'] # Keep swings enabled self.enable_swings = True # Parameterize the transition with animations self.state = GameState.HIT_BALL self.state_data = { 'events': [(event.Event(SocketRacket.HIT_TIME, self.generic_flash(color_scale=strength)), None), (clear_event.ClearEvent(), None)] }
def on_sio_game_restart(self, data): # Callback for when the game ends print 'socketio: game_restart' # Disable swings self.enable_swings = False # Restart the state machine self.state = GameState.COLOR_SELECTION # Parameterize the transition with animations self.state_data = { 'events': [(event.Event(SocketRacket.RESET_TIME, self.generic_flash(color=False)), None), (event.Event( SocketRacket.COLOR_TRANS_TIME, self.generic_color_trans(None, SocketRacket.COLOR_CLEAR)), None), (clear_event.ClearEvent(), None)] }
def on_sio_init_color_reject(self): # Callback for a color rejection event print 'socketio: init_color_reject' # Disable swings (not at the game yet) self.enable_swings = False # Parameterize the transition with animations self.state = GameState.COLOR_SELECTION self.state_data = { 'events': [(event.Event(SIORacket.COLOR_TRANS_TIME, self.generic_color_trans(None, SIORacket.COLOR_BAD)), None), (event.Event(SIORacket.COLOR_REJECT_TIME, self.generic_flash()), SIORacket.COLOR_BAD), (clear_event.ClearEvent(clear_color=True), SIORacket.COLOR_BAD), (event.Event(SIORacket.COLOR_TRANS_TIME, self.generic_color_trans(SIORacket.COLOR_BAD, None)), None)] }
def on_sio_game_missed_ball(self): # Callback for a missed ball event print 'socketio: game_missed_ball' # Keep swings enabled self.enable_swings = True # Parameterize the transition with animations self.state = GameState.LOST_RALLY self.state_data = { 'events': [(event.Event(SIORacket.COLOR_TRANS_TIME, self.generic_color_trans(None, SIORacket.COLOR_LOSE)), None), (event.Event(SIORacket.LOST_RALLY_TIME, self.generic_flash(freq=2)), SIORacket.COLOR_LOSE), (clear_event.ClearEvent(clear_color=True), SIORacket.COLOR_LOSE), (event.Event(SIORacket.COLOR_TRANS_TIME, self.generic_color_trans(SIORacket.COLOR_LOSE, None)), None)] }
def on_button(self, controller, buttons): # Method to parse button presses # Temporary to cycle through animations # Pressing the PS button simulates an in-order server event if (psmoveapi.Button.PS in buttons): if (self.state == GameState.COLOR_WAIT): if (bool(random.randint(0, 1))): self.on_sio_init_color_reject() else: self.on_sio_init_color_confirm() elif (self.state == GameState.START_WAIT): self.on_sio_game_is_server() elif (self.state == GameState.GAMEPLAY): end_rally = False if (bool(random.randint(0, 3))): self.on_sio_game_hit_ball({'strength': 0.75}) if (not bool(random.randint(0, 5))): self.on_sio_game_won_rally() end_rally = True else: self.on_sio_game_missed_ball() end_rally = True if (end_rally and not bool(random.randint(0, 5))): if (bool(random.randint(0, 1))): self.on_sio_game_over({'is_winner': False}) else: self.on_sio_game_over({'is_winner': True}) return # Color choosing logic if (self.state == GameState.COLOR_SELECTION): choices = (psmoveapi.Button.SQUARE, psmoveapi.Button.TRIANGLE, psmoveapi.Button.CROSS, psmoveapi.Button.CIRCLE) # Cycle through button options for button in choices: if (button in buttons): self.color_choice = SIORacket.COLORS[button] controller.color = psmoveapi.RGB(*self.color_choice) return # Color confirmation logic if ((self.color_choice is not None) and (psmoveapi.Button.MOVE in buttons)): self.sio_init_color_choice(self.color_choice) # Signal a transition to the next state self.state = GameState.COLOR_WAIT self.state_data = { 'events': [(event.Event( SIORacket.COLOR_WAIT_TIME, self.generic_flash(rumble_scale=0.75, color_scale=0.75)), None), (clear_event.ClearEvent(), None)] }