Search in sources :

Example 1 with Sendable

use of se.oort.diplicity.Sendable in project android-diplicity by zond.

the class GameActivity method showMap.

public void showMap() {
    hideAllExcept(R.id.map_view);
    final Sendable<String> renderer = new Sendable<String>() {

        @Override
        public void send(String url) {
            ((MapView) findViewById(R.id.map_view)).load(GameActivity.this, url);
        }
    };
    if (game.Started) {
        findViewById(R.id.rewind).setVisibility(View.VISIBLE);
        FloatingActionButton firstPhaseButton = (FloatingActionButton) findViewById(R.id.rewind);
        if (phaseMeta.PhaseOrdinal < 3) {
            firstPhaseButton.setEnabled(false);
            firstPhaseButton.setAlpha(0.3f);
        } else {
            firstPhaseButton.setEnabled(true);
            firstPhaseButton.setAlpha(1.0f);
            firstPhaseButton.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    firstPhase();
                }
            });
        }
        findViewById(R.id.previous).setVisibility(View.VISIBLE);
        FloatingActionButton previousPhaseButton = (FloatingActionButton) findViewById(R.id.previous);
        if (phaseMeta.PhaseOrdinal < 2) {
            previousPhaseButton.setEnabled(false);
            previousPhaseButton.setAlpha(0.3f);
        } else {
            previousPhaseButton.setEnabled(true);
            previousPhaseButton.setAlpha(1.0f);
            previousPhaseButton.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    prevPhase();
                }
            });
        }
        findViewById(R.id.next).setVisibility(View.VISIBLE);
        FloatingActionButton nextPhaseButton = (FloatingActionButton) findViewById(R.id.next);
        if (game.NewestPhaseMeta != null && game.NewestPhaseMeta.get(0).PhaseOrdinal <= phaseMeta.PhaseOrdinal) {
            nextPhaseButton.setEnabled(false);
            nextPhaseButton.setAlpha(0.3f);
        } else {
            nextPhaseButton.setEnabled(true);
            nextPhaseButton.setAlpha(1.0f);
            nextPhaseButton.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    nextPhase();
                }
            });
        }
        FloatingActionButton lastPhaseButton = (FloatingActionButton) findViewById(R.id.fast_forward);
        if (game.NewestPhaseMeta != null && game.NewestPhaseMeta.get(0).PhaseOrdinal <= phaseMeta.PhaseOrdinal + 1) {
            lastPhaseButton.setEnabled(false);
            lastPhaseButton.setAlpha(0.3f);
        } else {
            lastPhaseButton.setEnabled(true);
            lastPhaseButton.setAlpha(1.0f);
            findViewById(R.id.fast_forward).setVisibility(View.VISIBLE);
            lastPhaseButton.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    lastPhase();
                }
            });
        }
        String url = getBaseURL() + "Game/" + game.ID + "/Phase/" + phaseMeta.PhaseOrdinal + "/Map";
        if (getLocalDevelopmentMode() && !getLocalDevelopmentModeFakeID().equals("")) {
            url = url + "?fake-id=" + getLocalDevelopmentModeFakeID();
        }
        renderer.send(url);
        if (member != null && !phaseMeta.Resolved) {
            handleReq(JoinObservable.when(JoinObservable.from(optionsService.GetOptions(game.ID, phaseMeta.PhaseOrdinal.toString())).and(orderService.ListOrders(game.ID, phaseMeta.PhaseOrdinal.toString())).and(phaseService.PhaseLoad(game.ID, phaseMeta.PhaseOrdinal.toString())).then(new Func3<SingleContainer<Map<String, OptionsService.Option>>, MultiContainer<Order>, SingleContainer<Phase>, Object>() {

                @Override
                public Object call(SingleContainer<Map<String, OptionsService.Option>> opts, MultiContainer<Order> ords, SingleContainer<Phase> phase) {
                    options = opts.Properties;
                    orders.clear();
                    for (SingleContainer<Order> orderContainer : ords.Properties) {
                        String srcProvince = orderContainer.Properties.Parts.get(0);
                        if (srcProvince.indexOf('/') != -1) {
                            srcProvince = srcProvince.substring(0, srcProvince.indexOf('/'));
                        }
                        orders.put(srcProvince, orderContainer.Properties);
                    }
                    boolean hasBuildOpts = false;
                    boolean hasDisbandOpts = false;
                    for (OptionsService.Option opt : options.values()) {
                        if (opt.Next.containsKey("Build")) {
                            hasBuildOpts = true;
                        }
                        if (opt.Next.containsKey("Disband")) {
                            hasDisbandOpts = true;
                        }
                    }
                    int units = 0;
                    int scs = 0;
                    for (UnitWrapper unit : phase.Properties.Units) {
                        if (unit.Unit.Nation.equals(member.Nation)) {
                            units++;
                        }
                    }
                    for (SC sc : phase.Properties.SCs) {
                        if (sc.Owner.equals(member.Nation)) {
                            scs++;
                        }
                    }
                    if (hasBuildOpts && units < scs) {
                        Toast.makeText(GameActivity.this, getResources().getString(R.string.you_can_build_n_this_phase, getResources().getQuantityString(R.plurals.unit, scs - units, scs - units)), Toast.LENGTH_LONG).show();
                    } else if (hasDisbandOpts && scs < units) {
                        Toast.makeText(GameActivity.this, getResources().getString(R.string.you_have_to_disband_n_this_phase, getResources().getQuantityString(R.plurals.unit, units - scs, units - scs)), Toast.LENGTH_LONG).show();
                    }
                    return null;
                }
            })).toObservable(), new Sendable<Object>() {

                @Override
                public void send(Object o) {
                    acceptOrders();
                }
            }, getResources().getString(R.string.loading_state));
        }
    } else {
        findViewById(R.id.rewind).setVisibility(View.GONE);
        findViewById(R.id.previous).setVisibility(View.GONE);
        findViewById(R.id.next).setVisibility(View.GONE);
        findViewById(R.id.fast_forward).setVisibility(View.GONE);
        handleReq(variantService.GetStartPhase(game.Variant), new Sendable<SingleContainer<VariantService.Phase>>() {

            @Override
            public void send(SingleContainer<VariantService.Phase> phaseSingleContainer) {
                String url = null;
                for (Link link : phaseSingleContainer.Links) {
                    if (link.Rel.equals("map")) {
                        url = link.URL;
                        break;
                    }
                }
                if (url != null) {
                    renderer.send(url);
                } else {
                    App.firebaseCrashReport("No map URL found in variant " + game.Variant + "?");
                    Toast.makeText(getBaseContext(), R.string.unknown_error, Toast.LENGTH_SHORT).show();
                }
            }
        }, getResources().getString(R.string.loading_start_state));
    }
}
Also used : VariantService(se.oort.diplicity.VariantService) Phase(se.oort.diplicity.apigen.Phase) SC(se.oort.diplicity.apigen.SC) MultiContainer(se.oort.diplicity.apigen.MultiContainer) FloatingActionButton(android.support.design.widget.FloatingActionButton) Func3(rx.functions.Func3) Order(se.oort.diplicity.apigen.Order) OptionsService(se.oort.diplicity.OptionsService) UnitWrapper(se.oort.diplicity.apigen.UnitWrapper) NavigationView(android.support.design.widget.NavigationView) UserView(se.oort.diplicity.UserView) View(android.view.View) AdapterView(android.widget.AdapterView) TextView(android.widget.TextView) ListView(android.widget.ListView) ScrollView(android.widget.ScrollView) SingleContainer(se.oort.diplicity.apigen.SingleContainer) Sendable(se.oort.diplicity.Sendable) Map(java.util.Map) HashMap(java.util.HashMap) Link(se.oort.diplicity.apigen.Link)

