public void SomethingChanged(UIGameEventDataWrapper changedData) { switch (changedData.EventType) { case UIEvent.Coin_Removed: CoinRemoved(changedData.Point, changedData.Player); break; case UIEvent.Game_Won: GameWon(changedData.Player); break; case UIEvent.Game_Restarted: Restart(); break; default: Log.Error("Unknown Changed-Type for update, type was: " + changedData.EventType); break; } }
private void Restart(int startWith, bool publish) { try { if (startWith < 0) startWith = 0; Log.DebugFormat("Restart Game, startWith {0}", startWith); // avoid overflow startWith %= SetupSvc.AllLevels.Count; // remember current if (currentGameIndex != startWith) currentGameIndex = startWith; // fire event so that dependand modules (e.g. VM) get informed if (publish) EA.GetEvent<MsgGameControl>().Publish(new MsgGameControl() { LevelIndex = startWith, Typ = EventType.Restart }); // Notify clients UIGameEventDataWrapper wrp = new UIGameEventDataWrapper() { EventType = UIEvent.Game_Restarted }; Action<int, object> act = GetSomethingChangedAction(wrp); NetworkSvc.NotifyClients(Clients, wrp, act); } catch (Exception e) { Log.Fatal(e); throw ThrowWCFException(e, 500); } }
private Action<int, object> GetSomethingChangedAction(UIGameEventDataWrapper wrp) { return new Action<int, object>((int idx, object o) => { int i = 0; while (i <= 1) { try { Log.DebugFormat("Call Something changed for client with ip {0}", IPHelper.LongToIP(Clients[idx].Ip)); if (((IClientChannel)Clients[idx].Svc).State != CommunicationState.Opened) { var c = Clients[idx].Factory.CreateChannel(); Clients[idx].Svc = c; } Clients[idx].Svc.SomethingChanged(wrp); break; } catch (Exception e) { Log.Warn(e); // Rethrow the 2nd Exception to delete the client out of the list if (i > 0) throw e; } ++i; } }); }
public void GameWon(int playerID) { try { UIGameEventDataWrapper wrp = new UIGameEventDataWrapper() { EventType = UIEvent.Game_Won, Player = GetPlayer(playerID) }; Action<int, object> act = GetSomethingChangedAction(wrp); NetworkSvc.NotifyClients(Clients, wrp, act); } catch (Exception e) { Log.Fatal(e); throw ThrowWCFException(e, 500); } }
public virtual void CoinRemoved(Point point, int playerID) { try { Log.DebugFormat("CoinRemoved: (Poinst: {0}, playerID: {1}, amount of clients: {2}", point, playerID, Clients.Count); UIGameEventDataWrapper wrp = new UIGameEventDataWrapper() { Point = point, EventType = UIEvent.Coin_Removed, Player = GetPlayer(playerID) }; Action<int, object> act = GetSomethingChangedAction(wrp); NetworkSvc.NotifyClients(Clients, wrp, act); } catch (Exception e) { Log.Fatal(e); throw ThrowWCFException(e, 500); } }