-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathcyber-stasis.go
2223 lines (2034 loc) · 73.4 KB
/
cyber-stasis.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package main
import (
"encoding/base64"
"encoding/json"
"fmt"
"log"
"math"
"net/http"
"sort"
"strconv"
"strings"
"time"
"github.com/NYTimes/gziphandler"
"github.com/foolin/mixer"
"github.com/maxence-charriere/go-app/v10/pkg/app"
shell "github.com/stateless-minds/go-ipfs-api"
)
const dbNameSupplyDemand = "demand_supply"
const dbNameCitizenReputation = "citizen_reputation"
const (
topicDemand = "demand"
topicCritical = "critical"
)
const (
Hour = "hour"
Day = "day"
Week = "week"
Month = "month"
Year = "year"
Custom = "custom"
NotificationSuccess NotificationStatus = "success"
NotificationInfo NotificationStatus = "info"
NotificationWarning NotificationStatus = "warning"
NotificationDanger NotificationStatus = "danger"
NotificationPrimary NotificationStatus = "primary"
)
// pubsub is a component that does a simple pubsub on ipfs. A component is a
// customizable, independent, and reusable UI element. It is created by
// embedding app.Compo into a struct.
type pubsub struct {
app.Compo
topic string
demandRequest
sendRequest
demandRequests map[string]demandRequest
categories []string
sh *shell.Shell
sub *shell.PubSubSubscription
citizenID string
newComer bool
timeRange []int
timeFormat []string
ratio []int
index []int
hasChildren bool
filteredRequests []int
validRequests []int
filteredandValidRequests int
filteredWaterRequests []int
filteredFoodRequests []int
filteredHousingRequests []int
filteredOtherRequests []int
lastWaterRequest int
lastFoodRequest int
lastHousingRequest int
lastOtherRequest int
ranks []ranking
showMessages bool
showRatio bool
showTime bool
showChart bool
showRanks bool
counterDemand int
counterSupply int
counterSameTime int
counterSameTimeFulfilled int
coordinates []coordinate
category string
period string
stats string
multiplyer int
notifications map[string]notification
notificationID int
globalEvent bool
resource string
}
type Shortage struct {
Category string
Description string
}
type NotificationStatus string
type notification struct {
id int
status string
header string
message string
}
type coordinate struct {
id int
top int
left int
angle float64
distance float64
}
type ranking struct {
citizenID string
demandRatio float64 // number of personal demands compared to total
supplyRatio float64 // number of personal supplies compared to total
reputationIndex float64 // supplyRatio compared to demandRatio
}
type citizenReputation struct {
ID string `json:"_id" validate:"uuid_rfc4122"`
Type string `json:"type" validate:"uuid_rfc4122"`
CitizenID string `json:"citizenId" validate:"uuid_rfc4122"`
ReputationIndex float64 `json:"reputationIndex" validate:"uuid_rfc4122"`
}
type sendRequest struct {
ID int
Category string
Quantity string
Details string
Fulfilled bool
CreatedAt time.Time
FulfilledAt time.Time
}
type demandRequest struct {
ID int
CitizenID string
Category string
Quantity string
Details string
CreatedAt time.Time
Fulfilled bool
FulfilledBy string
FulfilledAt time.Time
}
func (p *pubsub) OnMount(ctx app.Context) {
p.topic = topicDemand
sh := shell.NewShell("localhost:5001")
p.sh = sh
myPeer, err := p.sh.ID()
if err != nil {
log.Fatal(err)
}
/*** Test sending critical messages from all categories ***/
// ctx.Async(func() {
// category := "housing"
// desc := "Global shortage of housing"
// s := Shortage{
// Category: category,
// Description: desc,
// }
// shortage, err := json.Marshal(s)
// if err != nil {
// log.Fatal(err)
// }
// p.sh.PubSubPublish(topicCritical, string(shortage))
// time.Sleep(2 * time.Second)
// category = "water"
// desc = "Global shortage of water"
// s = Shortage{
// Category: category,
// Description: desc,
// }
// shortage, err = json.Marshal(s)
// if err != nil {
// log.Fatal(err)
// }
// p.sh.PubSubPublish(topicCritical, string(shortage))
// time.Sleep(2 * time.Second)
// category = "food"
// desc = "Global shortage of food"
// s = Shortage{
// Category: category,
// Description: desc,
// }
// shortage, err = json.Marshal(s)
// if err != nil {
// log.Fatal(err)
// }
// p.sh.PubSubPublish(topicCritical, string(shortage))
// })
citizenID := myPeer.ID[len(myPeer.ID)-8:]
// replace password with your own
password := "mysecretpassword"
p.citizenID = mixer.EncodeString(password, citizenID)
p.subscribe(ctx)
p.demandRequests = make(map[string]demandRequest)
p.FetchAllRequests(ctx, app.Event{})
p.setTimeAxis(Hour)
// 0 to 1 supply/demand
for i := 1; i < 11; i++ {
p.ratio = append(p.ratio, i)
}
// default categories
p.categories = append(p.categories, "all", "water", "food", "housing", "other")
p.category = "All"
p.showRatio = true
p.showTime = true
p.showRanks = false
// create welcome notification
p.notifications = make(map[string]notification)
}
func (p *pubsub) setTimeAxis(period string) {
p.period = period
p.timeRange = []int{}
p.timeFormat = []string{}
switch period {
case "hour":
now := time.Now()
// defining duration
d := (10 * time.Minute)
now = now.Round(d)
before := now.Add(time.Hour * -1)
for i := 1; i < 7; i++ {
p.timeRange = append(p.timeRange, i)
before = before.Add(time.Minute * 10)
min := strconv.Itoa(before.Minute())
if before.Minute() == 0 {
min += "0"
}
p.timeFormat = append(p.timeFormat, strconv.Itoa(before.Hour())+":"+min)
}
p.multiplyer = 132
case "day":
for i := 1; i < 25; i++ {
p.timeRange = append(p.timeRange, i)
p.timeFormat = append(p.timeFormat, strconv.Itoa(i)+":00")
}
p.multiplyer = 33
case "week":
now := time.Now()
n := 8
for i := 1; i < n; i++ {
p.timeRange = append(p.timeRange, i)
before := now.AddDate(0, 0, i+1-n)
p.timeFormat = append(p.timeFormat, strconv.Itoa(before.Day())+". "+time.Now().Month().String())
}
p.multiplyer = 113
case "month":
before := time.Now().AddDate(0, 0, -30)
for i := 1; i < 31; i++ {
p.timeRange = append(p.timeRange, i)
before = before.AddDate(0, 0, 1)
p.timeFormat = append(p.timeFormat, strconv.Itoa(before.Day())+". "+before.Month().String())
}
p.multiplyer = 26
case "year":
now := time.Now()
before := now.AddDate(-1, 0, 0)
for i := 1; i < 13; i++ {
p.timeRange = append(p.timeRange, i)
before = before.AddDate(0, 1, 0)
p.timeFormat = append(p.timeFormat, before.Month().String()+". "+strconv.Itoa(before.Year()))
}
p.multiplyer = 66
}
}
// The Render method is where the component appearance is defined. Here, a
// "pubsub World!" is displayed as a heading.
func (p *pubsub) Render() app.UI {
return app.Div().Class("container").Body(
app.Link().Rel("stylesheet").Href("https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css").CrossOrigin("anonymous"),
app.Script().Src("https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js").CrossOrigin("anonymous"),
app.Div().Class("square_box box_three"),
app.Div().Class("square_box box_four"),
app.If(len(p.notifications) > 0, func() app.UI {
return app.Section().Body(
app.Div().Class("container").Body(
app.Div().Class("row").Body(
app.Range(p.notifications).Map(func(s string) app.UI {
return app.Div().Class("col-sm-12").Body(
app.Div().Class("alert fade alert-simple alert-"+p.notifications[s].status+" alert-dismissible text-left font__family-montserrat font__size-16 font__weight-light brk-library-rendered rendered show").Body(
app.Button().ID("notify-"+strconv.Itoa(p.notifications[s].id)).Class("btn-close btn-close-white").Type("button").DataSet("bs-dismiss", "alert").Aria("label", "Close").Value(p.notifications[s].id),
app.I().Class("start-icon far fa-check-circle faa-tada animated"),
app.Strong().Class("font__weight-semibold").Text(p.notifications[s].header+" "),
app.Text(p.notifications[s].message),
),
)
}),
),
),
)
}),
app.H1().Class("pb-0 logo").Body(
app.Text("Cyber-Stasis"),
app.Details().Body(
app.Summary().Body(
app.Small().Body(
app.Div().Class("button").Text("How to Play"),
),
app.Div().Class("details-modal-overlay"),
),
app.Div().Class("details-modal").Body(
app.Div().Class("details-modal-close").Body(
app.Button().Class("btn-close btn-sm").Type("button").Aria("label", "Close"),
),
app.Div().Class("details-modal-title").Body(
app.H1().Text("How to Play"),
),
app.Div().Class("details-modal-content").Body(
app.Div().Body(
app.Details().Class("how-to-play").Open(true).Body(
app.Summary().Class("accordion").Text("The Story"),
app.Div().Text("Welcome people from the past. I am here to tell you what happened after the Great Reset. The year is 2105. Humanity now lives in a post-capitalism era. We no longer work by schedule and necessity. Automation has replaced all repetitive and dangerous jobs. The concept of work has diminished. There are no countries and the concept of money has long been forgotten. Private property was abolished and corporations were transformed into cooperatives. We are all connected in an open source p2p network. Every day we open up our Cyber Stasis dashboards to request goods and services we need for the day and to provide what we can."),
),
app.Details().Class("how-to-play").Body(
app.Summary().Class("accordion").Text("The Mission"),
app.Div().Text("The mission of Cyber Stasis is to prototype a working economic simulator as an alternative to the monetary system. It's goal is to be a living proof that the market economy can operate without exchange. Recent advancement in p2p technology made possible the release of the Cyber Stasis dashboard as an open-source technology which is free and hosted by everyone who uses it. This allows the project to run forever as long as there are people participating. Hopefully this experiment can help academic institutions to research alternative economic models further for the betterment of society as a whole."),
),
app.Details().Class("how-to-play").Body(
app.Summary().Class("accordion").Text("Guidelines"),
app.Div().Body(
app.Ol().Class("list-group list-group-numbered").Body(
app.Li().Class("list-group-item d-flex justify-content-between align-items-start").Body(
app.Div().Class("ms-2 me-auto").Body(
app.Div().Class("fw-bold").Text("Economic simulator"),
app.Text("Cyber Stasis is an economic simulator in the form of a fictional game based on global real-time demand and supply."),
),
),
app.Li().Class("list-group-item d-flex justify-content-between align-items-start").Body(
app.Div().Class("ms-2 me-auto").Body(
app.Div().Class("fw-bold").Text("Real-time demand/supply graph"),
app.Text("The graph reflects all demand and supply requests and is updated in real-time."),
),
),
app.Li().Class("list-group-item d-flex justify-content-between align-items-start").Body(
app.Div().Class("ms-2 me-auto").Body(
app.Div().Class("fw-bold").Text("Supply can be sent only in response to an existing demand"),
app.Text("Send only goods and services you can provide in real life."),
),
),
app.Li().Class("list-group-item d-flex justify-content-between align-items-start").Body(
app.Div().Class("ms-2 me-auto").Body(
app.Div().Class("fw-bold").Text("Keep it real"),
app.Text("Send requests for your real daily needs to make the whole simulation as accurate as possible."),
),
),
app.Li().Class("list-group-item d-flex justify-content-between align-items-start").Body(
app.Div().Class("ms-2 me-auto").Body(
app.Div().Class("fw-bold").Text("Global events"),
app.Text("When the supply/demand ratio drops below certain thresholds global events are triggered and sent as notifications such as global shortage of water, food and housing."),
),
),
app.Li().Class("list-group-item d-flex justify-content-between align-items-start").Body(
app.Div().Class("ms-2 me-auto").Body(
app.Div().Class("fw-bold").Text("Do what you do in real life"),
app.Text("Ask for things you need and supply things you provide."),
),
),
app.Li().Class("list-group-item d-flex justify-content-between align-items-start").Body(
app.Div().Class("ms-2 me-auto").Body(
app.Div().Class("fw-bold").Text("Rankings"),
app.Text("Rankings reflect the level of contribution and usefulness of members to society. They take all factors into account and are calculated by a formula. The Reputation Index is the score in the game. Provide more than you consume and become the most valuable member of society!"),
),
),
app.Li().Class("list-group-item d-flex justify-content-between align-items-start").Body(
app.Div().Class("ms-2 me-auto").Body(
app.Div().Class("fw-bold").Text("No collection of user data"),
app.Text("Cyber Stasis does not collect any personal user data."),
),
),
app.Li().Class("list-group-item d-flex justify-content-between align-items-start").Body(
app.Div().Class("ms-2 me-auto").Body(
app.Div().Class("fw-bold").Text("User generated content is fictional"),
app.Text("All user generated content is fictional and creators are not responsibile for it."),
),
),
app.Li().Class("list-group-item d-flex justify-content-between align-items-start").Body(
app.Div().Class("ms-2 me-auto").Body(
app.Div().Class("fw-bold").Text("If you like it support it"),
app.Text("This is an open source community project. Feel free to improve it or fork it and use it for your projects. Donations are welcome."),
),
),
),
),
),
app.Details().Class("how-to-play").Body(
app.Summary().Class("accordion").Text("Inspirations"),
app.Div().Body(
app.Ol().Class("list-group list-group-numbered").Body(
app.Li().Class("list-group-item d-flex justify-content-between align-items-start").Body(
app.Div().Class("ms-2 me-auto").Body(
app.Div().Class("fw-bold").Text("Auroville"),
app.A().Href("https://auroville-learning.net").Text("https://auroville-learning.net"),
),
),
app.Li().Class("list-group-item d-flex justify-content-between align-items-start").Body(
app.Div().Class("ms-2 me-auto").Body(
app.Div().Class("fw-bold").Text("CyberSyn"),
app.A().Href("https://en.wikipedia.org/wiki/Project_Cybersyn").Text("https://en.wikipedia.org/wiki/Project_Cybersyn"),
),
),
app.Li().Class("list-group-item d-flex justify-content-between align-items-start").Body(
app.Div().Class("ms-2 me-auto").Body(
app.Div().Class("fw-bold").Text("The Venus Project"),
app.A().Href("https://www.thevenusproject.com").Text("https://www.thevenusproject.com"),
),
),
app.Li().Class("list-group-item d-flex justify-content-between align-items-start").Body(
app.Div().Class("ms-2 me-auto").Body(
app.Div().Class("fw-bold").Text("OGAS"),
app.A().Href("https://en.wikipedia.org/wiki/OGAS").Text("https://en.wikipedia.org/wiki/OGAS"),
),
),
),
),
),
app.Details().Class("how-to-play").Body(
app.Summary().Class("accordion").Text("Acknowledgments"),
app.Div().Body(
app.Ol().Class("list-group list-group-numbered").Body(
app.Li().Class("list-group-item d-flex justify-content-between align-items-start").Body(
app.Div().Class("ms-2 me-auto").Body(
app.Div().Class("fw-bold").Text("IPFS"),
app.A().Href("https://ipfs.io").Text("https://ipfs.io"),
),
),
app.Li().Class("list-group-item d-flex justify-content-between align-items-start").Body(
app.Div().Class("ms-2 me-auto").Body(
app.Div().Class("fw-bold").Text("Berty"),
app.A().Href("https://berty.tech").Text("https://berty.tech"),
),
),
app.Li().Class("list-group-item d-flex justify-content-between align-items-start").Body(
app.Div().Class("ms-2 me-auto").Body(
app.Div().Class("fw-bold").Text("GO-APP"),
app.A().Href("https://go-app.dev").Text("https://go-app.dev"),
),
),
app.Li().Class("list-group-item d-flex justify-content-between align-items-start").Body(
app.Div().Class("ms-2 me-auto").Body(
app.Div().Class("fw-bold").Text("Alessio Atzeni"),
app.A().Href("https://www.alessioatzeni.com/blog/css3-graph-animation").Text("https://www.alessioatzeni.com/blog/css3-graph-animation"),
),
),
app.Li().Class("list-group-item d-flex justify-content-between align-items-start").Body(
app.Div().Class("ms-2 me-auto").Body(
app.Div().Class("fw-bold").Text("Niels Voogt"),
app.A().Href("https://codepen.io/NielsVoogt/pen/XWjPdjO").Text("https://codepen.io/NielsVoogt/pen/XWjPdjO"),
),
),
app.Li().Class("list-group-item d-flex justify-content-between align-items-start").Body(
app.Div().Class("ms-2 me-auto").Body(
app.Div().Class("fw-bold").Text("Håvard Brynjulfsen"),
app.A().Href("https://codepen.io/havardob/pen/abBJgQo").Text("https://codepen.io/havardob/pen/abBJgQo"),
),
),
),
),
),
app.Details().Class("how-to-play").Body(
app.Summary().Class("accordion").Text("Support Us"),
app.Div().Body(
app.A().Href("https://opencollective.com/stateless-minds-collective").Text("https://opencollective.com/stateless-minds-collective"),
),
),
app.Details().Class("how-to-play").Body(
app.Summary().Class("accordion").Text("Terms of Service"),
app.Div().Body(
app.Ul().Class("list-group").Body(
app.Li().Class("list-group-item d-flex justify-content-between align-items-start").Body(
app.Div().Class("ms-2 me-auto").Body(
app.Div().Class("fw-bold").Text("Introduction"),
app.Text("Cyber Stasis is an economic simulator in the form of a fictional game based on global real-time demand and supply. By using the application you are implicitly agreeing to share your peer id with the IPFS public network."),
),
),
app.Li().Class("list-group-item d-flex justify-content-between align-items-start").Body(
app.Div().Class("ms-2 me-auto").Body(
app.Div().Class("fw-bold").Text("Application Hosting"),
app.Text("Cyber Stasis is a decentralized application and is hosted on a public peer to peer network. By using the application you agree to host it on the public IPFS network free of charge for as long as your usage is."),
),
),
app.Li().Class("list-group-item d-flex justify-content-between align-items-start").Body(
app.Div().Class("ms-2 me-auto").Body(
app.Div().Class("fw-bold").Text("User-Generated Content"),
app.Text("All published content is user-generated, fictional and creators are not responsible for it."),
),
),
),
),
),
app.Details().Class("how-to-play").Body(
app.Summary().Class("accordion").Text("Privacy Policy"),
app.Div().Body(
app.Ul().Class("list-group").Body(
app.Li().Class("list-group-item d-flex justify-content-between align-items-start").Body(
app.Div().Class("ms-2 me-auto").Body(
app.Div().Class("fw-bold").Text("Personal Data"),
app.Text("There is no personal information collected within Cyber Stasis. We store a small portion of your peer ID encrypted as a non-unique identifier which is used for displaying the ranks interface."),
),
),
app.Li().Class("list-group-item d-flex justify-content-between align-items-start").Body(
app.Div().Class("ms-2 me-auto").Body(
app.Div().Class("fw-bold").Text("Cookies"),
app.Text("Cyber Stasis does not use cookies."),
),
),
app.Li().Class("list-group-item d-flex justify-content-between align-items-start").Body(
app.Div().Class("ms-2 me-auto").Body(
app.Div().Class("fw-bold").Text("Links to External Websites"),
app.Text("Cyber Stasis contains links to other websites. If you click on a third-party link, you will be directed to that site. Note that these external sites are not operated by Cyber Stasis. Therefore, it is strongly advised that you review the Privacy Policy of these websites. Cyber Stasis has no control over and assumes no responsibility for the content, privacy policies, or practices of any third-party sites or services."),
),
),
app.Li().Class("list-group-item d-flex justify-content-between align-items-start").Body(
app.Div().Class("ms-2 me-auto").Body(
app.Div().Class("fw-bold").Text("Changes to this Privacy Policy"),
app.Text("This Privacy Policy might be updated from time to time. Thus, it is advised to review this page periodically for any changes. You will be notified of any changes from this page. Changes are effective immediately after they are posted on this page."),
),
),
),
),
),
),
),
),
),
),
app.Div().Class("container d-flex justify-content-center pb-5").Body(
app.Div().Class("card").Body(
app.If(p.showMessages, func() app.UI {
return app.H6().Class("card-title").Text("Pending Requests")
}),
app.If(p.showMessages, func() app.UI {
return app.Div().Class("card-body").Body(
app.Range(p.index).Slice(func(i int) app.UI {
i = p.index[i]
if p.demandRequests[strconv.Itoa(i)].ID > 0 && !p.demandRequests[strconv.Itoa(i)].Fulfilled {
return app.Div().Class("d-flex flex-row p-3").Body(
app.Img().Src("https://img.icons8.com/color/48/000000/circled-user-female-skin-type-7.png").Width(30).Height(30),
app.Div().Class("chat ml-3 p-3").Body(
app.Span().Class("pe-2").Body(
app.Span().Class("badge rounded-pill bg-info text-dark").Text(strings.ToUpper(p.demandRequests[strconv.Itoa(i)].Category)),
app.P().Class("card-text pt-3").Text("Quantity: "+p.demandRequests[strconv.Itoa(i)].Quantity),
app.P().Class("card-text").Text("Details: "+p.demandRequests[strconv.Itoa(i)].Details),
app.Div().Class("row d-flex justify-content-center align-content-center ps-3 pe-3").Body(
app.Button().Class("btn btn-outline-primary btn-sm rounded-pill").ID(strconv.Itoa(p.demandRequests[strconv.Itoa(i)].ID)).Body(app.Text("Send Supply")).OnClick(p.sendSupply),
),
),
),
)
}
return nil
}))
}),
app.H6().Class("card-title").Text("What do you need today?"),
app.Div().Class("form-group").Body(
app.Select().Class("form-select").Aria("label", "Demand category").Body(
app.Range(p.categories).Slice(func(i int) app.UI {
if p.categories[i] == "all" {
return app.Option().Selected(true).Value("").Text("Select Category")
} else {
return app.Option().Value(p.categories[i]).Text(strings.Title(p.categories[i]))
}
}),
).Required(true).OnClick(p.onSelect),
app.Input().ID("quantity").Class("form-control").Name("quantity").Type("number").Placeholder("Quantity").OnKeyUp(p.onInput),
app.Textarea().Class("form-control").Rows(3).Placeholder("Details").OnKeyUp(p.onMessage),
),
app.Button().Class("btn btn-outline-info mt-2").ID("submitDemand").Body(app.Text("Send Request")).OnClick(p.sendDemand).Disabled(true),
// app.Button().Class("btn btn-outline-secondary").ID("FetchAllRequests").Body(app.Text("Get Requests")).OnClick(p.FetchAllRequests),
// app.Button().Class("btn btn-outline-warning").ID("dummydata").Body(app.Text("Dummy Data")).OnClick(p.dummyData),
// app.Button().Class("btn btn-outline-danger").ID("deleteRequests").Body(app.Text("Delete Requests")).OnClick(p.deleteRequests),
),
app.Div().ID("secondary").Class("container").Body(
app.Button().Class("btn btn-outline-info category").Text("Other").Value("Other").OnClick(p.onSelectCategory),
app.Button().Class("btn btn-outline-info category").Text("Housing").Value("Housing").OnClick(p.onSelectCategory),
app.Button().Class("btn btn-outline-info category").Text("Food").Value("Food").OnClick(p.onSelectCategory),
app.Button().Class("btn btn-outline-info category").Text("Water").Value("Water").OnClick(p.onSelectCategory),
app.Button().ID("category-all").Class("btn btn-outline-info category active").Text("All").Value("All").OnClick(p.onSelectCategory),
app.Button().ID("global-stats").Class("btn btn-outline-info stats active").Text("Global Stats").Value("Global").OnClick(p.onSelectStats),
app.Button().ID("ranks").Class("btn btn-outline-info ranks").Text("Ranks").Value("Ranks").OnClick(p.onSelectRanks),
app.Button().Class("btn btn-outline-info period").Text("1 Year").Value(Year).OnClick(p.onSelectPeriod),
app.Button().Class("btn btn-outline-info period").Text("1 Month").Value(Month).OnClick(p.onSelectPeriod),
app.Button().Class("btn btn-outline-info period").Text("1 Week").Value(Week).OnClick(p.onSelectPeriod),
app.Button().Class("btn btn-outline-info period").Text("1 Day").Value(Day).OnClick(p.onSelectPeriod),
app.Button().ID("period-hour").Class("btn btn-outline-info period active").Text("1 Hour").Value(Hour).OnClick(p.onSelectPeriod),
app.Button().ID("my-stats").Class("btn btn-outline-info stats").Text("My Stats").Value("Personal").OnClick(p.onSelectStats),
app.Div().Class("content running").Body(
app.If(p.showRanks, func() app.UI {
return app.Ol().Class("list-group list-group-numbered").Body(
app.Range(p.ranks).Slice(func(i int) app.UI {
var class string
if p.ranks[i].citizenID == p.citizenID {
class = "active"
}
if p.ranks[i].citizenID == "" {
return nil
}
return app.Li().Class("list-group-item "+class+" d-flex justify-content-between align-items-start").Body(
app.Div().Class("ms-2 me-auto").Body(
app.Div().Class("fw-bold").Text("Citizen"),
app.Span().Class("badge bg-primary rounded-pill").Text(p.ranks[i].citizenID),
),
app.Div().Class("ms-2 me-auto").Body(
app.Div().Class("fw-bold").Text("Demand"),
app.Span().Class("badge bg-primary rounded-pill").Text(p.ranks[i].demandRatio),
),
app.Div().Class("ms-2 me-auto").Body(
app.Div().Class("fw-bold").Text("Supply"),
app.Span().Class("badge bg-primary rounded-pill").Text(p.ranks[i].supplyRatio),
),
app.Div().Class("ms-2 me-auto").Body(
app.Div().Class("fw-bold").Text("Reputation"),
app.Span().Class("badge bg-primary rounded-pill").Text(p.ranks[i].reputationIndex),
),
)
}),
)
}),
app.If(p.showRatio, func() app.UI {
return app.Range(p.ratio).Slice(func(i int) app.UI {
return app.Div().Class("range").Style("top", strconv.Itoa(390-(p.ratio[i]*40))+"px").Style("left", "0").Body(
app.A().Href("#").Body(app.Small().Text("Supply/Demand Ratio: " + fmt.Sprintf("%.1f", float32(p.ratio[i])/10))),
)
})
}),
app.If(p.showTime, func() app.UI {
return app.Range(p.timeRange).Slice(func(i int) app.UI {
return app.Div().Class("time").Style("top", "390px").Style("left", strconv.Itoa(p.timeRange[i]*p.multiplyer)+"px").Body(
app.A().Href("#").Body(app.Small().Text(p.timeFormat[i])),
)
})
}),
app.If(p.showChart, func() app.UI {
return app.If(len(p.filteredRequests) > 0, func() app.UI {
return app.Range(p.filteredRequests).Slice(func(i int) app.UI {
if p.stats == "Personal" && p.demandRequests[strconv.Itoa(p.filteredRequests[i])].CitizenID != p.citizenID {
return nil
}
t := time.Now()
// defining duration
d := (10 * time.Minute)
t = t.Round(d)
var left int
switch p.period {
case "hour":
index := 6
tt := t.Sub(p.demandRequests[strconv.Itoa(p.filteredRequests[i])].CreatedAt)
if tt.Minutes() >= 60 || tt.Minutes() < 0 {
p.filteredandValidRequests--
return nil
}
if p.filteredandValidRequests < 2 {
return nil
}
p.validRequests = append(p.validRequests, p.filteredRequests[i])
if p.filteredRequests[i] == p.validRequests[0] {
for n := 1; n < 6; n++ {
switch m := tt.Minutes(); {
case int(m) >= 10*n:
index--
}
}
p.counterSameTime++
if p.demandRequests[strconv.Itoa(p.filteredRequests[i])].Fulfilled {
p.counterSameTimeFulfilled++
}
if p.demandRequests[strconv.Itoa(p.filteredRequests[i+1])].CreatedAt.Sub(p.demandRequests[strconv.Itoa(p.filteredRequests[i])].CreatedAt).Minutes() < 10 {
return nil
}
} else {
if p.demandRequests[strconv.Itoa(p.filteredRequests[i])].CreatedAt.Sub(p.demandRequests[strconv.Itoa(p.filteredRequests[i-1])].CreatedAt).Minutes() < 10 {
p.counterSameTime++
if p.demandRequests[strconv.Itoa(p.filteredRequests[i])].Fulfilled {
p.counterSameTimeFulfilled++
}
p.hasChildren = true
return nil
}
var ttt time.Duration
if p.hasChildren {
ttt = t.Sub(p.demandRequests[strconv.Itoa(p.filteredRequests[i-1])].CreatedAt)
} else {
ttt = tt
}
for n := 1; n < 6; n++ {
switch m := ttt.Minutes(); {
case int(m) >= 10*n:
index--
}
}
}
left = index * p.multiplyer
case "day":
index := 24
t = time.Date(time.Now().Year(), time.Now().Month(), time.Now().Day(), 24, 0, 0, 0, time.Local)
tt := t.Sub(p.demandRequests[strconv.Itoa(p.filteredRequests[i])].CreatedAt)
if tt.Hours() > 24 || tt.Hours() < 0 {
p.filteredandValidRequests--
return nil
}
if p.filteredandValidRequests < 2 {
return nil
}
p.validRequests = append(p.validRequests, p.filteredRequests[i])
if p.filteredRequests[i] == p.validRequests[0] {
index = p.demandRequests[strconv.Itoa(p.filteredRequests[i])].CreatedAt.Hour()
p.counterSameTime++
if p.demandRequests[strconv.Itoa(p.filteredRequests[i])].Fulfilled {
p.counterSameTimeFulfilled++
}
if p.demandRequests[strconv.Itoa(p.filteredRequests[i+1])].CreatedAt.Sub(p.demandRequests[strconv.Itoa(p.filteredRequests[i])].CreatedAt).Hours() < 1 {
return nil
}
} else {
index = p.demandRequests[strconv.Itoa(p.filteredRequests[i-1])].CreatedAt.Hour()
if p.demandRequests[strconv.Itoa(p.filteredRequests[i])].CreatedAt.Sub(p.demandRequests[strconv.Itoa(p.filteredRequests[i-1])].CreatedAt).Hours() < 1 {
p.counterSameTime++
if p.demandRequests[strconv.Itoa(p.filteredRequests[i])].Fulfilled {
p.counterSameTimeFulfilled++
}
p.hasChildren = true
return nil
} else {
if !p.hasChildren {
index = p.demandRequests[strconv.Itoa(p.filteredRequests[i])].CreatedAt.Hour()
}
}
}
left = index * p.multiplyer
case "week":
index := 7
t = time.Date(time.Now().Year(), time.Now().Month(), time.Now().Day(), 24, 0, 0, 0, time.Local)
tt := t.Sub(p.demandRequests[strconv.Itoa(p.filteredRequests[i])].CreatedAt)
if tt.Hours() > 168 || tt.Hours() < 0 {
p.filteredandValidRequests--
return nil
}
if p.filteredandValidRequests < 2 {
return nil
}
p.validRequests = append(p.validRequests, p.filteredRequests[i])
if p.filteredRequests[i] == p.validRequests[0] {
for n := 1; n < 7; n++ {
switch h := tt.Hours(); {
case int(h) > 24*n:
index--
}
}
p.counterSameTime++
if p.demandRequests[strconv.Itoa(p.filteredRequests[i])].Fulfilled {
p.counterSameTimeFulfilled++
}
if p.demandRequests[strconv.Itoa(p.filteredRequests[i+1])].CreatedAt.Sub(p.demandRequests[strconv.Itoa(p.filteredRequests[i])].CreatedAt).Hours() < 24 {
return nil
}
} else {
if p.demandRequests[strconv.Itoa(p.filteredRequests[i])].CreatedAt.Sub(p.demandRequests[strconv.Itoa(p.filteredRequests[i-1])].CreatedAt).Hours() < 24 {
p.counterSameTime++
if p.demandRequests[strconv.Itoa(p.filteredRequests[i])].Fulfilled {
p.counterSameTimeFulfilled++
}
p.hasChildren = true
return nil
}
var ttt time.Duration
if p.hasChildren {
ttt = t.Sub(p.demandRequests[strconv.Itoa(p.filteredRequests[i-1])].CreatedAt)
} else {
ttt = tt
if p.demandRequests[strconv.Itoa(p.filteredRequests[i])].Fulfilled {
p.counterSameTimeFulfilled++
}
}
for n := 1; n < 7; n++ {
switch m := ttt.Hours(); {
case int(m) > 24*n:
index--
}
}
}
left = index * p.multiplyer
case "month":
index := 30
t = time.Date(time.Now().Year(), time.Now().Month(), time.Now().Day(), 24, 0, 0, 0, time.Local)
tt := t.Sub(p.demandRequests[strconv.Itoa(p.filteredRequests[i])].CreatedAt)
if tt.Hours() > 30*24 || tt.Hours() < 0 {
p.filteredandValidRequests--
return nil
}
if p.filteredandValidRequests < 2 {
return nil
}
p.validRequests = append(p.validRequests, p.filteredRequests[i])
if p.filteredRequests[i] == p.validRequests[0] {
for n := 1; n < 30; n++ {
switch h := tt.Hours(); {
case int(h) > 24*n:
index--
}
}
p.counterSameTime++
if p.demandRequests[strconv.Itoa(p.filteredRequests[i])].Fulfilled {
p.counterSameTimeFulfilled++
}
if p.demandRequests[strconv.Itoa(p.filteredRequests[i+1])].CreatedAt.Sub(p.demandRequests[strconv.Itoa(p.filteredRequests[i])].CreatedAt).Hours() < 24 {
return nil
}
} else {
if p.demandRequests[strconv.Itoa(p.filteredRequests[i])].CreatedAt.Sub(p.demandRequests[strconv.Itoa(p.filteredRequests[i-1])].CreatedAt).Hours() < 24 {
p.counterSameTime++
if p.demandRequests[strconv.Itoa(p.filteredRequests[i])].Fulfilled {
p.counterSameTimeFulfilled++
}
p.hasChildren = true
return nil
}
var ttt time.Duration
if p.hasChildren {
ttt = t.Sub(p.demandRequests[strconv.Itoa(p.filteredRequests[i-1])].CreatedAt)
} else {
ttt = tt
if p.demandRequests[strconv.Itoa(p.filteredRequests[i])].Fulfilled {
p.counterSameTimeFulfilled++
}
}
for n := 1; n < 30; n++ {
switch m := ttt.Hours(); {
case int(m) > 24*n:
index--
}
}
}
left = index * p.multiplyer
case "year":
index := 12
t = time.Date(time.Now().Year(), time.Now().Month(), time.Now().Day(), 24, 0, 0, 0, time.Local)
tt := t.Sub(p.demandRequests[strconv.Itoa(p.filteredRequests[i])].CreatedAt)
if tt.Hours() > 356*24 || tt.Hours() < 0 {
p.filteredandValidRequests--
return nil
}
if p.filteredandValidRequests < 2 {
return nil
}
p.validRequests = append(p.validRequests, p.filteredRequests[i])
if p.filteredRequests[i] == p.validRequests[0] {
for n := 1; n < 12; n++ {
switch h := tt.Hours(); {
case int(h) > 30*24*n:
index--
}
}
p.counterSameTime++
if p.demandRequests[strconv.Itoa(p.filteredRequests[i])].Fulfilled {
p.counterSameTimeFulfilled++
}
if p.demandRequests[strconv.Itoa(p.filteredRequests[i+1])].CreatedAt.Sub(p.demandRequests[strconv.Itoa(p.filteredRequests[i])].CreatedAt).Hours() < 30*24 {
return nil
}
} else {
if p.demandRequests[strconv.Itoa(p.filteredRequests[i])].CreatedAt.Sub(p.demandRequests[strconv.Itoa(p.filteredRequests[i-1])].CreatedAt).Hours() < 30*24 {
p.counterSameTime++
if p.demandRequests[strconv.Itoa(p.filteredRequests[i])].Fulfilled {
p.counterSameTimeFulfilled++
}
p.hasChildren = true
return nil
}
var ttt time.Duration
if p.hasChildren {
ttt = t.Sub(p.demandRequests[strconv.Itoa(p.filteredRequests[i-1])].CreatedAt)
} else {
ttt = tt
if p.demandRequests[strconv.Itoa(p.filteredRequests[i])].Fulfilled {
p.counterSameTimeFulfilled++
}
}
for n := 1; n < 12; n++ {
switch m := ttt.Hours(); {
case int(m) > 30*24*n:
index--
}
}
}
left = index * p.multiplyer
}
ratio := (float64(p.counterSameTimeFulfilled) / float64(p.counterSameTime)) * 10
if t.Sub(p.demandRequests[strconv.Itoa(p.filteredRequests[i])].CreatedAt).Minutes() <= 10 {
if ratio/10 < 0.6 {
p.globalEvent = true
p.resource = strings.ToLower(p.demandRequests[strconv.Itoa(p.filteredRequests[i])].Category)
}
}
top := 390 - int(ratio)*40
p.coordinates = append(p.coordinates, coordinate{id: i, top: top, left: left})
if len(p.coordinates) == 1 {
requests := strconv.Itoa(p.counterSameTime)
p.counterSameTime = 0
p.counterSameTimeFulfilled = 0
p.counterSameTime++
if p.demandRequests[strconv.Itoa(p.filteredRequests[i])].Fulfilled {
p.counterSameTimeFulfilled++
}
return app.Div().ID("ball"+strconv.Itoa(p.filteredRequests[i])).Class("ball").Style("top", strconv.Itoa(top)+"px").Style("left", strconv.Itoa(left)+"px").Body(
app.A().Href("#").Body(app.Small().Text(requests+" Requests")),
app.Div().ID("pulse"+strconv.Itoa(p.filteredRequests[i])).Class("pulse").Style("top", "-1px").Style("left", "-1px"),
)
} else {
last := false
switch id := p.demandRequests[strconv.Itoa(p.filteredRequests[i])].ID; {
case id == p.lastWaterRequest:
last = true
case id == p.lastFoodRequest:
last = true
case id == p.lastHousingRequest:
last = true
case id == p.lastOtherRequest:
last = true
default:
last = false
}
if last && !p.hasChildren {
p.counterSameTime++
if p.demandRequests[strconv.Itoa(p.filteredRequests[i])].Fulfilled {
p.counterSameTimeFulfilled++
}
}
ratio := (float64(p.counterSameTimeFulfilled) / float64(p.counterSameTime)) * 10
top := 390 - int(ratio)*40
prevElement := p.coordinates[len(p.coordinates)-2]
topDiff := float64(top) - float64(prevElement.top)
leftDiff := float64(left) - float64(prevElement.left)
p.coordinates[len(p.coordinates)-1].distance = math.Sqrt(topDiff*topDiff + leftDiff*leftDiff)
p.coordinates[len(p.coordinates)-1].angle = math.Atan2(float64(prevElement.top)-float64(top), float64(prevElement.left)-float64(left)) * (180 / math.Pi)
requests := strconv.Itoa(p.counterSameTime)
p.counterSameTime = 0
p.counterSameTimeFulfilled = 0
if !last {
p.counterSameTime++
if p.demandRequests[strconv.Itoa(p.filteredRequests[i])].Fulfilled {
p.counterSameTimeFulfilled++
}
}