Example 2 with Sendable

use of se.oort.diplicity.Sendable in project android-diplicity by zond.

the class GameActivity method setOrder.

private void setOrder(String province, List<String> parts) {
    Sendable<SingleContainer<Order>> handler = new Sendable<SingleContainer<Order>>() {

        @Override
        public void send(SingleContainer<Order> orderSingleContainer) {
            MapView mv = (MapView) findViewById(R.id.map_view);
            mv.evaluateJS("window.map.removeOrders()");
            for (Map.Entry<String, Order> entry : orders.entrySet()) {
                mv.evaluateJS("window.map.addOrder(" + new Gson().toJson(entry.getValue().Parts) + ", col" + entry.getValue().Nation + ");");
            }
        }
    };
    if (parts == null || parts.size() == 0) {
        if (orders.containsKey(province)) {
            orders.remove(province);
            handleReq(orderService.OrderDelete(game.ID, phaseMeta.PhaseOrdinal.toString(), province), handler, getResources().getString(R.string.removing_order));
        }
    } else {
        Order order = new Order();
        order.GameID = game.ID;
        order.Nation = member.Nation;
        order.PhaseOrdinal = phaseMeta.PhaseOrdinal;
        order.Parts = parts;
        orders.put(province, order);
        handleReq(orderService.OrderCreate(order, game.ID, phaseMeta.PhaseOrdinal.toString()), handler, getResources().getString(R.string.saving_order));
    }
}
Also used : SingleContainer(se.oort.diplicity.apigen.SingleContainer) Order(se.oort.diplicity.apigen.Order) Sendable(se.oort.diplicity.Sendable) Gson(com.google.gson.Gson) Map(java.util.Map) HashMap(java.util.HashMap)

Example 3 with Sendable

use of se.oort.diplicity.Sendable in project android-diplicity by zond.

the class GameActivity method showPhases.

public void showPhases(boolean loadPhases) {
    hideAllExcept(R.id.phases_view);
    final Sendable<MultiContainer<Phase>> renderer = new Sendable<MultiContainer<Phase>>() {

        @Override
        public void send(MultiContainer<Phase> phaseMultiContainer) {
            final List<PhaseElement> phases = new ArrayList<>();
            for (int i = 0; i < phaseMultiContainer.Properties.size(); i++) {
                phases.add(new PhaseElement(phaseMultiContainer.Properties.get(phaseMultiContainer.Properties.size() - i - 1).Properties));
            }
            ListView phasesView = (ListView) findViewById(R.id.phases_view);
            phasesView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                    Gson gson = new Gson();
                    phaseMeta = gson.fromJson(gson.toJson(phases.get(i).phase), PhaseMeta.class);
                    currentView = R.id.nav_map;
                    showMap();
                }
            });
            phasesView.setAdapter(new ArrayAdapter<PhaseElement>(GameActivity.this, android.R.layout.simple_list_item_1, phases));
        }
    };
    if (loadPhases || phases == null) {
        handleReq(phaseService.ListPhases(game.ID), new Sendable<MultiContainer<Phase>>() {

            @Override
            public void send(MultiContainer<Phase> phaseMultiContainer) {
                phases = phaseMultiContainer;
                renderer.send(phaseMultiContainer);
            }
        }, getResources().getString(R.string.loading_phases));
    } else {
        renderer.send(phases);
    }
}
Also used : Phase(se.oort.diplicity.apigen.Phase) ArrayList(java.util.ArrayList) Gson(com.google.gson.Gson) NavigationView(android.support.design.widget.NavigationView) UserView(se.oort.diplicity.UserView) View(android.view.View) AdapterView(android.widget.AdapterView) TextView(android.widget.TextView) ListView(android.widget.ListView) ScrollView(android.widget.ScrollView) ListView(android.widget.ListView) MultiContainer(se.oort.diplicity.apigen.MultiContainer) Sendable(se.oort.diplicity.Sendable) AdapterView(android.widget.AdapterView)

Example 4 with Sendable

use of se.oort.diplicity.Sendable in project android-diplicity by zond.

the class GameActivity method showPhaseResults.

public void showPhaseResults() {
    setVisibility(View.GONE, R.id.nmr_members, R.id.nmr_members_label, R.id.active_members, R.id.active_members_label, R.id.ready_members, R.id.ready_members_label);
    hideAllExcept(R.id.phase_results_view);
    final ScrollView phaseResultsView = (ScrollView) findViewById(R.id.phase_results_view);
    phaseResultsView.setOnTouchListener(new View.OnTouchListener() {

        @Override
        public boolean onTouch(View view, MotionEvent motionEvent) {
            flickFrameLayout.onTouchEvent(motionEvent);
            phaseResultsView.onTouchEvent(motionEvent);
            return true;
        }
    });
    if (phaseMeta.Resolved) {
        handleReq(phaseResultService.PhaseResultLoad(game.ID, phaseMeta.PhaseOrdinal.toString()), new Sendable<SingleContainer<PhaseResult>>() {

            @Override
            public void send(SingleContainer<PhaseResult> phaseResultSingleContainer) {
                MemberListPopulater populater = new MemberListPopulater();
                if (phaseResultSingleContainer.Properties.NMRUsers != null) {
                    populater.populate(R.id.nmr_members, phaseResultSingleContainer.Properties.NMRUsers);
                    setVisibility(View.VISIBLE, R.id.nmr_members, R.id.nmr_members_label);
                }
                if (phaseResultSingleContainer.Properties.ActiveUsers != null) {
                    populater.populate(R.id.active_members, phaseResultSingleContainer.Properties.ActiveUsers);
                    setVisibility(View.VISIBLE, R.id.active_members, R.id.active_members_label);
                }
                if (phaseResultSingleContainer.Properties.ReadyUsers != null) {
                    populater.populate(R.id.ready_members, phaseResultSingleContainer.Properties.ReadyUsers);
                    setVisibility(View.VISIBLE, R.id.ready_members, R.id.ready_members_label);
                }
            }
        }, new ErrorHandler(404, new Sendable<HttpException>() {

            @Override
            public void send(HttpException e) {
            }
        }), getResources().getString(R.string.loading_phase_result));
    }
}
Also used : NavigationView(android.support.design.widget.NavigationView) UserView(se.oort.diplicity.UserView) View(android.view.View) AdapterView(android.widget.AdapterView) TextView(android.widget.TextView) ListView(android.widget.ListView) ScrollView(android.widget.ScrollView) MotionEvent(android.view.MotionEvent) SingleContainer(se.oort.diplicity.apigen.SingleContainer) PhaseResult(se.oort.diplicity.apigen.PhaseResult) ScrollView(android.widget.ScrollView) Sendable(se.oort.diplicity.Sendable) HttpException(retrofit2.adapter.rxjava.HttpException)

Example 5 with Sendable

use of se.oort.diplicity.Sendable in project android-diplicity by zond.

the class PressActivity method onPostCreate.

@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    setContentView(R.layout.activity_press);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    byte[] serializedMember = getIntent().getByteArrayExtra(SERIALIZED_MEMBER_KEY);
    if (serializedMember != null) {
        member = (Member) unserialize(serializedMember);
    }
    byte[] serializedChannel = getIntent().getByteArrayExtra(SERIALIZED_CHANNEL_KEY);
    channel = (ChannelService.Channel) unserialize(serializedChannel);
    byte[] serializedGame = getIntent().getByteArrayExtra(SERIALIZED_GAME_KEY);
    game = (Game) unserialize(serializedGame);
    ((TextView) findViewById(R.id.multi_line_title)).setText(TextUtils.join(", ", channel.Members));
    if (member == null) {
        findViewById(R.id.send_message_button).setVisibility(View.GONE);
        findViewById(R.id.new_message_body).setVisibility(View.GONE);
    } else {
        final TextInputEditText body = (TextInputEditText) findViewById(R.id.new_message_body);
        body.setText(prefs.getString(draftKey(), ""));
        body.addTextChangedListener(new TextWatcher() {

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                prefs.edit().putString(draftKey(), body.getText().toString()).apply();
            }
        });
        ((FloatingActionButton) findViewById(R.id.send_message_button)).setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                Message message = new Message();
                message.Body = ((EditText) findViewById(R.id.new_message_body)).getText().toString();
                message.ChannelMembers = channel.Members;
                message.CreatedAt = new Date();
                message.Age = new Ticker(new Date(), (long) 0);
                message.GameID = channel.GameID;
                message.Sender = member.Nation;
                handleReq(messageService.MessageCreate(message, channel.GameID), new Sendable<SingleContainer<Message>>() {

                    @Override
                    public void send(SingleContainer<Message> messageSingleContainer) {
                        ((EditText) findViewById(R.id.new_message_body)).setText("");
                        prefs.edit().putString(draftKey(), "").apply();
                        loadMessages(false);
                    }
                }, getResources().getString(R.string.sending_message));
            }
        });
    }
    loadMessages(true);
    Toast.makeText(this, R.string.all_press_becomes_public, Toast.LENGTH_SHORT).show();
}
Also used : TextInputEditText(android.support.design.widget.TextInputEditText) EditText(android.widget.EditText) Message(se.oort.diplicity.apigen.Message) Ticker(se.oort.diplicity.apigen.Ticker) ImageView(android.widget.ImageView) UserView(se.oort.diplicity.UserView) View(android.view.View) NestedScrollView(android.support.v4.widget.NestedScrollView) TextView(android.widget.TextView) Date(java.util.Date) SingleContainer(se.oort.diplicity.apigen.SingleContainer) ChannelService(se.oort.diplicity.ChannelService) Sendable(se.oort.diplicity.Sendable) TextInputEditText(android.support.design.widget.TextInputEditText) TextWatcher(android.text.TextWatcher) Editable(android.text.Editable) FloatingActionButton(android.support.design.widget.FloatingActionButton) TextView(android.widget.TextView) Toolbar(android.support.v7.widget.Toolbar)

Aggregations

Sendable (se.oort.diplicity.Sendable)5 View (android.view.View)4 TextView (android.widget.TextView)4 UserView (se.oort.diplicity.UserView)4 SingleContainer (se.oort.diplicity.apigen.SingleContainer)4 NavigationView (android.support.design.widget.NavigationView)3 AdapterView (android.widget.AdapterView)3 ListView (android.widget.ListView)3 ScrollView (android.widget.ScrollView)3 FloatingActionButton (android.support.design.widget.FloatingActionButton)2 Gson (com.google.gson.Gson)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 MultiContainer (se.oort.diplicity.apigen.MultiContainer)2 Order (se.oort.diplicity.apigen.Order)2 Phase (se.oort.diplicity.apigen.Phase)2 TextInputEditText (android.support.design.widget.TextInputEditText)1 NestedScrollView (android.support.v4.widget.NestedScrollView)1 Toolbar (android.support.v7.widget.Toolbar)1 Editable (android.text.Editable)1